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