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