poulpy/core/src/tests/generics/packing.rs
core/src/tests/generics/packing.rsRaw
1use std::collections::HashMap;
2
3use backend::hal::{
4 api::{
5 ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigSubSmallBInplace,
6 VecZnxRotateInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
7 },
8 layouts::{Backend, Module, ScratchOwned},
9 oep::{
10 ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
11 TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
12 },
13};
14use sampling::source::Source;
15
16use crate::{
17 GLWEOperations, GLWEPacker,
18 layouts::{
19 GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret,
20 prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
21 },
22 trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily, GLWEPackingFamily},
23};
24
25use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
26
27pub trait PackingTestModuleFamily<B: Backend> = GLWEPackingFamily<B>
28 + GLWESecretPreparedModuleFamily<B>
29 + GGLWESwitchingKeyEncryptSkFamily<B>
30 + GLWEKeyswitchFamily<B>
31 + GLWEDecryptFamily<B>
32 + VecZnxSwithcDegree
33 + VecZnxAddScalarInplace
34 + VecZnxRotateInplace
35 + VecZnxAutomorphism
36 + VecZnxBigSubSmallBInplace<B>
37 + VmpPMatAlloc<B>
38 + VmpPMatPrepare<B>;
39
40pub trait PackingTestScratchFamily<B: Backend> = TakeVecZnxDftImpl<B>
41 + TakeVecZnxBigImpl<B>
42 + TakeSvpPPolImpl<B>
43 + ScratchOwnedAllocImpl<B>
44 + ScratchOwnedBorrowImpl<B>
45 + ScratchAvailableImpl<B>
46 + TakeScalarZnxImpl<B>
47 + TakeVecZnxImpl<B>;
48
49pub fn test_glwe_packing<B: Backend>(module: &Module<B>)
50where
51 Module<B>: PackingTestModuleFamily<B>,
52 B: PackingTestScratchFamily<B>,
53{
54 let mut source_xs: Source = Source::new([0u8; 32]);
55 let mut source_xe: Source = Source::new([0u8; 32]);
56 let mut source_xa: Source = Source::new([0u8; 32]);
57
58 let n: usize = module.n();
59 let basek: usize = 18;
60 let k_ct: usize = 36;
61 let pt_k: usize = 18;
62 let rank: usize = 3;
63 let sigma: f64 = 3.2;
64 let digits: usize = 1;
65 let k_ksk: usize = k_ct + basek * digits;
66
67 let rows: usize = k_ct.div_ceil(basek * digits);
68
69 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
70 GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct)
71 | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank)
72 | GLWEPacker::scratch_space(module, n, basek, k_ct, k_ksk, digits, rank),
73 );
74
75 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
76 sk.fill_ternary_prob(0.5, &mut source_xs);
77 let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
78
79 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_ct);
80 let mut data: Vec<i64> = vec![0i64; n];
81 data.iter_mut().enumerate().for_each(|(i, x)| {
82 *x = i as i64;
83 });
84
85 pt.encode_vec_i64(&data, pt_k);
86
87 let gal_els: Vec<i64> = GLWEPacker::galois_elements(module);
88
89 let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
90 let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
91 gal_els.iter().for_each(|gal_el| {
92 tmp.encrypt_sk(
93 module,
94 *gal_el,
95 &sk,
96 &mut source_xa,
97 &mut source_xe,
98 sigma,
99 scratch.borrow(),
100 );
101 let atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow());
102 auto_keys.insert(*gal_el, atk_prepared);
103 });
104
105 let log_batch: usize = 0;
106
107 let mut packer: GLWEPacker = GLWEPacker::new(n, log_batch, basek, k_ct, rank);
108
109 let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
110
111 ct.encrypt_sk(
112 module,
113 &pt,
114 &sk_dft,
115 &mut source_xa,
116 &mut source_xe,
117 sigma,
118 scratch.borrow(),
119 );
120
121 let log_n: usize = module.log_n();
122
123 (0..n >> log_batch).for_each(|i| {
124 ct.encrypt_sk(
125 module,
126 &pt,
127 &sk_dft,
128 &mut source_xa,
129 &mut source_xe,
130 sigma,
131 scratch.borrow(),
132 );
133
134 pt.rotate_inplace(module, -(1 << log_batch)); // X^-batch * pt
135
136 if reverse_bits_msb(i, log_n as u32) % 5 == 0 {
137 packer.add(module, Some(&ct), &auto_keys, scratch.borrow());
138 } else {
139 packer.add(
140 module,
141 None::<&GLWECiphertext<Vec<u8>>>,
142 &auto_keys,
143 scratch.borrow(),
144 )
145 }
146 });
147
148 let mut res = GLWECiphertext::alloc(n, basek, k_ct, rank);
149 packer.flush(module, &mut res);
150
151 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_ct);
152 let mut data: Vec<i64> = vec![0i64; n];
153 data.iter_mut().enumerate().for_each(|(i, x)| {
154 if i % 5 == 0 {
155 *x = reverse_bits_msb(i, log_n as u32) as i64;
156 }
157 });
158
159 pt_want.encode_vec_i64(&data, pt_k);
160
161 res.decrypt(module, &mut pt, &sk_dft, scratch.borrow());
162
163 pt.sub_inplace_ab(module, &pt_want);
164
165 let noise_have: f64 = pt.std().log2();
166 // println!("noise_have: {}", noise_have);
167 assert!(
168 noise_have < -((k_ct - basek) as f64),
169 "noise: {}",
170 noise_have
171 );
172}
173
174#[inline(always)]
175fn reverse_bits_msb(x: usize, n: u32) -> usize {
176 x.reverse_bits() >> (usize::BITS - n)
177}