Updated vec_znx to stacked memory layout

This commit is contained in:
Jean-Philippe Bossuat
2025-04-24 19:05:26 +02:00
parent 04d74e589b
commit 83a7617f4b
8 changed files with 399 additions and 232 deletions

View File

@@ -18,7 +18,7 @@ fn main() {
let seed: [u8; 32] = [0; 32];
let mut source: Source = Source::new(seed);
let mut res: VecZnx = module.new_vec_znx(cols);
let mut res: VecZnx = module.new_vec_znx(1, cols);
// s <- Z_{-1, 0, 1}[X]/(X^{N}+1)
let mut s: Scalar = Scalar::new(n);
@@ -31,7 +31,7 @@ fn main() {
module.svp_prepare(&mut s_ppol, &s);
// a <- Z_{2^prec}[X]/(X^{N}+1)
let mut a: VecZnx = module.new_vec_znx(cols);
let mut a: VecZnx = module.new_vec_znx(1, cols);
module.fill_uniform(log_base2k, &mut a, cols, &mut source);
// Scratch space for DFT values
@@ -46,21 +46,21 @@ fn main() {
// buf_big <- IDFT(buf_dft) (not normalized)
module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft);
let mut m: VecZnx = module.new_vec_znx(msg_cols);
let mut m: VecZnx = module.new_vec_znx(1, msg_cols);
let mut want: Vec<i64> = vec![0; n];
want.iter_mut()
.for_each(|x| *x = source.next_u64n(16, 15) as i64);
// m
m.encode_vec_i64(log_base2k, log_scale, &want, 4);
m.encode_vec_i64(0, log_base2k, log_scale, &want, 4);
m.normalize(log_base2k, &mut carry);
// buf_big <- m - buf_big
module.vec_znx_big_sub_small_a_inplace(&mut buf_big, &m);
// b <- normalize(buf_big) + e
let mut b: VecZnx = module.new_vec_znx(cols);
let mut b: VecZnx = module.new_vec_znx(1, cols);
module.vec_znx_big_normalize(log_base2k, &mut b, &buf_big, &mut carry);
module.add_normal(
log_base2k,
@@ -85,7 +85,7 @@ fn main() {
// have = m * 2^{log_scale} + e
let mut have: Vec<i64> = vec![i64::default(); n];
res.decode_vec_i64(log_base2k, res.cols() * log_base2k, &mut have);
res.decode_vec_i64(0, log_base2k, res.cols() * log_base2k, &mut have);
let scale: f64 = (1 << (res.cols() * log_base2k - log_scale)) as f64;
izip!(want.iter(), have.iter())

View File

@@ -1,6 +1,6 @@
use base2k::{
BACKEND, Encoding, Infos, Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxOps, VecZnxVec, VmpPMat,
VmpPMatOps, alloc_aligned,
BACKEND, Encoding, Infos, Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxOps, VmpPMat, VmpPMatOps,
alloc_aligned,
};
fn main() {
@@ -23,26 +23,20 @@ fn main() {
let mut a_values: Vec<i64> = vec![i64::default(); n];
a_values[1] = (1 << log_base2k) + 1;
let mut a: VecZnx = module.new_vec_znx(cols);
a.encode_vec_i64(log_base2k, log_k, &a_values, 32);
let mut a: VecZnx = module.new_vec_znx(1, rows);
a.encode_vec_i64(0, log_base2k, log_k, &a_values, 32);
a.normalize(log_base2k, &mut buf);
a.print(a.cols(), n);
a.print(0, a.cols(), n);
println!();
let mut vecznx: Vec<VecZnx> = Vec::new();
(0..rows).for_each(|_| {
vecznx.push(module.new_vec_znx(cols));
});
(0..rows).for_each(|i| {
vecznx[i].raw_mut()[i * n + 1] = 1 as i64;
});
let slices: Vec<&[i64]> = vecznx.dblptr();
let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols);
module.vmp_prepare_dblptr(&mut vmp_pmat, &slices, &mut buf);
(0..a.cols()).for_each(|row_i| {
let mut tmp: VecZnx = module.new_vec_znx(1, cols);
tmp.at_mut(row_i)[1] = 1 as i64;
module.vmp_prepare_row(&mut vmp_pmat, tmp.raw(), row_i, &mut buf);
});
let mut c_dft: VecZnxDft = module.new_vec_znx_dft(cols);
module.vmp_apply_dft(&mut c_dft, &a, &vmp_pmat, &mut buf);
@@ -50,13 +44,13 @@ fn main() {
let mut c_big: VecZnxBig = c_dft.as_vec_znx_big();
module.vec_znx_idft_tmp_a(&mut c_big, &mut c_dft);
let mut res: VecZnx = module.new_vec_znx(cols);
let mut res: VecZnx = module.new_vec_znx(1, rows);
module.vec_znx_big_normalize(log_base2k, &mut res, &c_big, &mut buf);
let mut values_res: Vec<i64> = vec![i64::default(); n];
res.decode_vec_i64(log_base2k, log_k, &mut values_res);
res.decode_vec_i64(0, log_base2k, log_k, &mut values_res);
res.print(res.cols(), n);
res.print(0, res.cols(), n);
module.free();