poulpy/poulpy-cpu-avx/examples/rlwe_encrypt.rs
poulpy-cpu-avx/examples/rlwe_encrypt.rsRaw
1use itertools::izip;
2
3#[cfg(all(feature = "enable-avx", target_arch = "x86_64", target_feature = "avx2", target_feature = "fma"))]
4use poulpy_cpu_avx::FFT64Avx as BackendImpl;
5#[cfg(not(all(feature = "enable-avx", target_arch = "x86_64", target_feature = "avx2", target_feature = "fma")))]
6use poulpy_cpu_ref::FFT64Ref as BackendImpl;
7
8use poulpy_hal::{
9 api::{
10 ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPrepare, VecZnxAddNormal,
11 VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace,
12 VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxNormalizeInplace,
13 },
14 layouts::{Module, ScalarZnx, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, ZnxInfos},
15 source::Source,
16};
17
18fn main() {
19 let n: usize = 16;
20 let base2k: usize = 18;
21 let ct_size: usize = 3;
22 let msg_size: usize = 2;
23 let log_scale: usize = msg_size * base2k - 5;
24 let module: Module<BackendImpl> = Module::<BackendImpl>::new(n as u64);
25
26 let mut scratch: ScratchOwned<BackendImpl> = ScratchOwned::<BackendImpl>::alloc(module.vec_znx_big_normalize_tmp_bytes());
27
28 let seed: [u8; 32] = [0; 32];
29 let mut source: Source = Source::new(seed);
30
31 // s <- Z_{-1, 0, 1}[X]/(X^{N}+1)
32 let mut s: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), 1);
33 s.fill_ternary_prob(0, 0.5, &mut source);
34
35 // Buffer to store s in the DFT domain
36 let mut s_dft: SvpPPol<Vec<u8>, BackendImpl> = module.svp_ppol_alloc(s.cols());
37
38 // s_dft <- DFT(s)
39 module.svp_prepare(&mut s_dft, 0, &s, 0);
40
41 // Allocates a VecZnx with two columns: ct=(0, 0)
42 let mut ct: VecZnx<Vec<u8>> = VecZnx::alloc(
43 module.n(),
44 2, // Number of columns
45 ct_size, // Number of small poly per column
46 );
47
48 // Fill the second column with random values: ct = (0, a)
49 module.vec_znx_fill_uniform(base2k, &mut ct, 1, &mut source);
50
51 let mut buf_dft: VecZnxDft<Vec<u8>, BackendImpl> = module.vec_znx_dft_alloc(1, ct_size);
52
53 module.vec_znx_dft_apply(1, 0, &mut buf_dft, 0, &ct, 1);
54
55 // Applies DFT(ct[1]) * DFT(s)
56 module.svp_apply_dft_to_dft_inplace(
57 &mut buf_dft, // DFT(ct[1] * s)
58 0, // Selects the first column of res
59 &s_dft, // DFT(s)
60 0, // Selects the first column of s_dft
61 );
62
63 // Alias scratch space (VecZnxDft<B> is always at least as big as VecZnxBig<B>)
64
65 // BIG(ct[1] * s) <- IDFT(DFT(ct[1] * s)) (not normalized)
66 let mut buf_big: VecZnxBig<Vec<u8>, BackendImpl> = module.vec_znx_big_alloc(1, ct_size);
67 module.vec_znx_idft_apply_tmpa(&mut buf_big, 0, &mut buf_dft, 0);
68
69 // Creates a plaintext: VecZnx with 1 column
70 let mut m = VecZnx::alloc(
71 module.n(),
72 1, // Number of columns
73 msg_size, // Number of small polynomials
74 );
75 let mut want: Vec<i64> = vec![0; n];
76 want.iter_mut()
77 .for_each(|x| *x = source.next_u64n(16, 15) as i64);
78 m.encode_vec_i64(base2k, 0, log_scale, &want);
79 module.vec_znx_normalize_inplace(base2k, &mut m, 0, scratch.borrow());
80
81 // m - BIG(ct[1] * s)
82 module.vec_znx_big_sub_small_negate_inplace(
83 &mut buf_big,
84 0, // Selects the first column of the receiver
85 &m,
86 0, // Selects the first column of the message
87 );
88
89 // Normalizes back to VecZnx
90 // ct[0] <- m - BIG(c1 * s)
91 module.vec_znx_big_normalize(
92 base2k,
93 &mut ct,
94 0, // Selects the first column of ct (ct[0])
95 base2k,
96 &buf_big,
97 0, // Selects the first column of buf_big
98 scratch.borrow(),
99 );
100
101 // Add noise to ct[0]
102 // ct[0] <- ct[0] + e
103 module.vec_znx_add_normal(
104 base2k,
105 &mut ct,
106 0, // Selects the first column of ct (ct[0])
107 base2k * ct_size, // Scaling of the noise: 2^{-base2k * limbs}
108 &mut source,
109 3.2, // Standard deviation
110 3.2 * 6.0, // Truncatation bound
111 );
112
113 // Final ciphertext: ct = (-a * s + m + e, a)
114
115 // Decryption
116
117 // DFT(ct[1] * s)
118 module.vec_znx_dft_apply(1, 0, &mut buf_dft, 0, &ct, 1);
119 module.svp_apply_dft_to_dft_inplace(
120 &mut buf_dft,
121 0, // Selects the first column of res.
122 &s_dft,
123 0,
124 );
125
126 // BIG(c1 * s) = IDFT(DFT(c1 * s))
127 module.vec_znx_idft_apply_tmpa(&mut buf_big, 0, &mut buf_dft, 0);
128
129 // BIG(c1 * s) + ct[0]
130 module.vec_znx_big_add_small_inplace(&mut buf_big, 0, &ct, 0);
131
132 // m + e <- BIG(ct[1] * s + ct[0])
133 let mut res = VecZnx::alloc(module.n(), 1, ct_size);
134 module.vec_znx_big_normalize(base2k, &mut res, 0, base2k, &buf_big, 0, scratch.borrow());
135
136 // have = m * 2^{log_scale} + e
137 let mut have: Vec<i64> = vec![i64::default(); n];
138 res.decode_vec_i64(base2k, 0, ct_size * base2k, &mut have);
139 let scale: f64 = (1 << (res.size() * base2k - log_scale)) as f64;
140 izip!(want.iter(), have.iter())
141 .enumerate()
142 .for_each(|(i, (a, b))| {
143 println!("{}: {} {}", i, a, (*b as f64) / scale);
144 });
145}