arnaucube
arnaucube/poulpyPublic Self-hosted repository
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, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret,
 15        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        + AutomorphismKeyEncryptSk<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: AutomorphismKeyLayout = AutomorphismKeyLayout {
 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                | AutomorphismKey::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: AutomorphismKey<Vec<u8>> = AutomorphismKey::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, 5, &auto_keys, scratch.borrow());
118        glwe_out.trace_inplace(module, 5, module.log_n(), &auto_keys, scratch.borrow());
119
120        (0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]);
121
122        glwe_out.decrypt(module, &mut pt_have, &sk_dft, scratch.borrow());
123
124        module.vec_znx_sub_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
125        module.vec_znx_normalize_inplace(base2k, &mut pt_want.data, 0, scratch.borrow());
126
127        let noise_have: f64 = pt_want.std().log2();
128
129        let mut noise_want: f64 = var_noise_gglwe_product(
130            n as f64,
131            base2k,
132            0.5,
133            0.5,
134            1.0 / 12.0,
135            SIGMA * SIGMA,
136            0.0,
137            rank as f64,
138            k,
139            k_autokey,
140        );
141        noise_want += SIGMA * SIGMA * (-2.0 * (k) as f64).exp2();
142        noise_want += n as f64 * 1.0 / 12.0 * 0.5 * rank as f64 * (-2.0 * (k) as f64).exp2();
143        noise_want = noise_want.sqrt().log2();
144
145        assert!(
146            (noise_have - noise_want).abs() < 1.0,
147            "{noise_have} > {noise_want}"
148        );
149    }
150}