Adding or subtracting LWE ciphertexts together is a leveled operation that increases noise and potentially consumer padding and modify the encoding. It is one of the fundamental FHE operation.
​ | ​ |
Operation | ​​ |
Type | Leveled |
Side effects | Increases noise Potentially consumes padding Potentially modifies encoding |
The common way to add ciphertexts is to consume bits of padding and update the interval of their respective encoders. This can be achieved using the short form method add
(or add_inplace
) which simply takes another ciphertext as argument. This is an alias for the more verbose add_with_padding
(or add_with_padding_inplace
).
The constraint however is that the ciphertexts need to be encoded in the same interval and with the same number of padding bits. The result of adding and will be encoded in the interval with one less bit of padding.
Homomorphic subtraction works exactly the same as addition, using the sub_with_padding
(or sub_with_padding_inplace
) methods.
Here is a code example:
use concrete::*;​fn main() -> Result<(), CryptoAPIError> {// generate a secret keylet secret_key = LWESecretKey::new(&LWE128_630);​// the two values to addlet m1 = 8.2;let m2 = 5.6;// Encode in [0, 10[ with 8 bits of precision and 1 bit of paddinglet encoder = Encoder::new(0., 10., 8, 1)?;// encrypt plaintextslet mut c1 = LWE::encode_encrypt(&secret_key, m1, &encoder)?;let c2 = LWE::encode_encrypt(&secret_key, m2, &encoder)?;​// add the two ciphertexts homomorphically, and store in c1c1.add_with_padding_inplace(&c2)?;​// decrypt and decode the resultlet m3: f64 = c1.decrypt_decode(&secret_key)?;// print the result and compare to non-FHE additionprintln!("Real: {}, FHE: {}", m1 + m2, m3);Ok(())}
And in vectorized form:
// message vectors to addlet mv1: Vec<f64> = vec![1.2, 4.3, 0.11, 3.1, 6.7];let mv2: Vec<f64> = vec![7.0, 1.0, 8.2, 3.7, 9.4];​// Encode in [0, 10[ with 8 bits of precision and 1 bit of paddinglet encoder = Encoder::new(0., 10., 8, 1)?;​// encode encryptlet mut cv1 = VectorLWE::encode_encrypt(&secret_key, &mv1, &encoder)?;let cv2 = VectorLWE::encode_encrypt(&secret_key, &mv2, &encoder)?;​// add ciphertext vectors element-wisecv1.add_with_padding_inplace(&cv2)?;
If the interval of the output of the homomorphic addition is known, it is possible to add ciphertexts without consuming padding. To do so, you can use the add_with_new_min
(or add_with_new_min_inplace
) method that takes as arguments the ciphertext to add and the minimum new_min
of the interval of the result.
There are constraints when using this method, namely that the ciphertexts must be encoded in intervals of the same size, meaning and have the same precision. The interval of the output ciphertext will then be .
If the result of the homomorphic addition is outside of the specified range, the behavior is undefined and the result most likely to be incorrect.
Subtracting ciphertexts without consuming padding is not yet implemented in Concrete.
Here is a code example:
use concrete::*;​fn main() -> Result<(), CryptoAPIError> {// generate a secret keylet secret_key = LWESecretKey::new(&LWE128_630);​// the two values to addlet m1 = 8.;let m2 = 9.;// Encode in [0, 10[ with 8 bits of precision and 1 bit of paddinglet encoder = Encoder::new(0., 10., 8, 1)?;// encrypt plaintextslet mut c1 = LWE::encode_encrypt(&secret_key, m1, &encoder)?;let c2 = LWE::encode_encrypt(&secret_key, m2, &encoder)?;​// add the two ciphertexts homomorphicallylet new_min = 10.;c1.add_with_new_min_inplace(&c2, new_min)?;​// decrypt and decode the resultlet m3: f64 = c1.decrypt_decode(&secret_key)?;// print the result and compare to non-FHE additionprintln!("Real: {}, FHE: {}", m1 + m2, m3);Ok(())}