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, Dnum, Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecretPrepared, GLWESecretPreparedFactory, LWEInfos,
5 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::{
16 bdd_arithmetic::{
17 FheUintPrepared, GGSWBlindRotation,
18 tests::test_suite::{TEST_BASE2K, TEST_RANK, TestContext},
19 },
20 blind_rotation::BlindRotationAlgo,
21};
22
23pub fn test_scalar_to_ggsw_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 + GGSWBlindRotation<u32, BE>
30 + GGSWNoise<BE>
31 + GLWEDecrypt<BE>
32 + GLWEEncryptSk<BE>
33 + VecZnxRotateInplace<BE>,
34 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
35 Scratch<BE>: ScratchTakeCore<BE>,
36{
37 let module: &Module<BE> = &test_context.module;
38 let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
39
40 let base2k: Base2K = TEST_BASE2K.into();
41 let rank: Rank = TEST_RANK.into();
42 let k_ggsw_res: TorusPrecision = TorusPrecision(39);
43 let k_ggsw_apply: TorusPrecision = TorusPrecision(52);
44
45 let ggsw_res_infos: GGSWLayout = GGSWLayout {
46 n: module.n().into(),
47 base2k,
48 k: k_ggsw_res,
49 rank,
50 dnum: Dnum(2),
51 dsize: Dsize(1),
52 };
53
54 let ggsw_k_infos: GGSWLayout = GGSWLayout {
55 n: module.n().into(),
56 base2k,
57 k: k_ggsw_apply,
58 rank,
59 dnum: Dnum(3),
60 dsize: Dsize(1),
61 };
62
63 let mut source: Source = Source::new([6u8; 32]);
64 let mut source_xa: Source = Source::new([2u8; 32]);
65 let mut source_xe: Source = Source::new([3u8; 32]);
66
67 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
68 let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_res_infos);
69
70 let mut scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), 1);
71 scalar
72 .raw_mut()
73 .iter_mut()
74 .enumerate()
75 .for_each(|(i, x)| *x = i as i64);
76
77 let k: u32 = source.next_u32();
78
79 // println!("k: {k}");
80
81 let mut k_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_k_infos);
82 k_enc_prep.encrypt_sk(
83 module,
84 k,
85 sk_glwe_prep,
86 &mut source_xa,
87 &mut source_xe,
88 scratch.borrow(),
89 );
90
91 let base: [usize; 2] = [module.log_n() >> 1, module.log_n() - (module.log_n() >> 1)];
92
93 assert_eq!(base.iter().sum::<usize>(), module.log_n());
94
95 // Starting bit
96 let mut bit_start: usize = 0;
97
98 let max_noise = |col_i: usize| {
99 let mut noise: f64 = -(ggsw_res_infos.size() as f64 * base2k.as_usize() as f64) + SIGMA.log2() + 3.0;
100 noise += 0.5 * ggsw_res_infos.log_n() as f64;
101 if col_i != 0 {
102 noise += 0.5 * ggsw_res_infos.log_n() as f64
103 }
104 noise
105 };
106
107 for _ in 0..32_usize.div_ceil(module.log_n()) {
108 // By how many bits to left shift
109 let mut bit_step: usize = 0;
110
111 for digit in base {
112 let mask: u32 = (1 << digit) - 1;
113
114 // How many bits to take
115 let bit_size: usize = (32 - bit_start).min(digit);
116
117 module.scalar_to_ggsw_blind_rotation(
118 &mut res,
119 &scalar,
120 &k_enc_prep,
121 false,
122 bit_start,
123 bit_size,
124 bit_step,
125 scratch.borrow(),
126 );
127
128 let rot: i64 = (((k >> bit_start) & mask) << bit_step) as i64;
129
130 let mut scalar_want: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), 1);
131 scalar_want.raw_mut().copy_from_slice(scalar.raw());
132
133 module.vec_znx_rotate_inplace(-rot, &mut scalar_want.as_vec_znx_mut(), 0, scratch.borrow());
134
135 // res.print_noise(&module, &sk_glwe_prep, &scalar_want);
136
137 res.assert_noise(module, sk_glwe_prep, &scalar_want, &max_noise);
138
139 bit_step += digit;
140 bit_start += digit;
141
142 if bit_start >= 32 {
143 break;
144 }
145 }
146 }
147}