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