poulpy/core/src/glwe_ciphertext.rs
core/src/glwe_ciphertext.rsRaw
1use base2k::{
2 AddNormal, Backend, FFT64, FillUniform, MatZnxDft, MatZnxDftOps, MatZnxDftScratch, MatZnxDftToRef, Module, ScalarZnxAlloc,
3 ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScalarZnxDftToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigAlloc,
4 VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps,
5 VecZnxToMut, VecZnxToRef, ZnxInfos, ZnxZero,
6};
7use sampling::source::Source;
8
9use crate::{
10 SIX_SIGMA,
11 automorphism::AutomorphismKey,
12 elem::Infos,
13 ggsw_ciphertext::GGSWCiphertext,
14 glwe_ciphertext_fourier::GLWECiphertextFourier,
15 glwe_plaintext::GLWEPlaintext,
16 keys::{GLWEPublicKey, SecretDistribution, SecretKeyFourier},
17 keyswitch_key::GLWESwitchingKey,
18 utils::derive_size,
19};
20
21pub struct GLWECiphertext<C> {
22 pub data: VecZnx<C>,
23 pub basek: usize,
24 pub k: usize,
25}
26
27impl GLWECiphertext<Vec<u8>> {
28 pub fn new<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> Self {
29 Self {
30 data: module.new_vec_znx(rank + 1, derive_size(basek, k)),
31 basek,
32 k,
33 }
34 }
35}
36
37impl<T> Infos for GLWECiphertext<T> {
38 type Inner = VecZnx<T>;
39
40 fn inner(&self) -> &Self::Inner {
41 &self.data
42 }
43
44 fn basek(&self) -> usize {
45 self.basek
46 }
47
48 fn k(&self) -> usize {
49 self.k
50 }
51}
52
53impl<T> GLWECiphertext<T> {
54 pub fn rank(&self) -> usize {
55 self.cols() - 1
56 }
57}
58
59impl<C> VecZnxToMut for GLWECiphertext<C>
60where
61 VecZnx<C>: VecZnxToMut,
62{
63 fn to_mut(&mut self) -> VecZnx<&mut [u8]> {
64 self.data.to_mut()
65 }
66}
67
68impl<C> VecZnxToRef for GLWECiphertext<C>
69where
70 VecZnx<C>: VecZnxToRef,
71{
72 fn to_ref(&self) -> VecZnx<&[u8]> {
73 self.data.to_ref()
74 }
75}
76
77impl<C> GLWECiphertext<C>
78where
79 VecZnx<C>: VecZnxToRef,
80{
81 #[allow(dead_code)]
82 pub(crate) fn dft<R>(&self, module: &Module<FFT64>, res: &mut GLWECiphertextFourier<R, FFT64>)
83 where
84 VecZnxDft<R, FFT64>: VecZnxDftToMut<FFT64> + ZnxInfos,
85 {
86 #[cfg(debug_assertions)]
87 {
88 assert_eq!(self.rank(), res.rank());
89 assert_eq!(self.basek(), res.basek())
90 }
91
92 (0..self.rank() + 1).for_each(|i| {
93 module.vec_znx_dft(res, i, self, i);
94 })
95 }
96}
97
98impl GLWECiphertext<Vec<u8>> {
99 pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
100 module.vec_znx_big_normalize_tmp_bytes()
101 + module.bytes_of_vec_znx_dft(1, ct_size)
102 + module.bytes_of_vec_znx_big(1, ct_size)
103 }
104 pub fn encrypt_pk_scratch_space(module: &Module<FFT64>, pk_size: usize) -> usize {
105 ((module.bytes_of_vec_znx_dft(1, pk_size) + module.bytes_of_vec_znx_big(1, pk_size)) | module.bytes_of_scalar_znx(1))
106 + module.bytes_of_scalar_znx_dft(1)
107 + module.vec_znx_big_normalize_tmp_bytes()
108 }
109
110 pub fn decrypt_scratch_space(module: &Module<FFT64>, ct_size: usize) -> usize {
111 (module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, ct_size))
112 + module.bytes_of_vec_znx_big(1, ct_size)
113 }
114
115 pub fn keyswitch_scratch_space(
116 module: &Module<FFT64>,
117 out_size: usize,
118 out_rank: usize,
119 in_size: usize,
120 in_rank: usize,
121 ksk_size: usize,
122 ) -> usize {
123 let res_dft: usize = module.bytes_of_vec_znx_dft(out_rank + 1, ksk_size);
124 let vmp: usize = module.vmp_apply_tmp_bytes(out_size, in_size, in_size, in_rank, out_rank + 1, ksk_size)
125 + module.bytes_of_vec_znx_dft(in_rank, in_size);
126 let normalize: usize = module.vec_znx_big_normalize_tmp_bytes();
127
128 return res_dft + (vmp | normalize);
129 }
130
131 pub fn keyswitch_from_fourier_scratch_space(
132 module: &Module<FFT64>,
133 out_size: usize,
134 out_rank: usize,
135 in_size: usize,
136 in_rank: usize,
137 ksk_size: usize,
138 ) -> usize {
139 let res_dft = module.bytes_of_vec_znx_dft(out_rank + 1, ksk_size);
140
141 let vmp: usize = module.vmp_apply_tmp_bytes(out_size, in_size, in_size, in_rank, out_rank + 1, ksk_size)
142 + module.bytes_of_vec_znx_dft(in_rank, in_size);
143
144 let norm: usize = module.vec_znx_big_normalize_tmp_bytes();
145
146 res_dft + (vmp | norm)
147 }
148
149 pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, out_size: usize, out_rank: usize, ksk_size: usize) -> usize {
150 GLWECiphertext::keyswitch_scratch_space(module, out_size, out_rank, out_size, out_rank, ksk_size)
151 }
152
153 pub fn automorphism_scratch_space(
154 module: &Module<FFT64>,
155 out_size: usize,
156 out_rank: usize,
157 in_size: usize,
158 autokey_size: usize,
159 ) -> usize {
160 GLWECiphertext::keyswitch_scratch_space(module, out_size, out_rank, in_size, out_rank, autokey_size)
161 }
162
163 pub fn automorphism_inplace_scratch_space(
164 module: &Module<FFT64>,
165 out_size: usize,
166 out_rank: usize,
167 autokey_size: usize,
168 ) -> usize {
169 GLWECiphertext::keyswitch_scratch_space(module, out_size, out_rank, out_size, out_rank, autokey_size)
170 }
171
172 pub fn external_product_scratch_space(
173 module: &Module<FFT64>,
174 out_size: usize,
175 out_rank: usize,
176 in_size: usize,
177 ggsw_size: usize,
178 ) -> usize {
179 let res_dft: usize = module.bytes_of_vec_znx_dft(out_rank + 1, ggsw_size);
180 let vmp: usize = module.bytes_of_vec_znx_dft(out_rank + 1, in_size)
181 + module.vmp_apply_tmp_bytes(
182 out_size,
183 in_size,
184 in_size, // rows
185 out_rank + 1, // cols in
186 out_rank + 1, // cols out
187 ggsw_size,
188 );
189 let normalize: usize = module.vec_znx_big_normalize_tmp_bytes();
190
191 res_dft + (vmp | normalize)
192 }
193
194 pub fn external_product_inplace_scratch_space(
195 module: &Module<FFT64>,
196 out_size: usize,
197 out_rank: usize,
198 ggsw_size: usize,
199 ) -> usize {
200 GLWECiphertext::external_product_scratch_space(module, out_size, out_rank, out_size, ggsw_size)
201 }
202}
203
204impl<DataSelf> GLWECiphertext<DataSelf>
205where
206 VecZnx<DataSelf>: VecZnxToMut + VecZnxToRef,
207{
208 pub fn encrypt_sk<DataPt, DataSk>(
209 &mut self,
210 module: &Module<FFT64>,
211 pt: &GLWEPlaintext<DataPt>,
212 sk_dft: &SecretKeyFourier<DataSk, FFT64>,
213 source_xa: &mut Source,
214 source_xe: &mut Source,
215 sigma: f64,
216 scratch: &mut Scratch,
217 ) where
218 VecZnx<DataPt>: VecZnxToRef,
219 ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
220 {
221 self.encrypt_sk_private(
222 module,
223 Some((pt, 0)),
224 sk_dft,
225 source_xa,
226 source_xe,
227 sigma,
228 scratch,
229 );
230 }
231
232 pub fn encrypt_zero_sk<DataSk>(
233 &mut self,
234 module: &Module<FFT64>,
235 sk_dft: &SecretKeyFourier<DataSk, FFT64>,
236 source_xa: &mut Source,
237 source_xe: &mut Source,
238 sigma: f64,
239 scratch: &mut Scratch,
240 ) where
241 ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
242 {
243 self.encrypt_sk_private(module, None, sk_dft, source_xa, source_xe, sigma, scratch);
244 }
245
246 pub fn encrypt_pk<DataPt, DataPk>(
247 &mut self,
248 module: &Module<FFT64>,
249 pt: &GLWEPlaintext<DataPt>,
250 pk: &GLWEPublicKey<DataPk, FFT64>,
251 source_xu: &mut Source,
252 source_xe: &mut Source,
253 sigma: f64,
254 scratch: &mut Scratch,
255 ) where
256 VecZnx<DataPt>: VecZnxToRef,
257 VecZnxDft<DataPk, FFT64>: VecZnxDftToRef<FFT64>,
258 {
259 self.encrypt_pk_private(
260 module,
261 Some((pt, 0)),
262 pk,
263 source_xu,
264 source_xe,
265 sigma,
266 scratch,
267 );
268 }
269
270 pub fn encrypt_zero_pk<DataPk>(
271 &mut self,
272 module: &Module<FFT64>,
273 pk: &GLWEPublicKey<DataPk, FFT64>,
274 source_xu: &mut Source,
275 source_xe: &mut Source,
276 sigma: f64,
277 scratch: &mut Scratch,
278 ) where
279 VecZnxDft<DataPk, FFT64>: VecZnxDftToRef<FFT64>,
280 {
281 self.encrypt_pk_private(module, None, pk, source_xu, source_xe, sigma, scratch);
282 }
283
284 pub fn automorphism<DataLhs, DataRhs>(
285 &mut self,
286 module: &Module<FFT64>,
287 lhs: &GLWECiphertext<DataLhs>,
288 rhs: &AutomorphismKey<DataRhs, FFT64>,
289 scratch: &mut Scratch,
290 ) where
291 VecZnx<DataLhs>: VecZnxToRef,
292 MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
293 {
294 self.keyswitch(module, lhs, &rhs.key, scratch);
295 (0..self.rank() + 1).for_each(|i| {
296 module.vec_znx_automorphism_inplace(rhs.p(), self, i);
297 })
298 }
299
300 pub fn automorphism_inplace<DataRhs>(
301 &mut self,
302 module: &Module<FFT64>,
303 rhs: &AutomorphismKey<DataRhs, FFT64>,
304 scratch: &mut Scratch,
305 ) where
306 MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
307 {
308 self.keyswitch_inplace(module, &rhs.key, scratch);
309 (0..self.rank() + 1).for_each(|i| {
310 module.vec_znx_automorphism_inplace(rhs.p(), self, i);
311 })
312 }
313
314 pub(crate) fn keyswitch_from_fourier<DataLhs, DataRhs>(
315 &mut self,
316 module: &Module<FFT64>,
317 lhs: &GLWECiphertextFourier<DataLhs, FFT64>,
318 rhs: &GLWESwitchingKey<DataRhs, FFT64>,
319 scratch: &mut Scratch,
320 ) where
321 VecZnxDft<DataLhs, FFT64>: VecZnxDftToRef<FFT64>,
322 MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
323 {
324 let basek: usize = self.basek();
325
326 #[cfg(debug_assertions)]
327 {
328 assert_eq!(lhs.rank(), rhs.rank_in());
329 assert_eq!(self.rank(), rhs.rank_out());
330 assert_eq!(self.basek(), basek);
331 assert_eq!(lhs.basek(), basek);
332 assert_eq!(rhs.n(), module.n());
333 assert_eq!(self.n(), module.n());
334 assert_eq!(lhs.n(), module.n());
335 assert!(
336 scratch.available()
337 >= GLWECiphertext::keyswitch_from_fourier_scratch_space(
338 module,
339 self.size(),
340 self.rank(),
341 lhs.size(),
342 lhs.rank(),
343 rhs.size(),
344 )
345 );
346 }
347
348 let cols_in: usize = rhs.rank_in();
349 let cols_out: usize = rhs.rank_out() + 1;
350
351 // Buffer of the result of VMP in DFT
352 let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, cols_out, rhs.size()); // Todo optimise
353
354 {
355 // Applies VMP
356 let (mut ai_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, cols_in, lhs.size());
357 (0..cols_in).for_each(|col_i| {
358 module.vec_znx_dft_copy(&mut ai_dft, col_i, lhs, col_i + 1);
359 });
360 module.vmp_apply(&mut res_dft, &ai_dft, rhs, scratch2);
361 }
362
363 module.vec_znx_dft_add_inplace(&mut res_dft, 0, lhs, 0);
364
365 // Switches result of VMP outside of DFT
366 let res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume::<&mut [u8]>(res_dft);
367
368 (0..cols_out).for_each(|i| {
369 module.vec_znx_big_normalize(basek, self, i, &res_big, i, scratch1);
370 });
371 }
372
373 pub fn keyswitch<DataLhs, DataRhs>(
374 &mut self,
375 module: &Module<FFT64>,
376 lhs: &GLWECiphertext<DataLhs>,
377 rhs: &GLWESwitchingKey<DataRhs, FFT64>,
378 scratch: &mut Scratch,
379 ) where
380 VecZnx<DataLhs>: VecZnxToRef,
381 MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
382 {
383 let basek: usize = self.basek();
384
385 #[cfg(debug_assertions)]
386 {
387 assert_eq!(lhs.rank(), rhs.rank_in());
388 assert_eq!(self.rank(), rhs.rank_out());
389 assert_eq!(self.basek(), basek);
390 assert_eq!(lhs.basek(), basek);
391 assert_eq!(rhs.n(), module.n());
392 assert_eq!(self.n(), module.n());
393 assert_eq!(lhs.n(), module.n());
394 assert!(
395 scratch.available()
396 >= GLWECiphertext::keyswitch_scratch_space(
397 module,
398 self.size(),
399 self.rank(),
400 lhs.size(),
401 lhs.rank(),
402 rhs.size(),
403 )
404 );
405 }
406
407 let cols_in: usize = rhs.rank_in();
408 let cols_out: usize = rhs.rank_out() + 1;
409
410 let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, cols_out, rhs.size()); // Todo optimise
411
412 {
413 let (mut ai_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, cols_in, lhs.size());
414 (0..cols_in).for_each(|col_i| {
415 module.vec_znx_dft(&mut ai_dft, col_i, lhs, col_i + 1);
416 });
417 module.vmp_apply(&mut res_dft, &ai_dft, rhs, scratch2);
418 }
419
420 let mut res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(res_dft);
421
422 module.vec_znx_big_add_small_inplace(&mut res_big, 0, lhs, 0);
423
424 (0..cols_out).for_each(|i| {
425 module.vec_znx_big_normalize(basek, self, i, &res_big, i, scratch1);
426 });
427 }
428
429 pub fn keyswitch_inplace<DataRhs>(
430 &mut self,
431 module: &Module<FFT64>,
432 rhs: &GLWESwitchingKey<DataRhs, FFT64>,
433 scratch: &mut Scratch,
434 ) where
435 MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
436 {
437 unsafe {
438 let self_ptr: *mut GLWECiphertext<DataSelf> = self as *mut GLWECiphertext<DataSelf>;
439 self.keyswitch(&module, &*self_ptr, rhs, scratch);
440 }
441 }
442
443 pub fn external_product<DataLhs, DataRhs>(
444 &mut self,
445 module: &Module<FFT64>,
446 lhs: &GLWECiphertext<DataLhs>,
447 rhs: &GGSWCiphertext<DataRhs, FFT64>,
448 scratch: &mut Scratch,
449 ) where
450 VecZnx<DataLhs>: VecZnxToRef,
451 MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
452 {
453 let basek: usize = self.basek();
454
455 #[cfg(debug_assertions)]
456 {
457 assert_eq!(rhs.rank(), lhs.rank());
458 assert_eq!(rhs.rank(), self.rank());
459 assert_eq!(self.basek(), basek);
460 assert_eq!(lhs.basek(), basek);
461 assert_eq!(rhs.n(), module.n());
462 assert_eq!(self.n(), module.n());
463 assert_eq!(lhs.n(), module.n());
464 }
465
466 let cols: usize = rhs.rank() + 1;
467
468 let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, cols, rhs.size()); // Todo optimise
469
470 {
471 let (mut a_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, cols, lhs.size());
472 (0..cols).for_each(|col_i| {
473 module.vec_znx_dft(&mut a_dft, col_i, lhs, col_i);
474 });
475 module.vmp_apply(&mut res_dft, &a_dft, rhs, scratch2);
476 }
477
478 let res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(res_dft);
479
480 (0..cols).for_each(|i| {
481 module.vec_znx_big_normalize(basek, self, i, &res_big, i, scratch1);
482 });
483 }
484
485 pub fn external_product_inplace<DataRhs>(
486 &mut self,
487 module: &Module<FFT64>,
488 rhs: &GGSWCiphertext<DataRhs, FFT64>,
489 scratch: &mut Scratch,
490 ) where
491 MatZnxDft<DataRhs, FFT64>: MatZnxDftToRef<FFT64>,
492 {
493 unsafe {
494 let self_ptr: *mut GLWECiphertext<DataSelf> = self as *mut GLWECiphertext<DataSelf>;
495 self.external_product(&module, &*self_ptr, rhs, scratch);
496 }
497 }
498
499 pub(crate) fn encrypt_sk_private<DataPt, DataSk>(
500 &mut self,
501 module: &Module<FFT64>,
502 pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
503 sk_dft: &SecretKeyFourier<DataSk, FFT64>,
504 source_xa: &mut Source,
505 source_xe: &mut Source,
506 sigma: f64,
507 scratch: &mut Scratch,
508 ) where
509 VecZnx<DataPt>: VecZnxToRef,
510 ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
511 {
512 #[cfg(debug_assertions)]
513 {
514 assert_eq!(self.rank(), sk_dft.rank());
515 assert_eq!(sk_dft.n(), module.n());
516 assert_eq!(self.n(), module.n());
517 if let Some((pt, col)) = pt {
518 assert_eq!(pt.n(), module.n());
519 assert!(col < self.rank() + 1);
520 }
521 }
522
523 let log_base2k: usize = self.basek();
524 let log_k: usize = self.k();
525 let size: usize = self.size();
526 let cols: usize = self.rank() + 1;
527
528 let (mut c0_big, scratch_1) = scratch.tmp_vec_znx(module, 1, size);
529 c0_big.zero();
530
531 {
532 // c[i] = uniform
533 // c[0] -= c[i] * s[i],
534 (1..cols).for_each(|i| {
535 let (mut ci_dft, scratch_2) = scratch_1.tmp_vec_znx_dft(module, 1, size);
536
537 // c[i] = uniform
538 self.data.fill_uniform(log_base2k, i, size, source_xa);
539
540 // c[i] = norm(IDFT(DFT(c[i]) * DFT(s[i])))
541 module.vec_znx_dft(&mut ci_dft, 0, self, i);
542 module.svp_apply_inplace(&mut ci_dft, 0, sk_dft, i - 1);
543 let ci_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(ci_dft);
544
545 // use c[0] as buffer, which is overwritten later by the normalization step
546 module.vec_znx_big_normalize(log_base2k, self, 0, &ci_big, 0, scratch_2);
547
548 // c0_tmp = -c[i] * s[i] (use c[0] as buffer)
549 module.vec_znx_sub_ab_inplace(&mut c0_big, 0, self, 0);
550
551 // c[i] += m if col = i
552 if let Some((pt, col)) = pt {
553 if i == col {
554 module.vec_znx_add_inplace(self, i, pt, 0);
555 module.vec_znx_normalize_inplace(log_base2k, self, i, scratch_2);
556 }
557 }
558 });
559 }
560
561 // c[0] += e
562 c0_big.add_normal(log_base2k, 0, log_k, source_xe, sigma, sigma * SIX_SIGMA);
563
564 // c[0] += m if col = 0
565 if let Some((pt, col)) = pt {
566 if col == 0 {
567 module.vec_znx_add_inplace(&mut c0_big, 0, pt, 0);
568 }
569 }
570
571 // c[0] = norm(c[0])
572 module.vec_znx_normalize(log_base2k, self, 0, &c0_big, 0, scratch_1);
573 }
574
575 pub(crate) fn encrypt_pk_private<DataPt, DataPk>(
576 &mut self,
577 module: &Module<FFT64>,
578 pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
579 pk: &GLWEPublicKey<DataPk, FFT64>,
580 source_xu: &mut Source,
581 source_xe: &mut Source,
582 sigma: f64,
583 scratch: &mut Scratch,
584 ) where
585 VecZnx<DataPt>: VecZnxToRef,
586 VecZnxDft<DataPk, FFT64>: VecZnxDftToRef<FFT64>,
587 {
588 #[cfg(debug_assertions)]
589 {
590 assert_eq!(self.basek(), pk.basek());
591 assert_eq!(self.n(), module.n());
592 assert_eq!(pk.n(), module.n());
593 assert_eq!(self.rank(), pk.rank());
594 if let Some((pt, _)) = pt {
595 assert_eq!(pt.basek(), pk.basek());
596 assert_eq!(pt.n(), module.n());
597 }
598 }
599
600 let log_base2k: usize = pk.basek();
601 let size_pk: usize = pk.size();
602 let cols: usize = self.rank() + 1;
603
604 // Generates u according to the underlying secret distribution.
605 let (mut u_dft, scratch_1) = scratch.tmp_scalar_znx_dft(module, 1);
606
607 {
608 let (mut u, _) = scratch_1.tmp_scalar_znx(module, 1);
609 match pk.dist {
610 SecretDistribution::NONE => panic!(
611 "invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through \
612 Self::generate"
613 ),
614 SecretDistribution::TernaryFixed(hw) => u.fill_ternary_hw(0, hw, source_xu),
615 SecretDistribution::TernaryProb(prob) => u.fill_ternary_prob(0, prob, source_xu),
616 SecretDistribution::ZERO => {}
617 }
618
619 module.svp_prepare(&mut u_dft, 0, &u, 0);
620 }
621
622 // ct[i] = pk[i] * u + ei (+ m if col = i)
623 (0..cols).for_each(|i| {
624 let (mut ci_dft, scratch_2) = scratch_1.tmp_vec_znx_dft(module, 1, size_pk);
625 // ci_dft = DFT(u) * DFT(pk[i])
626 module.svp_apply(&mut ci_dft, 0, &u_dft, 0, pk, i);
627
628 // ci_big = u * p[i]
629 let mut ci_big = module.vec_znx_idft_consume(ci_dft);
630
631 // ci_big = u * pk[i] + e
632 ci_big.add_normal(log_base2k, 0, pk.k(), source_xe, sigma, sigma * SIX_SIGMA);
633
634 // ci_big = u * pk[i] + e + m (if col = i)
635 if let Some((pt, col)) = pt {
636 if col == i {
637 module.vec_znx_big_add_small_inplace(&mut ci_big, 0, pt, 0);
638 }
639 }
640
641 // ct[i] = norm(ci_big)
642 module.vec_znx_big_normalize(log_base2k, self, i, &ci_big, 0, scratch_2);
643 });
644 }
645}
646
647impl<DataSelf> GLWECiphertext<DataSelf>
648where
649 VecZnx<DataSelf>: VecZnxToRef,
650{
651 pub fn decrypt<DataPt, DataSk>(
652 &self,
653 module: &Module<FFT64>,
654 pt: &mut GLWEPlaintext<DataPt>,
655 sk_dft: &SecretKeyFourier<DataSk, FFT64>,
656 scratch: &mut Scratch,
657 ) where
658 VecZnx<DataPt>: VecZnxToMut,
659 ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
660 {
661 #[cfg(debug_assertions)]
662 {
663 assert_eq!(self.rank(), sk_dft.rank());
664 assert_eq!(self.n(), module.n());
665 assert_eq!(pt.n(), module.n());
666 assert_eq!(sk_dft.n(), module.n());
667 }
668
669 let cols: usize = self.rank() + 1;
670
671 let (mut c0_big, scratch_1) = scratch.tmp_vec_znx_big(module, 1, self.size()); // TODO optimize size when pt << ct
672 c0_big.zero();
673
674 {
675 (1..cols).for_each(|i| {
676 // ci_dft = DFT(a[i]) * DFT(s[i])
677 let (mut ci_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, self.size()); // TODO optimize size when pt << ct
678 module.vec_znx_dft(&mut ci_dft, 0, self, i);
679 module.svp_apply_inplace(&mut ci_dft, 0, sk_dft, i - 1);
680 let ci_big = module.vec_znx_idft_consume(ci_dft);
681
682 // c0_big += a[i] * s[i]
683 module.vec_znx_big_add_inplace(&mut c0_big, 0, &ci_big, 0);
684 });
685 }
686
687 // c0_big = (a * s) + (-a * s + m + e) = BIG(m + e)
688 module.vec_znx_big_add_small_inplace(&mut c0_big, 0, self, 0);
689
690 // pt = norm(BIG(m + e))
691 module.vec_znx_big_normalize(self.basek(), pt, 0, &mut c0_big, 0, scratch_1);
692
693 pt.basek = self.basek();
694 pt.k = pt.k().min(self.k());
695 }
696}