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