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, Degree, Dnum, Dsize, GGSWLayout, GGSWPreparedFactory, GLWE, GLWELayout, GLWEPlaintext, GLWESecret,
5 GLWESecretPrepared, GLWESecretPreparedFactory, LWEInfos, 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::bdd_arithmetic::{FheUintBlocksPrepared, GLWEBlindRotation};
16
17pub fn test_glwe_to_glwe_blind_rotation<BE: Backend>()
18where
19 Module<BE>: ModuleNew<BE>
20 + GLWESecretPreparedFactory<BE>
21 + GGSWPreparedFactory<BE>
22 + GGSWEncryptSk<BE>
23 + GLWEBlindRotation<u32, BE>
24 + GLWEDecrypt<BE>
25 + GLWEEncryptSk<BE>,
26 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
27 Scratch<BE>: ScratchTakeCore<BE>,
28{
29 let n: Degree = Degree(1 << 11);
30 let base2k: Base2K = Base2K(13);
31 let rank: Rank = Rank(1);
32 let k_glwe: TorusPrecision = TorusPrecision(26);
33 let k_ggsw: TorusPrecision = TorusPrecision(39);
34 let dnum: Dnum = Dnum(3);
35
36 let glwe_infos: GLWELayout = GLWELayout {
37 n,
38 base2k,
39 k: k_glwe,
40 rank,
41 };
42 let ggsw_infos: GGSWLayout = GGSWLayout {
43 n,
44 base2k,
45 k: k_ggsw,
46 rank,
47 dnum,
48 dsize: Dsize(1),
49 };
50
51 let n_glwe: usize = glwe_infos.n().into();
52
53 let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
54 let mut source: Source = Source::new([6u8; 32]);
55 let mut source_xs: Source = Source::new([1u8; 32]);
56 let mut source_xa: Source = Source::new([2u8; 32]);
57 let mut source_xe: Source = Source::new([3u8; 32]);
58
59 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
60
61 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
62 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
63 let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos);
64 sk_glwe_prep.prepare(&module, &sk_glwe);
65
66 let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
67
68 let mut test_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
69 let mut data: Vec<i64> = vec![0i64; module.n()];
70 data.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
71 test_glwe.encode_vec_i64(&data, base2k.as_usize().into());
72
73 println!("pt: {}", test_glwe);
74
75 let k: u32 = source.next_u32();
76
77 println!("k: {k}");
78
79 let mut k_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> =
80 FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_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] = [6, 5];
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 mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
98
99 for _ in 0..32_usize.div_ceil(module.log_n()) {
100 // By how many bits to left shift
101 let mut bit_step: usize = 0;
102
103 for digit in base {
104 let mask: u32 = (1 << digit) - 1;
105
106 // How many bits to take
107 let bit_size: usize = (32 - bit_start).min(digit);
108
109 module.glwe_to_glwe_blind_rotation(
110 &mut res,
111 &test_glwe,
112 &k_enc_prep,
113 bit_start,
114 bit_size,
115 bit_step,
116 scratch.borrow(),
117 );
118
119 res.decrypt(&module, &mut pt, &sk_glwe_prep, scratch.borrow());
120
121 assert_eq!(
122 (((k >> bit_start) & mask) << bit_step) as i64,
123 pt.decode_coeff_i64(base2k.as_usize().into(), 0)
124 );
125
126 bit_step += digit;
127 bit_start += digit;
128
129 if bit_start >= 32 {
130 break;
131 }
132 }
133 }
134}