poulpy/core/src/test_fft64/glwe_fourier.rs
core/src/test_fft64/glwe_fourier.rsRaw
1use crate::{
2 elem::Infos,
3 ggsw_ciphertext::GGSWCiphertext,
4 glwe_ciphertext::GLWECiphertext,
5 glwe_ciphertext_fourier::GLWECiphertextFourier,
6 glwe_plaintext::GLWEPlaintext,
7 keys::{SecretKey, SecretKeyFourier},
8 keyswitch_key::GLWESwitchingKey,
9 test_fft64::{gglwe::log2_std_noise_gglwe_product, ggsw::noise_ggsw_product},
10};
11use backend::{FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, VecZnxToMut, ZnxViewMut};
12use sampling::source::Source;
13
14#[test]
15fn keyswitch() {
16 (1..4).for_each(|rank_in| {
17 (1..4).for_each(|rank_out| {
18 println!("test keyswitch rank_in: {} rank_out: {}", rank_in, rank_out);
19 test_keyswitch(12, 12, 60, 45, 60, rank_in, rank_out, 3.2);
20 });
21 });
22}
23
24#[test]
25fn keyswitch_inplace() {
26 (1..4).for_each(|rank| {
27 println!("test keyswitch_inplace rank: {}", rank);
28 test_keyswitch_inplace(12, 12, 60, 45, rank, 3.2);
29 });
30}
31
32#[test]
33fn external_product() {
34 (1..4).for_each(|rank| {
35 println!("test external_product rank: {}", rank);
36 test_external_product(12, 12, 60, 45, 60, rank, 3.2);
37 });
38}
39
40#[test]
41fn external_product_inplace() {
42 (1..4).for_each(|rank| {
43 println!("test external_product rank: {}", rank);
44 test_external_product_inplace(12, 15, 60, 60, rank, 3.2);
45 });
46}
47
48fn test_keyswitch(
49 log_n: usize,
50 basek: usize,
51 k_ksk: usize,
52 k_ct_in: usize,
53 k_ct_out: usize,
54 rank_in: usize,
55 rank_out: usize,
56 sigma: f64,
57) {
58 let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
59
60 let rows: usize = (k_ct_in + basek - 1) / basek;
61
62 let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::alloc(&module, basek, k_ksk, rows, rank_in, rank_out);
63 let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank_in);
64 let mut ct_glwe_dft_in: GLWECiphertextFourier<Vec<u8>, FFT64> =
65 GLWECiphertextFourier::alloc(&module, basek, k_ct_in, rank_in);
66 let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, rank_out);
67 let mut ct_glwe_dft_out: GLWECiphertextFourier<Vec<u8>, FFT64> =
68 GLWECiphertextFourier::alloc(&module, basek, k_ct_out, rank_out);
69 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_in);
70 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_out);
71
72 let mut source_xs: Source = Source::new([0u8; 32]);
73 let mut source_xe: Source = Source::new([0u8; 32]);
74 let mut source_xa: Source = Source::new([0u8; 32]);
75
76 // Random input plaintext
77 pt_want
78 .data
79 .fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
80
81 let mut scratch: ScratchOwned = ScratchOwned::new(
82 GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ksk.size())
83 | GLWECiphertext::decrypt_scratch_space(&module, ct_glwe_out.size())
84 | GLWECiphertext::encrypt_sk_scratch_space(&module, ct_glwe_in.size())
85 | GLWECiphertextFourier::keyswitch_scratch_space(
86 &module,
87 ct_glwe_out.size(),
88 rank_out,
89 ct_glwe_in.size(),
90 rank_in,
91 ksk.size(),
92 ),
93 );
94
95 let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
96 sk_in.fill_ternary_prob(0.5, &mut source_xs);
97
98 let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
99 sk_in_dft.dft(&module, &sk_in);
100
101 let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
102 sk_out.fill_ternary_prob(0.5, &mut source_xs);
103
104 let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
105 sk_out_dft.dft(&module, &sk_out);
106
107 ksk.encrypt_sk(
108 &module,
109 &sk_in,
110 &sk_out_dft,
111 &mut source_xa,
112 &mut source_xe,
113 sigma,
114 scratch.borrow(),
115 );
116
117 ct_glwe_in.encrypt_sk(
118 &module,
119 &pt_want,
120 &sk_in_dft,
121 &mut source_xa,
122 &mut source_xe,
123 sigma,
124 scratch.borrow(),
125 );
126
127 ct_glwe_in.dft(&module, &mut ct_glwe_dft_in);
128 ct_glwe_dft_out.keyswitch(&module, &ct_glwe_dft_in, &ksk, scratch.borrow());
129 ct_glwe_dft_out.idft(&module, &mut ct_glwe_out, scratch.borrow());
130
131 ct_glwe_out.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
132
133 module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
134
135 let noise_have: f64 = pt_have.data.std(0, basek).log2();
136 let noise_want: f64 = log2_std_noise_gglwe_product(
137 module.n() as f64,
138 basek,
139 0.5,
140 0.5,
141 0f64,
142 sigma * sigma,
143 0f64,
144 rank_in as f64,
145 k_ct_in,
146 k_ksk,
147 );
148
149 assert!(
150 (noise_have - noise_want).abs() <= 0.1,
151 "{} {}",
152 noise_have,
153 noise_want
154 );
155}
156
157fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize, rank: usize, sigma: f64) {
158 let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
159 let rows: usize = (k_ct + basek - 1) / basek;
160
161 let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::alloc(&module, basek, k_ksk, rows, rank, rank);
162 let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
163 let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ct, rank);
164 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
165 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
166
167 let mut source_xs: Source = Source::new([0u8; 32]);
168 let mut source_xe: Source = Source::new([0u8; 32]);
169 let mut source_xa: Source = Source::new([0u8; 32]);
170
171 // Random input plaintext
172 pt_want
173 .data
174 .fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
175
176 let mut scratch: ScratchOwned = ScratchOwned::new(
177 GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ksk.size())
178 | GLWECiphertext::decrypt_scratch_space(&module, ct_glwe.size())
179 | GLWECiphertext::encrypt_sk_scratch_space(&module, ct_glwe.size())
180 | GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, ct_rlwe_dft.size(), ksk.size(), rank),
181 );
182
183 let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
184 sk_in.fill_ternary_prob(0.5, &mut source_xs);
185
186 let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
187 sk_in_dft.dft(&module, &sk_in);
188
189 let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
190 sk_out.fill_ternary_prob(0.5, &mut source_xs);
191
192 let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
193 sk_out_dft.dft(&module, &sk_out);
194
195 ksk.encrypt_sk(
196 &module,
197 &sk_in,
198 &sk_out_dft,
199 &mut source_xa,
200 &mut source_xe,
201 sigma,
202 scratch.borrow(),
203 );
204
205 ct_glwe.encrypt_sk(
206 &module,
207 &pt_want,
208 &sk_in_dft,
209 &mut source_xa,
210 &mut source_xe,
211 sigma,
212 scratch.borrow(),
213 );
214
215 ct_glwe.dft(&module, &mut ct_rlwe_dft);
216 ct_rlwe_dft.keyswitch_inplace(&module, &ksk, scratch.borrow());
217 ct_rlwe_dft.idft(&module, &mut ct_glwe, scratch.borrow());
218
219 ct_glwe.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
220
221 module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
222
223 let noise_have: f64 = pt_have.data.std(0, basek).log2();
224 let noise_want: f64 = log2_std_noise_gglwe_product(
225 module.n() as f64,
226 basek,
227 0.5,
228 0.5,
229 0f64,
230 sigma * sigma,
231 0f64,
232 rank as f64,
233 k_ct,
234 k_ksk,
235 );
236
237 assert!(
238 (noise_have - noise_want).abs() <= 0.1,
239 "{} {}",
240 noise_have,
241 noise_want
242 );
243}
244
245fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usize, k_ct_out: usize, rank: usize, sigma: f64) {
246 let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
247
248 let rows: usize = (k_ct_in + basek - 1) / basek;
249
250 let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
251 let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_in, rank);
252 let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct_out, rank);
253 let mut ct_in_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ct_in, rank);
254 let mut ct_out_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ct_out, rank);
255 let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
256 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_in);
257 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct_out);
258
259 let mut source_xs: Source = Source::new([0u8; 32]);
260 let mut source_xe: Source = Source::new([0u8; 32]);
261 let mut source_xa: Source = Source::new([0u8; 32]);
262
263 // Random input plaintext
264 pt_want
265 .data
266 .fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
267
268 pt_want.to_mut().at_mut(0, 0)[1] = 1;
269
270 let k: usize = 1;
271
272 pt_rgsw.raw_mut()[k] = 1; // X^{k}
273
274 let mut scratch: ScratchOwned = ScratchOwned::new(
275 GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
276 | GLWECiphertext::decrypt_scratch_space(&module, ct_out.size())
277 | GLWECiphertext::encrypt_sk_scratch_space(&module, ct_in.size())
278 | GLWECiphertextFourier::external_product_scratch_space(&module, ct_out.size(), ct_in.size(), ct_rgsw.size(), rank),
279 );
280
281 let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
282 sk.fill_ternary_prob(0.5, &mut source_xs);
283
284 let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
285 sk_dft.dft(&module, &sk);
286
287 ct_rgsw.encrypt_sk(
288 &module,
289 &pt_rgsw,
290 &sk_dft,
291 &mut source_xa,
292 &mut source_xe,
293 sigma,
294 scratch.borrow(),
295 );
296
297 ct_in.encrypt_sk(
298 &module,
299 &pt_want,
300 &sk_dft,
301 &mut source_xa,
302 &mut source_xe,
303 sigma,
304 scratch.borrow(),
305 );
306
307 ct_in.dft(&module, &mut ct_in_dft);
308 ct_out_dft.external_product(&module, &ct_in_dft, &ct_rgsw, scratch.borrow());
309 ct_out_dft.idft(&module, &mut ct_out, scratch.borrow());
310
311 ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
312
313 module.vec_znx_rotate_inplace(k as i64, &mut pt_want, 0);
314
315 module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
316
317 let noise_have: f64 = pt_have.data.std(0, basek).log2();
318
319 let var_gct_err_lhs: f64 = sigma * sigma;
320 let var_gct_err_rhs: f64 = 0f64;
321
322 let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
323 let var_a0_err: f64 = sigma * sigma;
324 let var_a1_err: f64 = 1f64 / 12f64;
325
326 let noise_want: f64 = noise_ggsw_product(
327 module.n() as f64,
328 basek,
329 0.5,
330 var_msg,
331 var_a0_err,
332 var_a1_err,
333 var_gct_err_lhs,
334 var_gct_err_rhs,
335 rank as f64,
336 k_ct_in,
337 k_ggsw,
338 );
339
340 assert!(
341 (noise_have - noise_want).abs() <= 0.1,
342 "{} {}",
343 noise_have,
344 noise_want
345 );
346}
347
348fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct: usize, rank: usize, sigma: f64) {
349 let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
350 let rows: usize = (k_ct + basek - 1) / basek;
351
352 let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::alloc(&module, basek, k_ggsw, rows, rank);
353 let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_ct, rank);
354 let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ct, rank);
355 let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
356 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
357 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ct);
358
359 let mut source_xs: Source = Source::new([0u8; 32]);
360 let mut source_xe: Source = Source::new([0u8; 32]);
361 let mut source_xa: Source = Source::new([0u8; 32]);
362
363 // Random input plaintext
364 pt_want
365 .data
366 .fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
367
368 pt_want.to_mut().at_mut(0, 0)[1] = 1;
369
370 let k: usize = 1;
371
372 pt_rgsw.raw_mut()[k] = 1; // X^{k}
373
374 let mut scratch: ScratchOwned = ScratchOwned::new(
375 GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_ggsw.size())
376 | GLWECiphertext::decrypt_scratch_space(&module, ct.size())
377 | GLWECiphertext::encrypt_sk_scratch_space(&module, ct.size())
378 | GLWECiphertextFourier::external_product_inplace_scratch_space(&module, ct.size(), ct_ggsw.size(), rank),
379 );
380
381 let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
382 sk.fill_ternary_prob(0.5, &mut source_xs);
383
384 let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
385 sk_dft.dft(&module, &sk);
386
387 ct_ggsw.encrypt_sk(
388 &module,
389 &pt_rgsw,
390 &sk_dft,
391 &mut source_xa,
392 &mut source_xe,
393 sigma,
394 scratch.borrow(),
395 );
396
397 ct.encrypt_sk(
398 &module,
399 &pt_want,
400 &sk_dft,
401 &mut source_xa,
402 &mut source_xe,
403 sigma,
404 scratch.borrow(),
405 );
406
407 ct.dft(&module, &mut ct_rlwe_dft);
408 ct_rlwe_dft.external_product_inplace(&module, &ct_ggsw, scratch.borrow());
409 ct_rlwe_dft.idft(&module, &mut ct, scratch.borrow());
410
411 ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
412
413 module.vec_znx_rotate_inplace(k as i64, &mut pt_want, 0);
414
415 module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
416
417 let noise_have: f64 = pt_have.data.std(0, basek).log2();
418
419 let var_gct_err_lhs: f64 = sigma * sigma;
420 let var_gct_err_rhs: f64 = 0f64;
421
422 let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
423 let var_a0_err: f64 = sigma * sigma;
424 let var_a1_err: f64 = 1f64 / 12f64;
425
426 let noise_want: f64 = noise_ggsw_product(
427 module.n() as f64,
428 basek,
429 0.5,
430 var_msg,
431 var_a0_err,
432 var_a1_err,
433 var_gct_err_lhs,
434 var_gct_err_rhs,
435 rank as f64,
436 k_ct,
437 k_ggsw,
438 );
439
440 assert!(
441 (noise_have - noise_want).abs() <= 0.1,
442 "{} {}",
443 noise_have,
444 noise_want
445 );
446}