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