concrete_integer/server_key/radix_parallel/scalar_add.rs
1use crate::ciphertext::RadixCiphertext;
2use crate::ServerKey;
3
4impl ServerKey {
5 /// Computes homomorphically the addition of ciphertext with a scalar.
6 ///
7 /// The result is returned in a new ciphertext.
8 ///
9 /// # Example
10 ///
11 /// ```rust
12 /// use concrete_integer::gen_keys_radix;
13 /// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
14 ///
15 /// // We have 4 * 2 = 8 bits of message
16 /// let size = 4;
17 /// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
18 ///
19 /// let msg = 4;
20 /// let scalar = 40;
21 ///
22 /// let mut ct = cks.encrypt(msg);
23 ///
24 /// // Compute homomorphically an addition:
25 /// let ct_res = sks.smart_scalar_add_parallelized(&mut ct, scalar);
26 ///
27 /// // Decrypt:
28 /// let dec = cks.decrypt(&ct_res);
29 /// assert_eq!(msg + scalar, dec);
30 /// ```
31 pub fn smart_scalar_add_parallelized(
32 &self,
33 ct: &mut RadixCiphertext,
34 scalar: u64,
35 ) -> RadixCiphertext {
36 if !self.is_scalar_add_possible(ct, scalar) {
37 self.full_propagate_parallelized(ct);
38 }
39 self.unchecked_scalar_add(ct, scalar)
40 }
41
42 /// Computes homomorphically the addition of ciphertext with a scalar.
43 ///
44 /// The result is assigned to the `ct_left` ciphertext.
45 ///
46 /// # Example
47 ///
48 /// ```rust
49 /// use concrete_integer::gen_keys_radix;
50 /// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
51 ///
52 /// // We have 4 * 2 = 8 bits of message
53 /// let size = 4;
54 /// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
55 ///
56 /// let msg = 129;
57 /// let scalar = 40;
58 ///
59 /// let mut ct = cks.encrypt(msg);
60 ///
61 /// // Compute homomorphically an addition:
62 /// sks.smart_scalar_add_assign_parallelized(&mut ct, scalar);
63 ///
64 /// // Decrypt:
65 /// let dec = cks.decrypt(&ct);
66 /// assert_eq!(msg + scalar, dec);
67 /// ```
68 pub fn smart_scalar_add_assign_parallelized(&self, ct: &mut RadixCiphertext, scalar: u64) {
69 if !self.is_scalar_add_possible(ct, scalar) {
70 self.full_propagate_parallelized(ct);
71 }
72 self.unchecked_scalar_add_assign(ct, scalar);
73 }
74}