poulpy/base2k/examples/rlwe_encrypt.rs
base2k/examples/rlwe_encrypt.rsRaw
1use base2k::{
2 Encoding, FFT64, Module, Sampling, Scalar, ScalarZnxDft, ScalarZnxDftOps, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft,
3 VecZnxDftOps, VecZnxOps, ZnxInfos, alloc_aligned,
4};
5use itertools::izip;
6use sampling::source::Source;
7
8fn main() {
9 let n: usize = 16;
10 let log_base2k: usize = 18;
11 let ct_size: usize = 3;
12 let msg_size: usize = 2;
13 let log_scale: usize = msg_size * log_base2k - 5;
14 let module: Module<FFT64> = Module::<FFT64>::new(n);
15
16 let mut carry: Vec<u8> = alloc_aligned(module.vec_znx_big_normalize_tmp_bytes(2));
17
18 let seed: [u8; 32] = [0; 32];
19 let mut source: Source = Source::new(seed);
20
21 // s <- Z_{-1, 0, 1}[X]/(X^{N}+1)
22 let mut s: Scalar = Scalar::new(n);
23 s.fill_ternary_prob(0.5, &mut source);
24
25 // Buffer to store s in the DFT domain
26 let mut s_ppol: ScalarZnxDft<FFT64> = module.new_svp_ppol();
27
28 // s_ppol <- DFT(s)
29 module.svp_prepare(&mut s_ppol, &s);
30
31 // ct = (c0, c1)
32 let mut ct: VecZnx = module.new_vec_znx(2, ct_size);
33
34 // Fill c1 with random values
35 module.fill_uniform(log_base2k, &mut ct, 1, ct_size, &mut source);
36
37 // Scratch space for DFT values
38 let mut buf_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft(1, ct.size());
39
40 // Applies buf_dft <- s * c1
41 module.svp_apply_dft(
42 &mut buf_dft, // DFT(c1 * s)
43 &s_ppol,
44 &ct,
45 1, // c1
46 );
47
48 // Alias scratch space (VecZnxDftis always at least as big as VecZnxBig)
49 let mut buf_big: VecZnxBig<FFT64> = buf_dft.as_vec_znx_big();
50
51 // BIG(c1 * s) <- IDFT(DFT(c1 * s)) (not normalized)
52 module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft);
53
54 // m <- (0)
55 let mut m: VecZnx = module.new_vec_znx(1, msg_size);
56 let mut want: Vec<i64> = vec![0; n];
57 want.iter_mut()
58 .for_each(|x| *x = source.next_u64n(16, 15) as i64);
59 m.encode_vec_i64(0, log_base2k, log_scale, &want, 4);
60 m.normalize(log_base2k, &mut carry);
61
62 // m - BIG(c1 * s)
63 module.vec_znx_big_sub_small_ab_inplace(&mut buf_big, &m);
64
65 // c0 <- m - BIG(c1 * s)
66 module.vec_znx_big_normalize(log_base2k, &mut ct, &buf_big, &mut carry);
67
68 ct.print(ct.sl());
69
70 // (c0 + e, c1)
71 module.add_normal(
72 log_base2k,
73 &mut ct,
74 0, // c0
75 log_base2k * ct_size,
76 &mut source,
77 3.2,
78 19.0,
79 );
80
81 // Decrypt
82
83 // DFT(c1 * s)
84 module.svp_apply_dft(&mut buf_dft, &s_ppol, &ct, 1);
85 // BIG(c1 * s) = IDFT(DFT(c1 * s))
86 module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft);
87
88 // BIG(c1 * s) + c0
89 module.vec_znx_big_add_small_inplace(&mut buf_big, &ct);
90
91 // m + e <- BIG(c1 * s + c0)
92 let mut res: VecZnx = module.new_vec_znx(1, ct_size);
93 module.vec_znx_big_normalize(log_base2k, &mut res, &buf_big, &mut carry);
94
95 // have = m * 2^{log_scale} + e
96 let mut have: Vec<i64> = vec![i64::default(); n];
97 res.decode_vec_i64(0, log_base2k, res.size() * log_base2k, &mut have);
98
99 let scale: f64 = (1 << (res.size() * log_base2k - log_scale)) as f64;
100 izip!(want.iter(), have.iter())
101 .enumerate()
102 .for_each(|(i, (a, b))| {
103 println!("{}: {} {}", i, a, (*b as f64) / scale);
104 })
105}