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 DFT, IDFT, IDFTConsume, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApply, SvpApplyInplace, SvpPPolAlloc,
4 SvpPPolAllocBytes, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace,
5 VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd,
6 VecZnxDftAddInplace, VecZnxDftAllocBytes, VecZnxDftSubABInplace, VecZnxDftZero, VecZnxFillUniform, VecZnxIDFTTmpBytes,
7 VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate,
8 VecZnxRotateInplace, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes,
9 VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace,
10 },
11 layouts::{Backend, Module, ScratchOwned, ZnxView},
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 + VecZnxIDFTTmpBytes
37 + IDFT<B>
38 + VecZnxDftAdd<B>
39 + VecZnxDftAddInplace<B>
40 + DFT<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 + IDFTConsume<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 + ZnFillUniform
70 + ZnAddNormal
71 + ZnNormalizeInplace<B>,
72 B: Backend
73 + VecZnxDftAllocBytesImpl<B>
74 + VecZnxBigAllocBytesImpl<B>
75 + ScratchOwnedAllocImpl<B>
76 + ScratchOwnedBorrowImpl<B>
77 + TakeVecZnxDftImpl<B>
78 + TakeVecZnxBigImpl<B>
79 + TakeVecZnxDftSliceImpl<B>
80 + ScratchAvailableImpl<B>
81 + TakeVecZnxImpl<B>
82 + TakeVecZnxSliceImpl<B>,
83{
84 let n: usize = module.n();
85 let basek: usize = 19;
86 let k_lwe: usize = 24;
87 let k_brk: usize = 3 * basek;
88 let rows_brk: usize = 2; // Ensures first limb is noise-free.
89 let k_lut: usize = basek;
90 let k_res: usize = 2 * basek;
91 let rank: usize = 1;
92
93 let log_message_modulus = 4;
94
95 let message_modulus: usize = 1 << log_message_modulus;
96
97 let mut source_xs: Source = Source::new([2u8; 32]);
98 let mut source_xe: Source = Source::new([2u8; 32]);
99 let mut source_xa: Source = Source::new([1u8; 32]);
100
101 let mut scratch: ScratchOwned<B> = ScratchOwned::<B>::alloc(BlindRotationKey::generate_from_sk_scratch_space(
102 module, basek, k_brk, rank,
103 ));
104
105 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
106 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
107 let sk_glwe_dft: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
108
109 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
110 sk_lwe.fill_binary_block(block_size, &mut source_xs);
111
112 let mut scratch_br: ScratchOwned<B> = ScratchOwned::<B>::alloc(cggi_blind_rotate_scratch_space(
113 module,
114 block_size,
115 extension_factor,
116 basek,
117 k_res,
118 k_brk,
119 rows_brk,
120 rank,
121 ));
122
123 let mut brk: BlindRotationKey<Vec<u8>, CGGI> =
124 BlindRotationKey::<Vec<u8>, CGGI>::alloc(n, n_lwe, basek, k_brk, rows_brk, rank);
125
126 brk.encrypt_sk(
127 module,
128 &sk_glwe_dft,
129 &sk_lwe,
130 &mut source_xa,
131 &mut source_xe,
132 scratch.borrow(),
133 );
134
135 let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
136
137 let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
138
139 let x: i64 = 15 % (message_modulus as i64);
140
141 pt_lwe.encode_i64(x, log_message_modulus + 1);
142
143 lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);
144
145 let f = |x: i64| -> i64 { 2 * x + 1 };
146
147 let mut f_vec: Vec<i64> = vec![0i64; message_modulus];
148 f_vec
149 .iter_mut()
150 .enumerate()
151 .for_each(|(i, x)| *x = f(i as i64));
152
153 let mut lut: LookUpTable = LookUpTable::alloc(module, basek, k_lut, extension_factor);
154 lut.set(module, &f_vec, log_message_modulus + 1);
155
156 let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_res, rank);
157
158 let brk_prepared: BlindRotationKeyPrepared<Vec<u8>, CGGI, B> = brk.prepare_alloc(module, scratch.borrow());
159
160 brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow());
161
162 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_res);
163
164 res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
165
166 let mut lwe_2n: Vec<i64> = vec![0i64; lwe.n() + 1]; // TODO: from scratch space
167
168 mod_switch_2n(
169 2 * lut.domain_size(),
170 &mut lwe_2n,
171 &lwe.to_ref(),
172 lut.rotation_direction(),
173 );
174
175 let pt_want: i64 = (lwe_2n[0]
176 + lwe_2n[1..]
177 .iter()
178 .zip(sk_lwe.raw())
179 .map(|(x, y)| x * y)
180 .sum::<i64>())
181 & (2 * lut.domain_size() - 1) as i64;
182
183 lut.rotate(module, pt_want);
184
185 // First limb should be exactly equal (test are parameterized such that the noise does not reach
186 // the first limb)
187 assert_eq!(pt_have.data.at(0, 0), lut.data[0].at(0, 0));
188
189 // Verify that it effectively compute f(x)
190 let mut have: i64 = pt_have.decode_coeff_i64(log_message_modulus + 1, 0);
191
192 // Get positive representative and assert equality
193 have = (have + message_modulus as i64) % (message_modulus as i64);
194
195 assert_eq!(have, f(x) % (message_modulus as i64));
196}