1use poulpy_hal::{
2 api::ModuleLogN,
3 layouts::{Backend, GaloisElement, Module, Scratch},
4};
5
6use crate::{
7 GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore,
8 glwe_trace::GLWETrace,
9 layouts::{
10 GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEAutomorphismKeyHelper, GLWEInfos, GLWEToMut, GLWEToRef, GetGaloisElement,
11 LWEInfos,
12 },
13};
14
15pub struct GLWEPacker {
20 accumulators: Vec<Accumulator>,
21 log_batch: usize,
22 counter: usize,
23}
24
25struct Accumulator {
28 data: GLWE<Vec<u8>>,
29 value: bool, control: bool, }
32
33impl Accumulator {
34 pub fn alloc<A>(infos: &A) -> Self
43 where
44 A: GLWEInfos,
45 {
46 Self {
47 data: GLWE::alloc_from_infos(infos),
48 value: false,
49 control: false,
50 }
51 }
52}
53
54impl GLWEPacker {
55 pub fn alloc<A>(infos: &A, log_batch: usize) -> Self
65 where
66 A: GLWEInfos,
67 {
68 let mut accumulators: Vec<Accumulator> = Vec::<Accumulator>::new();
69 let log_n: usize = infos.n().log2();
70 (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(infos)));
71 GLWEPacker {
72 accumulators,
73 log_batch,
74 counter: 0,
75 }
76 }
77
78 fn reset(&mut self) {
80 for i in 0..self.accumulators.len() {
81 self.accumulators[i].value = false;
82 self.accumulators[i].control = false;
83 }
84 self.counter = 0;
85 }
86
87 pub fn tmp_bytes<R, K, M, BE: Backend>(module: &M, res_infos: &R, key_infos: &K) -> usize
89 where
90 R: GLWEInfos,
91 K: GGLWEInfos,
92 M: GLWEPackerOps<BE>,
93 {
94 GLWE::bytes_of_from_infos(res_infos)
95 + module
96 .glwe_rsh_tmp_byte()
97 .max(module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos))
98 }
99
100 pub fn galois_elements<M, BE: Backend>(module: &M) -> Vec<i64>
101 where
102 M: GLWETrace<BE>,
103 {
104 module.glwe_trace_galois_elements()
105 }
106
107 pub fn add<A, K, H, M, BE: Backend>(&mut self, module: &M, a: Option<&A>, auto_keys: &H, scratch: &mut Scratch<BE>)
117 where
118 A: GLWEToRef + GLWEInfos,
119 K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
120 H: GLWEAutomorphismKeyHelper<K, BE>,
121 M: GLWEPackerOps<BE>,
122 Scratch<BE>: ScratchTakeCore<BE>,
123 {
124 assert!(
125 (self.counter as u32) < self.accumulators[0].data.n(),
126 "Packing limit of {} reached",
127 self.accumulators[0].data.n().0 as usize >> self.log_batch
128 );
129
130 module.packer_add(self, a, self.log_batch, auto_keys, scratch);
131 self.counter += 1 << self.log_batch;
132 }
133
134 pub fn flush<R, M, BE: Backend>(&mut self, module: &M, res: &mut R, scratch: &mut Scratch<BE>)
136 where
137 R: GLWEToMut + GLWEInfos,
138 M: GLWEPackerOps<BE>,
139 Scratch<BE>: ScratchTakeCore<BE>,
140 {
141 assert!(self.counter as u32 == self.accumulators[0].data.n());
142
143 let out: &GLWE<Vec<u8>> = &self.accumulators[module.log_n() - self.log_batch - 1].data;
144
145 if out.base2k() == res.base2k() {
146 module.glwe_copy(res, out)
147 } else {
148 module.glwe_normalize(res, out, scratch);
149 }
150
151 self.reset();
152 }
153}
154
155impl<BE: Backend> GLWEPackerOps<BE> for Module<BE> where
156 Self: Sized
157 + ModuleLogN
158 + GLWEAutomorphism<BE>
159 + GaloisElement
160 + GLWERotate<BE>
161 + GLWESub
162 + GLWEShift<BE>
163 + GLWEAdd
164 + GLWENormalize<BE>
165 + GLWECopy
166 + GLWEAutomorphism<BE>
167 + GaloisElement
168 + GLWERotate<BE>
169 + GLWESub
170 + GLWEShift<BE>
171 + GLWEAdd
172 + GLWENormalize<BE>
173{
174}
175
176pub trait GLWEPackerOps<BE: Backend>
177where
178 Self: Sized
179 + ModuleLogN
180 + GLWEAutomorphism<BE>
181 + GaloisElement
182 + GLWERotate<BE>
183 + GLWESub
184 + GLWEShift<BE>
185 + GLWEAdd
186 + GLWENormalize<BE>
187 + GLWECopy
188 + GLWEAutomorphism<BE>
189 + GaloisElement
190 + GLWERotate<BE>
191 + GLWESub
192 + GLWEShift<BE>
193 + GLWEAdd
194 + GLWENormalize<BE>,
195{
196 fn packer_add<A, K, H>(&self, packer: &mut GLWEPacker, a: Option<&A>, i: usize, auto_keys: &H, scratch: &mut Scratch<BE>)
197 where
198 A: GLWEToRef + GLWEInfos,
199 K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
200 H: GLWEAutomorphismKeyHelper<K, BE>,
201 Scratch<BE>: ScratchTakeCore<BE>,
202 {
203 pack_core(self, a, &mut packer.accumulators, i, auto_keys, scratch)
204 }
205}
206
207fn pack_core<A, K, H, M, BE: Backend>(
208 module: &M,
209 a: Option<&A>,
210 accumulators: &mut [Accumulator],
211 i: usize,
212 auto_keys: &H,
213 scratch: &mut Scratch<BE>,
214) where
215 A: GLWEToRef + GLWEInfos,
216 M: ModuleLogN
217 + GLWEAutomorphism<BE>
218 + GaloisElement
219 + GLWERotate<BE>
220 + GLWESub
221 + GLWEShift<BE>
222 + GLWEAdd
223 + GLWENormalize<BE>
224 + GLWECopy
225 + GLWEAutomorphism<BE>
226 + GaloisElement
227 + GLWERotate<BE>
228 + GLWESub
229 + GLWEShift<BE>
230 + GLWEAdd
231 + GLWENormalize<BE>,
232 K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
233 H: GLWEAutomorphismKeyHelper<K, BE>,
234 Scratch<BE>: ScratchTakeCore<BE>,
235{
236 let log_n: usize = module.log_n();
237
238 if i == log_n {
239 return;
240 }
241
242 let (acc_prev, acc_next) = accumulators.split_at_mut(1);
244
245 if !acc_prev[0].control {
247 let acc_mut_ref: &mut Accumulator = &mut acc_prev[0]; if let Some(a_ref) = a {
251 if a_ref.base2k() == acc_mut_ref.data.base2k() {
252 module.glwe_copy(&mut acc_mut_ref.data, a_ref);
253 } else {
254 module.glwe_normalize(&mut acc_mut_ref.data, a_ref, scratch);
255 }
256 acc_mut_ref.value = true
257 } else {
258 acc_mut_ref.value = false
259 }
260 acc_mut_ref.control = true; } else {
262 combine(module, &mut acc_prev[0], a, i, auto_keys, scratch);
264 acc_prev[0].control = false;
265
266 if acc_prev[0].value {
268 pack_core(
269 module,
270 Some(&acc_prev[0].data),
271 acc_next,
272 i + 1,
273 auto_keys,
274 scratch,
275 );
276 } else {
277 pack_core(
278 module,
279 None::<&GLWE<Vec<u8>>>,
280 acc_next,
281 i + 1,
282 auto_keys,
283 scratch,
284 );
285 }
286 }
287}
288
289fn combine<B, K, H, M, BE: Backend>(
290 module: &M,
291 acc: &mut Accumulator,
292 b: Option<&B>,
293 i: usize,
294 auto_keys: &H,
295 scratch: &mut Scratch<BE>,
296) where
297 B: GLWEToRef + GLWEInfos,
298 B: GLWEToRef + GLWEInfos,
299 M: ModuleLogN
300 + GLWEAutomorphism<BE>
301 + GaloisElement
302 + GLWERotate<BE>
303 + GLWESub
304 + GLWEShift<BE>
305 + GLWEAdd
306 + GLWENormalize<BE>
307 + GLWECopy
308 + GLWEAutomorphism<BE>
309 + GaloisElement
310 + GLWERotate<BE>
311 + GLWESub
312 + GLWEShift<BE>
313 + GLWEAdd
314 + GLWENormalize<BE>,
315 K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
316 H: GLWEAutomorphismKeyHelper<K, BE>,
317 Scratch<BE>: ScratchTakeCore<BE>,
318{
319 let log_n: usize = acc.data.n().log2();
320 let a: &mut GLWE<Vec<u8>> = &mut acc.data;
321
322 let gal_el: i64 = if i == 0 {
323 -1
324 } else {
325 module.galois_element(1 << (i - 1))
326 };
327
328 let t: i64 = 1 << (log_n - i - 1);
329
330 if acc.value {
341 if let Some(b) = b {
342 let (mut tmp, scratch_1) = scratch.take_glwe(a);
343
344 module.glwe_rotate_inplace(-t, a, scratch_1);
346
347 module.glwe_sub(&mut tmp, a, b);
349 module.glwe_rsh(1, &mut tmp, scratch_1);
350 module.glwe_add_inplace(a, b);
352
353 module.glwe_rsh(1, a, scratch_1);
354 module.glwe_normalize_inplace(&mut tmp, scratch_1);
355
356 if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) {
358 module.glwe_automorphism_inplace(&mut tmp, auto_key, scratch_1);
359 } else {
360 panic!("auto_key[{gal_el}] not found");
361 }
362
363 module.glwe_sub_inplace(a, &tmp);
365 module.glwe_normalize_inplace(a, scratch_1);
366
367 module.glwe_rotate_inplace(t, a, scratch_1);
371 } else {
372 module.glwe_rsh(1, a, scratch);
373 if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) {
375 module.glwe_automorphism_add_inplace(a, auto_key, scratch);
376 } else {
377 panic!("auto_key[{gal_el}] not found");
378 }
379 }
380 } else if let Some(b) = b {
381 let (mut tmp_b, scratch_1) = scratch.take_glwe(a);
382 module.glwe_rotate(t, &mut tmp_b, b);
383 module.glwe_rsh(1, &mut tmp_b, scratch_1);
384
385 if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) {
387 module.glwe_automorphism_sub_negate(a, &tmp_b, auto_key, scratch_1);
388 } else {
389 panic!("auto_key[{gal_el}] not found");
390 }
391
392 acc.value = true;
393 }
394}