arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/tests/test_suite/glwe_packer.rs
poulpy-core/src/tests/test_suite/glwe_packer.rsRaw
  1use std::collections::HashMap;
  2
  3use poulpy_hal::{
  4    api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
  5    layouts::{Backend, Module, Scratch, ScratchOwned},
  6    source::Source,
  7};
  8
  9use crate::{
 10    GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPackerOps, GLWERotate, GLWESub, ScratchTakeCore,
 11    layouts::{
 12        GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext,
 13        GLWESecret, GLWESecretPreparedFactory,
 14        prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared},
 15    },
 16};
 17
 18pub fn test_glwe_packer<BE: Backend>(module: &Module<BE>)
 19where
 20    Module<BE>: GLWEEncryptSk<BE>
 21        + GLWEAutomorphismKeyEncryptSk<BE>
 22        + GLWEAutomorphismKeyPreparedFactory<BE>
 23        + GLWEPackerOps<BE>
 24        + GLWESecretPreparedFactory<BE>
 25        + GLWESub
 26        + GLWEDecrypt<BE>
 27        + GLWERotate<BE>,
 28    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 29    Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
 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 n: usize = module.n();
 36    let out_base2k: usize = 15;
 37    let key_base2k: usize = 10;
 38    let k_ct: usize = 36;
 39    let pt_k: usize = 18;
 40    let rank: usize = 3;
 41    let dsize: usize = 1;
 42    let k_ksk: usize = k_ct + key_base2k * dsize;
 43
 44    let dnum: usize = k_ct.div_ceil(key_base2k * dsize);
 45
 46    let glwe_out_infos: GLWELayout = GLWELayout {
 47        n: n.into(),
 48        base2k: out_base2k.into(),
 49        k: k_ct.into(),
 50        rank: rank.into(),
 51    };
 52
 53    let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout {
 54        n: n.into(),
 55        base2k: key_base2k.into(),
 56        k: k_ksk.into(),
 57        rank: rank.into(),
 58        dsize: dsize.into(),
 59        dnum: dnum.into(),
 60    };
 61
 62    let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
 63        GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
 64            | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos)
 65            | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos),
 66    );
 67
 68    let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos);
 69    sk.fill_ternary_prob(0.5, &mut source_xs);
 70
 71    let mut sk_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
 72    sk_dft.prepare(module, &sk);
 73
 74    let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
 75    let mut data: Vec<i64> = vec![0i64; n];
 76    data.iter_mut().enumerate().for_each(|(i, x)| {
 77        *x = i as i64;
 78    });
 79
 80    pt.encode_vec_i64(&data, pt_k.into());
 81
 82    let gal_els: Vec<i64> = GLWEPacker::galois_elements(module);
 83
 84    let mut auto_keys: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new();
 85    let mut tmp: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&key_infos);
 86    gal_els.iter().for_each(|gal_el| {
 87        tmp.encrypt_sk(module, *gal_el, &sk, &mut source_xa, &mut source_xe, scratch.borrow());
 88        let mut atk_prepared: GLWEAutomorphismKeyPrepared<Vec<u8>, BE> =
 89            GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &tmp);
 90        atk_prepared.prepare(module, &tmp, scratch.borrow());
 91        auto_keys.insert(*gal_el, atk_prepared);
 92    });
 93
 94    let log_batch: usize = 0;
 95
 96    let mut packer: GLWEPacker = GLWEPacker::alloc(&glwe_out_infos, log_batch);
 97
 98    let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
 99
100    ct.encrypt_sk(module, &pt, &sk_dft, &mut source_xa, &mut source_xe, scratch.borrow());
101
102    let log_n: usize = module.log_n();
103
104    (0..n >> log_batch).for_each(|i| {
105        ct.encrypt_sk(module, &pt, &sk_dft, &mut source_xa, &mut source_xe, scratch.borrow());
106
107        module.glwe_rotate_inplace(-(1 << log_batch), &mut pt, scratch.borrow()); // X^-batch * pt
108
109        if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) {
110            packer.add(module, Some(&ct), &auto_keys, scratch.borrow());
111        } else {
112            packer.add(module, None::<&GLWE<Vec<u8>>>, &auto_keys, scratch.borrow())
113        }
114    });
115
116    let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
117    packer.flush(module, &mut res, scratch.borrow());
118
119    let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
120    let mut data: Vec<i64> = vec![0i64; n];
121    data.iter_mut().enumerate().for_each(|(i, x)| {
122        if i.is_multiple_of(5) {
123            *x = reverse_bits_msb(i, log_n as u32) as i64;
124        }
125    });
126
127    pt_want.encode_vec_i64(&data, pt_k.into());
128
129    res.decrypt(module, &mut pt, &sk_dft, scratch.borrow());
130
131    module.glwe_sub_inplace(&mut pt, &pt_want);
132
133    let noise_have: f64 = pt.stats().std().log2();
134
135    assert!(noise_have < -((k_ct - out_base2k) as f64), "noise: {noise_have}");
136}
137
138#[inline(always)]
139fn reverse_bits_msb(x: usize, n: u32) -> usize {
140    x.reverse_bits() >> (usize::BITS - n)
141}