poulpy/poulpy-core/src/tests/test_suite/trace.rs
poulpy-core/src/tests/test_suite/trace.rsRaw
1use std::collections::HashMap;
2
3use poulpy_hal::{
4 api::{
5 ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare,
6 VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigAddInplace,
7 VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, VecZnxBigNormalize,
8 VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply,
9 VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes,
10 VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubABInplace, VecZnxSwitchRing, VmpApplyDftToDft,
11 VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare,
12 },
13 layouts::{Backend, Module, ScratchOwned, ZnxView, ZnxViewMut},
14 oep::{
15 ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
16 TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
17 },
18 source::Source,
19};
20
21use crate::{
22 encryption::SIGMA,
23 layouts::{
24 GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
25 prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
26 },
27 noise::var_noise_gglwe_product,
28};
29
30pub fn test_glwe_trace_inplace<B>(module: &Module<B>)
31where
32 Module<B>: VecZnxDftAllocBytes
33 + VecZnxAutomorphism
34 + VecZnxBigAutomorphismInplace<B>
35 + VecZnxBigSubSmallBInplace<B>
36 + VecZnxRshInplace<B>
37 + VecZnxRotateInplace<B>
38 + VecZnxBigNormalize<B>
39 + VecZnxDftApply<B>
40 + SvpApplyDftToDftInplace<B>
41 + VecZnxIdftApplyConsume<B>
42 + VecZnxFillUniform
43 + VecZnxSubABInplace
44 + VecZnxAddInplace
45 + VecZnxNormalizeInplace<B>
46 + VecZnxAddNormal
47 + VecZnxNormalize<B>
48 + VecZnxSub
49 + SvpPrepare<B>
50 + SvpPPolAllocBytes
51 + SvpPPolAlloc<B>
52 + VecZnxBigAllocBytes
53 + VecZnxBigAddInplace<B>
54 + VecZnxBigAddSmallInplace<B>
55 + VecZnxNormalizeTmpBytes
56 + VecZnxAddScalarInplace
57 + VmpPMatAlloc<B>
58 + VmpPrepare<B>
59 + VmpApplyDftToDftTmpBytes
60 + VmpApplyDftToDft<B>
61 + VmpApplyDftToDftAdd<B>
62 + VecZnxBigNormalizeTmpBytes
63 + VecZnxSwitchRing
64 + VecZnxCopy,
65 B: Backend
66 + TakeVecZnxDftImpl<B>
67 + TakeVecZnxBigImpl<B>
68 + TakeSvpPPolImpl<B>
69 + ScratchOwnedAllocImpl<B>
70 + ScratchOwnedBorrowImpl<B>
71 + ScratchAvailableImpl<B>
72 + TakeScalarZnxImpl<B>
73 + TakeVecZnxImpl<B>,
74{
75 let basek: usize = 8;
76 let k: usize = 54;
77
78 (1..3).for_each(|rank| {
79 let n: usize = module.n();
80 let k_autokey: usize = k + basek;
81
82 let digits: usize = 1;
83 let rows: usize = k.div_ceil(basek * digits);
84
85 let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k, rank);
86 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k);
87 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k);
88
89 let mut source_xs: Source = Source::new([0u8; 32]);
90 let mut source_xe: Source = Source::new([0u8; 32]);
91 let mut source_xa: Source = Source::new([0u8; 32]);
92
93 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
94 GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct.k())
95 | GLWECiphertext::decrypt_scratch_space(module, basek, ct.k())
96 | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_autokey, rank)
97 | GLWECiphertext::trace_inplace_scratch_space(module, basek, ct.k(), k_autokey, digits, rank),
98 );
99
100 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
101 sk.fill_ternary_prob(0.5, &mut source_xs);
102 let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
103
104 let mut data_want: Vec<i64> = vec![0i64; n];
105
106 data_want
107 .iter_mut()
108 .for_each(|x| *x = source_xa.next_i64() & 0xFF);
109
110 module.vec_znx_fill_uniform(basek, &mut pt_have.data, 0, &mut source_xa);
111
112 ct.encrypt_sk(
113 module,
114 &pt_have,
115 &sk_dft,
116 &mut source_xa,
117 &mut source_xe,
118 scratch.borrow(),
119 );
120
121 let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
122 let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module);
123 let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_autokey, rows, digits, rank);
124 gal_els.iter().for_each(|gal_el| {
125 tmp.encrypt_sk(
126 module,
127 *gal_el,
128 &sk,
129 &mut source_xa,
130 &mut source_xe,
131 scratch.borrow(),
132 );
133 let atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow());
134 auto_keys.insert(*gal_el, atk_prepared);
135 });
136
137 ct.trace_inplace(module, 0, 5, &auto_keys, scratch.borrow());
138 ct.trace_inplace(module, 5, module.log_n(), &auto_keys, scratch.borrow());
139
140 (0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]);
141
142 ct.decrypt(module, &mut pt_have, &sk_dft, scratch.borrow());
143
144 module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
145 module.vec_znx_normalize_inplace(basek, &mut pt_want.data, 0, scratch.borrow());
146
147 let noise_have: f64 = pt_want.std().log2();
148
149 let mut noise_want: f64 = var_noise_gglwe_product(
150 n as f64,
151 basek,
152 0.5,
153 0.5,
154 1.0 / 12.0,
155 SIGMA * SIGMA,
156 0.0,
157 rank as f64,
158 k,
159 k_autokey,
160 );
161 noise_want += SIGMA * SIGMA * (-2.0 * (k) as f64).exp2();
162 noise_want += n as f64 * 1.0 / 12.0 * 0.5 * rank as f64 * (-2.0 * (k) as f64).exp2();
163 noise_want = noise_want.sqrt().log2();
164
165 assert!(
166 (noise_have - noise_want).abs() < 1.0,
167 "{} > {}",
168 noise_have,
169 noise_want
170 );
171 });
172}