arnaucube
arnaucube/poulpyPublic Self-hosted repository
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());
 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_dft: ScalarZnxDft<FFT64> = module.new_scalar_znx_dft();
 27
 28    // s_dft <- DFT(s)
 29    module.svp_prepare(&mut s_dft, &s);
 30
 31    // Allocates a VecZnx with two columns: ct=(0, 0)
 32    let mut ct: VecZnx = module.new_vec_znx(
 33        2,       // Number of columns
 34        ct_size, // Number of small poly per column
 35    );
 36
 37    // Fill the second column with random values: ct = (0, a)
 38    module.fill_uniform(log_base2k, &mut ct, 1, ct_size, &mut source);
 39
 40    // Scratch space for DFT values
 41    let mut buf_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft(
 42        1,         // Number of columns
 43        ct.size(), // Number of polynomials per column
 44    );
 45
 46    // Applies DFT(ct[1]) * DFT(s)
 47    module.svp_apply_dft(
 48        &mut buf_dft, // DFT(ct[1] * s)
 49        0,            // Selects the first column of res
 50        &s_dft,       // DFT(s)
 51        &ct,
 52        1, // Selects the second column of ct
 53    );
 54
 55    // Alias scratch space (VecZnxDft<B> is always at least as big as VecZnxBig<B>)
 56    let mut buf_big: VecZnxBig<FFT64> = buf_dft.alias_as_vec_znx_big();
 57
 58    // BIG(ct[1] * s) <- IDFT(DFT(ct[1] * s)) (not normalized)
 59    module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
 60
 61    // Creates a plaintext: VecZnx with 1 column
 62    let mut m: VecZnx = module.new_vec_znx(
 63        1,        // Number of columns
 64        msg_size, // Number of small polynomials
 65    );
 66    let mut want: Vec<i64> = vec![0; n];
 67    want.iter_mut()
 68        .for_each(|x| *x = source.next_u64n(16, 15) as i64);
 69    m.encode_vec_i64(0, log_base2k, log_scale, &want, 4);
 70    m.normalize(log_base2k, &mut carry);
 71
 72    // m - BIG(ct[1] * s)
 73    module.vec_znx_big_sub_small_a_inplace(
 74        &mut buf_big,
 75        0, // Selects the first column of the receiver
 76        &m,
 77        0, // Selects the first column of the message
 78    );
 79
 80    // Normalizes back to VecZnx
 81    // ct[0] <- m - BIG(c1 * s)
 82    module.vec_znx_big_normalize(
 83        log_base2k, &mut ct, 0, // Selects the first column of ct (ct[0])
 84        &buf_big, 0, // Selects the first column of buf_big
 85        &mut carry,
 86    );
 87
 88    // Add noise to ct[0]
 89    // ct[0] <- ct[0] + e
 90    module.add_normal(
 91        log_base2k,
 92        &mut ct,
 93        0,                    // Selects the first column of ct (ct[0])
 94        log_base2k * ct_size, // Scaling of the noise: 2^{-log_base2k * 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.svp_apply_dft(
106        &mut buf_dft,
107        0, // Selects the first column of res.
108        &s_dft,
109        &ct,
110        1, // Selects the second column of ct (ct[1])
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: VecZnx = module.new_vec_znx(1, ct_size);
121    module.vec_znx_big_normalize(log_base2k, &mut res, 0, &buf_big, 0, &mut carry);
122
123    // have = m * 2^{log_scale} + e
124    let mut have: Vec<i64> = vec![i64::default(); n];
125    res.decode_vec_i64(0, log_base2k, res.size() * log_base2k, &mut have);
126
127    let scale: f64 = (1 << (res.size() * log_base2k - 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}