poulpy/core/src/encryption/compressed/gglwe_ct.rs
core/src/encryption/compressed/gglwe_ct.rsRaw
1use backend::hal::{
2 api::{
3 ScratchAvailable, SvpApplyInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace,
4 VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxFillUniform,
5 VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace, ZnxZero,
6 },
7 layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
8};
9use sampling::source::Source;
10
11use crate::{
12 TakeGLWEPt,
13 encryption::glwe_encrypt_sk_internal,
14 layouts::{GGLWECiphertext, Infos, compressed::GGLWECiphertextCompressed, prepared::GLWESecretPrepared},
15};
16
17impl GGLWECiphertextCompressed<Vec<u8>> {
18 pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize
19 where
20 Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
21 {
22 GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k)
23 }
24}
25
26impl<D: DataMut> GGLWECiphertextCompressed<D> {
27 pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
28 &mut self,
29 module: &Module<B>,
30 pt: &ScalarZnx<DataPt>,
31 sk: &GLWESecretPrepared<DataSk, B>,
32 seed: [u8; 32],
33 source_xe: &mut Source,
34 sigma: f64,
35 scratch: &mut Scratch<B>,
36 ) where
37 Module<B>: VecZnxAddScalarInplace
38 + VecZnxDftAllocBytes
39 + VecZnxBigNormalize<B>
40 + VecZnxDftFromVecZnx<B>
41 + SvpApplyInplace<B>
42 + VecZnxDftToVecZnxBigConsume<B>
43 + VecZnxNormalizeTmpBytes
44 + VecZnxFillUniform
45 + VecZnxSubABInplace
46 + VecZnxAddInplace
47 + VecZnxNormalizeInplace<B>
48 + VecZnxAddNormal
49 + VecZnxNormalize<B>
50 + VecZnxSub,
51 Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
52 {
53 #[cfg(debug_assertions)]
54 {
55 use backend::hal::api::ZnxInfos;
56
57 assert_eq!(
58 self.rank_in(),
59 pt.cols(),
60 "self.rank_in(): {} != pt.cols(): {}",
61 self.rank_in(),
62 pt.cols()
63 );
64 assert_eq!(
65 self.rank_out(),
66 sk.rank(),
67 "self.rank_out(): {} != sk.rank(): {}",
68 self.rank_out(),
69 sk.rank()
70 );
71 assert_eq!(self.n(), sk.n());
72 assert_eq!(pt.n(), sk.n());
73 assert!(
74 scratch.available()
75 >= GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k()),
76 "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(module, self.rank()={}, self.size()={}): {}",
77 scratch.available(),
78 self.rank(),
79 self.size(),
80 GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k())
81 );
82 assert!(
83 self.rows() * self.digits() * self.basek() <= self.k(),
84 "self.rows() : {} * self.digits() : {} * self.basek() : {} = {} >= self.k() = {}",
85 self.rows(),
86 self.digits(),
87 self.basek(),
88 self.rows() * self.digits() * self.basek(),
89 self.k()
90 );
91 }
92
93 let rows: usize = self.rows();
94 let digits: usize = self.digits();
95 let basek: usize = self.basek();
96 let k: usize = self.k();
97 let rank_in: usize = self.rank_in();
98 let cols: usize = self.rank_out() + 1;
99
100 let mut source_xa = Source::new(seed);
101
102 let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(sk.n(), basek, k);
103 (0..rank_in).for_each(|col_i| {
104 (0..rows).for_each(|row_i| {
105 // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
106 tmp_pt.data.zero(); // zeroes for next iteration
107 module.vec_znx_add_scalar_inplace(
108 &mut tmp_pt.data,
109 0,
110 (digits - 1) + row_i * digits,
111 pt,
112 col_i,
113 );
114 module.vec_znx_normalize_inplace(basek, &mut tmp_pt.data, 0, scrach_1);
115
116 let (seed, mut source_xa_tmp) = source_xa.branch();
117 self.seed[col_i * rows + row_i] = seed;
118
119 glwe_encrypt_sk_internal(
120 module,
121 self.basek(),
122 self.k(),
123 &mut self.at_mut(row_i, col_i).data,
124 cols,
125 true,
126 Some((&tmp_pt, 0)),
127 sk,
128 &mut source_xa_tmp,
129 source_xe,
130 sigma,
131 scrach_1,
132 );
133 });
134 });
135 }
136}