poulpy/base2k/examples/rlwe_encrypt.rs
base2k/examples/rlwe_encrypt.rsRaw
1use base2k::{
2 Encoding, FFT64, Module, Sampling, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftOps, VecZnx, VecZnxBig, VecZnxBigOps,
3 VecZnxDft, 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 tmp_bytes_norm: Vec<u8> = alloc_aligned(module.vec_znx_big_normalize_tmp_bytes());
17 let mut tmp_bytes_dft = alloc_aligned(module.bytes_of_vec_znx_dft(1, ct_size));
18
19 let seed: [u8; 32] = [0; 32];
20 let mut source: Source = Source::new(seed);
21
22 // s <- Z_{-1, 0, 1}[X]/(X^{N}+1)
23 let mut s: Scalar = module.new_scalar(1);
24 s.fill_ternary_prob(0, 0.5, &mut source);
25
26 // Buffer to store s in the DFT domain
27 let mut s_dft: ScalarZnxDft<FFT64> = module.new_scalar_znx_dft(s.cols());
28
29 // s_dft <- DFT(s)
30 module.svp_prepare(&mut s_dft, 0, &s, 0);
31
32 // Allocates a VecZnx with two columns: ct=(0, 0)
33 let mut ct: VecZnx = module.new_vec_znx(
34 2, // Number of columns
35 ct_size, // Number of small poly per column
36 );
37
38 // Fill the second column with random values: ct = (0, a)
39 module.fill_uniform(log_base2k, &mut ct, 1, ct_size, &mut source);
40
41 // Scratch space for DFT values
42 let mut buf_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft_from_bytes_borrow(
43 1, // Number of columns
44 ct.size(), // Number of polynomials per column
45 &mut tmp_bytes_dft,
46 );
47
48 // Applies DFT(ct[1]) * DFT(s)
49 module.svp_apply_dft(
50 &mut buf_dft, // DFT(ct[1] * s)
51 0, // Selects the first column of res
52 &s_dft, // DFT(s)
53 0, // Selects the first column of s_dft
54 &ct,
55 1, // Selects the second column of ct
56 );
57
58 // Alias scratch space (VecZnxDft<B> is always at least as big as VecZnxBig<B>)
59 let mut buf_big: VecZnxBig<FFT64> = buf_dft.alias_as_vec_znx_big();
60
61 // BIG(ct[1] * s) <- IDFT(DFT(ct[1] * s)) (not normalized)
62 module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
63
64 // Creates a plaintext: VecZnx with 1 column
65 let mut m: VecZnx = module.new_vec_znx(
66 1, // Number of columns
67 msg_size, // Number of small polynomials
68 );
69 let mut want: Vec<i64> = vec![0; n];
70 want.iter_mut()
71 .for_each(|x| *x = source.next_u64n(16, 15) as i64);
72 m.encode_vec_i64(0, log_base2k, log_scale, &want, 4);
73 m.normalize(log_base2k, 0, &mut tmp_bytes_norm);
74
75 // m - BIG(ct[1] * s)
76 module.vec_znx_big_sub_small_a_inplace(
77 &mut buf_big,
78 0, // Selects the first column of the receiver
79 &m,
80 0, // Selects the first column of the message
81 );
82
83 // Normalizes back to VecZnx
84 // ct[0] <- m - BIG(c1 * s)
85 module.vec_znx_big_normalize(
86 log_base2k,
87 &mut ct,
88 0, // Selects the first column of ct (ct[0])
89 &buf_big,
90 0, // Selects the first column of buf_big
91 &mut tmp_bytes_norm,
92 );
93
94 // Add noise to ct[0]
95 // ct[0] <- ct[0] + e
96 module.add_normal(
97 log_base2k,
98 &mut ct,
99 0, // Selects the first column of ct (ct[0])
100 log_base2k * ct_size, // Scaling of the noise: 2^{-log_base2k * limbs}
101 &mut source,
102 3.2, // Standard deviation
103 19.0, // Truncatation bound
104 );
105
106 // Final ciphertext: ct = (-a * s + m + e, a)
107
108 // Decryption
109
110 // DFT(ct[1] * s)
111 module.svp_apply_dft(
112 &mut buf_dft,
113 0, // Selects the first column of res.
114 &s_dft,
115 0,
116 &ct,
117 1, // Selects the second column of ct (ct[1])
118 );
119
120 // BIG(c1 * s) = IDFT(DFT(c1 * s))
121 module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
122
123 // BIG(c1 * s) + ct[0]
124 module.vec_znx_big_add_small_inplace(&mut buf_big, 0, &ct, 0);
125
126 // m + e <- BIG(ct[1] * s + ct[0])
127 let mut res: VecZnx = module.new_vec_znx(1, ct_size);
128 module.vec_znx_big_normalize(log_base2k, &mut res, 0, &buf_big, 0, &mut tmp_bytes_norm);
129
130 // have = m * 2^{log_scale} + e
131 let mut have: Vec<i64> = vec![i64::default(); n];
132 res.decode_vec_i64(0, log_base2k, res.size() * log_base2k, &mut have);
133
134 let scale: f64 = (1 << (res.size() * log_base2k - log_scale)) as f64;
135 izip!(want.iter(), have.iter())
136 .enumerate()
137 .for_each(|(i, (a, b))| {
138 println!("{}: {} {}", i, a, (*b as f64) / scale);
139 })
140}