poulpy/poulpy-core/src/tests/test_suite/conversion.rs
poulpy-core/src/tests/test_suite/conversion.rsRaw
1use poulpy_hal::{
2 api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, VecZnxNormalize},
3 layouts::{Backend, FillUniform, Module, Scratch, ScratchOwned, ZnxView},
4 source::Source,
5};
6use rug::Float;
7
8use crate::{
9 GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWENoise, GLWENormalize, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk,
10 LWEFromGLWE, LWEToGLWESwitchingKeyEncryptSk, SIGMA, ScratchTakeCore,
11 layouts::{
12 Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKey,
13 GLWEToLWEKeyLayout, GLWEToLWEKeyPrepared, GLWEToLWEKeyPreparedFactory, LWE, LWEInfos, LWELayout, LWEPlaintext, LWESecret,
14 LWEToGLWEKey, LWEToGLWEKeyLayout, LWEToGLWEKeyPrepared, LWEToGLWEKeyPreparedFactory, Rank, TorusPrecision,
15 prepared::GLWESecretPrepared,
16 },
17};
18
19pub fn test_glwe_base2k_conversion<BE: Backend>(module: &Module<BE>)
20where
21 Module<BE>: GLWEEncryptSk<BE>
22 + GLWEDecrypt<BE>
23 + GLWENormalize<BE>
24 + VecZnxFillUniform
25 + GLWESecretPreparedFactory<BE>
26 + GLWENoise<BE>,
27 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
28 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
29{
30 let n_glwe: Degree = Degree(module.n() as u32);
31
32 let mut source_xs: Source = Source::new([0u8; 32]);
33 let mut source_xa: Source = Source::new([0u8; 32]);
34 let mut source_xe: Source = Source::new([0u8; 32]);
35
36 for rank in 1_usize..3 {
37 for bases in [[12, 8], [8, 12]] {
38 let glwe_infos_in: GLWELayout = GLWELayout {
39 n: n_glwe,
40 base2k: Base2K(bases[0]),
41 k: TorusPrecision(34),
42 rank: Rank(rank as u32),
43 };
44
45 let glwe_infos_out: GLWELayout = GLWELayout {
46 n: n_glwe,
47 base2k: Base2K(bases[1]),
48 k: TorusPrecision(34),
49 rank: Rank(rank as u32),
50 };
51
52 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(module.n().into(), rank.into());
53 sk.fill_ternary_prob(0.5, &mut source_xs);
54
55 let mut sk_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
56 sk_prep.prepare(module, &sk);
57
58 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
59 GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos_in).max(module.glwe_noise_tmp_bytes(&glwe_infos_out)),
60 );
61
62 let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos_in);
63 let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos_out);
64
65 let pt_in: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos_in);
66 let pt_out: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos_out);
67
68 ct_in.encrypt_sk(
69 module,
70 &pt_in,
71 &sk_prep,
72 &mut source_xa,
73 &mut source_xe,
74 scratch.borrow(),
75 );
76
77 let mut data: Vec<Float> = (0..module.n()).map(|_| Float::with_val(128, 0)).collect();
78 ct_in
79 .data()
80 .decode_vec_float(ct_in.base2k().into(), 0, &mut data);
81
82 ct_out.fill_uniform(ct_out.base2k().into(), &mut source_xa);
83 module.glwe_normalize(&mut ct_out, &ct_in, scratch.borrow());
84
85 let mut data_conv: Vec<Float> = (0..module.n()).map(|_| Float::with_val(128, 0)).collect();
86 ct_out
87 .data()
88 .decode_vec_float(ct_out.base2k().into(), 0, &mut data_conv);
89
90 assert!(
91 ct_out
92 .noise(module, &pt_out, &sk_prep, scratch.borrow())
93 .std()
94 .log2()
95 <= -(ct_out.k().as_u32() as f64) + SIGMA.log2() + 0.50
96 )
97 }
98 }
99}
100
101pub fn test_lwe_to_glwe<BE: Backend>(module: &Module<BE>)
102where
103 Module<BE>: GLWEFromLWE<BE>
104 + LWEToGLWESwitchingKeyEncryptSk<BE>
105 + GLWEDecrypt<BE>
106 + GLWESecretPreparedFactory<BE>
107 + LWEEncryptSk<BE>
108 + LWEToGLWEKeyPreparedFactory<BE>
109 + VecZnxNormalize<BE>,
110 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
111 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
112{
113 let n_glwe: Degree = Degree(module.n() as u32);
114 let n_lwe: Degree = Degree(22);
115
116 let rank: Rank = Rank(2);
117 let k_lwe_pt: TorusPrecision = TorusPrecision(8);
118
119 let mut source_xs: Source = Source::new([0u8; 32]);
120 let mut source_xa: Source = Source::new([0u8; 32]);
121 let mut source_xe: Source = Source::new([0u8; 32]);
122
123 let lwe_to_glwe_infos: LWEToGLWEKeyLayout = LWEToGLWEKeyLayout {
124 n: n_glwe,
125 base2k: Base2K(13),
126 k: TorusPrecision(92),
127 dnum: Dnum(2),
128 rank_out: rank,
129 };
130
131 let glwe_infos: GLWELayout = GLWELayout {
132 n: n_glwe,
133 base2k: Base2K(15),
134 k: TorusPrecision(75),
135 rank,
136 };
137
138 let lwe_infos: LWELayout = LWELayout {
139 n: n_lwe,
140 base2k: Base2K(17),
141 k: TorusPrecision(75),
142 };
143
144 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
145 LWEToGLWEKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos)
146 | GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos)
147 | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
148 );
149
150 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
151 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
152
153 let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe);
154 sk_glwe_prepared.prepare(module, &sk_glwe);
155
156 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
157 sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
158
159 let data: i64 = 17;
160
161 let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
162 lwe_pt.encode_i64(data, k_lwe_pt);
163
164 let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
165 lwe_ct.encrypt_sk(
166 module,
167 &lwe_pt,
168 &sk_lwe,
169 &mut source_xa,
170 &mut source_xe,
171 scratch.borrow(),
172 );
173
174 let mut ksk: LWEToGLWEKey<Vec<u8>> = LWEToGLWEKey::alloc_from_infos(&lwe_to_glwe_infos);
175
176 ksk.encrypt_sk(
177 module,
178 &sk_lwe,
179 &sk_glwe_prepared,
180 &mut source_xa,
181 &mut source_xe,
182 scratch.borrow(),
183 );
184
185 let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
186
187 let mut ksk_prepared: LWEToGLWEKeyPrepared<Vec<u8>, BE> = LWEToGLWEKeyPrepared::alloc_from_infos(module, &ksk);
188 ksk_prepared.prepare(module, &ksk, scratch.borrow());
189
190 glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
191
192 let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
193 glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow());
194
195 let mut lwe_pt_conv = LWEPlaintext::alloc(glwe_pt.base2k(), lwe_pt.k());
196
197 module.vec_znx_normalize(
198 glwe_pt.base2k().as_usize(),
199 lwe_pt_conv.data_mut(),
200 0,
201 lwe_pt.base2k().as_usize(),
202 lwe_pt.data(),
203 0,
204 scratch.borrow(),
205 );
206
207 assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt_conv.data.at(0, 0)[0]);
208}
209
210pub fn test_glwe_to_lwe<BE: Backend>(module: &Module<BE>)
211where
212 Module<BE>: GLWEFromLWE<BE>
213 + GLWEToLWESwitchingKeyEncryptSk<BE>
214 + GLWEEncryptSk<BE>
215 + LWEDecrypt<BE>
216 + LWEFromGLWE<BE>
217 + GLWEDecrypt<BE>
218 + GLWESecretPreparedFactory<BE>
219 + GLWEToLWESwitchingKeyEncryptSk<BE>
220 + GLWEToLWEKeyPreparedFactory<BE>
221 + VecZnxNormalize<BE>,
222 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
223 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
224{
225 let n_glwe: Degree = Degree(module.n() as u32);
226 let n_lwe: Degree = Degree(22);
227
228 let rank: Rank = Rank(2);
229 let k_lwe_pt: TorusPrecision = TorusPrecision(8);
230
231 let glwe_to_lwe_infos: GLWEToLWEKeyLayout = GLWEToLWEKeyLayout {
232 n: n_glwe,
233 base2k: Base2K(13),
234 k: TorusPrecision(91),
235 dnum: Dnum(2),
236 rank_in: rank,
237 };
238
239 let glwe_infos: GLWELayout = GLWELayout {
240 n: n_glwe,
241 base2k: Base2K(17),
242 k: TorusPrecision(72),
243 rank,
244 };
245
246 let lwe_infos: LWELayout = LWELayout {
247 n: n_lwe,
248 base2k: Base2K(15),
249 k: TorusPrecision(72),
250 };
251
252 let mut source_xs: Source = Source::new([0u8; 32]);
253 let mut source_xa: Source = Source::new([0u8; 32]);
254 let mut source_xe: Source = Source::new([0u8; 32]);
255
256 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
257 GLWEToLWEKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos)
258 | LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos)
259 | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
260 );
261
262 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
263 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
264
265 let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe);
266 sk_glwe_prepared.prepare(module, &sk_glwe);
267
268 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
269 sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
270
271 let a_idx: usize = 1;
272
273 let mut data: Vec<i64> = vec![0i64; module.n()];
274 data[a_idx] = 17;
275 let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
276 glwe_pt.encode_vec_i64(&data, k_lwe_pt);
277
278 let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
279 glwe_ct.encrypt_sk(
280 module,
281 &glwe_pt,
282 &sk_glwe_prepared,
283 &mut source_xa,
284 &mut source_xe,
285 scratch.borrow(),
286 );
287
288 let mut ksk: GLWEToLWEKey<Vec<u8>> = GLWEToLWEKey::alloc_from_infos(&glwe_to_lwe_infos);
289
290 ksk.encrypt_sk(
291 module,
292 &sk_lwe,
293 &sk_glwe,
294 &mut source_xa,
295 &mut source_xe,
296 scratch.borrow(),
297 );
298
299 let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
300
301 let mut ksk_prepared: GLWEToLWEKeyPrepared<Vec<u8>, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk);
302 ksk_prepared.prepare(module, &ksk, scratch.borrow());
303
304 lwe_ct.from_glwe(module, &glwe_ct, a_idx, &ksk_prepared, scratch.borrow());
305
306 let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
307 lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe, scratch.borrow());
308
309 let mut glwe_pt_conv = GLWEPlaintext::alloc(glwe_ct.n(), lwe_pt.base2k(), lwe_pt.k());
310
311 module.vec_znx_normalize(
312 lwe_pt.base2k().as_usize(),
313 glwe_pt_conv.data_mut(),
314 0,
315 glwe_ct.base2k().as_usize(),
316 glwe_pt.data(),
317 0,
318 scratch.borrow(),
319 );
320
321 assert_eq!(glwe_pt_conv.data.at(0, 0)[a_idx], lwe_pt.data.at(0, 0)[0]);
322}