poulpy/core/src/blind_rotation/test_fft64/cggi.rs
core/src/blind_rotation/test_fft64/cggi.rsRaw
1use core::time;
2use std::time::Instant;
3
4use backend::{Encoding, Module, ScratchOwned, FFT64};
5use sampling::source::Source;
6
7use crate::{
8 blind_rotation::{ccgi::{cggi_blind_rotate, cggi_blind_rotate_scratch_space}, key::BlindRotationKeyCGGI}, lwe::LWEPlaintext, FourierGLWESecret, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWESecret
9};
10
11#[test]
12fn blind_rotation() {
13 let module: Module<FFT64> = Module::<FFT64>::new(2048);
14 let basek: usize = 17;
15
16 let n_lwe: usize = 1071;
17
18 let k_lwe: usize = 22;
19 let k_brk: usize = 54;
20 let rows_brk: usize = 1;
21 let k_lut: usize = 44;
22 let rank: usize = 1;
23 let block_size: usize = 7;
24
25 let mut source_xs: Source = Source::new([0u8; 32]);
26 let mut source_xe: Source = Source::new([0u8; 32]);
27 let mut source_xa: Source = Source::new([0u8; 32]);
28
29 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
30 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
31 let sk_glwe_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk_glwe);
32
33 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
34 sk_lwe.fill_binary_block(block_size, &mut source_xs);
35
36 let mut scratch: ScratchOwned = ScratchOwned::new(BlindRotationKeyCGGI::generate_from_sk_scratch_space(
37 &module, basek, k_brk, rank,
38 ) | cggi_blind_rotate_scratch_space(&module, basek, k_lut, k_brk, rows_brk, rank));
39
40 let start: Instant = Instant::now();
41 let mut brk: BlindRotationKeyCGGI<FFT64> = BlindRotationKeyCGGI::allocate(&module, n_lwe, basek, k_brk, rows_brk, rank);
42 brk.generate_from_sk(
43 &module,
44 &sk_glwe_dft,
45 &sk_lwe,
46 &mut source_xa,
47 &mut source_xe,
48 3.2,
49 scratch.borrow(),
50 );
51 let duration: std::time::Duration = start.elapsed();
52 println!("brk-gen: {} ms", duration.as_millis());
53
54 let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
55
56 let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
57
58 pt_lwe.data.encode_coeff_i64(0, basek, 7, 0, 63, 7);
59
60 println!("{}", pt_lwe.data);
61
62 lwe.encrypt_sk(&pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe, 3.2);
63
64 lwe.decrypt(&mut pt_lwe, &sk_lwe);
65
66 println!("{}", pt_lwe.data);
67
68 let lut: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_lut);
69
70 let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_lut, rank);
71
72 let start: Instant = Instant::now();
73 (0..32).for_each(|i|{
74 cggi_blind_rotate(&module, &mut res, &lwe, &lut, &brk, scratch.borrow());
75 });
76
77 let duration: std::time::Duration = start.elapsed();
78 println!("blind-rotate: {} ms", duration.as_millis());
79
80 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_lut);
81
82 res.decrypt(&module , &mut pt, &sk_glwe_dft, scratch.borrow());
83
84 println!("{}", pt.data);
85}