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, GGSWInfos, GGSWLayout, GGSWPreparedFactory, GLWEInfos, 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::{
16 bdd_arithmetic::{
17 FheUintPrepared, GGSWBlindRotation,
18 tests::test_suite::{TEST_FHEUINT_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_FHEUINT_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 let mut k_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> =
80 FheUintPrepared::<Vec<u8>, u32, BE>::alloc_from_infos(module, &ggsw_k_infos);
81 k_enc_prep.encrypt_sk(
82 module,
83 k,
84 sk_glwe_prep,
85 &mut source_xa,
86 &mut source_xe,
87 scratch.borrow(),
88 );
89
90 let base: [usize; 2] = [module.log_n() >> 1, module.log_n() - (module.log_n() >> 1)];
91
92 assert_eq!(base.iter().sum::<usize>(), module.log_n());
93
94 // Starting bit
95 let mut bit_start: usize = 0;
96
97 let max_noise = |col_i: usize| {
98 let mut noise: f64 = -(ggsw_res_infos.size() as f64 * base2k.as_usize() as f64) + SIGMA.log2() + 3.0;
99 noise += 0.5 * ggsw_res_infos.log_n() as f64;
100 if col_i != 0 {
101 noise += 0.5 * ggsw_res_infos.log_n() as f64
102 }
103 noise
104 };
105
106 for _ in 0..32_usize.div_ceil(module.log_n()) {
107 // By how many bits to left shift
108 let mut bit_step: usize = 0;
109
110 for digit in base {
111 let mask: u32 = (1 << digit) - 1;
112
113 // How many bits to take
114 let bit_size: usize = (32 - bit_start).min(digit);
115
116 module.scalar_to_ggsw_blind_rotation(
117 &mut res,
118 &scalar,
119 &k_enc_prep,
120 false,
121 bit_start,
122 bit_size,
123 bit_step,
124 scratch.borrow(),
125 );
126
127 let rot: i64 = (((k >> bit_start) & mask) << bit_step) as i64;
128
129 let mut scalar_want: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), 1);
130 scalar_want.raw_mut().copy_from_slice(scalar.raw());
131
132 module.vec_znx_rotate_inplace(-rot, &mut scalar_want.as_vec_znx_mut(), 0, scratch.borrow());
133
134 for row in 0..res.dnum().as_usize() {
135 for col in 0..res.rank().as_usize() + 1 {
136 assert!(
137 res.noise(
138 module,
139 row,
140 col,
141 &scalar_want,
142 sk_glwe_prep,
143 scratch.borrow()
144 )
145 .std()
146 .log2()
147 <= max_noise(col)
148 )
149 }
150 }
151
152 bit_step += digit;
153 bit_start += digit;
154
155 if bit_start >= 32 {
156 break;
157 }
158 }
159 }
160}