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 let k: u32 = source.next_u32();
74
75 let mut k_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> =
76 FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
77 k_enc_prep.encrypt_sk(
78 &module,
79 k,
80 &sk_glwe_prep,
81 &mut source_xa,
82 &mut source_xe,
83 scratch.borrow(),
84 );
85
86 let base: [usize; 2] = [6, 5];
87
88 assert_eq!(base.iter().sum::<usize>(), module.log_n());
89
90 // Starting bit
91 let mut bit_start: usize = 0;
92
93 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
94
95 for _ in 0..32_usize.div_ceil(module.log_n()) {
96 // By how many bits to left shift
97 let mut bit_step: usize = 0;
98
99 for digit in base {
100 let mask: u32 = (1 << digit) - 1;
101
102 // How many bits to take
103 let bit_size: usize = (32 - bit_start).min(digit);
104
105 module.glwe_to_glwe_blind_rotation(
106 &mut res,
107 &test_glwe,
108 &k_enc_prep,
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}