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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use crate::integer::ciphertext::IntegerRadixCiphertext;
use crate::integer::ServerKey;
impl ServerKey {
/// Computes homomorphically a multiplication between a ciphertext encrypting an integer value
/// and another encrypting a shortint value.
///
/// This function computes the operation without checking if it exceeds the capacity of the
/// ciphertext.
///
/// The result is assigned to the `ct_left` ciphertext.
///
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
/// let size = 4;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, size);
///
/// let clear_1 = 170;
/// let clear_2 = 3;
///
/// // Encrypt two messages
/// let mut ct_left = cks.encrypt(clear_1);
/// let ct_right = cks.encrypt_one_block(clear_2);
///
/// // Compute homomorphically a multiplication
/// sks.unchecked_block_mul_assign(&mut ct_left, &ct_right, 0);
///
/// // Decrypt
/// let res: u64 = cks.decrypt(&ct_left);
/// assert_eq!((clear_1 * clear_2) % 256, res);
/// ```
pub fn unchecked_block_mul_assign<T>(
&self,
ct_left: &mut T,
ct_right: &crate::shortint::Ciphertext,
index: usize,
) where
T: IntegerRadixCiphertext,
{
*ct_left = self.unchecked_block_mul(ct_left, ct_right, index);
}
/// Computes homomorphically a multiplication between a ciphertexts encrypting an integer
/// value and another encrypting a shortint value.
///
/// This function computes the operation without checking if it exceeds the capacity of the
/// ciphertext.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
/// let size = 4;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, size);
///
/// let clear_1 = 55;
/// let clear_2 = 3;
///
/// // Encrypt two messages
/// let ct_left = cks.encrypt(clear_1);
/// let ct_right = cks.encrypt_one_block(clear_2);
///
/// // Compute homomorphically a multiplication
/// let ct_res = sks.unchecked_block_mul(&ct_left, &ct_right, 0);
///
/// // Decrypt
/// let res: u64 = cks.decrypt(&ct_res);
/// assert_eq!((clear_1 * clear_2) % 256, res);
/// ```
pub fn unchecked_block_mul<T>(
&self,
ct1: &T,
ct2: &crate::shortint::Ciphertext,
index: usize,
) -> T
where
T: IntegerRadixCiphertext,
{
let shifted_ct = self.blockshift(ct1, index);
let mut result_lsb = shifted_ct.clone();
let mut result_msb = shifted_ct;
for res_lsb_i in result_lsb.blocks_mut()[index..].iter_mut() {
self.key.unchecked_mul_lsb_assign(res_lsb_i, ct2);
}
let len = result_msb.blocks_mut().len() - 1;
for res_msb_i in result_msb.blocks_mut()[index..len].iter_mut() {
self.key.unchecked_mul_msb_assign(res_msb_i, ct2);
}
result_msb = self.blockshift(&result_msb, 1);
self.unchecked_add(&result_lsb, &result_msb)
}
/// Computes homomorphically a multiplication between a ciphertext encrypting integer value
/// and another encrypting a shortint value.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
/// let size = 4;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, size);
///
/// let clear_1 = 170;
/// let clear_2 = 3;
///
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt(clear_1);
/// let mut ctxt_2 = cks.encrypt_one_block(clear_2);
///
/// // Compute homomorphically a multiplication
/// let ct_res = sks.smart_block_mul(&mut ctxt_1, &mut ctxt_2, 0);
///
/// // Decrypt
/// let res: u64 = cks.decrypt(&ct_res);
/// assert_eq!((clear_1 * clear_2) % 256, res);
/// ```
// by convention smart operations take mut refs to their inputs, even if they do not modify them
#[allow(clippy::needless_pass_by_ref_mut)]
pub fn smart_block_mul<T>(
&self,
ct1: &mut T,
ct2: &mut crate::shortint::Ciphertext,
index: usize,
) -> T
where
T: IntegerRadixCiphertext,
{
//Makes sure we can do the multiplications
self.full_propagate(ct1);
let shifted_ct = self.blockshift(ct1, index);
let mut result_lsb = shifted_ct.clone();
let mut result_msb = shifted_ct;
for res_lsb_i in result_lsb.blocks_mut()[index..].iter_mut() {
self.key.unchecked_mul_lsb_assign(res_lsb_i, ct2);
}
let len = result_msb.blocks().len() - 1;
for res_msb_i in result_msb.blocks_mut()[index..len].iter_mut() {
self.key.unchecked_mul_msb_assign(res_msb_i, ct2);
}
result_msb = self.blockshift(&result_msb, 1);
self.smart_add(&mut result_lsb, &mut result_msb)
}
pub fn smart_block_mul_assign<T>(
&self,
ct1: &mut T,
ct2: &mut crate::shortint::Ciphertext,
index: usize,
) where
T: IntegerRadixCiphertext,
{
*ct1 = self.smart_block_mul(ct1, ct2, index);
}
/// Computes homomorphically a multiplication between two ciphertexts encrypting integer values.
///
/// This function computes the operation without checking if it exceeds the capacity of the
/// ciphertext.
///
/// The result is assigned to the `ct_left` ciphertext.
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
/// let size = 4;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, size);
///
/// let clear_1 = 255;
/// let clear_2 = 143;
///
/// // Encrypt two messages
/// let ctxt_1 = cks.encrypt(clear_1);
/// let ctxt_2 = cks.encrypt(clear_2);
///
/// // Compute homomorphically a multiplication
/// let ct_res = sks.unchecked_mul(&ctxt_1, &ctxt_2);
///
/// // Decrypt
/// let res: u64 = cks.decrypt(&ct_res);
/// assert_eq!((clear_1 * clear_2) % 256, res);
/// ```
pub fn unchecked_mul_assign<T>(&self, ct1: &mut T, ct2: &T)
where
T: IntegerRadixCiphertext,
{
*ct1 = self.unchecked_mul(ct1, ct2);
}
/// Computes homomorphically a multiplication between two ciphertexts encrypting integer values.
///
/// This function computes the operation without checking if it exceeds the capacity of the
/// ciphertext.
///
/// The result is returned as a new ciphertext.
pub fn unchecked_mul<T>(&self, ct1: &T, ct2: &T) -> T
where
T: IntegerRadixCiphertext,
{
let mut result = self.create_trivial_zero_radix(ct1.blocks().len());
for (i, ct2_i) in ct2.blocks().iter().enumerate() {
let mut tmp = self.unchecked_block_mul(ct1, ct2_i, i);
self.smart_add_assign(&mut result, &mut tmp);
}
result
}
/// Computes homomorphically a multiplication between two ciphertexts encrypting integer values.
///
/// The result is assigned to the `ct_left` ciphertext.
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
/// let size = 4;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, size);
///
/// let clear_1 = 170;
/// let clear_2 = 6;
///
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt(clear_1);
/// let mut ctxt_2 = cks.encrypt(clear_2);
///
/// // Compute homomorphically a multiplication
/// let ct_res = sks.smart_mul(&mut ctxt_1, &mut ctxt_2);
/// // Decrypt
/// let res: u64 = cks.decrypt(&ct_res);
/// assert_eq!((clear_1 * clear_2) % 256, res);
/// ```
pub fn smart_mul_assign<T>(&self, ct1: &mut T, ct2: &mut T)
where
T: IntegerRadixCiphertext,
{
*ct1 = self.smart_mul(ct1, ct2);
}
/// Computes homomorphically a multiplication between two ciphertexts encrypting integer values.
///
/// The result is returned as a new ciphertext.
pub fn smart_mul<T>(&self, ct1: &mut T, ct2: &mut T) -> T
where
T: IntegerRadixCiphertext,
{
self.full_propagate(ct1);
self.full_propagate(ct2);
let mut result = self.create_trivial_zero_radix(ct1.blocks().len());
for (i, ct2_i) in ct2.blocks().iter().enumerate() {
let mut tmp = self.unchecked_block_mul(ct1, ct2_i, i);
self.smart_add_assign(&mut result, &mut tmp);
}
result
}
}