arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/blind_rotation/test_fft64/cggi.rs
core/src/blind_rotation/test_fft64/cggi.rsRaw
  1use backend::{Encoding, FFT64, Module, ScratchOwned, ZnxView};
  2use sampling::source::Source;
  3
  4use crate::{
  5    FourierGLWESecret, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, LWECiphertext, LWESecret,
  6    blind_rotation::{
  7        ccgi::{cggi_blind_rotate, cggi_blind_rotate_scratch_space, negate_and_mod_switch_2n},
  8        key::BlindRotationKeyCGGI,
  9        lut::LookUpTable,
 10    },
 11    lwe::{LWEPlaintext, ciphertext::LWECiphertextToRef},
 12};
 13
 14#[test]
 15fn standard() {
 16    blind_rotatio_test(224, 1, 1);
 17}
 18
 19#[test]
 20fn block_binary() {
 21    blind_rotatio_test(224, 7, 1);
 22}
 23
 24#[test]
 25fn block_binary_extended() {
 26    blind_rotatio_test(224, 7, 2);
 27}
 28
 29fn blind_rotatio_test(n_lwe: usize, block_size: usize, extension_factor: usize) {
 30    let module: Module<FFT64> = Module::<FFT64>::new(512);
 31    let basek: usize = 19;
 32
 33    let k_lwe: usize = 24;
 34    let k_brk: usize = 3 * basek;
 35    let rows_brk: usize = 2; // Ensures first limb is noise-free.
 36    let k_lut: usize = 2 * basek;
 37    let rank: usize = 1;
 38
 39    let message_modulus: usize = 1 << 4;
 40
 41    let mut source_xs: Source = Source::new([1u8; 32]);
 42    let mut source_xe: Source = Source::new([1u8; 32]);
 43    let mut source_xa: Source = Source::new([1u8; 32]);
 44
 45    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
 46    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
 47    let sk_glwe_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk_glwe);
 48
 49    let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
 50    sk_lwe.fill_binary_block(block_size, &mut source_xs);
 51
 52    let mut scratch: ScratchOwned = ScratchOwned::new(BlindRotationKeyCGGI::generate_from_sk_scratch_space(
 53        &module, basek, k_brk, rank,
 54    ));
 55
 56    let mut scratch_br: ScratchOwned = ScratchOwned::new(cggi_blind_rotate_scratch_space(
 57        &module,
 58        extension_factor,
 59        basek,
 60        k_lut,
 61        k_brk,
 62        rows_brk,
 63        rank,
 64    ));
 65
 66    let mut brk: BlindRotationKeyCGGI<FFT64> = BlindRotationKeyCGGI::allocate(&module, n_lwe, basek, k_brk, rows_brk, rank);
 67
 68    brk.generate_from_sk(
 69        &module,
 70        &sk_glwe_dft,
 71        &sk_lwe,
 72        &mut source_xa,
 73        &mut source_xe,
 74        3.2,
 75        scratch.borrow(),
 76    );
 77
 78    let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
 79
 80    let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
 81
 82    let x: i64 = 2;
 83    let bits: usize = 8;
 84
 85    pt_lwe.data.encode_coeff_i64(0, basek, bits, 0, x, bits);
 86
 87    // println!("{}", pt_lwe.data);
 88
 89    lwe.encrypt_sk(&pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe, 3.2);
 90
 91    lwe.decrypt(&mut pt_lwe, &sk_lwe);
 92
 93    // println!("{}", pt_lwe.data);
 94
 95    let mut f: Vec<i64> = vec![0i64; message_modulus];
 96    f.iter_mut()
 97        .enumerate()
 98        .for_each(|(i, x)| *x = 2 * (i as i64) + 1);
 99
100    let mut lut: LookUpTable = LookUpTable::alloc(&module, basek, k_lut, extension_factor);
101    lut.set(&module, &f, message_modulus);
102
103    let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_lut, rank);
104
105    cggi_blind_rotate(&module, &mut res, &lwe, &lut, &brk, scratch_br.borrow());
106
107    println!("out_mut.data: {}", res.data);
108
109    let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_lut);
110
111    res.decrypt(&module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
112
113    println!("pt_have: {}", pt_have.data);
114
115    let mut lwe_2n: Vec<i64> = vec![0i64; lwe.n() + 1]; // TODO: from scratch space
116
117    negate_and_mod_switch_2n(2 * lut.domain_size(), &mut lwe_2n, &lwe.to_ref());
118
119    let pt_want: i64 = (lwe_2n[0]
120        + lwe_2n[1..]
121            .iter()
122            .zip(sk_lwe.data.at(0, 0))
123            .map(|(x, y)| x * y)
124            .sum::<i64>())
125        % (2 * lut.domain_size()) as i64;
126
127    // println!("pt_want: {}", pt_want);
128
129    lut.rotate(pt_want);
130
131    // lut.data.iter().for_each(|d| {
132    //     println!("{}", d);
133    // });
134
135    // First limb should be exactly equal (test are parameterized such that the noise does not reach
136    // the first limb)
137    assert_eq!(pt_have.data.at(0, 0), lut.data[0].at(0, 0));
138
139    // Then checks the noise
140    // module.vec_znx_sub_ab_inplace(&mut lut.data[0], 0, &pt_have.data, 0);
141    // let noise: f64 = lut.data[0].std(0, basek);
142    // println!("noise: {}", noise);
143    // assert!(noise < 1e-3);
144}