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