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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
pub mod boolean_value;
pub mod compact_list;
pub mod compressed_ciphertext_list;
pub mod compressed_noise_squashed_ciphertext_list;
pub mod info;
pub mod re_randomization;
pub mod squashed_noise;
use crate::core_crypto::gpu::lwe_ciphertext_list::CudaLweCiphertextList;
use crate::core_crypto::gpu::vec::CudaVec;
use crate::core_crypto::gpu::CudaStreams;
use crate::core_crypto::prelude::{LweCiphertextList, LweCiphertextOwned};
use crate::integer::parameters::LweDimension;
use crate::integer::{IntegerCiphertext, RadixCiphertext, SignedRadixCiphertext};
use crate::shortint::{Ciphertext, EncryptionKeyChoice};
use crate::GpuIndex;
use crate::integer::gpu::ciphertext::info::{CudaBlockInfo, CudaRadixCiphertextInfo};
pub use compressed_noise_squashed_ciphertext_list::*;
pub trait CudaIntegerRadixCiphertext: Sized {
const IS_SIGNED: bool;
fn as_ref(&self) -> &CudaRadixCiphertext;
fn as_mut(&mut self) -> &mut CudaRadixCiphertext;
fn from(ct: CudaRadixCiphertext) -> Self;
fn duplicate(&self, streams: &CudaStreams) -> Self {
Self::from(self.as_ref().duplicate(streams))
}
/// Moves `self` to the gpu on which the CudaStreams is
///
/// no-op if self is already on the correct gpu
fn move_to_stream(self, streams: &CudaStreams) -> Self {
if self.gpu_indexes() == streams.gpu_indexes() {
self
} else {
self.duplicate(streams)
}
}
fn into_inner(self) -> CudaRadixCiphertext;
/// # Safety
///
/// - `streams` __must__ be synchronized to guarantee computation has finished, and inputs must
/// not be dropped until streams is synchronised
unsafe fn duplicate_async(&self, streams: &CudaStreams) -> Self {
Self::from(self.as_ref().duplicate_async(streams))
}
fn block_carries_are_empty(&self) -> bool {
self.as_ref()
.info
.blocks
.iter()
.all(CudaBlockInfo::carry_is_empty)
}
fn holds_boolean_value(&self) -> bool {
self.as_ref().info.blocks[0].degree.get() <= 1
&& self.as_ref().info.blocks[1..]
.iter()
.all(|cuda_block_info| cuda_block_info.degree.get() == 0)
}
fn is_equal(&self, other: &Self, streams: &CudaStreams) -> bool {
self.as_ref().is_equal(other.as_ref(), streams)
}
fn gpu_indexes(&self) -> &[GpuIndex] {
&self.as_ref().d_blocks.0.d_vec.gpu_indexes
}
}
pub struct CudaRadixCiphertext {
pub d_blocks: CudaLweCiphertextList<u64>,
pub info: CudaRadixCiphertextInfo,
}
pub struct CudaUnsignedRadixCiphertext {
pub ciphertext: CudaRadixCiphertext,
}
pub struct CudaSignedRadixCiphertext {
pub ciphertext: CudaRadixCiphertext,
}
impl CudaIntegerRadixCiphertext for CudaUnsignedRadixCiphertext {
const IS_SIGNED: bool = false;
fn as_ref(&self) -> &CudaRadixCiphertext {
&self.ciphertext
}
fn as_mut(&mut self) -> &mut CudaRadixCiphertext {
&mut self.ciphertext
}
fn from(ct: CudaRadixCiphertext) -> Self {
Self { ciphertext: ct }
}
fn into_inner(self) -> CudaRadixCiphertext {
self.ciphertext
}
}
impl CudaIntegerRadixCiphertext for CudaSignedRadixCiphertext {
const IS_SIGNED: bool = true;
fn as_ref(&self) -> &CudaRadixCiphertext {
&self.ciphertext
}
fn as_mut(&mut self) -> &mut CudaRadixCiphertext {
&mut self.ciphertext
}
fn from(ct: CudaRadixCiphertext) -> Self {
Self { ciphertext: ct }
}
fn into_inner(self) -> CudaRadixCiphertext {
self.ciphertext
}
}
impl CudaRadixCiphertext {
pub fn from_cpu_blocks(blocks: &[Ciphertext], streams: &CudaStreams) -> Self {
let mut h_radix_ciphertext = blocks
.iter()
.flat_map(|block| block.ct.clone().into_container())
.collect::<Vec<_>>();
let lwe_size = blocks.first().unwrap().ct.lwe_size();
let ciphertext_modulus = blocks.first().unwrap().ct.ciphertext_modulus();
let h_ct = LweCiphertextList::from_container(
h_radix_ciphertext.as_mut_slice(),
lwe_size,
ciphertext_modulus,
);
let d_blocks = CudaLweCiphertextList::from_lwe_ciphertext_list(&h_ct, streams);
let info = CudaRadixCiphertextInfo {
blocks: blocks
.iter()
.map(|block| CudaBlockInfo {
degree: block.degree,
message_modulus: block.message_modulus,
carry_modulus: block.carry_modulus,
atomic_pattern: block.atomic_pattern,
noise_level: block.noise_level(),
})
.collect(),
};
Self { d_blocks, info }
}
pub fn to_cpu_blocks(&self, streams: &CudaStreams) -> Vec<Ciphertext> {
let h_lwe_ciphertext_list = self.d_blocks.to_lwe_ciphertext_list(streams);
let ciphertext_modulus = h_lwe_ciphertext_list.ciphertext_modulus();
let lwe_size = h_lwe_ciphertext_list.lwe_size().0;
h_lwe_ciphertext_list
.into_container()
.chunks(lwe_size)
.zip(&self.info.blocks)
.map(|(data, i)| {
Ciphertext::new(
LweCiphertextOwned::from_container(data.to_vec(), ciphertext_modulus),
i.degree,
i.noise_level,
i.message_modulus,
i.carry_modulus,
i.atomic_pattern,
)
})
.collect()
}
pub fn from_radix_ciphertext_vec<T: CudaIntegerRadixCiphertext>(
list: &[T],
streams: &CudaStreams,
) -> Self {
let lwes = CudaLweCiphertextList::from_vec_cuda_lwe_ciphertexts_list(
list.iter().map(|ciphertext| &ciphertext.as_ref().d_blocks),
streams,
);
let info = CudaRadixCiphertextInfo {
blocks: list
.iter()
.flat_map(|ciphertext| ciphertext.as_ref().info.blocks.iter())
.copied()
.collect::<Vec<CudaBlockInfo>>(),
};
Self::new(lwes, info)
}
}
impl CudaUnsignedRadixCiphertext {
pub fn new(d_blocks: CudaLweCiphertextList<u64>, info: CudaRadixCiphertextInfo) -> Self {
Self {
ciphertext: CudaRadixCiphertext { d_blocks, info },
}
}
/// Copies a RadixCiphertext to the GPU memory
///
/// # Example
///
/// ```rust
/// use tfhe::core_crypto::gpu::vec::GpuIndex;
/// use tfhe::core_crypto::gpu::CudaStreams;
/// use tfhe::integer::gpu::ciphertext::CudaUnsignedRadixCiphertext;
/// use tfhe::integer::gpu::gen_keys_radix_gpu;
/// use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// let size = 4;
///
/// let gpu_index = 0;
/// let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys_radix_gpu(
/// PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
/// size,
/// &streams,
/// );
///
/// let clear: u64 = 255;
///
/// // Encrypt two messages
/// let ctxt = cks.encrypt(clear);
///
/// let d_ctxt = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ctxt, &streams);
/// let h_ctxt = d_ctxt.to_radix_ciphertext(&streams);
///
/// assert_eq!(h_ctxt, ctxt);
/// ```
pub fn from_radix_ciphertext(radix: &RadixCiphertext, streams: &CudaStreams) -> Self {
Self {
ciphertext: CudaRadixCiphertext::from_cpu_blocks(radix.blocks(), streams),
}
}
pub fn copy_from_radix_ciphertext(&mut self, radix: &RadixCiphertext, streams: &CudaStreams) {
let mut h_radix_ciphertext = radix
.blocks
.iter()
.flat_map(|block| block.ct.clone().into_container())
.collect::<Vec<_>>();
unsafe {
self.ciphertext.d_blocks.0.d_vec.copy_from_cpu_async(
h_radix_ciphertext.as_mut_slice(),
streams,
0,
);
}
streams.synchronize();
self.ciphertext.info = CudaRadixCiphertextInfo {
blocks: radix
.blocks
.iter()
.map(|block| CudaBlockInfo {
degree: block.degree,
message_modulus: block.message_modulus,
carry_modulus: block.carry_modulus,
atomic_pattern: block.atomic_pattern,
noise_level: block.noise_level(),
})
.collect(),
};
}
/// ```rust
/// use tfhe::core_crypto::gpu::vec::GpuIndex;
/// use tfhe::core_crypto::gpu::CudaStreams;
/// use tfhe::integer::gpu::ciphertext::CudaUnsignedRadixCiphertext;
/// use tfhe::integer::gpu::gen_keys_radix_gpu;
/// use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
///
/// let gpu_index = 0;
/// let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));
///
/// // Generate the client key and the server key:
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix_gpu(
/// PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
/// num_blocks,
/// &streams,
/// );
///
/// let msg1 = 10u32;
/// let ct1 = cks.encrypt(msg1);
///
/// // Copy to GPU
/// let d_ct1 = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ct1, &streams);
/// let ct2 = d_ct1.to_radix_ciphertext(&streams);
/// let msg2 = cks.decrypt(&ct2);
///
/// assert_eq!(msg1, msg2);
/// ```
pub fn to_radix_ciphertext(&self, streams: &CudaStreams) -> RadixCiphertext {
RadixCiphertext::from(self.ciphertext.to_cpu_blocks(streams))
}
}
impl CudaSignedRadixCiphertext {
pub fn new(d_blocks: CudaLweCiphertextList<u64>, info: CudaRadixCiphertextInfo) -> Self {
Self {
ciphertext: CudaRadixCiphertext { d_blocks, info },
}
}
/// Copies a RadixCiphertext to the GPU memory
///
/// # Example
///
/// ```rust
/// use tfhe::core_crypto::gpu::vec::GpuIndex;
/// use tfhe::core_crypto::gpu::CudaStreams;
/// use tfhe::integer::gpu::ciphertext::CudaSignedRadixCiphertext;
/// use tfhe::integer::gpu::gen_keys_radix_gpu;
/// use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// let size = 4;
///
/// let gpu_index = 0;
/// let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys_radix_gpu(
/// PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
/// size,
/// &streams,
/// );
///
/// let clear: i64 = 255;
///
/// // Encrypt two messages
/// let ctxt = cks.encrypt_signed(clear);
///
/// let d_ctxt = CudaSignedRadixCiphertext::from_signed_radix_ciphertext(&ctxt, &streams);
/// let h_ctxt = d_ctxt.to_signed_radix_ciphertext(&streams);
///
/// assert_eq!(h_ctxt, ctxt);
/// ```
pub fn from_signed_radix_ciphertext(
radix: &SignedRadixCiphertext,
streams: &CudaStreams,
) -> Self {
Self {
ciphertext: CudaRadixCiphertext::from_cpu_blocks(radix.blocks(), streams),
}
}
pub fn copy_from_signed_radix_ciphertext(
&mut self,
radix: &SignedRadixCiphertext,
streams: &CudaStreams,
) {
let mut h_radix_ciphertext = radix
.blocks
.iter()
.flat_map(|block| block.ct.clone().into_container())
.collect::<Vec<_>>();
unsafe {
self.ciphertext.d_blocks.0.d_vec.copy_from_cpu_async(
h_radix_ciphertext.as_mut_slice(),
streams,
0,
);
}
streams.synchronize();
self.ciphertext.info = CudaRadixCiphertextInfo {
blocks: radix
.blocks
.iter()
.map(|block| CudaBlockInfo {
degree: block.degree,
message_modulus: block.message_modulus,
carry_modulus: block.carry_modulus,
atomic_pattern: block.atomic_pattern,
noise_level: block.noise_level(),
})
.collect(),
};
}
/// ```rust
/// use tfhe::core_crypto::gpu::vec::GpuIndex;
/// use tfhe::core_crypto::gpu::CudaStreams;
/// use tfhe::integer::gpu::ciphertext::CudaSignedRadixCiphertext;
/// use tfhe::integer::gpu::gen_keys_radix_gpu;
/// use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
///
/// let gpu_index = 0;
/// let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));
///
/// // Generate the client key and the server key:
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix_gpu(
/// PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
/// num_blocks,
/// &streams,
/// );
///
/// let msg1 = 10i32;
/// let ct1 = cks.encrypt_signed(msg1);
///
/// // Copy to GPU
/// let d_ct1 = CudaSignedRadixCiphertext::from_signed_radix_ciphertext(&ct1, &streams);
/// let ct2 = d_ct1.to_signed_radix_ciphertext(&streams);
/// let msg2 = cks.decrypt_signed(&ct2);
///
/// assert_eq!(msg1, msg2);
/// ```
pub fn to_signed_radix_ciphertext(&self, streams: &CudaStreams) -> SignedRadixCiphertext {
SignedRadixCiphertext::from(self.ciphertext.to_cpu_blocks(streams))
}
}
impl CudaRadixCiphertext {
pub fn new(d_blocks: CudaLweCiphertextList<u64>, info: CudaRadixCiphertextInfo) -> Self {
Self { d_blocks, info }
}
/// ```rust
/// use tfhe::core_crypto::gpu::vec::GpuIndex;
/// use tfhe::core_crypto::gpu::CudaStreams;
/// use tfhe::integer::gpu::ciphertext::{CudaIntegerRadixCiphertext, CudaSignedRadixCiphertext};
/// use tfhe::integer::gpu::gen_keys_radix_gpu;
/// use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
///
/// let gpu_index = 0;
/// let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));
///
/// // Generate the client key and the server key:
/// let num_blocks = 4;
/// let (cks, sks) = gen_keys_radix_gpu(
/// PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
/// num_blocks,
/// &streams,
/// );
///
/// let msg = 10i32;
/// let ct = cks.encrypt_signed(msg);
///
/// // Copy to GPU
/// let d_ct = CudaSignedRadixCiphertext::from_signed_radix_ciphertext(&ct, &streams);
/// let d_ct_copied = d_ct.duplicate(&streams);
///
/// let ct_copied = d_ct_copied.to_signed_radix_ciphertext(&streams);
/// let msg_copied = cks.decrypt_signed(&ct_copied);
///
/// assert_eq!(msg, msg_copied);
/// ```
pub fn duplicate(&self, streams: &CudaStreams) -> Self {
let ct = unsafe { self.duplicate_async(streams) };
streams.synchronize();
ct
}
/// # Safety
///
/// - `streams` __must__ be synchronized to guarantee computation has finished, and inputs must
/// not be dropped until streams is synchronised
pub unsafe fn duplicate_async(&self, streams: &CudaStreams) -> Self {
let lwe_ciphertext_count = self.d_blocks.lwe_ciphertext_count();
let ciphertext_modulus = self.d_blocks.ciphertext_modulus();
let mut d_ct = CudaVec::new_async(self.d_blocks.0.d_vec.len(), streams, 0);
d_ct.copy_from_gpu_async(&self.d_blocks.0.d_vec, streams, 0);
let d_blocks =
CudaLweCiphertextList::from_cuda_vec(d_ct, lwe_ciphertext_count, ciphertext_modulus);
Self {
d_blocks,
info: self.info.clone(),
}
}
fn is_equal(&self, other: &Self, streams: &CudaStreams) -> bool {
let self_size = self.d_blocks.0.d_vec.len();
let other_size = other.d_blocks.0.d_vec.len();
let mut self_container: Vec<u64> = vec![0; self_size];
let mut other_container: Vec<u64> = vec![0; other_size];
unsafe {
self.d_blocks
.0
.d_vec
.copy_to_cpu_async(self_container.as_mut_slice(), streams, 0);
other
.d_blocks
.0
.d_vec
.copy_to_cpu_async(other_container.as_mut_slice(), streams, 0);
}
streams.synchronize();
self_container == other_container
}
}
#[repr(u32)]
#[derive(Debug)]
pub enum KsType {
BigToSmall = 0,
SmallToBig = 1,
}
impl From<EncryptionKeyChoice> for KsType {
fn from(value: EncryptionKeyChoice) -> Self {
match value {
EncryptionKeyChoice::Big => Self::SmallToBig,
EncryptionKeyChoice::Small => Self::BigToSmall,
}
}
}