arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/ggsw_blind_rotations.rs
poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/ggsw_blind_rotations.rsRaw
  1use poulpy_core::{
  2    GGSWEncryptSk, GGSWNoise, GLWEDecrypt, GLWEEncryptSk, SIGMA, ScratchTakeCore,
  3    layouts::{
  4        Base2K, Degree, Dnum, Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecret, GLWESecretPrepared,
  5        GLWESecretPreparedFactory, LWEInfos, Rank, TorusPrecision,
  6    },
  7};
  8use poulpy_hal::{
  9    api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace},
 10    layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned, ZnxView, ZnxViewMut},
 11    source::Source,
 12};
 13use rand::RngCore;
 14
 15use crate::tfhe::bdd_arithmetic::{FheUintBlocksPrepared, GGSWBlindRotation};
 16
 17pub fn test_scalar_to_ggsw_blind_rotation<BE: Backend>()
 18where
 19    Module<BE>: ModuleNew<BE>
 20        + GLWESecretPreparedFactory<BE>
 21        + GGSWPreparedFactory<BE>
 22        + GGSWEncryptSk<BE>
 23        + GGSWBlindRotation<u32, BE>
 24        + GGSWNoise<BE>
 25        + GLWEDecrypt<BE>
 26        + GLWEEncryptSk<BE>
 27        + VecZnxRotateInplace<BE>,
 28    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 29    Scratch<BE>: ScratchTakeCore<BE>,
 30{
 31    let n: Degree = Degree(1 << 11);
 32    let base2k: Base2K = Base2K(13);
 33    let rank: Rank = Rank(1);
 34    let k_ggsw_res: TorusPrecision = TorusPrecision(39);
 35    let k_ggsw_apply: TorusPrecision = TorusPrecision(52);
 36
 37    let ggsw_res_infos: GGSWLayout = GGSWLayout {
 38        n,
 39        base2k,
 40        k: k_ggsw_res,
 41        rank,
 42        dnum: Dnum(2),
 43        dsize: Dsize(1),
 44    };
 45
 46    let ggsw_k_infos: GGSWLayout = GGSWLayout {
 47        n,
 48        base2k,
 49        k: k_ggsw_apply,
 50        rank,
 51        dnum: Dnum(3),
 52        dsize: Dsize(1),
 53    };
 54
 55    let n_glwe: usize = n.into();
 56
 57    let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
 58    let mut source: Source = Source::new([6u8; 32]);
 59    let mut source_xs: Source = Source::new([1u8; 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 sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
 66    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
 67    let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(&module, rank);
 68    sk_glwe_prep.prepare(&module, &sk_glwe);
 69
 70    let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_res_infos);
 71
 72    let mut scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n_glwe, 1);
 73    scalar
 74        .raw_mut()
 75        .iter_mut()
 76        .enumerate()
 77        .for_each(|(i, x)| *x = i as i64);
 78
 79    let k: u32 = source.next_u32();
 80
 81    // println!("k: {k}");
 82
 83    let mut k_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> =
 84        FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_k_infos);
 85    k_enc_prep.encrypt_sk(
 86        &module,
 87        k,
 88        &sk_glwe_prep,
 89        &mut source_xa,
 90        &mut source_xe,
 91        scratch.borrow(),
 92    );
 93
 94    let base: [usize; 2] = [6, 5];
 95
 96    assert_eq!(base.iter().sum::<usize>(), module.log_n());
 97
 98    // Starting bit
 99    let mut bit_start: usize = 0;
100
101    let max_noise = |col_i: usize| {
102        let mut noise: f64 = -(ggsw_res_infos.size() as f64 * base2k.as_usize() as f64) + SIGMA.log2() + 2.0;
103        noise += 0.5 * ggsw_res_infos.log_n() as f64;
104        if col_i != 0 {
105            noise += 0.5 * ggsw_res_infos.log_n() as f64
106        }
107        noise
108    };
109
110    for _ in 0..32_usize.div_ceil(module.log_n()) {
111        // By how many bits to left shift
112        let mut bit_step: usize = 0;
113
114        for digit in base {
115            let mask: u32 = (1 << digit) - 1;
116
117            // How many bits to take
118            let bit_size: usize = (32 - bit_start).min(digit);
119
120            module.scalar_to_ggsw_blind_rotation(
121                &mut res,
122                &scalar,
123                &k_enc_prep,
124                bit_start,
125                bit_size,
126                bit_step,
127                scratch.borrow(),
128            );
129
130            let rot: i64 = (((k >> bit_start) & mask) << bit_step) as i64;
131
132            let mut scalar_want: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), 1);
133            scalar_want.raw_mut().copy_from_slice(scalar.raw());
134
135            module.vec_znx_rotate_inplace(-rot, &mut scalar_want.as_vec_znx_mut(), 0, scratch.borrow());
136
137            // res.print_noise(&module, &sk_glwe_prep, &scalar_want);
138
139            res.assert_noise(&module, &sk_glwe_prep, &scalar_want, &max_noise);
140
141            bit_step += digit;
142            bit_start += digit;
143
144            if bit_start >= 32 {
145                break;
146            }
147        }
148    }
149}