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 base2k_out: usize = 15;
39 let base2k_key: usize = 10;
40 let k_ct: usize = 36;
41 let pt_k: usize = base2k_out;
42 let rank: usize = 3;
43 let dsize: usize = 1;
44 let k_ksk: usize = k_ct + base2k_key * dsize;
45
46 let dnum: usize = k_ct.div_ceil(base2k_key * dsize);
47
48 let glwe_out_infos: GLWELayout = GLWELayout {
49 n: n.into(),
50 base2k: base2k_out.into(),
51 k: k_ct.into(),
52 rank: rank.into(),
53 };
54
55 let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout {
56 n: n.into(),
57 base2k: base2k_key.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(
67 module, &key_infos,
68 ))
69 .max(module.glwe_pack_tmp_bytes(&glwe_out_infos, &key_infos)),
70 );
71
72 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos);
73 sk.fill_ternary_prob(0.5, &mut source_xs);
74
75 let mut sk_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
76 sk_prep.prepare(module, &sk);
77
78 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
79 let mut data: Vec<i64> = vec![0i64; n];
80 data.iter_mut().enumerate().for_each(|(i, x)| {
81 *x = i as i64;
82 });
83
84 pt.encode_vec_i64(&data, pt_k.into());
85
86 let gal_els: Vec<i64> = module.glwe_pack_galois_elements();
87
88 let mut auto_keys: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new();
89 let mut tmp: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&key_infos);
90 gal_els.iter().for_each(|gal_el| {
91 tmp.encrypt_sk(
92 module,
93 *gal_el,
94 &sk,
95 &mut source_xa,
96 &mut source_xe,
97 scratch.borrow(),
98 );
99 let mut atk_prepared: GLWEAutomorphismKeyPrepared<Vec<u8>, BE> =
100 GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &tmp);
101 atk_prepared.prepare(module, &tmp, scratch.borrow());
102 auto_keys.insert(*gal_el, atk_prepared);
103 });
104
105 let mut cts = (0..n)
106 .step_by(5)
107 .map(|_| {
108 let mut ct = GLWE::alloc_from_infos(&glwe_out_infos);
109 ct.encrypt_sk(
110 module,
111 &pt,
112 &sk_prep,
113 &mut source_xa,
114 &mut source_xe,
115 scratch.borrow(),
116 );
117 module.glwe_rotate_inplace(-5, &mut pt, scratch.borrow()); // X^-batch * pt
118 ct
119 })
120 .collect_vec();
121
122 let mut cts_map: HashMap<usize, &mut GLWE<Vec<u8>>> = HashMap::new();
123
124 for (i, ct) in cts.iter_mut().enumerate() {
125 cts_map.insert(5 * i, ct);
126 }
127
128 let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
129
130 module.glwe_pack(&mut res, cts_map, 0, &auto_keys, scratch.borrow());
131
132 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
133 let mut data: Vec<i64> = vec![0i64; n];
134 data.iter_mut().enumerate().for_each(|(i, x)| {
135 if i.is_multiple_of(5) {
136 *x = i as i64;
137 }
138 });
139
140 pt_want.encode_vec_i64(&data, pt_k.into());
141
142 assert!(
143 res.noise(module, &pt_want, &sk_prep, scratch.borrow())
144 .std()
145 .log2()
146 <= ((k_ct - base2k_out) as f64)
147 );
148}