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