poulpy/poulpy-core/src/tests/test_suite/packing.rs
poulpy-core/src/tests/test_suite/packing.rsRaw
1use std::collections::HashMap;
2
3use poulpy_hal::{
4 api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
5 layouts::{Backend, Module, Scratch, ScratchOwned},
6 source::Source,
7};
8
9use crate::{
10 AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore,
11 layouts::{
12 AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret,
13 GLWESecretPreparedFactory,
14 prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared},
15 },
16};
17
18pub fn test_glwe_packing<BE: Backend>(module: &Module<BE>)
19where
20 Module<BE>: GLWEEncryptSk<BE>
21 + AutomorphismKeyEncryptSk<BE>
22 + GLWEAutomorphismKeyPreparedFactory<BE>
23 + GLWEPacking<BE>
24 + GLWESecretPreparedFactory<BE>
25 + GLWESub
26 + GLWEDecrypt<BE>
27 + GLWERotate<BE>,
28 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
29 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
30{
31 let mut source_xs: Source = Source::new([0u8; 32]);
32 let mut source_xe: Source = Source::new([0u8; 32]);
33 let mut source_xa: Source = Source::new([0u8; 32]);
34
35 let n: usize = module.n();
36 let base2k: usize = 18;
37 let k_ct: usize = 36;
38 let pt_k: usize = 18;
39 let rank: usize = 3;
40 let dsize: usize = 1;
41 let k_ksk: usize = k_ct + base2k * dsize;
42
43 let dnum: usize = k_ct.div_ceil(base2k * dsize);
44
45 let glwe_out_infos: GLWELayout = GLWELayout {
46 n: n.into(),
47 base2k: base2k.into(),
48 k: k_ct.into(),
49 rank: rank.into(),
50 };
51
52 let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
53 n: n.into(),
54 base2k: base2k.into(),
55 k: k_ksk.into(),
56 rank: rank.into(),
57 dsize: dsize.into(),
58 dnum: dnum.into(),
59 };
60
61 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
62 GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
63 | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos)
64 | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos),
65 );
66
67 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos);
68 sk.fill_ternary_prob(0.5, &mut source_xs);
69
70 let mut sk_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
71 sk_dft.prepare(module, &sk);
72
73 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
74 let mut data: Vec<i64> = vec![0i64; n];
75 data.iter_mut().enumerate().for_each(|(i, x)| {
76 *x = i as i64;
77 });
78
79 pt.encode_vec_i64(&data, pt_k.into());
80
81 let gal_els: Vec<i64> = GLWEPacker::galois_elements(module);
82
83 let mut auto_keys: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new();
84 let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&key_infos);
85 gal_els.iter().for_each(|gal_el| {
86 tmp.encrypt_sk(
87 module,
88 *gal_el,
89 &sk,
90 &mut source_xa,
91 &mut source_xe,
92 scratch.borrow(),
93 );
94 let mut atk_prepared: GLWEAutomorphismKeyPrepared<Vec<u8>, BE> =
95 GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &tmp);
96 atk_prepared.prepare(module, &tmp, scratch.borrow());
97 auto_keys.insert(*gal_el, atk_prepared);
98 });
99
100 let log_batch: usize = 0;
101
102 let mut packer: GLWEPacker = GLWEPacker::alloc(&glwe_out_infos, log_batch);
103
104 let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
105
106 ct.encrypt_sk(
107 module,
108 &pt,
109 &sk_dft,
110 &mut source_xa,
111 &mut source_xe,
112 scratch.borrow(),
113 );
114
115 let log_n: usize = module.log_n();
116
117 (0..n >> log_batch).for_each(|i| {
118 ct.encrypt_sk(
119 module,
120 &pt,
121 &sk_dft,
122 &mut source_xa,
123 &mut source_xe,
124 scratch.borrow(),
125 );
126
127 module.glwe_rotate_inplace(-(1 << log_batch), &mut pt, scratch.borrow()); // X^-batch * pt
128
129 if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) {
130 packer.add(module, Some(&ct), &auto_keys, scratch.borrow());
131 } else {
132 packer.add(module, None::<&GLWE<Vec<u8>>>, &auto_keys, scratch.borrow())
133 }
134 });
135
136 let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
137 packer.flush(module, &mut res);
138
139 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
140 let mut data: Vec<i64> = vec![0i64; n];
141 data.iter_mut().enumerate().for_each(|(i, x)| {
142 if i.is_multiple_of(5) {
143 *x = reverse_bits_msb(i, log_n as u32) as i64;
144 }
145 });
146
147 pt_want.encode_vec_i64(&data, pt_k.into());
148
149 res.decrypt(module, &mut pt, &sk_dft, scratch.borrow());
150
151 module.glwe_sub_inplace(&mut pt, &pt_want);
152
153 let noise_have: f64 = pt.std().log2();
154
155 assert!(
156 noise_have < -((k_ct - base2k) as f64),
157 "noise: {noise_have}"
158 );
159}
160
161#[inline(always)]
162fn reverse_bits_msb(x: usize, n: u32) -> usize {
163 x.reverse_bits() >> (usize::BITS - n)
164}