1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::integer::ciphertext::CrtCiphertext;
use crate::integer::ServerKey;
use rayon::prelude::*;
impl ServerKey {
/// Computes homomorphically an addition between two ciphertexts encrypting integer
/// values in the CRT decomposition.
///
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128;
///
/// // Generate the client key and the server key:
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// let (cks, sks) = gen_keys_crt(PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, basis);
///
/// let clear_1 = 14;
/// let clear_2 = 14;
///
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt(clear_1);
/// let ctxt_2 = cks.encrypt(clear_2);
///
/// // Compute homomorphically a multiplication
/// sks.unchecked_crt_add_assign_parallelized(&mut ctxt_1, &ctxt_2);
///
/// // Decrypt
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 + clear_2) % modulus, res);
/// ```
pub fn unchecked_crt_add_assign_parallelized(
&self,
ct_left: &mut CrtCiphertext,
ct_right: &CrtCiphertext,
) {
ct_left
.blocks
.par_iter_mut()
.zip(ct_right.blocks.par_iter())
.for_each(|(ct_left, ct_right)| {
self.key.unchecked_add_assign(ct_left, ct_right);
});
}
pub fn unchecked_crt_add_parallelized(
&self,
ct_left: &CrtCiphertext,
ct_right: &CrtCiphertext,
) -> CrtCiphertext {
let mut ct_res = ct_left.clone();
self.unchecked_crt_add_assign_parallelized(&mut ct_res, ct_right);
ct_res
}
/// Computes homomorphically an addition between two ciphertexts encrypting integer values in
/// the CRT decomposition.
///
/// This checks that the addition is possible. In the case where the carry buffers are full,
/// then it is automatically cleared to allow the operation.
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128;
///
/// // Generate the client key and the server key:
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// let (cks, sks) = gen_keys_crt(PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, basis);
///
/// let clear_1 = 29;
/// let clear_2 = 29;
///
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt(clear_1);
/// let mut ctxt_2 = cks.encrypt(clear_2);
///
/// // Compute homomorphically a multiplication
/// sks.smart_crt_add_assign_parallelized(&mut ctxt_1, &mut ctxt_2);
///
/// // Decrypt
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 + clear_2) % modulus, res);
/// ```
pub fn smart_crt_add_assign_parallelized(
&self,
ct_left: &mut CrtCiphertext,
ct_right: &mut CrtCiphertext,
) {
if self.is_crt_add_possible(ct_left, ct_right).is_err() {
rayon::join(
|| self.full_extract_message_assign(ct_left),
|| self.full_extract_message_assign(ct_right),
);
}
self.is_crt_add_possible(ct_left, ct_right).unwrap();
self.unchecked_crt_add_assign_parallelized(ct_left, ct_right);
}
pub fn smart_crt_add_parallelized(
&self,
ct_left: &mut CrtCiphertext,
ct_right: &mut CrtCiphertext,
) -> CrtCiphertext {
if self.is_crt_add_possible(ct_left, ct_right).is_err() {
rayon::join(
|| self.full_extract_message_assign(ct_left),
|| self.full_extract_message_assign(ct_right),
);
}
self.is_crt_add_possible(ct_left, ct_right).unwrap();
self.unchecked_crt_add_parallelized(ct_left, ct_right)
}
}