arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/glwe_blind_rotation.rs
poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/glwe_blind_rotation.rsRaw
  1use poulpy_core::{
  2    GGSWEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore,
  3    layouts::{
  4        Base2K, Dnum, Dsize, GGSWLayout, GGSWPreparedFactory, GLWE, GLWELayout, GLWEPlaintext, GLWESecretPrepared,
  5        GLWESecretPreparedFactory, Rank, TorusPrecision,
  6    },
  7};
  8use poulpy_hal::{
  9    api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
 10    layouts::{Backend, Module, Scratch, ScratchOwned},
 11    source::Source,
 12};
 13use rand::RngCore;
 14
 15use crate::tfhe::{
 16    bdd_arithmetic::{
 17        FheUintPrepared, GLWEBlindRotation,
 18        tests::test_suite::{TEST_BASE2K, TEST_RANK, TestContext},
 19    },
 20    blind_rotation::BlindRotationAlgo,
 21};
 22
 23pub fn test_glwe_to_glwe_blind_rotation<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
 24where
 25    Module<BE>: ModuleNew<BE>
 26        + GLWESecretPreparedFactory<BE>
 27        + GGSWPreparedFactory<BE>
 28        + GGSWEncryptSk<BE>
 29        + GLWEBlindRotation<u32, BE>
 30        + GLWEDecrypt<BE>
 31        + GLWEEncryptSk<BE>,
 32    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 33    Scratch<BE>: ScratchTakeCore<BE>,
 34{
 35    let module: &Module<BE> = &test_context.module;
 36    let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
 37
 38    let base2k: Base2K = TEST_BASE2K.into();
 39    let rank: Rank = TEST_RANK.into();
 40    let k_glwe: TorusPrecision = TorusPrecision(26);
 41    let k_ggsw: TorusPrecision = TorusPrecision(39);
 42    let dnum: Dnum = Dnum(3);
 43
 44    let glwe_infos: GLWELayout = GLWELayout {
 45        n: module.n().into(),
 46        base2k,
 47        k: k_glwe,
 48        rank,
 49    };
 50    let ggsw_infos: GGSWLayout = GGSWLayout {
 51        n: module.n().into(),
 52        base2k,
 53        k: k_ggsw,
 54        rank,
 55        dnum,
 56        dsize: Dsize(1),
 57    };
 58
 59    let mut source: Source = Source::new([6u8; 32]);
 60    let mut source_xa: Source = Source::new([2u8; 32]);
 61    let mut source_xe: Source = Source::new([3u8; 32]);
 62
 63    let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
 64
 65    let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
 66
 67    let mut test_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
 68    let mut data: Vec<i64> = vec![0i64; module.n()];
 69    data.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
 70    test_glwe.encode_vec_i64(&data, base2k.as_usize().into());
 71
 72    let k: u32 = source.next_u32();
 73
 74    let mut k_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> =
 75        FheUintPrepared::<Vec<u8>, u32, BE>::alloc_from_infos(module, &ggsw_infos);
 76    k_enc_prep.encrypt_sk(
 77        module,
 78        k,
 79        sk_glwe_prep,
 80        &mut source_xa,
 81        &mut source_xe,
 82        scratch.borrow(),
 83    );
 84
 85    let base: [usize; 2] = [module.log_n() >> 1, module.log_n() - (module.log_n() >> 1)];
 86
 87    assert_eq!(base.iter().sum::<usize>(), module.log_n());
 88
 89    // Starting bit
 90    let mut bit_start: usize = 0;
 91
 92    let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
 93
 94    for _ in 0..32_usize.div_ceil(module.log_n()) {
 95        // By how many bits to left shift
 96        let mut bit_step: usize = 0;
 97
 98        for digit in base {
 99            let mask: u32 = (1 << digit) - 1;
100
101            // How many bits to take
102            let bit_size: usize = (32 - bit_start).min(digit);
103
104            module.glwe_blind_rotation(
105                &mut res,
106                &test_glwe,
107                &k_enc_prep,
108                false,
109                bit_start,
110                bit_size,
111                bit_step,
112                scratch.borrow(),
113            );
114
115            res.decrypt(module, &mut pt, sk_glwe_prep, scratch.borrow());
116
117            assert_eq!(
118                (((k >> bit_start) & mask) << bit_step) as i64,
119                pt.decode_coeff_i64(base2k.as_usize().into(), 0)
120            );
121
122            bit_step += digit;
123            bit_start += digit;
124
125            if bit_start >= 32 {
126                break;
127            }
128        }
129    }
130}