poulpy/poulpy-core/src/tests/test_suite/glwe_packing.rs
poulpy-core/src/tests/test_suite/glwe_packing.rsRaw
1use std::collections::HashMap;
2
3use itertools::Itertools;
4use poulpy_hal::{
5 api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
6 layouts::{Backend, Module, Scratch, ScratchOwned},
7 source::Source,
8};
9
10use crate::{
11 GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWENoise, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore,
12 layouts::{
13 GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext,
14 GLWESecret, GLWESecretPreparedFactory,
15 prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared},
16 },
17};
18
19pub fn test_glwe_packing<BE: Backend>(module: &Module<BE>)
20where
21 Module<BE>: GLWEEncryptSk<BE>
22 + GLWEAutomorphismKeyEncryptSk<BE>
23 + GLWEAutomorphismKeyPreparedFactory<BE>
24 + GLWEPacking<BE>
25 + GLWESecretPreparedFactory<BE>
26 + GLWESub
27 + GLWEDecrypt<BE>
28 + GLWERotate<BE>
29 + GLWENoise<BE>,
30 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
31 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
32{
33 let mut source_xs: Source = Source::new([0u8; 32]);
34 let mut source_xe: Source = Source::new([0u8; 32]);
35 let mut source_xa: Source = Source::new([0u8; 32]);
36
37 let n: usize = module.n();
38 let out_base2k: usize = 15;
39 let key_base2k: usize = 10;
40 let k_ct: usize = 36;
41 let pt_k: usize = out_base2k;
42 let rank: usize = 3;
43 let dsize: usize = 1;
44 let k_ksk: usize = k_ct + key_base2k * dsize;
45
46 let dnum: usize = k_ct.div_ceil(key_base2k * dsize);
47
48 let glwe_out_infos: GLWELayout = GLWELayout {
49 n: n.into(),
50 base2k: out_base2k.into(),
51 k: k_ct.into(),
52 rank: rank.into(),
53 };
54
55 let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout {
56 n: n.into(),
57 base2k: key_base2k.into(),
58 k: k_ksk.into(),
59 rank: rank.into(),
60 dsize: dsize.into(),
61 dnum: dnum.into(),
62 };
63
64 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
65 GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
66 .max(GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos))
67 .max(module.glwe_pack_tmp_bytes(&glwe_out_infos, &key_infos)),
68 );
69
70 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos);
71 sk.fill_ternary_prob(0.5, &mut source_xs);
72
73 let mut sk_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
74 sk_prep.prepare(module, &sk);
75
76 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
77 let mut data: Vec<i64> = vec![0i64; n];
78 data.iter_mut().enumerate().for_each(|(i, x)| {
79 *x = i as i64;
80 });
81
82 pt.encode_vec_i64(&data, pt_k.into());
83
84 let gal_els: Vec<i64> = module.glwe_pack_galois_elements();
85
86 let mut auto_keys: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new();
87 let mut tmp: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&key_infos);
88 gal_els.iter().for_each(|gal_el| {
89 tmp.encrypt_sk(module, *gal_el, &sk, &mut source_xa, &mut source_xe, scratch.borrow());
90 let mut atk_prepared: GLWEAutomorphismKeyPrepared<Vec<u8>, BE> =
91 GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &tmp);
92 atk_prepared.prepare(module, &tmp, scratch.borrow());
93 auto_keys.insert(*gal_el, atk_prepared);
94 });
95
96 let mut cts = (0..n)
97 .step_by(5)
98 .map(|_| {
99 let mut ct = GLWE::alloc_from_infos(&glwe_out_infos);
100 ct.encrypt_sk(module, &pt, &sk_prep, &mut source_xa, &mut source_xe, scratch.borrow());
101 module.glwe_rotate_inplace(-5, &mut pt, scratch.borrow()); // X^-batch * pt
102 ct
103 })
104 .collect_vec();
105
106 let mut cts_map: HashMap<usize, &mut GLWE<Vec<u8>>> = HashMap::new();
107
108 for (i, ct) in cts.iter_mut().enumerate() {
109 cts_map.insert(5 * i, ct);
110 }
111
112 let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
113
114 module.glwe_pack(&mut res, cts_map, 0, &auto_keys, scratch.borrow());
115
116 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
117 let mut data: Vec<i64> = vec![0i64; n];
118 data.iter_mut().enumerate().for_each(|(i, x)| {
119 if i.is_multiple_of(5) {
120 *x = i as i64;
121 }
122 });
123
124 pt_want.encode_vec_i64(&data, pt_k.into());
125
126 assert!(res.noise(module, &pt_want, &sk_prep, scratch.borrow()).std().log2() <= ((k_ct - out_base2k) as f64));
127}