poulpy/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs
poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rsRaw
1use poulpy_hal::{
2 api::{
3 ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApply, SvpApplyInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare,
4 VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
5 VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace,
6 VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftSubABInplace, VecZnxDftToVecZnxBig, VecZnxDftToVecZnxBigConsume,
7 VecZnxDftToVecZnxBigTmpBytes, VecZnxDftZero, VecZnxFillUniform, VecZnxMulXpMinusOneInplace, VecZnxNormalize,
8 VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxSub, VecZnxSubABInplace,
9 VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare, ZnxView,
10 },
11 layouts::{Backend, Module, ScratchOwned},
12 oep::{
13 ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl,
14 TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
15 },
16 source::Source,
17};
18
19use crate::tfhe::blind_rotation::{
20 BlincRotationExecute, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyPrepared, CGGI,
21 LookUpTable, cggi_blind_rotate_scratch_space, mod_switch_2n,
22};
23
24use poulpy_core::layouts::{
25 GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, LWECiphertext, LWECiphertextToRef, LWEPlaintext, LWESecret,
26 prepared::{GLWESecretPrepared, PrepareAlloc},
27};
28
29pub fn test_blind_rotation<B>(module: &Module<B>, n_lwe: usize, block_size: usize, extension_factor: usize)
30where
31 Module<B>: VecZnxBigAllocBytes
32 + VecZnxDftAllocBytes
33 + SvpPPolAllocBytes
34 + VmpApplyTmpBytes
35 + VecZnxBigNormalizeTmpBytes
36 + VecZnxDftToVecZnxBigTmpBytes
37 + VecZnxDftToVecZnxBig<B>
38 + VecZnxDftAdd<B>
39 + VecZnxDftAddInplace<B>
40 + VecZnxDftFromVecZnx<B>
41 + VecZnxDftZero<B>
42 + SvpApply<B>
43 + VecZnxDftSubABInplace<B>
44 + VecZnxBigAddSmallInplace<B>
45 + VecZnxRotate
46 + VecZnxAddInplace
47 + VecZnxSubABInplace
48 + VecZnxNormalize<B>
49 + VecZnxNormalizeInplace<B>
50 + VecZnxCopy
51 + VecZnxMulXpMinusOneInplace
52 + SvpPrepare<B>
53 + SvpPPolAlloc<B>
54 + SvpApplyInplace<B>
55 + VecZnxDftToVecZnxBigConsume<B>
56 + VecZnxBigAddInplace<B>
57 + VecZnxBigNormalize<B>
58 + VecZnxNormalizeTmpBytes
59 + VecZnxFillUniform
60 + VecZnxAddNormal
61 + VecZnxAddScalarInplace
62 + VecZnxRotateInplace
63 + VecZnxSwithcDegree
64 + VecZnxSub
65 + VmpPMatAlloc<B>
66 + VmpPrepare<B>
67 + VmpApply<B>
68 + VmpApplyAdd<B>,
69 B: Backend
70 + VecZnxDftAllocBytesImpl<B>
71 + VecZnxBigAllocBytesImpl<B>
72 + ScratchOwnedAllocImpl<B>
73 + ScratchOwnedBorrowImpl<B>
74 + TakeVecZnxDftImpl<B>
75 + TakeVecZnxBigImpl<B>
76 + TakeVecZnxDftSliceImpl<B>
77 + ScratchAvailableImpl<B>
78 + TakeVecZnxImpl<B>
79 + TakeVecZnxSliceImpl<B>,
80{
81 let n: usize = module.n();
82 let basek: usize = 19;
83 let k_lwe: usize = 24;
84 let k_brk: usize = 3 * basek;
85 let rows_brk: usize = 2; // Ensures first limb is noise-free.
86 let k_lut: usize = basek;
87 let k_res: usize = 2 * basek;
88 let rank: usize = 1;
89
90 let log_message_modulus = 4;
91
92 let message_modulus: usize = 1 << log_message_modulus;
93
94 let mut source_xs: Source = Source::new([2u8; 32]);
95 let mut source_xe: Source = Source::new([2u8; 32]);
96 let mut source_xa: Source = Source::new([1u8; 32]);
97
98 let mut scratch: ScratchOwned<B> = ScratchOwned::<B>::alloc(BlindRotationKey::generate_from_sk_scratch_space(
99 module, n, basek, k_brk, rank,
100 ));
101
102 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
103 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
104 let sk_glwe_dft: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
105
106 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
107 sk_lwe.fill_binary_block(block_size, &mut source_xs);
108
109 let mut scratch_br: ScratchOwned<B> = ScratchOwned::<B>::alloc(cggi_blind_rotate_scratch_space(
110 module,
111 n,
112 block_size,
113 extension_factor,
114 basek,
115 k_res,
116 k_brk,
117 rows_brk,
118 rank,
119 ));
120
121 let mut brk: BlindRotationKey<Vec<u8>, CGGI> =
122 BlindRotationKey::<Vec<u8>, CGGI>::alloc(n, n_lwe, basek, k_brk, rows_brk, rank);
123
124 brk.encrypt_sk(
125 module,
126 &sk_glwe_dft,
127 &sk_lwe,
128 &mut source_xa,
129 &mut source_xe,
130 scratch.borrow(),
131 );
132
133 let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
134
135 let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
136
137 let x: i64 = 15 % (message_modulus as i64);
138
139 pt_lwe.encode_i64(x, log_message_modulus + 1);
140
141 lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);
142
143 let f = |x: i64| -> i64 { 2 * x + 1 };
144
145 let mut f_vec: Vec<i64> = vec![0i64; message_modulus];
146 f_vec
147 .iter_mut()
148 .enumerate()
149 .for_each(|(i, x)| *x = f(i as i64));
150
151 let mut lut: LookUpTable = LookUpTable::alloc(n, basek, k_lut, extension_factor);
152 lut.set(module, &f_vec, log_message_modulus + 1);
153
154 let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_res, rank);
155
156 let brk_prepared: BlindRotationKeyPrepared<Vec<u8>, CGGI, B> = brk.prepare_alloc(module, scratch.borrow());
157
158 brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow());
159
160 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_res);
161
162 res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
163
164 let mut lwe_2n: Vec<i64> = vec![0i64; lwe.n() + 1]; // TODO: from scratch space
165
166 mod_switch_2n(
167 2 * lut.domain_size(),
168 &mut lwe_2n,
169 &lwe.to_ref(),
170 lut.rotation_direction(),
171 );
172
173 let pt_want: i64 = (lwe_2n[0]
174 + lwe_2n[1..]
175 .iter()
176 .zip(sk_lwe.raw())
177 .map(|(x, y)| x * y)
178 .sum::<i64>())
179 & (2 * lut.domain_size() - 1) as i64;
180
181 lut.rotate(module, pt_want);
182
183 // First limb should be exactly equal (test are parameterized such that the noise does not reach
184 // the first limb)
185 assert_eq!(pt_have.data.at(0, 0), lut.data[0].at(0, 0));
186
187 // Verify that it effectively compute f(x)
188 let mut have: i64 = pt_have.decode_coeff_i64(log_message_modulus + 1, 0);
189
190 // Get positive representative and assert equality
191 have = (have + message_modulus as i64) % (message_modulus as i64);
192
193 assert_eq!(have, f(x) % (message_modulus as i64));
194}