poulpy/poulpy-core/src/keyswitching/ggsw_ct.rs
poulpy-core/src/keyswitching/ggsw_ct.rsRaw
1use poulpy_hal::{
2 api::{
3 ScratchAvailable, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize,
4 VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAllocBytes, VecZnxDftCopy, VecZnxDftFromVecZnx,
5 VecZnxDftToVecZnxBigConsume, VecZnxDftToVecZnxBigTmpA, VecZnxNormalizeTmpBytes, VmpApply, VmpApplyAdd, VmpApplyTmpBytes,
6 ZnxInfos,
7 },
8 layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx, VmpPMat},
9};
10
11use crate::{
12 layouts::{
13 GGLWECiphertext, GGSWCiphertext, GLWECiphertext, Infos,
14 prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared},
15 },
16 operations::GLWEOperations,
17};
18
19impl GGSWCiphertext<Vec<u8>> {
20 pub(crate) fn expand_row_scratch_space<B: Backend>(
21 module: &Module<B>,
22 basek: usize,
23 self_k: usize,
24 k_tsk: usize,
25 digits: usize,
26 rank: usize,
27 ) -> usize
28 where
29 Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes,
30 {
31 let tsk_size: usize = k_tsk.div_ceil(basek);
32 let self_size_out: usize = self_k.div_ceil(basek);
33 let self_size_in: usize = self_size_out.div_ceil(digits);
34
35 let tmp_dft_i: usize = module.vec_znx_dft_alloc_bytes(rank + 1, tsk_size);
36 let tmp_a: usize = module.vec_znx_dft_alloc_bytes(1, self_size_in);
37 let vmp: usize = module.vmp_apply_tmp_bytes(
38 self_size_out,
39 self_size_in,
40 self_size_in,
41 rank,
42 rank,
43 tsk_size,
44 );
45 let tmp_idft: usize = module.vec_znx_big_alloc_bytes(1, tsk_size);
46 let norm: usize = module.vec_znx_normalize_tmp_bytes();
47 tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm))
48 }
49
50 #[allow(clippy::too_many_arguments)]
51 pub fn keyswitch_scratch_space<B: Backend>(
52 module: &Module<B>,
53 basek: usize,
54 k_out: usize,
55 k_in: usize,
56 k_ksk: usize,
57 digits_ksk: usize,
58 k_tsk: usize,
59 digits_tsk: usize,
60 rank: usize,
61 ) -> usize
62 where
63 Module<B>:
64 VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes,
65 {
66 let out_size: usize = k_out.div_ceil(basek);
67 let res_znx: usize = VecZnx::alloc_bytes(module.n(), rank + 1, out_size);
68 let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, out_size);
69 let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, basek, k_out, k_in, k_ksk, digits_ksk, rank, rank);
70 let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, basek, k_out, k_tsk, digits_tsk, rank);
71 let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, out_size);
72 res_znx + ci_dft + (ks | expand_rows | res_dft)
73 }
74
75 #[allow(clippy::too_many_arguments)]
76 pub fn keyswitch_inplace_scratch_space<B: Backend>(
77 module: &Module<B>,
78 basek: usize,
79 k_out: usize,
80 k_ksk: usize,
81 digits_ksk: usize,
82 k_tsk: usize,
83 digits_tsk: usize,
84 rank: usize,
85 ) -> usize
86 where
87 Module<B>:
88 VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes,
89 {
90 GGSWCiphertext::keyswitch_scratch_space(
91 module, basek, k_out, k_out, k_ksk, digits_ksk, k_tsk, digits_tsk, rank,
92 )
93 }
94}
95
96impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
97 pub fn from_gglwe<DataA, DataTsk, B: Backend>(
98 &mut self,
99 module: &Module<B>,
100 a: &GGLWECiphertext<DataA>,
101 tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
102 scratch: &mut Scratch<B>,
103 ) where
104 DataA: DataRef,
105 DataTsk: DataRef,
106 Module<B>: VecZnxCopy
107 + VecZnxDftAllocBytes
108 + VmpApplyTmpBytes
109 + VecZnxBigAllocBytes
110 + VecZnxNormalizeTmpBytes
111 + VecZnxDftFromVecZnx<B>
112 + VecZnxDftCopy<B>
113 + VmpApply<B>
114 + VmpApplyAdd<B>
115 + VecZnxDftAddInplace<B>
116 + VecZnxBigNormalize<B>
117 + VecZnxDftToVecZnxBigTmpA<B>,
118 Scratch<B>: ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B>,
119 {
120 #[cfg(debug_assertions)]
121 {
122 assert_eq!(self.rank(), a.rank());
123 assert_eq!(self.rows(), a.rows());
124 assert_eq!(self.n(), module.n());
125 assert_eq!(a.n(), module.n());
126 assert_eq!(tsk.n(), module.n());
127 }
128 (0..self.rows()).for_each(|row_i| {
129 self.at_mut(row_i, 0).copy(module, &a.at(row_i, 0));
130 });
131 self.expand_row(module, tsk, scratch);
132 }
133
134 pub fn keyswitch<DataLhs: DataRef, DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
135 &mut self,
136 module: &Module<B>,
137 lhs: &GGSWCiphertext<DataLhs>,
138 ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
139 tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
140 scratch: &mut Scratch<B>,
141 ) where
142 Module<B>: VecZnxDftAllocBytes
143 + VmpApplyTmpBytes
144 + VecZnxBigNormalizeTmpBytes
145 + VmpApply<B>
146 + VmpApplyAdd<B>
147 + VecZnxDftFromVecZnx<B>
148 + VecZnxDftToVecZnxBigConsume<B>
149 + VecZnxBigAddSmallInplace<B>
150 + VecZnxBigNormalize<B>
151 + VecZnxDftAllocBytes
152 + VecZnxBigAllocBytes
153 + VecZnxNormalizeTmpBytes
154 + VecZnxDftCopy<B>
155 + VecZnxDftAddInplace<B>
156 + VecZnxDftToVecZnxBigTmpA<B>,
157 Scratch<B>: ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B>,
158 {
159 self.keyswitch_internal(module, lhs, ksk, scratch);
160 self.expand_row(module, tsk, scratch);
161 }
162
163 pub fn keyswitch_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
164 &mut self,
165 module: &Module<B>,
166 ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
167 tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
168 scratch: &mut Scratch<B>,
169 ) where
170 Module<B>: VecZnxDftAllocBytes
171 + VmpApplyTmpBytes
172 + VecZnxBigNormalizeTmpBytes
173 + VmpApply<B>
174 + VmpApplyAdd<B>
175 + VecZnxDftFromVecZnx<B>
176 + VecZnxDftToVecZnxBigConsume<B>
177 + VecZnxBigAddSmallInplace<B>
178 + VecZnxBigNormalize<B>
179 + VecZnxDftAllocBytes
180 + VecZnxBigAllocBytes
181 + VecZnxNormalizeTmpBytes
182 + VecZnxDftCopy<B>
183 + VecZnxDftAddInplace<B>
184 + VecZnxDftToVecZnxBigTmpA<B>,
185 Scratch<B>: ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B>,
186 {
187 unsafe {
188 let self_ptr: *mut GGSWCiphertext<DataSelf> = self as *mut GGSWCiphertext<DataSelf>;
189 self.keyswitch(module, &*self_ptr, ksk, tsk, scratch);
190 }
191 }
192
193 pub fn expand_row<DataTsk: DataRef, B: Backend>(
194 &mut self,
195 module: &Module<B>,
196 tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
197 scratch: &mut Scratch<B>,
198 ) where
199 Module<B>: VecZnxDftAllocBytes
200 + VmpApplyTmpBytes
201 + VecZnxBigAllocBytes
202 + VecZnxNormalizeTmpBytes
203 + VecZnxDftFromVecZnx<B>
204 + VecZnxDftCopy<B>
205 + VmpApply<B>
206 + VmpApplyAdd<B>
207 + VecZnxDftAddInplace<B>
208 + VecZnxBigNormalize<B>
209 + VecZnxDftToVecZnxBigTmpA<B>,
210 Scratch<B>: ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B>,
211 {
212 assert!(
213 scratch.available()
214 >= GGSWCiphertext::expand_row_scratch_space(
215 module,
216 self.basek(),
217 self.k(),
218 tsk.k(),
219 tsk.digits(),
220 tsk.rank()
221 )
222 );
223
224 let n: usize = self.n();
225 let rank: usize = self.rank();
226 let cols: usize = rank + 1;
227
228 // Keyswitch the j-th row of the col 0
229 (0..self.rows()).for_each(|row_i| {
230 // Pre-compute DFT of (a0, a1, a2)
231 let (mut ci_dft, scratch1) = scratch.take_vec_znx_dft(n, cols, self.size());
232 (0..cols).for_each(|i| {
233 module.vec_znx_dft_from_vec_znx(1, 0, &mut ci_dft, i, &self.at(row_i, 0).data, i);
234 });
235
236 (1..cols).for_each(|col_j| {
237 // Example for rank 3:
238 //
239 // Note: M is a vector (m, Bm, B^2m, B^3m, ...), so each column is
240 // actually composed of that many rows and we focus on a specific row here
241 // implicitely given ci_dft.
242 //
243 // # Input
244 //
245 // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 )
246 // col 1: (0, 0, 0, 0)
247 // col 2: (0, 0, 0, 0)
248 // col 3: (0, 0, 0, 0)
249 //
250 // # Output
251 //
252 // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 )
253 // col 1: (-(b0s0 + b1s1 + b2s2) , b0 + M[i], b1 , b2 )
254 // col 2: (-(c0s0 + c1s1 + c2s2) , c0 , c1 + M[i], c2 )
255 // col 3: (-(d0s0 + d1s1 + d2s2) , d0 , d1 , d2 + M[i])
256
257 let digits: usize = tsk.digits();
258
259 let (mut tmp_dft_i, scratch2) = scratch1.take_vec_znx_dft(n, cols, tsk.size());
260 let (mut tmp_a, scratch3) = scratch2.take_vec_znx_dft(n, 1, ci_dft.size().div_ceil(digits));
261
262 {
263 // Performs a key-switch for each combination of s[i]*s[j], i.e. for a0, a1, a2
264 //
265 // # Example for col=1
266 //
267 // a0 * (-(f0s0 + f1s1 + f1s2) + s0^2, f0, f1, f2) = (-(a0f0s0 + a0f1s1 + a0f1s2) + a0s0^2, a0f0, a0f1, a0f2)
268 // +
269 // a1 * (-(g0s0 + g1s1 + g1s2) + s0s1, g0, g1, g2) = (-(a1g0s0 + a1g1s1 + a1g1s2) + a1s0s1, a1g0, a1g1, a1g2)
270 // +
271 // a2 * (-(h0s0 + h1s1 + h1s2) + s0s2, h0, h1, h2) = (-(a2h0s0 + a2h1s1 + a2h1s2) + a2s0s2, a2h0, a2h1, a2h2)
272 // =
273 // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2)
274 (1..cols).for_each(|col_i| {
275 let pmat: &VmpPMat<DataTsk, B> = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j])
276
277 // Extracts a[i] and multipies with Enc(s[i]s[j])
278 (0..digits).for_each(|di| {
279 tmp_a.set_size((ci_dft.size() + di) / digits);
280
281 // Small optimization for digits > 2
282 // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then
283 // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(digits-1) * B}.
284 // As such we can ignore the last digits-2 limbs safely of the sum of vmp products.
285 // It is possible to further ignore the last digits-1 limbs, but this introduce
286 // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same
287 // noise is kept with respect to the ideal functionality.
288 tmp_dft_i.set_size(tsk.size() - ((digits - di) as isize - 2).max(0) as usize);
289
290 module.vec_znx_dft_copy(digits, digits - 1 - di, &mut tmp_a, 0, &ci_dft, col_i);
291 if di == 0 && col_i == 1 {
292 module.vmp_apply(&mut tmp_dft_i, &tmp_a, pmat, scratch3);
293 } else {
294 module.vmp_apply_add(&mut tmp_dft_i, &tmp_a, pmat, di, scratch3);
295 }
296 });
297 });
298 }
299
300 // Adds -(sum a[i] * s[i]) + m) on the i-th column of tmp_idft_i
301 //
302 // (-(x0s0 + x1s1 + x2s2) + a0s0s0 + a1s0s1 + a2s0s2, x0, x1, x2)
303 // +
304 // (0, -(a0s0 + a1s1 + a2s2) + M[i], 0, 0)
305 // =
306 // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0 -(a0s0 + a1s1 + a2s2) + M[i], x1, x2)
307 // =
308 // (-(x0s0 + x1s1 + x2s2), x0 + M[i], x1, x2)
309 module.vec_znx_dft_add_inplace(&mut tmp_dft_i, col_j, &ci_dft, 0);
310 let (mut tmp_idft, scratch3) = scratch2.take_vec_znx_big(n, 1, tsk.size());
311 (0..cols).for_each(|i| {
312 module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut tmp_idft, 0, &mut tmp_dft_i, i);
313 module.vec_znx_big_normalize(
314 self.basek(),
315 &mut self.at_mut(row_i, col_j).data,
316 i,
317 &tmp_idft,
318 0,
319 scratch3,
320 );
321 });
322 })
323 })
324 }
325
326 fn keyswitch_internal<DataLhs: DataRef, DataKsk: DataRef, B: Backend>(
327 &mut self,
328 module: &Module<B>,
329 lhs: &GGSWCiphertext<DataLhs>,
330 ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
331 scratch: &mut Scratch<B>,
332 ) where
333 Module<B>: VecZnxDftAllocBytes
334 + VmpApplyTmpBytes
335 + VecZnxBigNormalizeTmpBytes
336 + VmpApply<B>
337 + VmpApplyAdd<B>
338 + VecZnxDftFromVecZnx<B>
339 + VecZnxDftToVecZnxBigConsume<B>
340 + VecZnxBigAddSmallInplace<B>
341 + VecZnxBigNormalize<B>,
342 Scratch<B>: ScratchAvailable + TakeVecZnxDft<B>,
343 {
344 // Keyswitch the j-th row of the col 0
345 (0..lhs.rows()).for_each(|row_i| {
346 // Key-switch column 0, i.e.
347 // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2)
348 self.at_mut(row_i, 0)
349 .keyswitch(module, &lhs.at(row_i, 0), ksk, scratch);
350 })
351 }
352}