arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/backend/examples/rlwe_encrypt.rs
backend/examples/rlwe_encrypt.rsRaw
  1use backend::{
  2    AddNormal, Decoding, Encoding, FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxDft, ScalarZnxDftAlloc,
  3    ScalarZnxDftOps, ScratchOwned, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft,
  4    VecZnxDftAlloc, VecZnxDftOps, VecZnxOps, ZnxInfos,
  5};
  6use itertools::izip;
  7use sampling::source::Source;
  8
  9fn main() {
 10    let n: usize = 16;
 11    let basek: usize = 18;
 12    let ct_size: usize = 3;
 13    let msg_size: usize = 2;
 14    let log_scale: usize = msg_size * basek - 5;
 15    let module: Module<FFT64> = Module::<FFT64>::new(n);
 16
 17    let mut scratch: ScratchOwned = ScratchOwned::new(module.vec_znx_big_normalize_tmp_bytes());
 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: ScalarZnx<Vec<u8>> = module.new_scalar_znx(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<Vec<u8>, 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<Vec<u8>> = 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    ct.fill_uniform(basek, 1, ct_size, &mut source);
 40
 41    let mut buf_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_size);
 42
 43    module.vec_znx_dft(1, 0, &mut buf_dft, 0, &ct, 1);
 44
 45    // Applies DFT(ct[1]) * DFT(s)
 46    module.svp_apply_inplace(
 47        &mut buf_dft, // DFT(ct[1] * s)
 48        0,            // Selects the first column of res
 49        &s_dft,       // DFT(s)
 50        0,            // Selects the first column of s_dft
 51    );
 52
 53    // Alias scratch space (VecZnxDft<B> is always at least as big as VecZnxBig<B>)
 54
 55    // BIG(ct[1] * s) <- IDFT(DFT(ct[1] * s)) (not normalized)
 56    let mut buf_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_size);
 57    module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
 58
 59    // Creates a plaintext: VecZnx with 1 column
 60    let mut m = module.new_vec_znx(
 61        1,        // Number of columns
 62        msg_size, // Number of small polynomials
 63    );
 64    let mut want: Vec<i64> = vec![0; n];
 65    want.iter_mut()
 66        .for_each(|x| *x = source.next_u64n(16, 15) as i64);
 67    m.encode_vec_i64(0, basek, log_scale, &want, 4);
 68    module.vec_znx_normalize_inplace(basek, &mut m, 0, scratch.borrow());
 69
 70    // m - BIG(ct[1] * s)
 71    module.vec_znx_big_sub_small_b_inplace(
 72        &mut buf_big,
 73        0, // Selects the first column of the receiver
 74        &m,
 75        0, // Selects the first column of the message
 76    );
 77
 78    // Normalizes back to VecZnx
 79    // ct[0] <- m - BIG(c1 * s)
 80    module.vec_znx_big_normalize(
 81        basek,
 82        &mut ct,
 83        0, // Selects the first column of ct (ct[0])
 84        &buf_big,
 85        0, // Selects the first column of buf_big
 86        scratch.borrow(),
 87    );
 88
 89    // Add noise to ct[0]
 90    // ct[0] <- ct[0] + e
 91    ct.add_normal(
 92        basek,
 93        0,               // Selects the first column of ct (ct[0])
 94        basek * ct_size, // Scaling of the noise: 2^{-basek * limbs}
 95        &mut source,
 96        3.2,  // Standard deviation
 97        19.0, // Truncatation bound
 98    );
 99
100    // Final ciphertext: ct = (-a * s + m + e, a)
101
102    // Decryption
103
104    // DFT(ct[1] * s)
105    module.vec_znx_dft(1, 0, &mut buf_dft, 0, &ct, 1);
106    module.svp_apply_inplace(
107        &mut buf_dft,
108        0, // Selects the first column of res.
109        &s_dft,
110        0,
111    );
112
113    // BIG(c1 * s) = IDFT(DFT(c1 * s))
114    module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
115
116    // BIG(c1 * s) + ct[0]
117    module.vec_znx_big_add_small_inplace(&mut buf_big, 0, &ct, 0);
118
119    // m + e <- BIG(ct[1] * s + ct[0])
120    let mut res = module.new_vec_znx(1, ct_size);
121    module.vec_znx_big_normalize(basek, &mut res, 0, &buf_big, 0, scratch.borrow());
122
123    // have = m * 2^{log_scale} + e
124    let mut have: Vec<i64> = vec![i64::default(); n];
125    res.decode_vec_i64(0, basek, res.size() * basek, &mut have);
126
127    let scale: f64 = (1 << (res.size() * basek - log_scale)) as f64;
128    izip!(want.iter(), have.iter())
129        .enumerate()
130        .for_each(|(i, (a, b))| {
131            println!("{}: {} {}", i, a, (*b as f64) / scale);
132        });
133}