poulpy/poulpy-core/src/tests/generics/trace.rs
poulpy-core/src/tests/generics/trace.rsRaw
1use std::collections::HashMap;
2
3use poulpy_hal::{
4 api::{
5 DFT, IDFTConsume, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare,
6 VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigAddInplace,
7 VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, VecZnxBigNormalize,
8 VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxFillUniform,
9 VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub,
10 VecZnxSubABInplace, VecZnxSwithcDegree, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc,
11 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>, basek: usize, k: usize, rank: usize)
31where
32 Module<B>: VecZnxDftAllocBytes
33 + VecZnxAutomorphism
34 + VecZnxBigAutomorphismInplace<B>
35 + VecZnxBigSubSmallBInplace<B>
36 + VecZnxRshInplace
37 + VecZnxRotateInplace
38 + VecZnxBigNormalize<B>
39 + DFT<B>
40 + SvpApplyInplace<B>
41 + IDFTConsume<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 + VecZnxSwithcDegree
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 n: usize = module.n();
76 let k_autokey: usize = k + basek;
77
78 let digits: usize = 1;
79 let rows: usize = k.div_ceil(basek * digits);
80
81 let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k, rank);
82 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k);
83 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k);
84
85 let mut source_xs: Source = Source::new([0u8; 32]);
86 let mut source_xe: Source = Source::new([0u8; 32]);
87 let mut source_xa: Source = Source::new([0u8; 32]);
88
89 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
90 GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct.k())
91 | GLWECiphertext::decrypt_scratch_space(module, basek, ct.k())
92 | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_autokey, rank)
93 | GLWECiphertext::trace_inplace_scratch_space(module, basek, ct.k(), k_autokey, digits, rank),
94 );
95
96 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
97 sk.fill_ternary_prob(0.5, &mut source_xs);
98 let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
99
100 let mut data_want: Vec<i64> = vec![0i64; n];
101
102 data_want
103 .iter_mut()
104 .for_each(|x| *x = source_xa.next_i64() & 0xFF);
105
106 module.vec_znx_fill_uniform(basek, &mut pt_have.data, 0, k, &mut source_xa);
107
108 ct.encrypt_sk(
109 module,
110 &pt_have,
111 &sk_dft,
112 &mut source_xa,
113 &mut source_xe,
114 scratch.borrow(),
115 );
116
117 let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
118 let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module);
119 let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_autokey, rows, digits, rank);
120 gal_els.iter().for_each(|gal_el| {
121 tmp.encrypt_sk(
122 module,
123 *gal_el,
124 &sk,
125 &mut source_xa,
126 &mut source_xe,
127 scratch.borrow(),
128 );
129 let atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow());
130 auto_keys.insert(*gal_el, atk_prepared);
131 });
132
133 ct.trace_inplace(module, 0, 5, &auto_keys, scratch.borrow());
134 ct.trace_inplace(module, 5, module.log_n(), &auto_keys, scratch.borrow());
135
136 (0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]);
137
138 ct.decrypt(module, &mut pt_have, &sk_dft, scratch.borrow());
139
140 module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
141 module.vec_znx_normalize_inplace(basek, &mut pt_want.data, 0, scratch.borrow());
142
143 let noise_have: f64 = pt_want.std().log2();
144
145 let mut noise_want: f64 = var_noise_gglwe_product(
146 n as f64,
147 basek,
148 0.5,
149 0.5,
150 1.0 / 12.0,
151 SIGMA * SIGMA,
152 0.0,
153 rank as f64,
154 k,
155 k_autokey,
156 );
157 noise_want += SIGMA * SIGMA * (-2.0 * (k) as f64).exp2();
158 noise_want += n as f64 * 1.0 / 12.0 * 0.5 * rank as f64 * (-2.0 * (k) as f64).exp2();
159 noise_want = noise_want.sqrt().log2();
160
161 assert!(
162 (noise_have - noise_want).abs() < 1.0,
163 "{} > {}",
164 noise_have,
165 noise_want
166 );
167}