rework as discussed

This commit is contained in:
Jean-Philippe Bossuat
2025-05-05 17:35:35 +02:00
parent bd105497fd
commit ffa363804b
16 changed files with 1154 additions and 1153 deletions

View File

@@ -1,6 +1,7 @@
use base2k::{
Encoding, FFT64, Module, Sampling, ScalarAlloc, ScalarZnxDftAlloc, ScalarZnxDftOps, ScratchOwned, VecZnxAlloc, VecZnxBigOps,
VecZnxBigScratch, VecZnxDftAlloc, VecZnxDftOps, ZnxInfos,
Encoding, FFT64, Module, Sampling, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScratchOwned,
VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps,
VecZnxOps, ZnxInfos,
};
use itertools::izip;
use sampling::source::Source;
@@ -13,24 +14,23 @@ fn main() {
let log_scale: usize = msg_size * log_base2k - 5;
let module: Module<FFT64> = Module::<FFT64>::new(n);
let mut scratch =
ScratchOwned::new((2 * module.bytes_of_vec_znx_dft(1, ct_size)) + 2 * module.vec_znx_big_normalize_tmp_bytes());
let mut scratch: ScratchOwned = ScratchOwned::new(module.vec_znx_big_normalize_tmp_bytes());
let seed: [u8; 32] = [0; 32];
let mut source: Source = Source::new(seed);
// s <- Z_{-1, 0, 1}[X]/(X^{N}+1)
let mut s = module.new_scalar(1);
let mut s: Scalar<Vec<u8>> = module.new_scalar(1);
s.fill_ternary_prob(0, 0.5, &mut source);
// Buffer to store s in the DFT domain
let mut s_dft = module.new_scalar_znx_dft(s.cols());
let mut s_dft: ScalarZnxDft<Vec<u8>, FFT64> = module.new_scalar_znx_dft(s.cols());
// s_dft <- DFT(s)
module.svp_prepare(&mut s_dft, 0, &s, 0);
// Allocates a VecZnx with two columns: ct=(0, 0)
let mut ct = module.new_vec_znx(
let mut ct: VecZnx<Vec<u8>> = module.new_vec_znx(
2, // Number of columns
ct_size, // Number of small poly per column
);
@@ -38,12 +38,10 @@ fn main() {
// 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 scratch = scratch.borrow();
let (mut buf_dft, scratch) = scratch.tmp_vec_znx_dft(&module, 1, ct_size);
let mut buf_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_size);
// Applies DFT(ct[1]) * DFT(s)
module.svp_apply_dft(
module.svp_apply(
&mut buf_dft, // DFT(ct[1] * s)
0, // Selects the first column of res
&s_dft, // DFT(s)
@@ -53,11 +51,10 @@ fn main() {
);
// Alias scratch space (VecZnxDft<B> is always at least as big as VecZnxBig<B>)
let (mut buf_big, scratch) = scratch.tmp_vec_znx_big(&module, 1, ct_size);
// BIG(ct[1] * s) <- IDFT(DFT(ct[1] * s)) (not normalized)
// Note: Since `vec_znx_idft_tmp_a` takes no argument for generic `Data` a full qualified path seems necessary
<Module<_> as VecZnxDftOps<_, &[u8], _>>::vec_znx_idft_tmp_a(&module, &mut buf_big, 0, &mut buf_dft, 0);
let mut buf_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_size);
module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
// Creates a plaintext: VecZnx with 1 column
let mut m = module.new_vec_znx(
@@ -68,8 +65,7 @@ fn main() {
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);
let (tmp_bytes_norm, scratch) = scratch.tmp_scalar_slice(n * std::mem::size_of::<i64>());
m.normalize(log_base2k, 0, tmp_bytes_norm);
module.vec_znx_normalize_inplace(log_base2k, &mut m, 0, scratch.borrow());
// m - BIG(ct[1] * s)
module.vec_znx_big_sub_small_b_inplace(
@@ -82,9 +78,12 @@ fn main() {
// 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
scratch,
log_base2k,
&mut ct,
0, // Selects the first column of ct (ct[0])
&buf_big,
0, // Selects the first column of buf_big
scratch.borrow(),
);
// Add noise to ct[0]
@@ -104,7 +103,7 @@ fn main() {
// Decryption
// DFT(ct[1] * s)
module.svp_apply_dft(
module.svp_apply(
&mut buf_dft,
0, // Selects the first column of res.
&s_dft,
@@ -114,14 +113,14 @@ fn main() {
);
// BIG(c1 * s) = IDFT(DFT(c1 * s))
<Module<_> as VecZnxDftOps<_, &[u8], _>>::vec_znx_idft_tmp_a(&module, &mut buf_big, 0, &mut buf_dft, 0);
module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
// BIG(c1 * s) + ct[0]
module.vec_znx_big_add_small_inplace(&mut buf_big, 0, &ct, 0);
// m + e <- BIG(ct[1] * s + ct[0])
let mut res = module.new_vec_znx(1, ct_size);
module.vec_znx_big_normalize(log_base2k, &mut res, 0, &buf_big, 0, scratch);
module.vec_znx_big_normalize(log_base2k, &mut res, 0, &buf_big, 0, scratch.borrow());
// have = m * 2^{log_scale} + e
let mut have: Vec<i64> = vec![i64::default(); n];

View File

@@ -1,78 +0,0 @@
// use base2k::{
// Encoding, FFT64, MatZnxDft, MatZnxDftOps, Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxOps,
// ZnxInfos, ZnxLayout, alloc_aligned,
// };
fn main() {
// let log_n: i32 = 5;
// let n: usize = 1 << log_n;
// let module: Module<FFT64> = Module::<FFT64>::new(n);
// let log_base2k: usize = 15;
// let a_cols: usize = 2;
// let a_size: usize = 5;
// let log_k: usize = log_base2k * a_size - 5;
// let mat_rows: usize = a_size;
// let mat_cols_in: usize = a_cols;
// let mat_cols_out: usize = 2;
// let mat_size: usize = a_size + 1;
// let mut tmp_bytes_vmp: Vec<u8> = alloc_aligned(
// module.vmp_prepare_row_tmp_bytes(mat_cols_out, mat_size)
// | module.vmp_apply_dft_tmp_bytes(
// a_size,
// a_size,
// mat_rows,
// mat_cols_in,
// mat_cols_out,
// mat_size,
// ),
// );
// let mut tmp_bytes_dft: Vec<u8> = alloc_aligned(module.bytes_of_vec_znx_dft(mat_cols_out, mat_size));
// let mut a: VecZnx = module.new_vec_znx(a_cols, a_size);
// (0..a_cols).for_each(|i| {
// let mut values: Vec<i64> = vec![i64::default(); n];
// values[1 + i] = (1 << log_base2k) + 1;
// a.encode_vec_i64(i, log_base2k, log_k, &values, 32);
// a.normalize(log_base2k, i, &mut tmp_bytes_vmp);
// a.print(n, i);
// println!();
// });
// let mut mat_znx_dft: MatZnxDft<FFT64> = module.new_mat_znx_dft(mat_rows, mat_cols_in, mat_cols_out, mat_size);
// (0..a.size()).for_each(|row_i| {
// let mut tmp: VecZnx = module.new_vec_znx(mat_cols_out, mat_size);
// (0..mat_cols_out).for_each(|j| {
// tmp.at_mut(j, row_i)[1 + j] = 1 as i64;
// });
// (0..mat_cols_in).for_each(|j| {
// module.vmp_prepare_row(&mut mat_znx_dft, row_i, j, &tmp, &mut tmp_bytes_vmp);
// })
// });
// let mut c_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft_from_bytes_borrow(mat_cols_out, mat_size, &mut tmp_bytes_dft);
// module.vmp_apply_dft(&mut c_dft, &a, &mat_znx_dft, &mut tmp_bytes_vmp);
// let mut res: VecZnx = module.new_vec_znx(mat_cols_out, a_size);
// let mut c_big: VecZnxBig<FFT64> = c_dft.alias_as_vec_znx_big();
// (0..mat_cols_out).for_each(|i| {
// module.vec_znx_idft_tmp_a(&mut c_big, i, &mut c_dft, i);
// module.vec_znx_big_normalize(log_base2k, &mut res, i, &c_big, i, &mut tmp_bytes_vmp);
// let mut values_res: Vec<i64> = vec![i64::default(); n];
// res.decode_vec_i64(i, log_base2k, log_k, &mut values_res);
// res.print(n, i);
// println!();
// println!("{:?}", values_res);
// println!();
// });
// module.free();
}