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, ScalarZnx, ScratchOwned, Stats, VecZnxOps, ZnxView, ZnxViewMut};
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, negate_and_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 = 18;
20
21 let n_lwe: usize = 1071;
22
23 let k_lwe: usize = 24;
24 let k_brk: usize = 3 * basek;
25 let rows_brk: usize = 2;
26 let k_lut: usize = 2 * basek;
27 let rank: usize = 1;
28 let block_size: usize = 7;
29
30 let extension_factor: usize = 2;
31
32 let message_modulus: usize = 1 << 6;
33
34 let mut source_xs: Source = Source::new([1u8; 32]);
35 let mut source_xe: Source = Source::new([1u8; 32]);
36 let mut source_xa: Source = Source::new([1u8; 32]);
37
38 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
39 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
40 let sk_glwe_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk_glwe);
41
42 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
43 sk_lwe.fill_binary_block(block_size, &mut source_xs);
44
45 sk_lwe.data.raw_mut()[0] = 0;
46
47 println!("sk_lwe: {:?}", sk_lwe.data.raw());
48
49 let mut scratch: ScratchOwned = ScratchOwned::new(
50 BlindRotationKeyCGGI::generate_from_sk_scratch_space(&module, basek, k_brk, rank)
51 | cggi_blind_rotate_scratch_space(
52 &module,
53 extension_factor,
54 basek,
55 k_lut,
56 k_brk,
57 rows_brk,
58 rank,
59 ),
60 );
61
62 let start: Instant = Instant::now();
63 let mut brk: BlindRotationKeyCGGI<FFT64> = BlindRotationKeyCGGI::allocate(&module, n_lwe, basek, k_brk, rows_brk, rank);
64
65 brk.generate_from_sk(
66 &module,
67 &sk_glwe_dft,
68 &sk_lwe,
69 &mut source_xa,
70 &mut source_xe,
71 3.2,
72 scratch.borrow(),
73 );
74
75 let duration: std::time::Duration = start.elapsed();
76 println!("brk-gen: {} ms", duration.as_millis());
77
78 let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
79
80 let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
81
82 let x: i64 = 1;
83 let bits: usize = 8;
84
85 pt_lwe.data.encode_coeff_i64(0, basek, bits, 0, x, bits);
86
87 println!("{}", pt_lwe.data);
88
89 lwe.encrypt_sk(&pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe, 3.2);
90
91 lwe.decrypt(&mut pt_lwe, &sk_lwe);
92
93 println!("{}", pt_lwe.data);
94
95 fn lut_fn(x: i64) -> i64 {
96 2 * x + 1
97 }
98
99 let mut lut: LookUpTable = LookUpTable::alloc(&module, basek, k_lut, extension_factor);
100 lut.set(&module, lut_fn, message_modulus);
101
102 let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_lut, rank);
103
104 let start: Instant = Instant::now();
105 (0..1).for_each(|_| {
106 cggi_blind_rotate(&module, &mut res, &lwe, &lut, &brk, scratch.borrow());
107 });
108
109 let duration: std::time::Duration = start.elapsed();
110 println!("blind-rotate: {} ms", duration.as_millis());
111
112 let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_lut);
113
114 res.decrypt(&module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
115
116 println!("pt_have: {}", pt_have.data);
117
118 let mut lwe_2n: Vec<i64> = vec![0i64; lwe.n() + 1]; // TODO: from scratch space
119
120 negate_and_mod_switch_2n(2 * lut.domain_size(), &mut lwe_2n, &lwe.to_ref());
121
122 let pt_want: i64 = (lwe_2n[0]
123 + lwe_2n[1..]
124 .iter()
125 .zip(sk_lwe.data.at(0, 0))
126 .map(|(x, y)| x * y)
127 .sum::<i64>())
128 % (2 * lut.domain_size()) as i64;
129
130 println!("pt_want: {}", pt_want);
131
132 lut.rotate(pt_want);
133
134 lut.data.iter().for_each(|d| {
135 println!("{}", d);
136 });
137
138 // First limb should be exactly equal (test are parameterized such that the noise does not reach
139 // the first limb)
140 // assert_eq!(pt_have.data.at_mut(0, 0), lut.data[0].at_mut(0, 0));
141
142 // Then checks the noise
143 module.vec_znx_sub_ab_inplace(&mut lut.data[0], 0, &pt_have.data, 0);
144 let noise: f64 = lut.data[0].std(0, basek);
145 println!("noise: {}", noise);
146}