poulpy/core/src/test_fft64/trace.rs
core/src/test_fft64/trace.rsRaw
1use std::collections::HashMap;
2
3use backend::{FFT64, FillUniform, Module, ScratchOwned, Stats, VecZnxOps, ZnxView, ZnxViewMut};
4use sampling::source::Source;
5
6use crate::{AutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, test_fft64::var_noise_gglwe_product};
7
8#[test]
9fn trace_inplace() {
10 let log_n: usize = 8;
11 (1..4).for_each(|rank| {
12 println!("test trace_inplace rank: {}", rank);
13 test_trace_inplace(log_n, 8, 54, 3.2, rank);
14 });
15}
16
17fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize) {
18 let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
19
20 let k_autokey: usize = k + basek;
21
22 let digits: usize = 1;
23 let rows: usize = k.div_ceil(basek * digits);
24
25 let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k, rank);
26 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k);
27 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k);
28
29 let mut source_xs: Source = Source::new([0u8; 32]);
30 let mut source_xe: Source = Source::new([0u8; 32]);
31 let mut source_xa: Source = Source::new([0u8; 32]);
32
33 let mut scratch: ScratchOwned = ScratchOwned::new(
34 GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
35 | GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k())
36 | AutomorphismKey::generate_from_sk_scratch_space(&module, basek, k_autokey, rank)
37 | GLWECiphertext::trace_inplace_scratch_space(&module, basek, ct.k(), k_autokey, digits, rank),
38 );
39
40 let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
41 sk.fill_ternary_prob(&&module, 0.5, &mut source_xs);
42
43 let mut data_want: Vec<i64> = vec![0i64; module.n()];
44
45 data_want
46 .iter_mut()
47 .for_each(|x| *x = source_xa.next_i64() & 0xFF);
48
49 pt_have
50 .data
51 .fill_uniform(basek, 0, pt_have.size(), &mut source_xa);
52
53 ct.encrypt_sk(
54 &module,
55 &pt_have,
56 &sk,
57 &mut source_xa,
58 &mut source_xe,
59 sigma,
60 scratch.borrow(),
61 );
62
63 let mut auto_keys: HashMap<i64, AutomorphismKey<Vec<u8>, FFT64>> = HashMap::new();
64 let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(&module);
65 gal_els.iter().for_each(|gal_el| {
66 let mut key: AutomorphismKey<Vec<u8>, FFT64> = AutomorphismKey::alloc(&module, basek, k_autokey, rows, digits, rank);
67 key.generate_from_sk(
68 &module,
69 *gal_el,
70 &sk,
71 &mut source_xa,
72 &mut source_xe,
73 sigma,
74 scratch.borrow(),
75 );
76 auto_keys.insert(*gal_el, key);
77 });
78
79 ct.trace_inplace(&module, 0, 5, &auto_keys, scratch.borrow());
80 ct.trace_inplace(&module, 5, log_n, &auto_keys, scratch.borrow());
81
82 (0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]);
83
84 ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
85
86 module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
87 module.vec_znx_normalize_inplace(basek, &mut pt_want.data, 0, scratch.borrow());
88
89 let noise_have = pt_want.data.std(0, basek).log2();
90
91 let mut noise_want: f64 = var_noise_gglwe_product(
92 module.n() as f64,
93 basek,
94 0.5,
95 0.5,
96 1.0 / 12.0,
97 sigma * sigma,
98 0.0,
99 rank as f64,
100 k,
101 k_autokey,
102 );
103 noise_want += sigma * sigma * (-2.0 * (k) as f64).exp2();
104 noise_want += module.n() as f64 * 1.0 / 12.0 * 0.5 * rank as f64 * (-2.0 * (k) as f64).exp2();
105 noise_want = noise_want.sqrt().log2();
106
107 assert!(
108 (noise_have - noise_want).abs() < 1.0,
109 "{} > {}",
110 noise_have,
111 noise_want
112 );
113}