poulpy/core/src/blind_rotation/test_fft64/cggi.rs
core/src/blind_rotation/test_fft64/cggi.rsRaw
1use std::time::Instant;
2
3use backend::{Encoding, FFT64, Module, ScratchOwned, Stats, VecZnxOps, ZnxView};
4use sampling::source::Source;
5
6use crate::{
7 FourierGLWESecret, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, LWECiphertext, LWESecret,
8 blind_rotation::{
9 ccgi::{cggi_blind_rotate, cggi_blind_rotate_scratch_space, mod_switch_2n},
10 key::BlindRotationKeyCGGI,
11 lut::LookUpTable,
12 },
13 lwe::{LWEPlaintext, ciphertext::LWECiphertextToRef},
14};
15
16#[test]
17fn blind_rotation() {
18 let module: Module<FFT64> = Module::<FFT64>::new(2048);
19 let basek: usize = 20;
20
21 let n_lwe: usize = 1071;
22
23 let k_lwe: usize = 22;
24 let k_brk: usize = 60;
25 let rows_brk: usize = 2;
26 let k_lut: usize = 60;
27 let rank: usize = 1;
28 let block_size: usize = 7;
29
30 let message_modulus: usize = 64;
31
32 let mut source_xs: Source = Source::new([0u8; 32]);
33 let mut source_xe: Source = Source::new([0u8; 32]);
34 let mut source_xa: Source = Source::new([0u8; 32]);
35
36 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
37 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
38 let sk_glwe_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk_glwe);
39
40 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
41 sk_lwe.fill_binary_block(block_size, &mut source_xs);
42
43 let mut scratch: ScratchOwned = ScratchOwned::new(
44 BlindRotationKeyCGGI::generate_from_sk_scratch_space(&module, basek, k_brk, rank)
45 | cggi_blind_rotate_scratch_space(&module, basek, k_lut, k_brk, rows_brk, rank),
46 );
47
48 let start: Instant = Instant::now();
49 let mut brk: BlindRotationKeyCGGI<FFT64> = BlindRotationKeyCGGI::allocate(&module, n_lwe, basek, k_brk, rows_brk, rank);
50
51 brk.generate_from_sk(
52 &module,
53 &sk_glwe_dft,
54 &sk_lwe,
55 &mut source_xa,
56 &mut source_xe,
57 3.2,
58 scratch.borrow(),
59 );
60
61 let duration: std::time::Duration = start.elapsed();
62 println!("brk-gen: {} ms", duration.as_millis());
63
64 let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
65
66 let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
67
68 let x: i64 = 0;
69 let bits: usize = 6;
70
71 pt_lwe.data.encode_coeff_i64(0, basek, bits, 0, x, bits);
72
73 println!("{}", pt_lwe.data);
74
75 lwe.encrypt_sk(&pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe, 3.2);
76
77 lwe.decrypt(&mut pt_lwe, &sk_lwe);
78
79 println!("{}", pt_lwe.data);
80
81 fn lut_fn(x: i64) -> i64 {
82 2 * x + 1
83 }
84
85 let mut lut: LookUpTable = LookUpTable::alloc(&module, basek, k_lut, 1);
86 lut.set(&module, lut_fn, message_modulus);
87
88 let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_lut, rank);
89
90 let start: Instant = Instant::now();
91 (0..1).for_each(|_| {
92 cggi_blind_rotate(&module, &mut res, &lwe, &lut, &brk, scratch.borrow());
93 });
94
95 let duration: std::time::Duration = start.elapsed();
96 println!("blind-rotate: {} ms", duration.as_millis());
97
98 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_lut);
99
100 res.decrypt(&module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
101
102 println!("pt_have: {}", pt_have.data);
103
104 let mut lwe_2n: Vec<i64> = vec![0i64; lwe.n() + 1]; // TODO: from scratch space
105
106 mod_switch_2n(&module, &mut lwe_2n, &lwe.to_ref());
107
108 let pt_want: i64 = (lwe_2n[0]
109 + lwe_2n[1..]
110 .iter()
111 .zip(sk_lwe.data.at(0, 0))
112 .map(|(x, y)| x * y)
113 .sum::<i64>())
114 % (module.n() as i64 * 2);
115
116 module.vec_znx_rotate_inplace(pt_want, &mut lut.data[0], 0);
117
118 println!("pt_want: {}", lut.data[0]);
119
120 module.vec_znx_sub_ab_inplace(&mut lut.data[0], 0, &pt_have.data, 0);
121
122 let noise: f64 = lut.data[0].std(0, basek);
123
124 println!("noise: {}", noise);
125}