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::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, VecZnxNormalizeInplace, VecZnxSubInplace},
5 layouts::{Backend, Module, Scratch, ScratchOwned, ZnxView, ZnxViewMut},
6 source::Source,
7};
8
9use crate::{
10 AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore,
11 encryption::SIGMA,
12 glwe_trace::GLWETrace,
13 layouts::{
14 AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout,
15 GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos,
16 prepared::{AutomorphismKeyPrepared, GLWESecretPrepared},
17 },
18 noise::var_noise_gglwe_product,
19};
20
21pub fn test_glwe_trace_inplace<BE: Backend>(module: &Module<BE>)
22where
23 Module<BE>: GLWETrace<BE>
24 + GLWEEncryptSk<BE>
25 + GLWEDecrypt<BE>
26 + AutomorphismKeyEncryptSk<BE>
27 + AutomorphismKeyPrepare<BE>
28 + AutomorphismKeyPreparedAlloc<BE>
29 + VecZnxFillUniform
30 + GLWESecretPrepare<BE>
31 + GLWESecretPreparedAlloc<BE>
32 + VecZnxSubInplace
33 + VecZnxNormalizeInplace<BE>,
34 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
35 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
36{
37 let base2k: usize = 8;
38 let k: usize = 54;
39
40 for rank in 1_usize..3 {
41 let n: usize = module.n();
42 let k_autokey: usize = k + base2k;
43
44 let dsize: usize = 1;
45 let dnum: usize = k.div_ceil(base2k * dsize);
46
47 let glwe_out_infos: GLWELayout = GLWELayout {
48 n: n.into(),
49 base2k: base2k.into(),
50 k: k.into(),
51 rank: rank.into(),
52 };
53
54 let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
55 n: n.into(),
56 base2k: base2k.into(),
57 k: k_autokey.into(),
58 rank: rank.into(),
59 dsize: dsize.into(),
60 dnum: dnum.into(),
61 };
62
63 let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
64 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
65 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
66
67 let mut source_xs: Source = Source::new([0u8; 32]);
68 let mut source_xe: Source = Source::new([0u8; 32]);
69 let mut source_xa: Source = Source::new([0u8; 32]);
70
71 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
72 GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
73 | GLWE::decrypt_tmp_bytes(module, &glwe_out_infos)
74 | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos)
75 | GLWE::trace_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &key_infos),
76 );
77
78 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos);
79 sk.fill_ternary_prob(0.5, &mut source_xs);
80
81 let mut sk_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
82 sk_dft.prepare(module, &sk);
83
84 let mut data_want: Vec<i64> = vec![0i64; n];
85
86 data_want
87 .iter_mut()
88 .for_each(|x| *x = source_xa.next_i64() & 0xFF);
89
90 module.vec_znx_fill_uniform(base2k, &mut pt_have.data, 0, &mut source_xa);
91
92 glwe_out.encrypt_sk(
93 module,
94 &pt_have,
95 &sk_dft,
96 &mut source_xa,
97 &mut source_xe,
98 scratch.borrow(),
99 );
100
101 let mut auto_keys: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new();
102 let gal_els: Vec<i64> = GLWE::trace_galois_elements(module);
103 let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&key_infos);
104 gal_els.iter().for_each(|gal_el| {
105 tmp.encrypt_sk(
106 module,
107 *gal_el,
108 &sk,
109 &mut source_xa,
110 &mut source_xe,
111 scratch.borrow(),
112 );
113 let mut atk_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &tmp);
114 atk_prepared.prepare(module, &tmp, scratch.borrow());
115 auto_keys.insert(*gal_el, atk_prepared);
116 });
117
118 glwe_out.trace_inplace(module, 0, 5, &auto_keys, scratch.borrow());
119 glwe_out.trace_inplace(module, 5, module.log_n(), &auto_keys, scratch.borrow());
120
121 (0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]);
122
123 glwe_out.decrypt(module, &mut pt_have, &sk_dft, scratch.borrow());
124
125 module.vec_znx_sub_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
126 module.vec_znx_normalize_inplace(base2k, &mut pt_want.data, 0, scratch.borrow());
127
128 let noise_have: f64 = pt_want.std().log2();
129
130 let mut noise_want: f64 = var_noise_gglwe_product(
131 n as f64,
132 base2k,
133 0.5,
134 0.5,
135 1.0 / 12.0,
136 SIGMA * SIGMA,
137 0.0,
138 rank as f64,
139 k,
140 k_autokey,
141 );
142 noise_want += SIGMA * SIGMA * (-2.0 * (k) as f64).exp2();
143 noise_want += n as f64 * 1.0 / 12.0 * 0.5 * rank as f64 * (-2.0 * (k) as f64).exp2();
144 noise_want = noise_want.sqrt().log2();
145
146 assert!(
147 (noise_have - noise_want).abs() < 1.0,
148 "{noise_have} > {noise_want}"
149 );
150 }
151}