mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
wip
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use base2k::{
|
||||
BACKEND, Encoding, Infos, Module, Sampling, Scalar, SvpPPol, SvpPPolOps, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft,
|
||||
Encoding, FFT64, Infos, Module, Sampling, Scalar, SvpPPol, SvpPPolOps, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft,
|
||||
VecZnxDftOps, VecZnxOps, alloc_aligned,
|
||||
};
|
||||
use itertools::izip;
|
||||
@@ -8,44 +8,48 @@ use sampling::source::Source;
|
||||
fn main() {
|
||||
let n: usize = 16;
|
||||
let log_base2k: usize = 18;
|
||||
let cols: usize = 3;
|
||||
let limbs: usize = 3;
|
||||
let msg_cols: usize = 2;
|
||||
let log_scale: usize = msg_cols * log_base2k - 5;
|
||||
let module: Module = Module::new(n, BACKEND::FFT64);
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(n);
|
||||
|
||||
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);
|
||||
|
||||
let mut res: VecZnx = module.new_vec_znx(1, cols);
|
||||
let mut res: VecZnx = module.new_vec_znx(1, limbs);
|
||||
|
||||
// s <- Z_{-1, 0, 1}[X]/(X^{N}+1)
|
||||
let mut s: Scalar = Scalar::new(n);
|
||||
s.fill_ternary_prob(0.5, &mut source);
|
||||
|
||||
// Buffer to store s in the DFT domain
|
||||
let mut s_ppol: SvpPPol = module.new_svp_ppol();
|
||||
let mut s_ppol: SvpPPol<FFT64> = module.new_svp_ppol();
|
||||
|
||||
// s_ppol <- DFT(s)
|
||||
module.svp_prepare(&mut s_ppol, &s);
|
||||
|
||||
// a <- Z_{2^prec}[X]/(X^{N}+1)
|
||||
let mut a: VecZnx = module.new_vec_znx(1, cols);
|
||||
module.fill_uniform(log_base2k, &mut a, cols, &mut source);
|
||||
let mut a: VecZnx = module.new_vec_znx(1, limbs);
|
||||
module.fill_uniform(log_base2k, &mut a, 0, limbs, &mut source);
|
||||
|
||||
|
||||
|
||||
// Scratch space for DFT values
|
||||
let mut buf_dft: VecZnxDft = module.new_vec_znx_dft(1, a.cols());
|
||||
let mut buf_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft(1, a.limbs());
|
||||
|
||||
// Applies buf_dft <- s * a
|
||||
module.svp_apply_dft(&mut buf_dft, &s_ppol, &a);
|
||||
|
||||
// Alias scratch space
|
||||
let mut buf_big: VecZnxBig = buf_dft.as_vec_znx_big();
|
||||
let mut buf_big: VecZnxBig<FFT64> = buf_dft.as_vec_znx_big();
|
||||
|
||||
// buf_big <- IDFT(buf_dft) (not normalized)
|
||||
module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft);
|
||||
|
||||
println!("{:?}", buf_big.raw());
|
||||
|
||||
let mut m: VecZnx = module.new_vec_znx(1, msg_cols);
|
||||
|
||||
let mut want: Vec<i64> = vec![0; n];
|
||||
@@ -59,13 +63,17 @@ fn main() {
|
||||
// buf_big <- m - buf_big
|
||||
module.vec_znx_big_sub_small_a_inplace(&mut buf_big, &m);
|
||||
|
||||
println!("{:?}", buf_big.raw());
|
||||
|
||||
// b <- normalize(buf_big) + e
|
||||
let mut b: VecZnx = module.new_vec_znx(1, cols);
|
||||
let mut b: VecZnx = module.new_vec_znx(1, limbs);
|
||||
module.vec_znx_big_normalize(log_base2k, &mut b, &buf_big, &mut carry);
|
||||
b.print(n);
|
||||
module.add_normal(
|
||||
log_base2k,
|
||||
&mut b,
|
||||
log_base2k * cols,
|
||||
0,
|
||||
log_base2k * limbs,
|
||||
&mut source,
|
||||
3.2,
|
||||
19.0,
|
||||
@@ -80,14 +88,18 @@ fn main() {
|
||||
// buf_big <- a * s + b
|
||||
module.vec_znx_big_add_small_inplace(&mut buf_big, &b);
|
||||
|
||||
println!("raw: {:?}", &buf_big.raw());
|
||||
|
||||
// res <- normalize(buf_big)
|
||||
module.vec_znx_big_normalize(log_base2k, &mut res, &buf_big, &mut carry);
|
||||
|
||||
|
||||
|
||||
// have = m * 2^{log_scale} + e
|
||||
let mut have: Vec<i64> = vec![i64::default(); n];
|
||||
res.decode_vec_i64(0, log_base2k, res.cols() * log_base2k, &mut have);
|
||||
res.decode_vec_i64(0, log_base2k, res.limbs() * log_base2k, &mut have);
|
||||
|
||||
let scale: f64 = (1 << (res.cols() * log_base2k - log_scale)) as f64;
|
||||
let scale: f64 = (1 << (res.limbs() * log_base2k - log_scale)) as f64;
|
||||
izip!(want.iter(), have.iter())
|
||||
.enumerate()
|
||||
.for_each(|(i, (a, b))| {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use base2k::{
|
||||
BACKEND, Encoding, Infos, Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxOps, VmpPMat, VmpPMatOps,
|
||||
Encoding, FFT64, Infos, Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxOps, VmpPMat, VmpPMatOps,
|
||||
alloc_aligned,
|
||||
};
|
||||
|
||||
@@ -7,50 +7,51 @@ fn main() {
|
||||
let log_n: i32 = 5;
|
||||
let n: usize = 1 << log_n;
|
||||
|
||||
let module: Module = Module::new(n, BACKEND::FFT64);
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(n);
|
||||
let log_base2k: usize = 15;
|
||||
let cols: usize = 5;
|
||||
let log_k: usize = log_base2k * cols - 5;
|
||||
let limbs_vec: usize = 5;
|
||||
let log_k: usize = log_base2k * limbs_vec - 5;
|
||||
|
||||
let rows: usize = cols;
|
||||
let cols: usize = cols + 1;
|
||||
let rows_mat: usize = limbs_vec;
|
||||
let limbs_mat: usize = limbs_vec + 1;
|
||||
|
||||
// Maximum size of the byte scratch needed
|
||||
let tmp_bytes: usize = module.vmp_prepare_tmp_bytes(rows, cols) | module.vmp_apply_dft_tmp_bytes(cols, cols, rows, cols);
|
||||
let tmp_bytes: usize = module.vmp_prepare_tmp_bytes(rows_mat, 1, limbs_mat)
|
||||
| module.vmp_apply_dft_tmp_bytes(limbs_vec, limbs_vec, rows_mat, limbs_mat);
|
||||
|
||||
let mut buf: Vec<u8> = alloc_aligned(tmp_bytes);
|
||||
|
||||
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(1, rows);
|
||||
let mut a: VecZnx = module.new_vec_znx(1, limbs_vec);
|
||||
a.encode_vec_i64(0, log_base2k, log_k, &a_values, 32);
|
||||
a.normalize(log_base2k, &mut buf);
|
||||
|
||||
a.print(0, a.cols(), n);
|
||||
a.print(n);
|
||||
println!();
|
||||
|
||||
let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(1, rows, cols);
|
||||
let mut vmp_pmat: VmpPMat<FFT64> = module.new_vmp_pmat(rows_mat, 1, limbs_mat);
|
||||
|
||||
(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;
|
||||
(0..a.limbs()).for_each(|row_i| {
|
||||
let mut tmp: VecZnx = module.new_vec_znx(1, limbs_mat);
|
||||
tmp.at_limb_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(1, cols);
|
||||
let mut c_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft(1, limbs_mat);
|
||||
module.vmp_apply_dft(&mut c_dft, &a, &vmp_pmat, &mut buf);
|
||||
|
||||
let mut c_big: VecZnxBig = c_dft.as_vec_znx_big();
|
||||
let mut c_big: VecZnxBig<FFT64> = 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(1, rows);
|
||||
let mut res: VecZnx = module.new_vec_znx(1, limbs_vec);
|
||||
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(0, log_base2k, log_k, &mut values_res);
|
||||
|
||||
res.print(0, res.cols(), n);
|
||||
res.print(n);
|
||||
|
||||
module.free();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user