poulpy/core/src/gglwe_ciphertext.rs
core/src/gglwe_ciphertext.rsRaw
1use base2k::{
2 Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftScratch, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx,
3 ScalarZnxDft, ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigOps, VecZnxBigScratch,
4 VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, VecZnxToMut, VecZnxToRef, ZnxInfos,
5 ZnxZero,
6};
7use sampling::source::Source;
8
9use crate::{
10 elem::{GetRow, Infos, SetRow},
11 glwe_ciphertext::GLWECiphertext,
12 glwe_ciphertext_fourier::GLWECiphertextFourier,
13 glwe_plaintext::GLWEPlaintext,
14 keys::SecretKeyFourier,
15 utils::derive_size,
16 vec_glwe_product::{VecGLWEProduct, VecGLWEProductScratchSpace},
17};
18
19pub struct GGLWECiphertext<C, B: Backend> {
20 pub(crate) data: MatZnxDft<C, B>,
21 pub(crate) basek: usize,
22 pub(crate) k: usize,
23}
24
25impl<B: Backend> GGLWECiphertext<Vec<u8>, B> {
26 pub fn new(module: &Module<B>, base2k: usize, k: usize, rows: usize, rank_in: usize, rank_out: usize) -> Self {
27 Self {
28 data: module.new_mat_znx_dft(rows, rank_in, rank_out + 1, derive_size(base2k, k)),
29 basek: base2k,
30 k,
31 }
32 }
33}
34
35impl<T, B: Backend> Infos for GGLWECiphertext<T, B> {
36 type Inner = MatZnxDft<T, B>;
37
38 fn inner(&self) -> &Self::Inner {
39 &self.data
40 }
41
42 fn basek(&self) -> usize {
43 self.basek
44 }
45
46 fn k(&self) -> usize {
47 self.k
48 }
49}
50
51impl<T, B: Backend> GGLWECiphertext<T, B> {
52 pub fn rank(&self) -> usize {
53 self.data.cols_out() - 1
54 }
55}
56
57impl<C, B: Backend> MatZnxDftToMut<B> for GGLWECiphertext<C, B>
58where
59 MatZnxDft<C, B>: MatZnxDftToMut<B>,
60{
61 fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> {
62 self.data.to_mut()
63 }
64}
65
66impl<C, B: Backend> MatZnxDftToRef<B> for GGLWECiphertext<C, B>
67where
68 MatZnxDft<C, B>: MatZnxDftToRef<B>,
69{
70 fn to_ref(&self) -> MatZnxDft<&[u8], B> {
71 self.data.to_ref()
72 }
73}
74
75impl GGLWECiphertext<Vec<u8>, FFT64> {
76 pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
77 GLWECiphertext::encrypt_sk_scratch_space(module, rank, size)
78 + module.bytes_of_vec_znx(rank + 1, size)
79 + module.bytes_of_vec_znx(1, size)
80 + module.bytes_of_vec_znx_dft(rank + 1, size)
81 }
82
83 pub fn encrypt_pk_scratch_space(_module: &Module<FFT64>, _rank: usize, _pk_size: usize) -> usize {
84 unimplemented!()
85 }
86}
87
88impl<DataSelf> GGLWECiphertext<DataSelf, FFT64>
89where
90 MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + ZnxInfos,
91{
92 pub fn encrypt_sk<DataPt, DataSk>(
93 &mut self,
94 module: &Module<FFT64>,
95 pt: &ScalarZnx<DataPt>,
96 sk_dft: &SecretKeyFourier<DataSk, FFT64>,
97 source_xa: &mut Source,
98 source_xe: &mut Source,
99 sigma: f64,
100 scratch: &mut Scratch,
101 ) where
102 ScalarZnx<DataPt>: ScalarZnxToRef,
103 ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
104 {
105 #[cfg(debug_assertions)]
106 {
107 assert_eq!(self.rank(), sk_dft.rank());
108 assert_eq!(self.n(), module.n());
109 assert_eq!(sk_dft.n(), module.n());
110 assert_eq!(pt.n(), module.n());
111 }
112
113 let rows: usize = self.rows();
114 let size: usize = self.size();
115 let basek: usize = self.basek();
116 let k: usize = self.k();
117
118 let cols: usize = self.rank() + 1;
119
120 let (tmp_znx_pt, scrach_1) = scratch.tmp_vec_znx(module, 1, size);
121 let (tmp_znx_ct, scrach_2) = scrach_1.tmp_vec_znx(module, cols, size);
122 let (tmp_znx_dft_ct, scratch_3) = scrach_2.tmp_vec_znx_dft(module, cols, size);
123
124 let mut vec_znx_pt: GLWEPlaintext<&mut [u8]> = GLWEPlaintext {
125 data: tmp_znx_pt,
126 basek,
127 k,
128 };
129
130 let mut vec_znx_ct: GLWECiphertext<&mut [u8]> = GLWECiphertext {
131 data: tmp_znx_ct,
132 basek,
133 k,
134 };
135
136 let mut vec_znx_ct_dft: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier {
137 data: tmp_znx_dft_ct,
138 basek,
139 k,
140 };
141
142 (0..rows).for_each(|row_i| {
143 // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
144 module.vec_znx_add_scalar_inplace(&mut vec_znx_pt, 0, row_i, pt, 0);
145 module.vec_znx_normalize_inplace(basek, &mut vec_znx_pt, 0, scratch_3);
146
147 // rlwe encrypt of vec_znx_pt into vec_znx_ct
148 vec_znx_ct.encrypt_sk(
149 module,
150 &vec_znx_pt,
151 sk_dft,
152 source_xa,
153 source_xe,
154 sigma,
155 scratch_3,
156 );
157
158 vec_znx_pt.data.zero(); // zeroes for next iteration
159
160 // Switch vec_znx_ct into DFT domain
161 vec_znx_ct.dft(module, &mut vec_znx_ct_dft);
162
163 // Stores vec_znx_dft_ct into thw i-th row of the MatZnxDft
164 module.vmp_prepare_row(self, row_i, 0, &vec_znx_ct_dft);
165 });
166 }
167}
168
169impl<C> GetRow<FFT64> for GGLWECiphertext<C, FFT64>
170where
171 MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>,
172{
173 fn get_row<R>(&self, module: &Module<FFT64>, row_i: usize, col_j: usize, res: &mut GLWECiphertextFourier<R, FFT64>)
174 where
175 VecZnxDft<R, FFT64>: VecZnxDftToMut<FFT64>,
176 {
177 #[cfg(debug_assertions)]
178 {
179 assert_eq!(col_j, 0);
180 }
181 module.vmp_extract_row(res, self, row_i, col_j);
182 }
183}
184
185impl<C> SetRow<FFT64> for GGLWECiphertext<C, FFT64>
186where
187 MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
188{
189 fn set_row<R>(&mut self, module: &Module<FFT64>, row_i: usize, col_j: usize, a: &GLWECiphertextFourier<R, FFT64>)
190 where
191 VecZnxDft<R, FFT64>: VecZnxDftToRef<FFT64>,
192 {
193 #[cfg(debug_assertions)]
194 {
195 assert_eq!(col_j, 0);
196 }
197 module.vmp_prepare_row(self, row_i, col_j, a);
198 }
199}
200
201impl VecGLWEProductScratchSpace for GGLWECiphertext<Vec<u8>, FFT64> {
202 fn prod_with_glwe_scratch_space(module: &Module<FFT64>, res_size: usize, a_size: usize, grlwe_size: usize) -> usize {
203 module.bytes_of_vec_znx_dft(2, grlwe_size)
204 + (module.vec_znx_big_normalize_tmp_bytes()
205 | (module.vmp_apply_tmp_bytes(res_size, a_size, a_size, 1, 2, grlwe_size)
206 + module.bytes_of_vec_znx_dft(1, a_size)))
207 }
208}
209
210impl<C> VecGLWEProduct for GGLWECiphertext<C, FFT64>
211where
212 MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64> + ZnxInfos,
213{
214 fn prod_with_glwe<R, A>(
215 &self,
216 module: &Module<FFT64>,
217 res: &mut GLWECiphertext<R>,
218 a: &GLWECiphertext<A>,
219 scratch: &mut Scratch,
220 ) where
221 MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>,
222 VecZnx<R>: VecZnxToMut,
223 VecZnx<A>: VecZnxToRef,
224 {
225 let log_base2k: usize = self.basek();
226
227 #[cfg(debug_assertions)]
228 {
229 assert_eq!(res.basek(), log_base2k);
230 assert_eq!(a.basek(), log_base2k);
231 assert_eq!(self.n(), module.n());
232 assert_eq!(res.n(), module.n());
233 assert_eq!(a.n(), module.n());
234 }
235
236 let (mut res_dft, scratch1) = scratch.tmp_vec_znx_dft(module, 2, self.size()); // Todo optimise
237
238 {
239 let (mut a1_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, 1, a.size());
240 module.vec_znx_dft(&mut a1_dft, 0, a, 1);
241 module.vmp_apply(&mut res_dft, &a1_dft, self, scratch2);
242 }
243
244 let mut res_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(res_dft);
245
246 module.vec_znx_big_add_small_inplace(&mut res_big, 0, a, 0);
247
248 module.vec_znx_big_normalize(log_base2k, res, 0, &res_big, 0, scratch1);
249 module.vec_znx_big_normalize(log_base2k, res, 1, &res_big, 1, scratch1);
250 }
251}