mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
working rlwe encryption example with interleaved polynomial
This commit is contained in:
@@ -13,7 +13,7 @@ fn main() {
|
||||
let log_scale: usize = msg_size * log_base2k - 5;
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(n);
|
||||
|
||||
let mut carry: Vec<u8> = alloc_aligned(module.vec_znx_big_normalize_tmp_bytes(2));
|
||||
let mut carry: Vec<u8> = alloc_aligned(module.vec_znx_big_normalize_tmp_bytes());
|
||||
|
||||
let seed: [u8; 32] = [0; 32];
|
||||
let mut source: Source = Source::new(seed);
|
||||
@@ -28,69 +28,95 @@ fn main() {
|
||||
// s_ppol <- DFT(s)
|
||||
module.svp_prepare(&mut s_ppol, &s);
|
||||
|
||||
// ct = (c0, c1)
|
||||
let mut ct: VecZnx = module.new_vec_znx(2, ct_size);
|
||||
// Allocates a VecZnx with two columns: ct=(0, 0)
|
||||
let mut ct: VecZnx = module.new_vec_znx(
|
||||
2, // Number of columns
|
||||
ct_size, // Number of small poly per column
|
||||
);
|
||||
|
||||
// Fill c1 with random values
|
||||
// Fill the second column with random values: ct = (0, a)
|
||||
module.fill_uniform(log_base2k, &mut ct, 1, ct_size, &mut source);
|
||||
|
||||
// Scratch space for DFT values
|
||||
let mut buf_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft(1, ct.size());
|
||||
|
||||
// Applies buf_dft <- s * c1
|
||||
module.svp_apply_dft(
|
||||
&mut buf_dft, // DFT(c1 * s)
|
||||
&s_ppol,
|
||||
&ct,
|
||||
1, // c1
|
||||
let mut buf_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft(
|
||||
1, // Number of columns
|
||||
ct.size(), // Number of polynomials per column
|
||||
);
|
||||
|
||||
// Alias scratch space (VecZnxDftis always at least as big as VecZnxBig)
|
||||
// Applies DFT(ct[1]) * DFT(s)
|
||||
module.svp_apply_dft(
|
||||
&mut buf_dft, // DFT(ct[1] * s)
|
||||
&s_ppol, // DFT(s)
|
||||
&ct,
|
||||
1, // Selects the second column of ct
|
||||
);
|
||||
|
||||
// Alias scratch space (VecZnxDft<B> is always at least as big as VecZnxBig<B>)
|
||||
let mut buf_big: VecZnxBig<FFT64> = buf_dft.as_vec_znx_big();
|
||||
|
||||
// BIG(c1 * s) <- IDFT(DFT(c1 * s)) (not normalized)
|
||||
// BIG(ct[1] * s) <- IDFT(DFT(ct[1] * s)) (not normalized)
|
||||
module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft);
|
||||
|
||||
// m <- (0)
|
||||
let mut m: VecZnx = module.new_vec_znx(1, msg_size);
|
||||
// Creates a plaintext: VecZnx with 1 column
|
||||
let mut m: VecZnx = module.new_vec_znx(
|
||||
1, // Number of columns
|
||||
msg_size, // Number of small polynomials
|
||||
);
|
||||
let mut want: Vec<i64> = vec![0; n];
|
||||
want.iter_mut()
|
||||
.for_each(|x| *x = source.next_u64n(16, 15) as i64);
|
||||
m.encode_vec_i64(0, log_base2k, log_scale, &want, 4);
|
||||
m.normalize(log_base2k, &mut carry);
|
||||
|
||||
// m - BIG(c1 * s)
|
||||
module.vec_znx_big_sub_small_ab_inplace(&mut buf_big, &m);
|
||||
// m - BIG(ct[1] * s)
|
||||
module.vec_znx_big_sub_small_a_inplace(
|
||||
&mut buf_big,
|
||||
0, // Selects the first column of the receiver
|
||||
&m,
|
||||
0, // Selects the first column of the message
|
||||
);
|
||||
|
||||
// c0 <- m - BIG(c1 * s)
|
||||
module.vec_znx_big_normalize(log_base2k, &mut ct, &buf_big, &mut carry);
|
||||
// Normalizes back to VecZnx
|
||||
// ct[0] <- m - BIG(c1 * s)
|
||||
module.vec_znx_big_normalize(
|
||||
log_base2k, &mut ct, 0, // Selects the first column of ct (ct[0])
|
||||
&buf_big, 0, // Selects the first column of buf_big
|
||||
&mut carry,
|
||||
);
|
||||
|
||||
ct.print(ct.sl());
|
||||
|
||||
// (c0 + e, c1)
|
||||
// Add noise to ct[0]
|
||||
// ct[0] <- ct[0] + e
|
||||
module.add_normal(
|
||||
log_base2k,
|
||||
&mut ct,
|
||||
0, // c0
|
||||
log_base2k * ct_size,
|
||||
0, // Selects the first column of ct (ct[0])
|
||||
log_base2k * ct_size, // Scaling of the noise: 2^{-log_base2k * limbs}
|
||||
&mut source,
|
||||
3.2,
|
||||
19.0,
|
||||
3.2, // Standard deviation
|
||||
19.0, // Truncatation bound
|
||||
);
|
||||
|
||||
// Decrypt
|
||||
// Final ciphertext: ct = (-a * s + m + e, a)
|
||||
|
||||
// Decryption
|
||||
|
||||
// DFT(ct[1] * s)
|
||||
module.svp_apply_dft(
|
||||
&mut buf_dft,
|
||||
&s_ppol,
|
||||
&ct,
|
||||
1, // Selects the second column of ct (ct[1])
|
||||
);
|
||||
|
||||
// DFT(c1 * s)
|
||||
module.svp_apply_dft(&mut buf_dft, &s_ppol, &ct, 1);
|
||||
// BIG(c1 * s) = IDFT(DFT(c1 * s))
|
||||
module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft);
|
||||
|
||||
// BIG(c1 * s) + c0
|
||||
module.vec_znx_big_add_small_inplace(&mut buf_big, &ct);
|
||||
// BIG(c1 * s) + ct[0]
|
||||
module.vec_znx_big_add_small_inplace(&mut buf_big, 0, &ct, 0);
|
||||
|
||||
// m + e <- BIG(c1 * s + c0)
|
||||
// m + e <- BIG(ct[1] * s + ct[0])
|
||||
let mut res: VecZnx = module.new_vec_znx(1, ct_size);
|
||||
module.vec_znx_big_normalize(log_base2k, &mut res, &buf_big, &mut carry);
|
||||
module.vec_znx_big_normalize(log_base2k, &mut res, 0, &buf_big, 0, &mut carry);
|
||||
|
||||
// have = m * 2^{log_scale} + e
|
||||
let mut have: Vec<i64> = vec![i64::default(); n];
|
||||
|
||||
Reference in New Issue
Block a user