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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use std::ops::RangeBounds;
use rayon::prelude::*;
use crate::error::InvalidRangeError;
use crate::integer::server_key::radix::slice::{normalize_range, slice_oneblock_clear_unaligned};
use crate::integer::{RadixCiphertext, ServerKey};
use crate::prelude::{CastFrom, CastInto};
impl ServerKey {
/// Extract a slice from a ciphertext. The size of the slice is a multiple of the block
/// size but it is not aligned on block boundaries, so we need to mix block n and (n+1) to
/// create a new block, using the lut function `slice_oneblock_clear_unaligned`.
fn scalar_blockslice_unaligned_parallelized(
&self,
ctxt: &RadixCiphertext,
start_block: usize,
block_count: usize,
offset: usize,
) -> RadixCiphertext {
assert!(offset < (self.message_modulus().0.ilog2() as usize));
assert!(start_block + block_count < ctxt.blocks.len());
let mut out: RadixCiphertext = self.create_trivial_zero_radix(block_count);
let lut = self
.key
.generate_lookup_table_bivariate(|current_block, next_block| {
slice_oneblock_clear_unaligned(
current_block,
next_block,
offset,
self.message_modulus().0.ilog2() as usize,
)
});
out.blocks
.par_iter_mut()
.enumerate()
.for_each(|(idx, block)| {
*block = self.key.apply_lookup_table_bivariate(
&ctxt.blocks[idx + start_block],
&ctxt.blocks[idx + start_block + 1],
&lut,
);
});
out
}
/// Extract a slice of bits from a ciphertext.
///
/// The result is returned as a new ciphertext. This function is more efficient
/// if the range starts on a block boundary.
///
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
///
/// // We have 4 * 2 = 8 bits of message
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, num_blocks);
///
/// let msg: u64 = 225;
/// let start_bit = 3;
/// let end_bit = 6;
///
/// // Encrypt the message:
/// let ct = cks.encrypt(msg);
///
/// let ct_res = sks
/// .unchecked_scalar_bitslice_parallelized(&ct, start_bit..end_bit)
/// .unwrap();
///
/// // Decrypt:
/// let clear = cks.decrypt(&ct_res);
/// assert_eq!((msg % (1 << end_bit)) >> start_bit, clear);
/// ```
pub fn unchecked_scalar_bitslice_parallelized<B, R>(
&self,
ctxt: &RadixCiphertext,
range: R,
) -> Result<RadixCiphertext, InvalidRangeError>
where
R: RangeBounds<B>,
B: CastFrom<usize> + CastInto<usize> + Copy,
{
let block_width = self.message_modulus().0.ilog2() as usize;
let range = normalize_range(&range, block_width * ctxt.blocks.len())?;
let slice_width = range.end - range.start;
// If the starting bit is block aligned, we can do most of the slicing with block copies.
// If it's not we must extract the bits with PBS. In either cases, we must extract the last
// bits with a PBS if the slice size is not a multiple of the block size.
let mut sliced = if range.start % block_width != 0 {
let (mut sliced, maybe_last_block) = rayon::join(
|| {
self.scalar_blockslice_unaligned_parallelized(
ctxt,
range.start / block_width,
slice_width / block_width,
range.start % block_width,
)
},
|| {
if slice_width % block_width != 0 {
Some(self.bitslice_remainder_unaligned(
ctxt,
range.start / block_width + slice_width / block_width,
range.start % block_width,
slice_width % block_width,
))
} else {
None
}
},
);
if let Some(last_block) = maybe_last_block {
sliced.blocks.push(last_block);
}
sliced
} else {
let mut sliced = self.scalar_blockslice_aligned(
ctxt,
range.start / block_width,
range.end / block_width,
);
if slice_width % block_width != 0 {
let last_block = self.bitslice_remainder(
ctxt,
range.end / block_width,
slice_width % block_width,
);
sliced.blocks.push(last_block);
}
sliced
};
// Extend with trivial zeroes to return an integer of the same size as the input one.
self.extend_radix_with_trivial_zero_blocks_msb_assign(&mut sliced, ctxt.blocks.len());
Ok(sliced)
}
/// Extract a slice of bits from a ciphertext.
///
/// The result is assigned to the input ciphertext. This function is more efficient
/// if the range starts on a block boundary.
///
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
///
/// // We have 4 * 2 = 8 bits of message
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, num_blocks);
///
/// let msg: u64 = 225;
/// let start_bit = 3;
/// let end_bit = 6;
///
/// // Encrypt the message:
/// let mut ct = cks.encrypt(msg);
///
/// sks.unchecked_scalar_bitslice_assign_parallelized(&mut ct, start_bit..end_bit)
/// .unwrap();
///
/// // Decrypt:
/// let clear = cks.decrypt(&ct);
/// assert_eq!((msg % (1 << end_bit)) >> start_bit, clear);
/// ```
pub fn unchecked_scalar_bitslice_assign_parallelized<B, R>(
&self,
ctxt: &mut RadixCiphertext,
range: R,
) -> Result<(), InvalidRangeError>
where
R: RangeBounds<B>,
B: CastFrom<usize> + CastInto<usize> + Copy,
{
*ctxt = self.unchecked_scalar_bitslice_parallelized(ctxt, range)?;
Ok(())
}
/// Extract a slice of bits from a ciphertext.
///
/// The result is returned as a new ciphertext. This function is more efficient
/// if the range starts on a block boundary.
///
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
///
/// // We have 4 * 2 = 8 bits of message
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, num_blocks);
///
/// let msg: u64 = 225;
/// let start_bit = 3;
/// let end_bit = 6;
///
/// // Encrypt the message:
/// let ct = cks.encrypt(msg);
///
/// let ct_res = sks
/// .scalar_bitslice_parallelized(&ct, start_bit..end_bit)
/// .unwrap();
///
/// // Decrypt:
/// let clear = cks.decrypt(&ct_res);
/// assert_eq!((msg % (1 << end_bit)) >> start_bit, clear);
/// ```
pub fn scalar_bitslice_parallelized<B, R>(
&self,
ctxt: &RadixCiphertext,
range: R,
) -> Result<RadixCiphertext, InvalidRangeError>
where
R: RangeBounds<B>,
B: CastFrom<usize> + CastInto<usize> + Copy,
{
if ctxt.block_carries_are_empty() {
self.unchecked_scalar_bitslice_parallelized(ctxt, range)
} else {
let mut ctxt = ctxt.clone();
self.full_propagate_parallelized(&mut ctxt);
self.unchecked_scalar_bitslice_parallelized(&ctxt, range)
}
}
/// Extract a slice of bits from a ciphertext.
///
/// The result is assigned to the input ciphertext. This function is more efficient
/// if the range starts on a block boundary.
///
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
///
/// // We have 4 * 2 = 8 bits of message
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, num_blocks);
///
/// let msg: u64 = 225;
/// let start_bit = 3;
/// let end_bit = 6;
///
/// // Encrypt the message:
/// let mut ct = cks.encrypt(msg);
///
/// sks.scalar_bitslice_assign_parallelized(&mut ct, start_bit..end_bit)
/// .unwrap();
///
/// // Decrypt:
/// let clear = cks.decrypt(&ct);
/// assert_eq!((msg % (1 << end_bit)) >> start_bit, clear);
/// ```
pub fn scalar_bitslice_assign_parallelized<B, R>(
&self,
ctxt: &mut RadixCiphertext,
range: R,
) -> Result<(), InvalidRangeError>
where
R: RangeBounds<B>,
B: CastFrom<usize> + CastInto<usize> + Copy,
{
if !ctxt.block_carries_are_empty() {
self.full_propagate_parallelized(ctxt);
}
self.unchecked_scalar_bitslice_assign_parallelized(ctxt, range)
}
/// Extract a slice of bits from a ciphertext.
///
/// The result is returned as a new ciphertext. This function is more efficient
/// if the range starts on a block boundary.
///
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
///
/// // We have 4 * 2 = 8 bits of message
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, num_blocks);
///
/// let msg: u64 = 225;
/// let start_bit = 3;
/// let end_bit = 6;
///
/// // Encrypt the message:
/// let mut ct = cks.encrypt(msg);
///
/// let ct_res = sks
/// .smart_scalar_bitslice_parallelized(&mut ct, start_bit..end_bit)
/// .unwrap();
///
/// // Decrypt:
/// let clear = cks.decrypt(&ct_res);
/// assert_eq!((msg % (1 << end_bit)) >> start_bit, clear);
/// ```
pub fn smart_scalar_bitslice_parallelized<B, R>(
&self,
ctxt: &mut RadixCiphertext,
range: R,
) -> Result<RadixCiphertext, InvalidRangeError>
where
R: RangeBounds<B>,
B: CastFrom<usize> + CastInto<usize> + Copy,
{
if !ctxt.block_carries_are_empty() {
self.full_propagate_parallelized(ctxt);
}
self.unchecked_scalar_bitslice_parallelized(ctxt, range)
}
/// Extract a slice of bits from a ciphertext.
///
/// The result is assigned to the input ciphertext. This function is more efficient
/// if the range starts on a block boundary.
///
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
///
/// // We have 4 * 2 = 8 bits of message
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, num_blocks);
///
/// let msg: u64 = 225;
/// let start_bit = 3;
/// let end_bit = 6;
///
/// // Encrypt the message:
/// let mut ct = cks.encrypt(msg);
///
/// sks.smart_scalar_bitslice_assign(&mut ct, start_bit..end_bit)
/// .unwrap();
///
/// // Decrypt:
/// let clear = cks.decrypt(&ct);
/// assert_eq!((msg % (1 << end_bit)) >> start_bit, clear);
/// ```
pub fn smart_scalar_bitslice_assign_parallelized<B, R>(
&self,
ctxt: &mut RadixCiphertext,
range: R,
) -> Result<(), InvalidRangeError>
where
R: RangeBounds<B>,
B: CastFrom<usize> + CastInto<usize> + Copy,
{
if !ctxt.block_carries_are_empty() {
self.full_propagate_parallelized(ctxt);
}
self.unchecked_scalar_bitslice_assign_parallelized(ctxt, range)
}
}