Add Zn type
jump to
@@ -18,7 +18,7 @@ let msg_size: usize = 2;
let log_scale: usize = msg_size * basek - 5; let module: Module<FFT64> = Module::<FFT64>::new(n as u64); - let mut scratch: ScratchOwned<FFT64> = ScratchOwned::<FFT64>::alloc(module.vec_znx_big_normalize_tmp_bytes(n)); + let mut scratch: ScratchOwned<FFT64> = ScratchOwned::<FFT64>::alloc(module.vec_znx_big_normalize_tmp_bytes()); let seed: [u8; 32] = [0; 32]; let mut source: Source = Source::new(seed);@@ -28,7 +28,7 @@ let mut s: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), 1);
s.fill_ternary_prob(0, 0.5, &mut source); // Buffer to store s in the DFT domain - let mut s_dft: SvpPPol<Vec<u8>, FFT64> = module.svp_ppol_alloc(n, s.cols()); + let mut s_dft: SvpPPol<Vec<u8>, FFT64> = module.svp_ppol_alloc(s.cols()); // s_dft <- DFT(s) module.svp_prepare(&mut s_dft, 0, &s, 0);@@ -43,7 +43,7 @@
// Fill the second column with random values: ct = (0, a) module.vec_znx_fill_uniform(basek, &mut ct, 1, ct_size * basek, &mut source); - let mut buf_dft: VecZnxDft<Vec<u8>, FFT64> = module.vec_znx_dft_alloc(n, 1, ct_size); + let mut buf_dft: VecZnxDft<Vec<u8>, FFT64> = module.vec_znx_dft_alloc(1, ct_size); module.vec_znx_dft_from_vec_znx(1, 0, &mut buf_dft, 0, &ct, 1);@@ -58,7 +58,7 @@
// Alias scratch space (VecZnxDft<B> is always at least as big as VecZnxBig<B>) // BIG(ct[1] * s) <- IDFT(DFT(ct[1] * s)) (not normalized) - let mut buf_big: VecZnxBig<Vec<u8>, FFT64> = module.vec_znx_big_alloc(n, 1, ct_size); + let mut buf_big: VecZnxBig<Vec<u8>, FFT64> = module.vec_znx_big_alloc(1, ct_size); module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut buf_big, 0, &mut buf_dft, 0); // Creates a plaintext: VecZnx with 1 column
@@ -1,15 +1,17 @@
-#[allow(non_camel_case_types)] -pub mod module; -#[allow(non_camel_case_types)] -pub mod svp; -#[allow(non_camel_case_types)] -pub mod vec_znx; -#[allow(dead_code)] -#[allow(non_camel_case_types)] -pub mod vec_znx_big; -#[allow(non_camel_case_types)] -pub mod vec_znx_dft; -#[allow(non_camel_case_types)] -pub mod vmp; -#[allow(non_camel_case_types)] -pub mod znx; +#[allow(non_camel_case_types)] +pub mod module; +#[allow(non_camel_case_types)] +pub mod svp; +#[allow(non_camel_case_types)] +pub mod vec_znx; +#[allow(dead_code)] +#[allow(non_camel_case_types)] +pub mod vec_znx_big; +#[allow(non_camel_case_types)] +pub mod vec_znx_dft; +#[allow(non_camel_case_types)] +pub mod vmp; +#[allow(non_camel_case_types)] +pub mod zn64; +#[allow(non_camel_case_types)] +pub mod znx;
@@ -103,7 +103,6 @@
unsafe extern "C" { pub unsafe fn vec_znx_normalize_base2k( module: *const MODULE, - n: u64, base2k: u64, res: *mut i64, res_size: u64,@@ -114,6 +113,7 @@ a_sl: u64,
tmp_space: *mut u8, ); } + unsafe extern "C" { - pub unsafe fn vec_znx_normalize_base2k_tmp_bytes(module: *const MODULE, n: u64) -> u64; + pub unsafe fn vec_znx_normalize_base2k_tmp_bytes(module: *const MODULE) -> u64; }
@@ -93,13 +93,12 @@ );
} unsafe extern "C" { - pub unsafe fn vec_znx_big_normalize_base2k_tmp_bytes(module: *const MODULE, n: u64) -> u64; + pub unsafe fn vec_znx_big_normalize_base2k_tmp_bytes(module: *const MODULE) -> u64; } unsafe extern "C" { pub unsafe fn vec_znx_big_normalize_base2k( module: *const MODULE, - n: u64, log2_base2k: u64, res: *mut i64, res_size: u64,@@ -113,7 +112,6 @@
unsafe extern "C" { pub unsafe fn vec_znx_big_range_normalize_base2k( module: *const MODULE, - n: u64, log2_base2k: u64, res: *mut i64, res_size: u64,@@ -127,7 +125,7 @@ );
} unsafe extern "C" { - pub unsafe fn vec_znx_big_range_normalize_base2k_tmp_bytes(module: *const MODULE, n: u64) -> u64; + pub unsafe fn vec_znx_big_range_normalize_base2k_tmp_bytes(module: *const MODULE) -> u64; } unsafe extern "C" {
@@ -43,7 +43,7 @@ tmp: *mut u8,
); } unsafe extern "C" { - pub unsafe fn vec_znx_idft_tmp_bytes(module: *const MODULE, n: u64) -> u64; + pub unsafe fn vec_znx_idft_tmp_bytes(module: *const MODULE) -> u64; } unsafe extern "C" { pub unsafe fn vec_znx_idft_tmp_a(
@@ -79,7 +79,6 @@
unsafe extern "C" { pub unsafe fn vmp_apply_dft_to_dft_tmp_bytes( module: *const MODULE, - nn: u64, res_size: u64, a_size: u64, nrows: u64,@@ -99,5 +98,5 @@ );
} unsafe extern "C" { - pub unsafe fn vmp_prepare_tmp_bytes(module: *const MODULE, nn: u64, nrows: u64, ncols: u64) -> u64; + pub unsafe fn vmp_prepare_tmp_bytes(module: *const MODULE, nrows: u64, ncols: u64) -> u64; }
@@ -0,0 +1,13 @@
+unsafe extern "C" { + pub unsafe fn zn64_normalize_base2k_ref( + n: u64, + base2k: u64, + res: *mut i64, + res_size: u64, + res_sl: u64, + a: *const i64, + a_size: u64, + a_sl: u64, + tmp_space: *mut u8, + ); +}
@@ -5,6 +5,7 @@ mod vec_znx;
mod vec_znx_big; mod vec_znx_dft; mod vmp_pmat; +mod zn; pub use module::FFT64;
@@ -25,8 +25,8 @@ ffi::{module::module_info_t, vec_znx, znx},
}; unsafe impl VecZnxNormalizeTmpBytesImpl<Self> for FFT64 { - fn vec_znx_normalize_tmp_bytes_impl(module: &Module<Self>, n: usize) -> usize { - unsafe { vec_znx::vec_znx_normalize_base2k_tmp_bytes(module.ptr() as *const module_info_t, n as u64) as usize } + fn vec_znx_normalize_tmp_bytes_impl(module: &Module<Self>) -> usize { + unsafe { vec_znx::vec_znx_normalize_base2k_tmp_bytes(module.ptr() as *const module_info_t) as usize } } }@@ -54,12 +54,11 @@ {
assert_eq!(res.n(), a.n()); } - let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_normalize_tmp_bytes(a.n())); + let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_normalize_tmp_bytes()); unsafe { vec_znx::vec_znx_normalize_base2k( module.ptr() as *const module_info_t, - a.n() as u64, basek as u64, res.at_mut_ptr(res_col, 0), res.size() as u64,@@ -88,12 +87,11 @@ A: VecZnxToMut,
{ let mut a: VecZnx<&mut [u8]> = a.to_mut(); - let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_normalize_tmp_bytes(a.n())); + let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_normalize_tmp_bytes()); unsafe { vec_znx::vec_znx_normalize_base2k( module.ptr() as *const module_info_t, - a.n() as u64, basek as u64, a.at_mut_ptr(a_col, 0), a.size() as u64,
@@ -569,8 +569,8 @@ }
} unsafe impl VecZnxBigNormalizeTmpBytesImpl<Self> for FFT64 { - fn vec_znx_big_normalize_tmp_bytes_impl(module: &Module<Self>, n: usize) -> usize { - unsafe { vec_znx::vec_znx_normalize_base2k_tmp_bytes(module.ptr(), n as u64) as usize } + fn vec_znx_big_normalize_tmp_bytes_impl(module: &Module<Self>) -> usize { + unsafe { vec_znx::vec_znx_normalize_base2k_tmp_bytes(module.ptr()) as usize } } }@@ -598,11 +598,10 @@ {
assert_eq!(res.n(), a.n()); } - let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_big_normalize_tmp_bytes(a.n())); + let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_big_normalize_tmp_bytes()); unsafe { vec_znx::vec_znx_normalize_base2k( module.ptr(), - a.n() as u64, basek as u64, res.at_mut_ptr(res_col, 0), res.size() as u64,
@@ -36,8 +36,8 @@ }
} unsafe impl VecZnxDftToVecZnxBigTmpBytesImpl<Self> for FFT64 { - fn vec_znx_dft_to_vec_znx_big_tmp_bytes_impl(module: &Module<Self>, n: usize) -> usize { - unsafe { vec_znx_dft::vec_znx_idft_tmp_bytes(module.ptr(), n as u64) as usize } + fn vec_znx_dft_to_vec_znx_big_tmp_bytes_impl(module: &Module<Self>) -> usize { + unsafe { vec_znx_dft::vec_znx_idft_tmp_bytes(module.ptr()) as usize } } }@@ -61,7 +61,7 @@ {
assert_eq!(res.n(), a.n()) } - let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_dft_to_vec_znx_big_tmp_bytes(a.n())); + let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_dft_to_vec_znx_big_tmp_bytes()); let min_size: usize = res.size().min(a.size());
@@ -41,18 +41,10 @@ }
} unsafe impl VmpPrepareTmpBytesImpl<FFT64> for FFT64 { - fn vmp_prepare_tmp_bytes_impl( - module: &Module<FFT64>, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> usize { + fn vmp_prepare_tmp_bytes_impl(module: &Module<FFT64>, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { unsafe { vmp::vmp_prepare_tmp_bytes( module.ptr(), - n as u64, (rows * cols_in) as u64, (cols_out * size) as u64, ) as usize@@ -102,8 +94,7 @@ a.size()
); } - let (tmp_bytes, _) = - scratch.take_slice(module.vmp_prepare_tmp_bytes(res.n(), a.rows(), a.cols_in(), a.cols_out(), a.size())); + let (tmp_bytes, _) = scratch.take_slice(module.vmp_prepare_tmp_bytes(a.rows(), a.cols_in(), a.cols_out(), a.size())); unsafe { vmp::vmp_prepare_contiguous(@@ -121,7 +112,6 @@
unsafe impl VmpApplyTmpBytesImpl<FFT64> for FFT64 { fn vmp_apply_tmp_bytes_impl( module: &Module<FFT64>, - n: usize, res_size: usize, a_size: usize, b_rows: usize,@@ -132,7 +122,6 @@ ) -> usize {
unsafe { vmp::vmp_apply_dft_to_dft_tmp_bytes( module.ptr(), - n as u64, (res_size * b_cols_out) as u64, (a_size * b_cols_in) as u64, (b_rows * b_cols_in) as u64,@@ -174,7 +163,6 @@ );
} let (tmp_bytes, _) = scratch.take_slice(module.vmp_apply_tmp_bytes( - res.n(), res.size(), a.size(), b.rows(),@@ -201,7 +189,6 @@
unsafe impl VmpApplyAddTmpBytesImpl<FFT64> for FFT64 { fn vmp_apply_add_tmp_bytes_impl( module: &Module<FFT64>, - n: usize, res_size: usize, a_size: usize, b_rows: usize,@@ -212,7 +199,6 @@ ) -> usize {
unsafe { vmp::vmp_apply_dft_to_dft_tmp_bytes( module.ptr(), - n as u64, (res_size * b_cols_out) as u64, (a_size * b_cols_in) as u64, (b_rows * b_cols_in) as u64,@@ -254,7 +240,6 @@ );
} let (tmp_bytes, _) = scratch.take_slice(module.vmp_apply_tmp_bytes( - res.n(), res.size(), a.size(), b.rows(),
@@ -0,0 +1,201 @@
+use poulpy_hal::{ + api::{TakeSlice, ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut}, + layouts::{Scratch, Zn, ZnToMut}, + oep::{ + TakeSliceImpl, ZnAddDistF64Impl, ZnAddNormalImpl, ZnFillDistF64Impl, ZnFillNormalImpl, ZnFillUniformImpl, + ZnNormalizeInplaceImpl, + }, + source::Source, +}; +use rand_distr::Normal; + +use crate::cpu_spqlios::{FFT64, ffi::zn64}; + +unsafe impl ZnNormalizeInplaceImpl<Self> for FFT64 +where + Self: TakeSliceImpl<Self>, +{ + fn zn_normalize_inplace_impl<A>(n: usize, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<Self>) + where + A: ZnToMut, + { + let mut a: Zn<&mut [u8]> = a.to_mut(); + + let (tmp_bytes, _) = scratch.take_slice(n * size_of::<i64>()); + + unsafe { + zn64::zn64_normalize_base2k_ref( + n as u64, + basek as u64, + a.at_mut_ptr(a_col, 0), + a.size() as u64, + a.sl() as u64, + a.at_ptr(a_col, 0), + a.size() as u64, + a.sl() as u64, + tmp_bytes.as_mut_ptr(), + ); + } + } +} + +unsafe impl ZnFillUniformImpl<Self> for FFT64 { + fn zn_fill_uniform_impl<R>(n: usize, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source) + where + R: ZnToMut, + { + let mut a: Zn<&mut [u8]> = res.to_mut(); + let base2k: u64 = 1 << basek; + let mask: u64 = base2k - 1; + let base2k_half: i64 = (base2k >> 1) as i64; + (0..k.div_ceil(basek)).for_each(|j| { + a.at_mut(res_col, j)[..n] + .iter_mut() + .for_each(|x| *x = (source.next_u64n(base2k, mask) as i64) - base2k_half); + }) + } +} + +unsafe impl ZnFillDistF64Impl<Self> for FFT64 { + fn zn_fill_dist_f64_impl<R, D: rand::prelude::Distribution<f64>>( + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + dist: D, + bound: f64, + ) where + R: ZnToMut, + { + let mut a: Zn<&mut [u8]> = res.to_mut(); + assert!( + (bound.log2().ceil() as i64) < 64, + "invalid bound: ceil(log2(bound))={} > 63", + (bound.log2().ceil() as i64) + ); + + let limb: usize = k.div_ceil(basek) - 1; + let basek_rem: usize = (limb + 1) * basek - k; + + if basek_rem != 0 { + a.at_mut(res_col, limb)[..n].iter_mut().for_each(|a| { + let mut dist_f64: f64 = dist.sample(source); + while dist_f64.abs() > bound { + dist_f64 = dist.sample(source) + } + *a = (dist_f64.round() as i64) << basek_rem; + }); + } else { + a.at_mut(res_col, limb)[..n].iter_mut().for_each(|a| { + let mut dist_f64: f64 = dist.sample(source); + while dist_f64.abs() > bound { + dist_f64 = dist.sample(source) + } + *a = dist_f64.round() as i64 + }); + } + } +} + +unsafe impl ZnAddDistF64Impl<Self> for FFT64 { + fn zn_add_dist_f64_impl<R, D: rand::prelude::Distribution<f64>>( + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + dist: D, + bound: f64, + ) where + R: ZnToMut, + { + let mut a: Zn<&mut [u8]> = res.to_mut(); + assert!( + (bound.log2().ceil() as i64) < 64, + "invalid bound: ceil(log2(bound))={} > 63", + (bound.log2().ceil() as i64) + ); + + let limb: usize = k.div_ceil(basek) - 1; + let basek_rem: usize = (limb + 1) * basek - k; + + if basek_rem != 0 { + a.at_mut(res_col, limb)[..n].iter_mut().for_each(|a| { + let mut dist_f64: f64 = dist.sample(source); + while dist_f64.abs() > bound { + dist_f64 = dist.sample(source) + } + *a += (dist_f64.round() as i64) << basek_rem; + }); + } else { + a.at_mut(res_col, limb)[..n].iter_mut().for_each(|a| { + let mut dist_f64: f64 = dist.sample(source); + while dist_f64.abs() > bound { + dist_f64 = dist.sample(source) + } + *a += dist_f64.round() as i64 + }); + } + } +} + +unsafe impl ZnFillNormalImpl<Self> for FFT64 +where + Self: ZnFillDistF64Impl<Self>, +{ + fn zn_fill_normal_impl<R>( + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + sigma: f64, + bound: f64, + ) where + R: ZnToMut, + { + Self::zn_fill_dist_f64_impl( + n, + basek, + res, + res_col, + k, + source, + Normal::new(0.0, sigma).unwrap(), + bound, + ); + } +} + +unsafe impl ZnAddNormalImpl<Self> for FFT64 +where + Self: ZnAddDistF64Impl<Self>, +{ + fn zn_add_normal_impl<R>( + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + sigma: f64, + bound: f64, + ) where + R: ZnToMut, + { + Self::zn_add_dist_f64_impl( + n, + basek, + res, + res_col, + k, + source, + Normal::new(0.0, sigma).unwrap(), + bound, + ); + } +}
@@ -44,11 +44,10 @@ let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct_out, rank);
let pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(&module, n, basek, ct_ggsw.k(), rank) - | GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct_glwe_in.k()) + GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, ct_ggsw.k(), rank) + | GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe_in.k()) | GLWECiphertext::external_product_scratch_space( &module, - n, basek, ct_glwe_out.k(), ct_glwe_in.k(),@@ -137,17 +136,9 @@ let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_glwe, rank);
let pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(&module, n, basek, ct_ggsw.k(), rank) - | GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct_glwe.k()) - | GLWECiphertext::external_product_inplace_scratch_space( - &module, - n, - basek, - ct_glwe.k(), - ct_ggsw.k(), - digits, - rank, - ), + GGSWCiphertext::encrypt_sk_scratch_space(&module, basek, ct_ggsw.k(), rank) + | GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_glwe.k()) + | GLWECiphertext::external_product_inplace_scratch_space(&module, basek, ct_glwe.k(), ct_ggsw.k(), digits, rank), ); let mut source_xs = Source::new([0u8; 32]);
@@ -45,11 +45,10 @@ let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_rlwe_in, rank_in);
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_rlwe_out, rank_out); let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(&module, n, basek, ksk.k(), rank_in, rank_out) - | GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct_in.k()) + GGLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank_in, rank_out) + | GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k()) | GLWECiphertext::keyswitch_scratch_space( &module, - n, basek, ct_out.k(), ct_in.k(),@@ -148,9 +147,9 @@ let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank, rank);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank); let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(&module, n, basek, ksk.k(), rank, rank) - | GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct.k()) - | GLWECiphertext::keyswitch_inplace_scratch_space(&module, n, basek, ct.k(), ksk.k(), digits, rank), + GGLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, ksk.k(), rank, rank) + | GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k()) + | GLWECiphertext::keyswitch_inplace_scratch_space(&module, basek, ct.k(), ksk.k(), digits, rank), ); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -45,8 +45,8 @@ let mut source_xa: Source = Source::new([2u8; 32]);
// Scratch space let mut scratch: ScratchOwned<FFT64> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(&module, n, basek, ct.k()) - | GLWECiphertext::decrypt_scratch_space(&module, n, basek, ct.k()), + GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k()) + | GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k()), ); // Generate secret-key
@@ -13,7 +13,6 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn automorphism_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -24,12 +23,11 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GLWECiphertext::keyswitch_scratch_space(module, n, basek, k_out, k_in, k_ksk, digits, rank, rank) + GLWECiphertext::keyswitch_scratch_space(module, basek, k_out, k_in, k_ksk, digits, rank, rank) } pub fn automorphism_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ksk: usize,@@ -39,7 +37,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGLWEAutomorphismKey::automorphism_scratch_space(module, n, basek, k_out, k_out, k_ksk, digits, rank) + GGLWEAutomorphismKey::automorphism_scratch_space(module, basek, k_out, k_out, k_ksk, digits, rank) } }
@@ -17,7 +17,6 @@ impl GGSWCiphertext<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn automorphism_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -32,17 +31,16 @@ Module<B>:
VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { let out_size: usize = k_out.div_ceil(basek); - let ci_dft: usize = module.vec_znx_dft_alloc_bytes(n, rank + 1, out_size); + let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, out_size); let ks_internal: usize = - GLWECiphertext::keyswitch_scratch_space(module, n, basek, k_out, k_in, k_ksk, digits_ksk, rank, rank); - let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, n, basek, k_out, k_tsk, digits_tsk, rank); + GLWECiphertext::keyswitch_scratch_space(module, basek, k_out, k_in, k_ksk, digits_ksk, rank, rank); + let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, basek, k_out, k_tsk, digits_tsk, rank); ci_dft + (ks_internal | expand) } #[allow(clippy::too_many_arguments)] pub fn automorphism_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ksk: usize,@@ -56,7 +54,7 @@ Module<B>:
VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { GGSWCiphertext::automorphism_scratch_space( - module, n, basek, k_out, k_out, k_ksk, digits_ksk, k_tsk, digits_tsk, rank, + module, basek, k_out, k_out, k_ksk, digits_ksk, k_tsk, digits_tsk, rank, ) } }@@ -117,7 +115,6 @@ assert!(
scratch.available() >= GGSWCiphertext::automorphism_scratch_space( module, - self.n(), self.basek(), self.k(), lhs.k(),
@@ -13,7 +13,6 @@ impl GLWECiphertext<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn automorphism_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -24,12 +23,11 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - Self::keyswitch_scratch_space(module, n, basek, k_out, k_in, k_ksk, digits, rank, rank) + Self::keyswitch_scratch_space(module, basek, k_out, k_in, k_ksk, digits, rank, rank) } pub fn automorphism_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ksk: usize,@@ -39,7 +37,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - Self::keyswitch_inplace_scratch_space(module, n, basek, k_out, k_ksk, digits, rank) + Self::keyswitch_inplace_scratch_space(module, basek, k_out, k_ksk, digits, rank) } }
@@ -15,7 +15,6 @@
impl LWECiphertext<Vec<u8>> { pub fn from_glwe_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_lwe: usize, k_glwe: usize,@@ -25,8 +24,8 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GLWECiphertext::bytes_of(n, basek, k_lwe, 1) - + GLWECiphertext::keyswitch_scratch_space(module, n, basek, k_lwe, k_glwe, k_ksk, 1, rank, 1) + GLWECiphertext::bytes_of(module.n(), basek, k_lwe, 1) + + GLWECiphertext::keyswitch_scratch_space(module, basek, k_lwe, k_glwe, k_ksk, 1, rank, 1) } }
@@ -15,7 +15,6 @@
impl GLWECiphertext<Vec<u8>> { pub fn from_lwe_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_lwe: usize, k_glwe: usize,@@ -25,8 +24,8 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GLWECiphertext::keyswitch_scratch_space(module, n, basek, k_glwe, k_lwe, k_ksk, 1, 1, rank) - + GLWECiphertext::bytes_of(n, basek, k_lwe, 1) + GLWECiphertext::keyswitch_scratch_space(module, basek, k_glwe, k_lwe, k_ksk, 1, 1, rank) + + GLWECiphertext::bytes_of(module.n(), basek, k_lwe, 1) } }
@@ -9,13 +9,12 @@
use crate::layouts::{GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared}; impl GLWECiphertext<Vec<u8>> { - pub fn decrypt_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize + pub fn decrypt_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize where Module<B>: VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { let size: usize = k.div_ceil(basek); - (module.vec_znx_normalize_tmp_bytes(n) | module.vec_znx_dft_alloc_bytes(n, 1, size)) - + module.vec_znx_dft_alloc_bytes(n, 1, size) + (module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_alloc_bytes(1, size)) + module.vec_znx_dft_alloc_bytes(1, size) } }
@@ -1,5 +1,5 @@
use poulpy_hal::{ - api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, ZnxView, ZnxViewMut}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnNormalizeInplace, ZnxView, ZnxViewMut}, layouts::{Backend, DataMut, DataRef, Module, ScratchOwned}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, };@@ -14,7 +14,7 @@ pub fn decrypt<DataPt, DataSk, B>(&self, module: &Module<B>, pt: &mut LWEPlaintext<DataPt>, sk: &LWESecret<DataSk>)
where DataPt: DataMut, DataSk: DataRef, - Module<B>: VecZnxNormalizeInplace<B>, + Module<B>: ZnNormalizeInplace<B>, B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, { #[cfg(debug_assertions)]@@ -30,7 +30,8 @@ .zip(sk.data.at(0, 0))
.map(|(x, y)| x * y) .sum::<i64>(); }); - module.vec_znx_normalize_inplace( + module.zn_normalize_inplace( + pt.n(), self.basek(), &mut pt.data, 0,
@@ -18,11 +18,12 @@ },
}; impl GGLWEAutomorphismKeyCompressed<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { - GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank) + GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, basek, k, rank, rank) + + GLWESecret::bytes_of(module.n(), rank) } }@@ -66,18 +67,12 @@ assert_eq!(self.rank_out(), self.rank_in());
assert_eq!(sk.rank(), self.rank()); assert!( scratch.available() - >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space( - module, - sk.n(), - self.basek(), - self.k(), - self.rank() - ), + >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(module, self.basek(), self.k(), self.rank()), "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space(module, self.rank()={}, self.size()={}): {}", scratch.available(), self.rank(), self.size(), - GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k(), self.rank()) + GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(module, self.basek(), self.k(), self.rank()) ) }
@@ -15,11 +15,11 @@ layouts::{GGLWECiphertext, Infos, compressed::GGLWECiphertextCompressed, prepared::GLWESecretPrepared},
}; impl GGLWECiphertextCompressed<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) + GGLWECiphertext::encrypt_sk_scratch_space(module, basek, k) } }@@ -71,13 +71,12 @@ );
assert_eq!(self.n(), sk.n()); assert_eq!(pt.n(), sk.n()); assert!( - scratch.available() - >= GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k()), + scratch.available() >= GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, self.basek(), self.k()), "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(module, self.rank()={}, self.size()={}): {}", scratch.available(), self.rank(), self.size(), - GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k()) + GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, self.basek(), self.k()) ); assert!( self.rows() * self.digits() * self.basek() <= self.k(),
@@ -17,7 +17,6 @@
impl GGLWESwitchingKeyCompressed<Vec<u8>> { pub fn encrypt_sk_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k: usize, rank_in: usize,@@ -26,9 +25,9 @@ ) -> usize
where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { - (GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1)) - + ScalarZnx::alloc_bytes(n, rank_in) - + GLWESecretPrepared::bytes_of(module, n, rank_out) + (GGLWECiphertext::encrypt_sk_scratch_space(module, basek, k) | ScalarZnx::alloc_bytes(module.n(), 1)) + + ScalarZnx::alloc_bytes(module.n(), rank_in) + + GLWESecretPrepared::bytes_of(module, rank_out) } }@@ -72,7 +71,6 @@ assert!(
scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space( module, - sk_out.n(), self.basek(), self.k(), self.rank_in(),@@ -82,7 +80,6 @@ "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
scratch.available(), GGLWESwitchingKey::encrypt_sk_scratch_space( module, - sk_out.n(), self.basek(), self.k(), self.rank_in(),
@@ -16,12 +16,12 @@ layouts::{GGLWETensorKey, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed, prepared::Prepare},
}; impl GGLWETensorKeyCompressed<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { - GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k, rank) + GGLWETensorKey::encrypt_sk_scratch_space(module, basek, k, rank) } }
@@ -15,11 +15,11 @@ layouts::{GGSWCiphertext, Infos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretPrepared},
}; impl GGSWCiphertextCompressed<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k, rank) + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k, rank) } }
@@ -14,11 +14,11 @@ layouts::{GLWECiphertext, GLWEPlaintext, Infos, compressed::GLWECiphertextCompressed, prepared::GLWESecretPrepared},
}; impl GLWECiphertextCompressed<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) + GLWECiphertext::encrypt_sk_scratch_space(module, basek, k) } }
@@ -15,15 +15,15 @@ layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, GLWESecret},
}; impl GGLWEAutomorphismKey<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank) + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank, rank) + GLWESecret::bytes_of(module.n(), rank) } - pub fn encrypt_pk_scratch_space<B: Backend>(module: &Module<B>, _n: usize, _basek: usize, _k: usize, _rank: usize) -> usize { - GGLWESwitchingKey::encrypt_pk_scratch_space(module, _n, _basek, _k, _rank, _rank) + pub fn encrypt_pk_scratch_space<B: Backend>(module: &Module<B>, _basek: usize, _k: usize, _rank: usize) -> usize { + GGLWESwitchingKey::encrypt_pk_scratch_space(module, _basek, _k, _rank, _rank) } }@@ -67,12 +67,12 @@ assert_eq!(self.rank_out(), self.rank_in());
assert_eq!(sk.rank(), self.rank()); assert!( scratch.available() - >= GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k(), self.rank()), + >= GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, self.basek(), self.k(), self.rank()), "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space(module, self.rank()={}, self.size()={}): {}", scratch.available(), self.rank(), self.size(), - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k(), self.rank()) + GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, self.basek(), self.k(), self.rank()) ) }
@@ -14,15 +14,15 @@ layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
}; impl GGLWECiphertext<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) - + (GLWEPlaintext::byte_of(n, basek, k) | module.vec_znx_normalize_tmp_bytes(n)) + GLWECiphertext::encrypt_sk_scratch_space(module, basek, k) + + (GLWEPlaintext::byte_of(module.n(), basek, k) | module.vec_znx_normalize_tmp_bytes()) } - pub fn encrypt_pk_scratch_space<B: Backend>(_module: &Module<B>, _n: usize, _basek: usize, _k: usize, _rank: usize) -> usize { + pub fn encrypt_pk_scratch_space<B: Backend>(_module: &Module<B>, _basek: usize, _k: usize, _rank: usize) -> usize { unimplemented!() } }@@ -75,12 +75,12 @@ );
assert_eq!(self.n(), sk.n()); assert_eq!(pt.n(), sk.n()); assert!( - scratch.available() >= GGLWECiphertext::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k()), + scratch.available() >= GGLWECiphertext::encrypt_sk_scratch_space(module, self.basek(), self.k()), "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(module, self.rank()={}, self.size()={}): {}", scratch.available(), self.rank(), self.size(), - GGLWECiphertext::encrypt_sk_scratch_space(module, sk.n(), self.basek(), self.k()) + GGLWECiphertext::encrypt_sk_scratch_space(module, self.basek(), self.k()) ); assert!( self.rows() * self.digits() * self.basek() <= self.k(),
@@ -17,7 +17,6 @@
impl GGLWESwitchingKey<Vec<u8>> { pub fn encrypt_sk_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k: usize, rank_in: usize,@@ -26,20 +25,19 @@ ) -> usize
where Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - (GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1)) - + ScalarZnx::alloc_bytes(n, rank_in) - + GLWESecretPrepared::bytes_of(module, n, rank_out) + (GGLWECiphertext::encrypt_sk_scratch_space(module, basek, k) | ScalarZnx::alloc_bytes(module.n(), 1)) + + ScalarZnx::alloc_bytes(module.n(), rank_in) + + GLWESecretPrepared::bytes_of(module, rank_out) } pub fn encrypt_pk_scratch_space<B: Backend>( module: &Module<B>, - _n: usize, _basek: usize, _k: usize, _rank_in: usize, _rank_out: usize, ) -> usize { - GGLWECiphertext::encrypt_pk_scratch_space(module, _n, _basek, _k, _rank_out) + GGLWECiphertext::encrypt_pk_scratch_space(module, _basek, _k, _rank_out) } }@@ -83,7 +81,6 @@ assert!(
scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space( module, - sk_out.n(), self.basek(), self.k(), self.rank_in(),@@ -93,7 +90,6 @@ "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
scratch.available(), GGLWESwitchingKey::encrypt_sk_scratch_space( module, - sk_out.n(), self.basek(), self.k(), self.rank_in(),
@@ -18,17 +18,17 @@ },
}; impl GGLWETensorKey<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { - GLWESecretPrepared::bytes_of(module, n, rank) - + module.vec_znx_dft_alloc_bytes(n, rank, 1) - + module.vec_znx_big_alloc_bytes(n, 1, 1) - + module.vec_znx_dft_alloc_bytes(n, 1, 1) - + GLWESecret::bytes_of(n, 1) - + GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecretPrepared::bytes_of(module, rank) + + module.vec_znx_dft_alloc_bytes(rank, 1) + + module.vec_znx_big_alloc_bytes(1, 1) + + module.vec_znx_dft_alloc_bytes(1, 1) + + GLWESecret::bytes_of(module.n(), 1) + + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank, rank) } }
@@ -14,15 +14,15 @@ layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GLWESecretPrepared},
}; impl GGSWCiphertext<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { let size = k.div_ceil(basek); - GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) - + VecZnx::alloc_bytes(n, rank + 1, size) - + VecZnx::alloc_bytes(n, 1, size) - + module.vec_znx_dft_alloc_bytes(n, rank + 1, size) + GLWECiphertext::encrypt_sk_scratch_space(module, basek, k) + + VecZnx::alloc_bytes(module.n(), rank + 1, size) + + VecZnx::alloc_bytes(module.n(), 1, size) + + module.vec_znx_dft_alloc_bytes(rank + 1, size) } }
@@ -19,21 +19,24 @@ },
}; impl GLWECiphertext<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { let size: usize = k.div_ceil(basek); - module.vec_znx_normalize_tmp_bytes(n) + 2 * VecZnx::alloc_bytes(n, 1, size) + module.vec_znx_dft_alloc_bytes(n, 1, size) + module.vec_znx_normalize_tmp_bytes() + + 2 * VecZnx::alloc_bytes(module.n(), 1, size) + + module.vec_znx_dft_alloc_bytes(1, size) } - pub fn encrypt_pk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize + pub fn encrypt_pk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize where Module<B>: VecZnxDftAllocBytes + SvpPPolAllocBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes, { let size: usize = k.div_ceil(basek); - ((module.vec_znx_dft_alloc_bytes(n, 1, size) + module.vec_znx_big_alloc_bytes(n, 1, size)) | ScalarZnx::alloc_bytes(n, 1)) - + module.svp_ppol_alloc_bytes(n, 1) - + module.vec_znx_normalize_tmp_bytes(n) + ((module.vec_znx_dft_alloc_bytes(1, size) + module.vec_znx_big_alloc_bytes(1, size)) + | ScalarZnx::alloc_bytes(module.n(), 1)) + + module.svp_ppol_alloc_bytes(1) + + module.vec_znx_normalize_tmp_bytes() } }@@ -69,10 +72,10 @@ assert_eq!(self.rank(), sk.rank());
assert_eq!(sk.n(), self.n()); assert_eq!(pt.n(), self.n()); assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self.n(), self.basek(), self.k()), + scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self.basek(), self.k()), "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(module, self.n(), self.basek(), self.k()) + GLWECiphertext::encrypt_sk_scratch_space(module, self.basek(), self.k()) ) }@@ -107,10 +110,10 @@ {
assert_eq!(self.rank(), sk.rank()); assert_eq!(sk.n(), self.n()); assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self.n(), self.basek(), self.k()), + scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self.basek(), self.k()), "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(module, self.n(), self.basek(), self.k()) + GLWECiphertext::encrypt_sk_scratch_space(module, self.basek(), self.k()) ) } self.encrypt_sk_internal(
@@ -54,7 +54,6 @@
// Its ok to allocate scratch space here since pk is usually generated only once. let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space( module, - self.n(), self.basek(), self.k(), ));
@@ -16,12 +16,13 @@ layouts::{GGLWESwitchingKey, GLWESecret, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretPrepared},
}; impl GLWEToLWESwitchingKey<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank_in: usize) -> usize where Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWESecretPrepared::bytes_of(module, n, rank_in) - + (GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in, 1) | GLWESecret::bytes_of(n, rank_in)) + GLWESecretPrepared::bytes_of(module, rank_in) + + (GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank_in, 1) + | GLWESecret::bytes_of(module.n(), rank_in)) } }
@@ -1,8 +1,6 @@
use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalizeInplace, ZnxView, ZnxViewMut, - }, - layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, VecZnx}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, ZnxView, ZnxViewMut}, + layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, Zn}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, source::Source, };@@ -23,7 +21,7 @@ source_xe: &mut Source,
) where DataPt: DataRef, DataSk: DataRef, - Module<B>: VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace<B>, + Module<B>: ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<B>, B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, { #[cfg(debug_assertions)]@@ -34,9 +32,9 @@
let basek: usize = self.basek(); let k: usize = self.k(); - module.vec_znx_fill_uniform(basek, &mut self.data, 0, k, source_xa); + module.zn_fill_uniform(self.n() + 1, basek, &mut self.data, 0, k, source_xa); - let mut tmp_znx: VecZnx<Vec<u8>> = VecZnx::alloc(1, 1, self.size()); + let mut tmp_znx: Zn<Vec<u8>> = Zn::alloc(1, 1, self.size()); let min_size = self.size().min(pt.size());@@ -57,9 +55,19 @@ .map(|(x, y)| x * y)
.sum::<i64>(); }); - module.vec_znx_add_normal(basek, &mut self.data, 0, k, source_xe, SIGMA, SIGMA_BOUND); + module.zn_add_normal( + 1, + basek, + &mut self.data, + 0, + k, + source_xe, + SIGMA, + SIGMA_BOUND, + ); - module.vec_znx_normalize_inplace( + module.zn_normalize_inplace( + 1, basek, &mut tmp_znx, 0,
@@ -15,13 +15,13 @@ layouts::{GGLWESwitchingKey, GLWESecret, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretPrepared},
}; impl LWESwitchingKey<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize where Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWESecret::bytes_of(n, 1) - + GLWESecretPrepared::bytes_of(module, n, 1) - + GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, 1) + GLWESecret::bytes_of(module.n(), 1) + + GLWESecretPrepared::bytes_of(module, 1) + + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, 1, 1) } }
@@ -15,11 +15,11 @@ layouts::{GGLWESwitchingKey, GLWESecret, LWESecret, LWEToGLWESwitchingKey},
}; impl LWEToGLWESwitchingKey<Vec<u8>> { - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank_out: usize) -> usize where Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, rank_out) + GLWESecret::bytes_of(n, 1) + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, 1, rank_out) + GLWESecret::bytes_of(module.n(), 1) } }
@@ -12,7 +12,6 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -23,12 +22,11 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::external_product_scratch_space(module, n, basek, k_out, k_in, ggsw_k, digits, rank) + GGLWESwitchingKey::external_product_scratch_space(module, basek, k_out, k_in, ggsw_k, digits, rank) } pub fn external_product_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, ggsw_k: usize,@@ -38,7 +36,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::external_product_inplace_scratch_space(module, n, basek, k_out, ggsw_k, digits, rank) + GGLWESwitchingKey::external_product_inplace_scratch_space(module, basek, k_out, ggsw_k, digits, rank) } }
@@ -12,7 +12,6 @@ impl GGLWESwitchingKey<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -23,12 +22,11 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_scratch_space(module, n, basek, k_out, k_in, k_ggsw, digits, rank) + GLWECiphertext::external_product_scratch_space(module, basek, k_out, k_in, k_ggsw, digits, rank) } pub fn external_product_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ggsw: usize,@@ -38,7 +36,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_inplace_scratch_space(module, n, basek, k_out, k_ggsw, digits, rank) + GLWECiphertext::external_product_inplace_scratch_space(module, basek, k_out, k_ggsw, digits, rank) } }
@@ -12,7 +12,6 @@ impl GGSWCiphertext<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -23,12 +22,11 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_scratch_space(module, n, basek, k_out, k_in, k_ggsw, digits, rank) + GLWECiphertext::external_product_scratch_space(module, basek, k_out, k_in, k_ggsw, digits, rank) } pub fn external_product_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ggsw: usize,@@ -38,7 +36,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_inplace_scratch_space(module, n, basek, k_out, k_ggsw, digits, rank) + GLWECiphertext::external_product_inplace_scratch_space(module, basek, k_out, k_ggsw, digits, rank) } }@@ -86,7 +84,6 @@ assert!(
scratch.available() >= GGSWCiphertext::external_product_scratch_space( module, - self.n(), self.basek(), self.k(), lhs.k(),
@@ -12,7 +12,6 @@ impl GLWECiphertext<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -26,10 +25,9 @@ {
let in_size: usize = k_in.div_ceil(basek).div_ceil(digits); let out_size: usize = k_out.div_ceil(basek); let ggsw_size: usize = k_ggsw.div_ceil(basek); - let res_dft: usize = module.vec_znx_dft_alloc_bytes(n, rank + 1, ggsw_size); - let a_dft: usize = module.vec_znx_dft_alloc_bytes(n, rank + 1, in_size); + let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, ggsw_size); + let a_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, in_size); let vmp: usize = module.vmp_apply_tmp_bytes( - n, out_size, in_size, in_size, // rows@@ -37,13 +35,12 @@ rank + 1, // cols in
rank + 1, // cols out ggsw_size, ); - let normalize: usize = module.vec_znx_normalize_tmp_bytes(n); + let normalize: usize = module.vec_znx_normalize_tmp_bytes(); res_dft + a_dft + (vmp | normalize) } pub fn external_product_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ggsw: usize,@@ -53,7 +50,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxNormalizeTmpBytes, { - Self::external_product_scratch_space(module, n, basek, k_out, k_out, k_ggsw, digits, rank) + Self::external_product_scratch_space(module, basek, k_out, k_out, k_ggsw, digits, rank) } }@@ -91,7 +88,6 @@ assert!(
scratch.available() >= GLWECiphertext::external_product_scratch_space( module, - self.n(), self.basek(), self.k(), lhs.k(),
@@ -89,7 +89,6 @@
/// Number of scratch space bytes required to call [Self::add]. pub fn scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, ct_k: usize, k_ksk: usize,@@ -99,7 +98,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - pack_core_scratch_space(module, n, basek, ct_k, k_ksk, digits, rank) + pack_core_scratch_space(module, basek, ct_k, k_ksk, digits, rank) } pub fn galois_elements<B: Backend>(module: &Module<B>) -> Vec<i64> {@@ -180,7 +179,6 @@ }
fn pack_core_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, ct_k: usize, k_ksk: usize,@@ -190,7 +188,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - combine_scratch_space(module, n, basek, ct_k, k_ksk, digits, rank) + combine_scratch_space(module, basek, ct_k, k_ksk, digits, rank) } fn pack_core<D: DataRef, DataAK: DataRef, B: Backend>(@@ -275,7 +273,6 @@ }
fn combine_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, ct_k: usize, k_ksk: usize,@@ -285,9 +282,9 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GLWECiphertext::bytes_of(n, basek, ct_k, rank) - + (GLWECiphertext::rsh_scratch_space(n) - | GLWECiphertext::automorphism_scratch_space(module, n, basek, ct_k, ct_k, k_ksk, digits, rank)) + GLWECiphertext::bytes_of(module.n(), basek, ct_k, rank) + + (GLWECiphertext::rsh_scratch_space(module.n()) + | GLWECiphertext::automorphism_scratch_space(module, basek, ct_k, ct_k, k_ksk, digits, rank)) } /// [combine] merges two ciphertexts together.
@@ -30,7 +30,6 @@
#[allow(clippy::too_many_arguments)] pub fn trace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, out_k: usize, in_k: usize,@@ -41,12 +40,11 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - Self::automorphism_inplace_scratch_space(module, n, basek, out_k.min(in_k), ksk_k, digits, rank) + Self::automorphism_inplace_scratch_space(module, basek, out_k.min(in_k), ksk_k, digits, rank) } pub fn trace_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, out_k: usize, ksk_k: usize,@@ -56,7 +54,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - Self::automorphism_inplace_scratch_space(module, n, basek, out_k, ksk_k, digits, rank) + Self::automorphism_inplace_scratch_space(module, basek, out_k, ksk_k, digits, rank) } }
@@ -15,7 +15,6 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn keyswitch_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -26,12 +25,11 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGLWESwitchingKey::keyswitch_scratch_space(module, n, basek, k_out, k_in, k_ksk, digits, rank, rank) + GGLWESwitchingKey::keyswitch_scratch_space(module, basek, k_out, k_in, k_ksk, digits, rank, rank) } pub fn keyswitch_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ksk: usize,@@ -41,7 +39,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGLWESwitchingKey::keyswitch_inplace_scratch_space(module, n, basek, k_out, k_ksk, digits, rank) + GGLWESwitchingKey::keyswitch_inplace_scratch_space(module, basek, k_out, k_ksk, digits, rank) } }@@ -92,7 +90,6 @@ impl GGLWESwitchingKey<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn keyswitch_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -104,14 +101,11 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GLWECiphertext::keyswitch_scratch_space( - module, n, basek, k_out, k_in, k_ksk, digits, rank_in, rank_out, - ) + GLWECiphertext::keyswitch_scratch_space(module, basek, k_out, k_in, k_ksk, digits, rank_in, rank_out) } pub fn keyswitch_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ksk: usize,@@ -121,7 +115,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - GLWECiphertext::keyswitch_inplace_scratch_space(module, n, basek, k_out, k_ksk, digits, rank) + GLWECiphertext::keyswitch_inplace_scratch_space(module, basek, k_out, k_ksk, digits, rank) } }
@@ -19,7 +19,6 @@
impl GGSWCiphertext<Vec<u8>> { pub(crate) fn expand_row_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, self_k: usize, k_tsk: usize,@@ -33,10 +32,9 @@ let tsk_size: usize = k_tsk.div_ceil(basek);
let self_size_out: usize = self_k.div_ceil(basek); let self_size_in: usize = self_size_out.div_ceil(digits); - let tmp_dft_i: usize = module.vec_znx_dft_alloc_bytes(n, rank + 1, tsk_size); - let tmp_a: usize = module.vec_znx_dft_alloc_bytes(n, 1, self_size_in); + let tmp_dft_i: usize = module.vec_znx_dft_alloc_bytes(rank + 1, tsk_size); + let tmp_a: usize = module.vec_znx_dft_alloc_bytes(1, self_size_in); let vmp: usize = module.vmp_apply_tmp_bytes( - n, self_size_out, self_size_in, self_size_in,@@ -44,15 +42,14 @@ rank,
rank, tsk_size, ); - let tmp_idft: usize = module.vec_znx_big_alloc_bytes(n, 1, tsk_size); - let norm: usize = module.vec_znx_normalize_tmp_bytes(n); + let tmp_idft: usize = module.vec_znx_big_alloc_bytes(1, tsk_size); + let norm: usize = module.vec_znx_normalize_tmp_bytes(); tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm)) } #[allow(clippy::too_many_arguments)] pub fn keyswitch_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -67,18 +64,17 @@ Module<B>:
VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { let out_size: usize = k_out.div_ceil(basek); - let res_znx: usize = VecZnx::alloc_bytes(n, rank + 1, out_size); - let ci_dft: usize = module.vec_znx_dft_alloc_bytes(n, rank + 1, out_size); - let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, n, basek, k_out, k_in, k_ksk, digits_ksk, rank, rank); - let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, n, basek, k_out, k_tsk, digits_tsk, rank); - let res_dft: usize = module.vec_znx_dft_alloc_bytes(n, rank + 1, out_size); + let res_znx: usize = VecZnx::alloc_bytes(module.n(), rank + 1, out_size); + let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, out_size); + let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, basek, k_out, k_in, k_ksk, digits_ksk, rank, rank); + let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, basek, k_out, k_tsk, digits_tsk, rank); + let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, out_size); res_znx + ci_dft + (ks | expand_rows | res_dft) } #[allow(clippy::too_many_arguments)] pub fn keyswitch_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ksk: usize,@@ -92,7 +88,7 @@ Module<B>:
VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { GGSWCiphertext::keyswitch_scratch_space( - module, n, basek, k_out, k_out, k_ksk, digits_ksk, k_tsk, digits_tsk, rank, + module, basek, k_out, k_out, k_ksk, digits_ksk, k_tsk, digits_tsk, rank, ) } }@@ -217,7 +213,6 @@ assert!(
scratch.available() >= GGSWCiphertext::expand_row_scratch_space( module, - self.n(), self.basek(), self.k(), tsk.k(),
@@ -12,7 +12,6 @@ impl GLWECiphertext<Vec<u8>> {
#[allow(clippy::too_many_arguments)] pub fn keyswitch_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_in: usize,@@ -27,24 +26,16 @@ {
let in_size: usize = k_in.div_ceil(basek).div_ceil(digits); let out_size: usize = k_out.div_ceil(basek); let ksk_size: usize = k_ksk.div_ceil(basek); - let res_dft: usize = module.vec_znx_dft_alloc_bytes(n, rank_out + 1, ksk_size); // TODO OPTIMIZE - let ai_dft: usize = module.vec_znx_dft_alloc_bytes(n, rank_in, in_size); - let vmp: usize = module.vmp_apply_tmp_bytes( - n, - out_size, - in_size, - in_size, - rank_in, - rank_out + 1, - ksk_size, - ) + module.vec_znx_dft_alloc_bytes(n, rank_in, in_size); - let normalize: usize = module.vec_znx_big_normalize_tmp_bytes(n); + let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank_out + 1, ksk_size); // TODO OPTIMIZE + let ai_dft: usize = module.vec_znx_dft_alloc_bytes(rank_in, in_size); + let vmp: usize = module.vmp_apply_tmp_bytes(out_size, in_size, in_size, rank_in, rank_out + 1, ksk_size) + + module.vec_znx_dft_alloc_bytes(rank_in, in_size); + let normalize: usize = module.vec_znx_big_normalize_tmp_bytes(); res_dft + ((ai_dft + vmp) | normalize) } pub fn keyswitch_inplace_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_out: usize, k_ksk: usize,@@ -54,7 +45,7 @@ ) -> usize
where Module<B>: VecZnxDftAllocBytes + VmpApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { - Self::keyswitch_scratch_space(module, n, basek, k_out, k_out, k_ksk, digits, rank, rank) + Self::keyswitch_scratch_space(module, basek, k_out, k_out, k_ksk, digits, rank, rank) } }@@ -95,7 +86,6 @@ assert!(
scratch.available() >= GLWECiphertext::keyswitch_scratch_space( module, - self.n(), self.basek(), self.k(), lhs.k(),@@ -117,7 +107,6 @@ )={}",
scratch.available(), GLWECiphertext::keyswitch_scratch_space( module, - self.n(), self.basek(), self.k(), lhs.k(),
@@ -15,7 +15,6 @@
impl LWECiphertext<Vec<u8>> { pub fn keyswitch_scratch_space<B: Backend>( module: &Module<B>, - n: usize, basek: usize, k_lwe_out: usize, k_lwe_in: usize,@@ -33,8 +32,8 @@ + VecZnxDftToVecZnxBigConsume<B>
+ VecZnxBigAddSmallInplace<B> + VecZnxBigNormalize<B>, { - GLWECiphertext::bytes_of(n, basek, k_lwe_out.max(k_lwe_in), 1) - + GLWECiphertext::keyswitch_inplace_scratch_space(module, n, basek, k_lwe_out, k_ksk, 1, 1) + GLWECiphertext::bytes_of(module.n(), basek, k_lwe_out.max(k_lwe_in), 1) + + GLWECiphertext::keyswitch_inplace_scratch_space(module, basek, k_lwe_out, k_ksk, 1, 1) } }
@@ -106,11 +106,11 @@ self.key.write_to(writer)
} } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWEAutomorphismKeyCompressed<DR>> for GGLWEAutomorphismKey<D> { - fn decompress(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKeyCompressed<DR>) - where - Module<B>: VecZnxFillUniform + VecZnxCopy, - { +impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWEAutomorphismKeyCompressed<DR>> for GGLWEAutomorphismKey<D> +where + Module<B>: VecZnxFillUniform + VecZnxCopy, +{ + fn decompress(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKeyCompressed<DR>) { self.key.decompress(module, &other.key); self.p = other.p; }
@@ -194,11 +194,11 @@ self.data.write_to(writer)
} } -impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGLWECiphertextCompressed<DR>> for GGLWECiphertext<D> { - fn decompress(&mut self, module: &Module<B>, other: &GGLWECiphertextCompressed<DR>) - where - Module<B>: VecZnxFillUniform + VecZnxCopy, - { +impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGLWECiphertextCompressed<DR>> for GGLWECiphertext<D> +where + Module<B>: VecZnxFillUniform + VecZnxCopy, +{ + fn decompress(&mut self, module: &Module<B>, other: &GGLWECiphertextCompressed<DR>) { #[cfg(debug_assertions)] { use poulpy_hal::api::ZnxInfos;
@@ -115,11 +115,11 @@ self.key.write_to(writer)
} } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWESwitchingKeyCompressed<DR>> for GGLWESwitchingKey<D> { - fn decompress(&mut self, module: &Module<B>, other: &GGLWESwitchingKeyCompressed<DR>) - where - Module<B>: VecZnxFillUniform + VecZnxCopy, - { +impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWESwitchingKeyCompressed<DR>> for GGLWESwitchingKey<D> +where + Module<B>: VecZnxFillUniform + VecZnxCopy, +{ + fn decompress(&mut self, module: &Module<B>, other: &GGLWESwitchingKeyCompressed<DR>) { self.key.decompress(module, &other.key); self.sk_in_n = other.sk_in_n; self.sk_out_n = other.sk_out_n;
@@ -139,11 +139,11 @@ &mut self.keys[i * rank + j - (i * (i + 1) / 2)]
} } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWETensorKeyCompressed<DR>> for GGLWETensorKey<D> { - fn decompress(&mut self, module: &Module<B>, other: &GGLWETensorKeyCompressed<DR>) - where - Module<B>: VecZnxFillUniform + VecZnxCopy, - { +impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWETensorKeyCompressed<DR>> for GGLWETensorKey<D> +where + Module<B>: VecZnxFillUniform + VecZnxCopy, +{ + fn decompress(&mut self, module: &Module<B>, other: &GGLWETensorKeyCompressed<DR>) { #[cfg(debug_assertions)] { assert_eq!(
@@ -185,11 +185,11 @@ self.data.write_to(writer)
} } -impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGSWCiphertextCompressed<DR>> for GGSWCiphertext<D> { - fn decompress(&mut self, module: &Module<B>, other: &GGSWCiphertextCompressed<DR>) - where - Module<B>: VecZnxFillUniform + VecZnxCopy, - { +impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGSWCiphertextCompressed<DR>> for GGSWCiphertext<D> +where + Module<B>: VecZnxFillUniform + VecZnxCopy, +{ + fn decompress(&mut self, module: &Module<B>, other: &GGSWCiphertextCompressed<DR>) { #[cfg(debug_assertions)] { assert_eq!(self.rank(), other.rank())
@@ -111,11 +111,11 @@ self.data.write_to(writer)
} } -impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GLWECiphertextCompressed<DR>> for GLWECiphertext<D> { - fn decompress(&mut self, module: &Module<B>, other: &GLWECiphertextCompressed<DR>) - where - Module<B>: VecZnxCopy + VecZnxFillUniform, - { +impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GLWECiphertextCompressed<DR>> for GLWECiphertext<D> +where + Module<B>: VecZnxFillUniform + VecZnxCopy, +{ + fn decompress(&mut self, module: &Module<B>, other: &GLWECiphertextCompressed<DR>) { #[cfg(debug_assertions)] { use poulpy_hal::api::ZnxInfos;
@@ -93,7 +93,7 @@ n, basek, k, rows, 1, rank_in, 1,
)) } - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank_in: usize) -> usize where Module<B>: VecZnxDftAllocBytes + VecZnxBigNormalize<B>@@ -112,6 +112,6 @@ + SvpPrepare<B>
+ SvpPPolAllocBytes + SvpPPolAlloc<B>, { - GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in) + GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank_in) } }
@@ -1,7 +1,7 @@
use std::fmt; use poulpy_hal::{ - api::{FillUniform, Reset, VecZnxFillUniform, ZnxInfos, ZnxView, ZnxViewMut}, + api::{FillUniform, Reset, ZnFillUniform, ZnxInfos, ZnxView, ZnxViewMut}, layouts::{Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, WriterTo}, source::Source, };@@ -117,13 +117,20 @@ self.data.write_to(writer)
} } -impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, LWECiphertextCompressed<DR>> for LWECiphertext<D> { - fn decompress(&mut self, module: &Module<B>, other: &LWECiphertextCompressed<DR>) - where - Module<B>: VecZnxFillUniform, - { - let mut source = Source::new(other.seed); - module.vec_znx_fill_uniform(other.basek(), &mut self.data, 0, other.k(), &mut source); +impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, LWECiphertextCompressed<DR>> for LWECiphertext<D> +where + Module<B>: ZnFillUniform, +{ + fn decompress(&mut self, module: &Module<B>, other: &LWECiphertextCompressed<DR>) { + let mut source: Source = Source::new(other.seed); + module.zn_fill_uniform( + self.n(), + other.basek(), + &mut self.data, + 0, + other.k(), + &mut source, + ); (0..self.size()).for_each(|i| { self.data.at_mut(0, i)[0] = other.data.at(0, i)[0]; });
@@ -94,7 +94,7 @@ n, basek, k, rows, 1, 1, 1,
)) } - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize where Module<B>: VecZnxDftAllocBytes + VecZnxBigNormalize<B>@@ -113,15 +113,15 @@ + SvpPrepare<B>
+ SvpPPolAllocBytes + SvpPPolAlloc<B>, { - LWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k) + LWESwitchingKey::encrypt_sk_scratch_space(module, basek, k) } } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWESwitchingKeyCompressed<DR>> for LWESwitchingKey<D> { - fn decompress(&mut self, module: &Module<B>, other: &LWESwitchingKeyCompressed<DR>) - where - Module<B>: VecZnxCopy + VecZnxFillUniform, - { +impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWESwitchingKeyCompressed<DR>> for LWESwitchingKey<D> +where + Module<B>: VecZnxFillUniform + VecZnxCopy, +{ + fn decompress(&mut self, module: &Module<B>, other: &LWESwitchingKeyCompressed<DR>) { self.0.decompress(module, &other.0); } }
@@ -95,7 +95,7 @@ n, basek, k, rows, 1, 1, rank_out,
)) } - pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize + pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank_out: usize) -> usize where Module<B>: VecZnxDftAllocBytes + VecZnxBigNormalize<B>@@ -114,15 +114,15 @@ + SvpPrepare<B>
+ SvpPPolAllocBytes + SvpPPolAlloc<B>, { - LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_out) + LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank_out) } } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWEToGLWESwitchingKeyCompressed<DR>> for LWEToGLWESwitchingKey<D> { - fn decompress(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKeyCompressed<DR>) - where - Module<B>: VecZnxCopy + VecZnxFillUniform, - { +impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWEToGLWESwitchingKeyCompressed<DR>> for LWEToGLWESwitchingKey<D> +where + Module<B>: VecZnxFillUniform + VecZnxCopy, +{ + fn decompress(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKeyCompressed<DR>) { self.0.decompress(module, &other.0); } }
@@ -20,13 +20,8 @@ pub use lwe_ct::*;
pub use lwe_ksk::*; pub use lwe_to_glwe_ksk::*; -use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, - layouts::{Backend, Module}, -}; +use poulpy_hal::layouts::{Backend, Module}; pub trait Decompress<B: Backend, C> { - fn decompress(&mut self, module: &Module<B>, other: &C) - where - Module<B>: VecZnxFillUniform + VecZnxCopy; + fn decompress(&mut self, module: &Module<B>, other: &C); }
@@ -2,25 +2,25 @@ use std::fmt;
use poulpy_hal::{ api::{FillUniform, Reset, ZnxInfos}, - layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo}, + layouts::{Data, DataMut, DataRef, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef}, source::Source, }; #[derive(PartialEq, Eq, Clone)] pub struct LWECiphertext<D: Data> { - pub(crate) data: VecZnx<D>, + pub(crate) data: Zn<D>, pub(crate) k: usize, pub(crate) basek: usize, } impl<D: DataRef> LWECiphertext<D> { - pub fn data(&self) -> &VecZnx<D> { + pub fn data(&self) -> &Zn<D> { &self.data } } impl<D: DataMut> LWECiphertext<D> { - pub fn data_mut(&mut self) -> &VecZnx<D> { + pub fn data_mut(&mut self) -> &Zn<D> { &mut self.data } }@@ -53,7 +53,7 @@ }
impl<D: DataMut> FillUniform for LWECiphertext<D> where - VecZnx<D>: FillUniform, + Zn<D>: FillUniform, { fn fill_uniform(&mut self, source: &mut Source) { self.data.fill_uniform(source);@@ -63,7 +63,7 @@
impl LWECiphertext<Vec<u8>> { pub fn alloc(n: usize, basek: usize, k: usize) -> Self { Self { - data: VecZnx::alloc(n + 1, 1, k.div_ceil(basek)), + data: Zn::alloc(n + 1, 1, k.div_ceil(basek)), k, basek, }@@ -72,9 +72,9 @@ }
impl<D: Data> Infos for LWECiphertext<D> where - VecZnx<D>: ZnxInfos, + Zn<D>: ZnxInfos, { - type Inner = VecZnx<D>; + type Inner = Zn<D>; fn n(&self) -> usize { &self.inner().n() - 1
@@ -1,11 +1,11 @@
use std::fmt; -use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, Zn, ZnToMut, ZnToRef}; use crate::layouts::{Infos, SetMetaData}; pub struct LWEPlaintext<D: Data> { - pub(crate) data: VecZnx<D>, + pub(crate) data: Zn<D>, pub(crate) k: usize, pub(crate) basek: usize, }@@ -13,7 +13,7 @@
impl LWEPlaintext<Vec<u8>> { pub fn alloc(basek: usize, k: usize) -> Self { Self { - data: VecZnx::alloc(1, 1, k.div_ceil(basek)), + data: Zn::alloc(1, 1, k.div_ceil(basek)), k, basek, }@@ -33,7 +33,7 @@ }
} impl<D: Data> Infos for LWEPlaintext<D> { - type Inner = VecZnx<D>; + type Inner = Zn<D>; fn inner(&self) -> &Self::Inner { &self.data@@ -89,7 +89,7 @@ }
} impl<D: DataMut> LWEPlaintext<D> { - pub fn data_mut(&mut self) -> &mut VecZnx<D> { + pub fn data_mut(&mut self) -> &mut Zn<D> { &mut self.data } }
@@ -15,21 +15,21 @@ pub(crate) p: i64,
} impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> { - pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module<B>: VmpPMatAlloc<B>, { GGLWEAutomorphismKeyPrepared::<Vec<u8>, B> { - key: GGLWESwitchingKeyPrepared::alloc(module, n, basek, k, rows, digits, rank, rank), + key: GGLWESwitchingKeyPrepared::alloc(module, basek, k, rows, digits, rank, rank), p: 0, } } - pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize + pub fn bytes_of(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize where Module<B>: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, rank, rank) + GGLWESwitchingKeyPrepared::bytes_of(module, basek, k, rows, digits, rank, rank) } }@@ -88,7 +88,6 @@ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> { let mut atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = GGLWEAutomorphismKeyPrepared::alloc( module, - self.n(), self.basek(), self.k(), self.rows(),
@@ -18,16 +18,7 @@ }
impl<B: Backend> GGLWECiphertextPrepared<Vec<u8>, B> { #[allow(clippy::too_many_arguments)] - pub fn alloc( - module: &Module<B>, - n: usize, - basek: usize, - k: usize, - rows: usize, - digits: usize, - rank_in: usize, - rank_out: usize, - ) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize) -> Self where Module<B>: VmpPMatAlloc<B>, {@@ -48,7 +39,7 @@ size
); Self { - data: module.vmp_pmat_alloc(n, rows, rank_in, rank_out + 1, size), + data: module.vmp_pmat_alloc(rows, rank_in, rank_out + 1, size), basek, k, digits,@@ -58,7 +49,6 @@
#[allow(clippy::too_many_arguments)] pub fn bytes_of( module: &Module<B>, - n: usize, basek: usize, k: usize, rows: usize,@@ -85,7 +75,7 @@ digits,
size ); - module.vmp_pmat_alloc_bytes(n, rows, rank_in, rank_out + 1, rows) + module.vmp_pmat_alloc_bytes(rows, rank_in, rank_out + 1, rows) } }@@ -142,7 +132,6 @@ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWECiphertextPrepared<Vec<u8>, B> { let mut atk_prepared: GGLWECiphertextPrepared<Vec<u8>, B> = GGLWECiphertextPrepared::alloc( module, - self.n(), self.basek(), self.k(), self.rows(),
@@ -17,21 +17,12 @@ }
impl<B: Backend> GGLWESwitchingKeyPrepared<Vec<u8>, B> { #[allow(clippy::too_many_arguments)] - pub fn alloc( - module: &Module<B>, - n: usize, - basek: usize, - k: usize, - rows: usize, - digits: usize, - rank_in: usize, - rank_out: usize, - ) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize) -> Self where Module<B>: VmpPMatAlloc<B>, { GGLWESwitchingKeyPrepared::<Vec<u8>, B> { - key: GGLWECiphertextPrepared::alloc(module, n, basek, k, rows, digits, rank_in, rank_out), + key: GGLWECiphertextPrepared::alloc(module, basek, k, rows, digits, rank_in, rank_out), sk_in_n: 0, sk_out_n: 0, }@@ -40,7 +31,6 @@
#[allow(clippy::too_many_arguments)] pub fn bytes_of( module: &Module<B>, - n: usize, basek: usize, k: usize, rows: usize,@@ -51,7 +41,7 @@ ) -> usize
where Module<B>: VmpPMatAllocBytes, { - GGLWECiphertextPrepared::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out) + GGLWECiphertextPrepared::bytes_of(module, basek, k, rows, digits, rank_in, rank_out) } }@@ -115,7 +105,6 @@ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWESwitchingKeyPrepared<Vec<u8>, B> { let mut atk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = GGLWESwitchingKeyPrepared::alloc( module, - self.n(), self.basek(), self.k(), self.rows(),
@@ -14,7 +14,7 @@ pub(crate) keys: Vec<GGLWESwitchingKeyPrepared<D, B>>,
} impl<B: Backend> GGLWETensorKeyPrepared<Vec<u8>, B> { - pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module<B>: VmpPMatAlloc<B>, {@@ -22,18 +22,18 @@ let mut keys: Vec<GGLWESwitchingKeyPrepared<Vec<u8>, B>> = Vec::new();
let pairs: usize = (((rank + 1) * rank) >> 1).max(1); (0..pairs).for_each(|_| { keys.push(GGLWESwitchingKeyPrepared::alloc( - module, n, basek, k, rows, digits, 1, rank, + module, basek, k, rows, digits, 1, rank, )); }); Self { keys } } - pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize + pub fn bytes_of(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize where Module<B>: VmpPMatAllocBytes, { let pairs: usize = (((rank + 1) * rank) >> 1).max(1); - pairs * GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, 1, rank) + pairs * GGLWESwitchingKeyPrepared::bytes_of(module, basek, k, rows, digits, 1, rank) } }@@ -118,7 +118,6 @@ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWETensorKeyPrepared<Vec<u8>, B> { let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = GGLWETensorKeyPrepared::alloc( module, - self.n(), self.basek(), self.k(), self.rows(),
@@ -17,7 +17,7 @@ pub(crate) digits: usize,
} impl<B: Backend> GGSWCiphertextPrepared<Vec<u8>, B> { - pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module<B>: VmpPMatAlloc<B>, {@@ -40,14 +40,14 @@ size
); Self { - data: module.vmp_pmat_alloc(n, rows, rank + 1, rank + 1, k.div_ceil(basek)), + data: module.vmp_pmat_alloc(rows, rank + 1, rank + 1, k.div_ceil(basek)), basek, k, digits, } } - pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize + pub fn bytes_of(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize where Module<B>: VmpPMatAllocBytes, {@@ -67,7 +67,7 @@ digits,
size ); - module.vmp_pmat_alloc_bytes(n, rows, rank + 1, rank + 1, size) + module.vmp_pmat_alloc_bytes(rows, rank + 1, rank + 1, size) } }@@ -122,7 +122,6 @@ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGSWCiphertextPrepared<Vec<u8>, B> { let mut ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = GGSWCiphertextPrepared::alloc( module, - self.n(), self.basek(), self.k(), self.rows(),
@@ -42,23 +42,23 @@ }
} impl<B: Backend> GLWEPublicKeyPrepared<Vec<u8>, B> { - pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rank: usize) -> Self where Module<B>: VecZnxDftAlloc<B>, { Self { - data: module.vec_znx_dft_alloc(n, rank + 1, k.div_ceil(basek)), + data: module.vec_znx_dft_alloc(rank + 1, k.div_ceil(basek)), basek, k, dist: Distribution::NONE, } } - pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn bytes_of(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: VecZnxDftAllocBytes, { - module.vec_znx_dft_alloc_bytes(n, rank + 1, k.div_ceil(basek)) + module.vec_znx_dft_alloc_bytes(rank + 1, k.div_ceil(basek)) } }@@ -68,7 +68,7 @@ Module<B>: VecZnxDftAlloc<B> + VecZnxDftFromVecZnx<B>,
{ fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GLWEPublicKeyPrepared<Vec<u8>, B> { let mut pk_prepared: GLWEPublicKeyPrepared<Vec<u8>, B> = - GLWEPublicKeyPrepared::alloc(module, self.n(), self.basek(), self.k(), self.rank()); + GLWEPublicKeyPrepared::alloc(module, self.basek(), self.k(), self.rank()); pk_prepared.prepare(module, self, scratch); pk_prepared }
@@ -17,21 +17,21 @@ pub(crate) dist: Distribution,
} impl<B: Backend> GLWESecretPrepared<Vec<u8>, B> { - pub fn alloc(module: &Module<B>, n: usize, rank: usize) -> Self + pub fn alloc(module: &Module<B>, rank: usize) -> Self where Module<B>: SvpPPolAlloc<B>, { Self { - data: module.svp_ppol_alloc(n, rank), + data: module.svp_ppol_alloc(rank), dist: Distribution::NONE, } } - pub fn bytes_of(module: &Module<B>, n: usize, rank: usize) -> usize + pub fn bytes_of(module: &Module<B>, rank: usize) -> usize where Module<B>: SvpPPolAllocBytes, { - module.svp_ppol_alloc_bytes(n, rank) + module.svp_ppol_alloc_bytes(rank) } }@@ -54,7 +54,7 @@ where
Module<B>: SvpPrepare<B> + SvpPPolAlloc<B>, { fn prepare_alloc(&self, module: &Module<B>, scratch: &mut poulpy_hal::layouts::Scratch<B>) -> GLWESecretPrepared<Vec<u8>, B> { - let mut sk_dft: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, self.n(), self.rank()); + let mut sk_dft: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, self.rank()); sk_dft.prepare(module, self, scratch); sk_dft }
@@ -46,20 +46,20 @@ }
} impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> { - pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_in: usize) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize, rank_in: usize) -> Self where Module<B>: VmpPMatAlloc<B>, { Self(GGLWESwitchingKeyPrepared::alloc( - module, n, basek, k, rows, 1, rank_in, 1, + module, basek, k, rows, 1, rank_in, 1, )) } - pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize) -> usize + pub fn bytes_of(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize) -> usize where Module<B>: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, rank_in, 1) + GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, basek, k, rows, digits, rank_in, 1) } }@@ -70,7 +70,6 @@ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> { let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = GLWEToLWESwitchingKeyPrepared::alloc( module, - self.0.n(), self.0.basek(), self.0.k(), self.0.rows(),
@@ -46,20 +46,20 @@ }
} impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> { - pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize) -> Self where Module<B>: VmpPMatAlloc<B>, { Self(GGLWESwitchingKeyPrepared::alloc( - module, n, basek, k, rows, 1, 1, 1, + module, basek, k, rows, 1, 1, 1, )) } - pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize) -> usize + pub fn bytes_of(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize) -> usize where Module<B>: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1) + GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, basek, k, rows, digits, 1, 1) } }@@ -68,13 +68,8 @@ where
Module<B>: VmpPrepare<B> + VmpPMatAlloc<B>, { fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> LWESwitchingKeyPrepared<Vec<u8>, B> { - let mut ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = LWESwitchingKeyPrepared::alloc( - module, - self.0.n(), - self.0.basek(), - self.0.k(), - self.0.rows(), - ); + let mut ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = + LWESwitchingKeyPrepared::alloc(module, self.0.basek(), self.0.k(), self.0.rows()); ksk_prepared.prepare(module, self, scratch); ksk_prepared }
@@ -47,20 +47,20 @@ }
} impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> { - pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self + pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self where Module<B>: VmpPMatAlloc<B>, { Self(GGLWESwitchingKeyPrepared::alloc( - module, n, basek, k, rows, 1, 1, rank_out, + module, basek, k, rows, 1, 1, rank_out, )) } - pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_out: usize) -> usize + pub fn bytes_of(module: &Module<B>, basek: usize, k: usize, rows: usize, digits: usize, rank_out: usize) -> usize where Module<B>: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out) + GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, basek, k, rows, digits, 1, rank_out) } }@@ -71,7 +71,6 @@ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> { let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = LWEToGLWESwitchingKeyPrepared::alloc( module, - self.0.n(), self.0.basek(), self.0.k(), self.0.rows(),
@@ -36,12 +36,7 @@ let digits: usize = self.digits();
let basek: usize = self.basek(); let k: usize = self.k(); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space( - module, - self.n(), - basek, - k, - )); + let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, basek, k)); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), basek, k); (0..self.rank_in()).for_each(|col_i| {
@@ -45,12 +45,11 @@ let digits: usize = self.digits();
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), basek, k); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), basek, k); - let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(self.n(), 1, self.size()); - let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(self.n(), 1, self.size()); + let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(1, self.size()); + let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(1, self.size()); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::decrypt_scratch_space(module, self.n(), basek, k) | module.vec_znx_normalize_tmp_bytes(self.n()), - ); + let mut scratch: ScratchOwned<B> = + ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, basek, k) | module.vec_znx_normalize_tmp_bytes()); (0..self.rank() + 1).for_each(|col_j| { (0..self.rows()).for_each(|row_i| {@@ -112,12 +111,11 @@ let digits: usize = self.digits();
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), basek, k); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), basek, k); - let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(self.n(), 1, self.size()); - let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(self.n(), 1, self.size()); + let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(1, self.size()); + let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(1, self.size()); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::decrypt_scratch_space(module, self.n(), basek, k) | module.vec_znx_normalize_tmp_bytes(module.n()), - ); + let mut scratch: ScratchOwned<B> = + ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, basek, k) | module.vec_znx_normalize_tmp_bytes()); (0..self.rank() + 1).for_each(|col_j| { (0..self.rows()).for_each(|row_i| {
@@ -41,7 +41,6 @@ let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), self.basek(), self.k());
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space( module, - self.n(), self.basek(), self.k(), ));
@@ -92,8 +92,8 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_apply, rank) - | GGLWEAutomorphismKey::automorphism_scratch_space(module, n, basek, k_out, k_in, k_apply, digits, rank), + GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_apply, rank) + | GGLWEAutomorphismKey::automorphism_scratch_space(module, basek, k_out, k_in, k_apply, digits, rank), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -120,7 +120,7 @@ scratch.borrow(),
); let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_apply, rows_apply, digits, rank); + GGLWEAutomorphismKeyPrepared::alloc(module, basek, k_apply, rows_apply, digits, rank); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());@@ -266,8 +266,8 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_apply, rank) - | GGLWEAutomorphismKey::automorphism_inplace_scratch_space(module, n, basek, k_in, k_apply, digits, rank), + GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_apply, rank) + | GGLWEAutomorphismKey::automorphism_inplace_scratch_space(module, basek, k_in, k_apply, digits, rank), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -294,7 +294,7 @@ scratch.borrow(),
); let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_apply, rows_apply, digits, rank); + GGLWEAutomorphismKeyPrepared::alloc(module, basek, k_apply, rows_apply, digits, rank); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());
@@ -102,11 +102,11 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k_in, rank) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank) - | GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k_tsk, rank) + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k_in, rank) + | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_ksk, rank) + | GGLWETensorKey::encrypt_sk_scratch_space(module, basek, k_tsk, rank) | GGSWCiphertext::automorphism_scratch_space( - module, n, basek, k_out, k_in, k_ksk, digits, k_tsk, digits, rank, + module, basek, k_out, k_in, k_ksk, digits, k_tsk, digits, rank, ), );@@ -144,11 +144,11 @@ scratch.borrow(),
); let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank); + GGLWEAutomorphismKeyPrepared::alloc(module, basek, k_ksk, rows, digits, rank); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = - GGLWETensorKeyPrepared::alloc(module, n, basek, k_tsk, rows, digits, rank); + GGLWETensorKeyPrepared::alloc(module, basek, k_tsk, rows, digits, rank); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism(@@ -255,10 +255,10 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct, rank) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank) - | GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k_tsk, rank) - | GGSWCiphertext::automorphism_inplace_scratch_space(module, n, basek, k_ct, k_ksk, digits, k_tsk, digits, rank), + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k_ct, rank) + | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_ksk, rank) + | GGLWETensorKey::encrypt_sk_scratch_space(module, basek, k_tsk, rank) + | GGSWCiphertext::automorphism_inplace_scratch_space(module, basek, k_ct, k_ksk, digits, k_tsk, digits, rank), ); let var_xs: f64 = 0.5;@@ -295,11 +295,11 @@ scratch.borrow(),
); let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank); + GGLWEAutomorphismKeyPrepared::alloc(module, basek, k_ksk, rows, digits, rank); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = - GGLWETensorKeyPrepared::alloc(module, n, basek, k_tsk, rows, digits, rank); + GGLWETensorKeyPrepared::alloc(module, basek, k_tsk, rows, digits, rank); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow());
@@ -89,12 +89,11 @@
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_in, &mut source_xa); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, autokey.k(), rank) - | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct_out.k()) - | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct_in.k()) + GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, autokey.k(), rank) + | GLWECiphertext::decrypt_scratch_space(module, basek, ct_out.k()) + | GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct_in.k()) | GLWECiphertext::automorphism_scratch_space( module, - n, basek, ct_out.k(), ct_in.k(),@@ -127,7 +126,7 @@ scratch.borrow(),
); let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank); + GGLWEAutomorphismKeyPrepared::alloc(module, basek, k_ksk, rows, digits, rank); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow());@@ -213,10 +212,10 @@
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_ct, &mut source_xa); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, autokey.k(), rank) - | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k()) - | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k()) - | GLWECiphertext::automorphism_inplace_scratch_space(module, n, basek, ct.k(), autokey.k(), digits, rank), + GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, autokey.k(), rank) + | GLWECiphertext::decrypt_scratch_space(module, basek, ct.k()) + | GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct.k()) + | GLWECiphertext::automorphism_inplace_scratch_space(module, basek, ct.k(), autokey.k(), digits, rank), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -242,7 +241,7 @@ scratch.borrow(),
); let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank); + GGLWEAutomorphismKeyPrepared::alloc(module, basek, k_ksk, rows, digits, rank); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow());
@@ -5,7 +5,7 @@ VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare, - ZnxView, + ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, ZnxView, }, layouts::{Backend, Module, ScratchOwned}, oep::{@@ -50,7 +50,10 @@ + VmpApply<B>
+ VmpApplyAdd<B> + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree - + VecZnxAutomorphismInplace, + + VecZnxAutomorphismInplace + + ZnNormalizeInplace<B> + + ZnFillUniform + + ZnAddNormal, B: Backend + TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B>@@ -79,9 +82,9 @@ let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank) - | GLWECiphertext::from_lwe_scratch_space(module, n, basek, k_lwe_ct, k_glwe_ct, k_ksk, rank) - | GLWECiphertext::decrypt_scratch_space(module, n, basek, k_glwe_ct), + LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k_ksk, rank) + | GLWECiphertext::from_lwe_scratch_space(module, basek, k_lwe_ct, k_glwe_ct, k_ksk, rank) + | GLWECiphertext::decrypt_scratch_space(module, basek, k_glwe_ct), ); let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -152,7 +155,8 @@ + VmpApply<B>
+ VmpApplyAdd<B> + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree - + VecZnxAutomorphismInplace, + + VecZnxAutomorphismInplace + + ZnNormalizeInplace<B>, B: Backend + TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B>@@ -181,9 +185,9 @@ let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank) - | LWECiphertext::from_glwe_scratch_space(module, n, basek, k_lwe_ct, k_glwe_ct, k_ksk, rank) - | GLWECiphertext::decrypt_scratch_space(module, n, basek, k_glwe_ct), + LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k_ksk, rank) + | LWECiphertext::from_glwe_scratch_space(module, basek, k_lwe_ct, k_glwe_ct, k_ksk, rank) + | GLWECiphertext::decrypt_scratch_space(module, basek, k_glwe_ct), ); let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -77,7 +77,7 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWEAutomorphismKey::encrypt_sk_scratch_space( - module, n, basek, k_ksk, rank, + module, basek, k_ksk, rank, )); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -169,7 +169,7 @@ let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWEAutomorphismKey::encrypt_sk_scratch_space( - module, n, basek, k_ksk, rank, + module, basek, k_ksk, rank, )); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -78,7 +78,7 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWESwitchingKey::encrypt_sk_scratch_space( - module, n, basek, k_ksk, rank_in, rank_out, + module, basek, k_ksk, rank_in, rank_out, )); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in);@@ -156,7 +156,7 @@ let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space( - module, n, basek, k_ksk, rank_in, rank_out, + module, basek, k_ksk, rank_in, rank_out, )); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in);
@@ -79,7 +79,7 @@
pt_scalar.fill_ternary_hw(0, n, &mut source_xs); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSWCiphertext::encrypt_sk_scratch_space( - module, n, basek, k, rank, + module, basek, k, rank, )); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -154,7 +154,7 @@
pt_scalar.fill_ternary_hw(0, n, &mut source_xs); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSWCiphertextCompressed::encrypt_sk_scratch_space( - module, n, basek, k, rank, + module, basek, k, rank, )); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -81,8 +81,8 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k()) - | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k()), + GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct.k()) + | GLWECiphertext::decrypt_scratch_space(module, basek, ct.k()), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -169,8 +169,8 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertextCompressed::encrypt_sk_scratch_space(module, n, basek, k_ct) - | GLWECiphertext::decrypt_scratch_space(module, n, basek, k_ct), + GLWECiphertextCompressed::encrypt_sk_scratch_space(module, basek, k_ct) + | GLWECiphertext::decrypt_scratch_space(module, basek, k_ct), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -263,8 +263,8 @@ let mut source_xe: Source = Source::new([1u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::decrypt_scratch_space(module, n, basek, k_ct) - | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct), + GLWECiphertext::decrypt_scratch_space(module, basek, k_ct) + | GLWECiphertext::encrypt_sk_scratch_space(module, basek, k_ct), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -331,9 +331,9 @@ let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xu: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k()) - | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k()) - | GLWECiphertext::encrypt_pk_scratch_space(module, n, basek, k_pk), + GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct.k()) + | GLWECiphertext::decrypt_scratch_space(module, basek, ct.k()) + | GLWECiphertext::encrypt_pk_scratch_space(module, basek, k_pk), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -75,7 +75,6 @@ let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWETensorKey::encrypt_sk_scratch_space( module, - n, basek, tensor_key.k(), rank,@@ -95,10 +94,10 @@ );
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k); - let mut sk_ij_dft = module.vec_znx_dft_alloc(n, 1, 1); - let mut sk_ij_big = module.vec_znx_big_alloc(n, 1, 1); + let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); + let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, 1); - let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(n, rank, 1); + let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(rank, 1); (0..rank).for_each(|i| { module.vec_znx_dft_from_vec_znx(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);@@ -185,7 +184,6 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWETensorKeyCompressed::encrypt_sk_scratch_space( module, - n, basek, tensor_key_compressed.k(), rank,@@ -204,10 +202,10 @@ tensor_key.decompress(module, &tensor_key_compressed);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k); - let mut sk_ij_dft = module.vec_znx_dft_alloc(n, 1, 1); - let mut sk_ij_big = module.vec_znx_big_alloc(n, 1, 1); + let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); + let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, 1); - let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(n, rank, 1); + let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(rank, 1); (0..rank).for_each(|i| { module.vec_znx_dft_from_vec_znx(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
@@ -93,9 +93,9 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_in, rank_in, rank_out) - | GGLWESwitchingKey::external_product_scratch_space(module, n, basek, k_out, k_in, k_ggsw, digits, rank_out) - | GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k_ggsw, rank_out), + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k_in, rank_in, rank_out) + | GGLWESwitchingKey::external_product_scratch_space(module, basek, k_out, k_in, k_ggsw, digits, rank_out) + | GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k_ggsw, rank_out), ); let r: usize = 1;@@ -231,9 +231,9 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ct, rank_in, rank_out) - | GGLWESwitchingKey::external_product_inplace_scratch_space(module, n, basek, k_ct, k_ggsw, digits, rank_out) - | GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k_ggsw, rank_out), + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k_ct, rank_in, rank_out) + | GGLWESwitchingKey::external_product_inplace_scratch_space(module, basek, k_ct, k_ggsw, digits, rank_out) + | GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k_ggsw, rank_out), ); let r: usize = 1;
@@ -99,8 +99,8 @@
pt_ggsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k_ggsw, rank) - | GGSWCiphertext::external_product_scratch_space(module, n, basek, k_out, k_in, k_ggsw, digits, rank), + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k_ggsw, rank) + | GGSWCiphertext::external_product_scratch_space(module, basek, k_out, k_in, k_ggsw, digits, rank), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -231,8 +231,8 @@
pt_ggsw_rhs.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k_ggsw, rank) - | GGSWCiphertext::external_product_inplace_scratch_space(module, n, basek, k_ct, k_ggsw, digits, rank), + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k_ggsw, rank) + | GGSWCiphertext::external_product_inplace_scratch_space(module, basek, k_ct, k_ggsw, digits, rank), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -92,11 +92,10 @@
pt_rgsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, ct_ggsw.k(), rank) - | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct_glwe_in.k()) + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, ct_ggsw.k(), rank) + | GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct_glwe_in.k()) | GLWECiphertext::external_product_scratch_space( module, - n, basek, ct_glwe_out.k(), ct_glwe_in.k(),@@ -225,9 +224,9 @@
pt_rgsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, ct_ggsw.k(), rank) - | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct_glwe.k()) - | GLWECiphertext::external_product_inplace_scratch_space(module, n, basek, ct_glwe.k(), ct_ggsw.k(), digits, rank), + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, ct_ggsw.k(), rank) + | GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct_glwe.k()) + | GLWECiphertext::external_product_inplace_scratch_space(module, basek, ct_glwe.k(), ct_ggsw.k(), digits, rank), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -97,7 +97,6 @@ let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch_enc: ScratchOwned<B> = ScratchOwned::alloc(GGLWESwitchingKey::encrypt_sk_scratch_space( module, - n, basek, k_ksk, rank_in_s0s1 | rank_out_s0s1,@@ -105,7 +104,6 @@ rank_out_s0s1 | rank_out_s1s2,
)); let mut scratch_apply: ScratchOwned<B> = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_scratch_space( module, - n, basek, k_out, k_in,@@ -237,14 +235,13 @@ let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch_enc: ScratchOwned<B> = ScratchOwned::alloc(GGLWESwitchingKey::encrypt_sk_scratch_space( module, - n, basek, k_ksk, rank_in | rank_out, rank_out, )); let mut scratch_apply: ScratchOwned<B> = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_inplace_scratch_space( - module, n, basek, k_ct, k_ksk, digits, rank_out, + module, basek, k_ct, k_ksk, digits, rank_out, )); let var_xs: f64 = 0.5;
@@ -94,11 +94,11 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k_in, rank) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank, rank) - | GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k_tsk, rank) + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k_in, rank) + | GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k_ksk, rank, rank) + | GGLWETensorKey::encrypt_sk_scratch_space(module, basek, k_tsk, rank) | GGSWCiphertext::keyswitch_scratch_space( - module, n, basek, k_out, k_in, k_ksk, digits, k_tsk, digits, rank, + module, basek, k_out, k_in, k_ksk, digits, k_tsk, digits, rank, ), );@@ -237,10 +237,10 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct, rank) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank, rank) - | GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k_tsk, rank) - | GGSWCiphertext::keyswitch_inplace_scratch_space(module, n, basek, k_ct, k_ksk, digits, k_tsk, digits, rank), + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k_ct, rank) + | GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k_ksk, rank, rank) + | GGLWETensorKey::encrypt_sk_scratch_space(module, basek, k_tsk, rank) + | GGSWCiphertext::keyswitch_inplace_scratch_space(module, basek, k_ct, k_ksk, digits, k_tsk, digits, rank), ); let var_xs: f64 = 0.5;
@@ -86,11 +86,10 @@
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_in, &mut source_xa); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, ksk.k(), rank_in, rank_out) - | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct_in.k()) + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, ksk.k(), rank_in, rank_out) + | GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct_in.k()) | GLWECiphertext::keyswitch_scratch_space( module, - n, basek, ct_out.k(), ct_in.k(),@@ -200,9 +199,9 @@
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_ct, &mut source_xa); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, ksk.k(), rank, rank) - | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct_glwe.k()) - | GLWECiphertext::keyswitch_inplace_scratch_space(module, n, basek, ct_glwe.k(), ksk.k(), digits, rank), + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, ksk.k(), rank, rank) + | GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct_glwe.k()) + | GLWECiphertext::keyswitch_inplace_scratch_space(module, basek, ct_glwe.k(), ksk.k(), digits, rank), ); let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -5,7 +5,7 @@ VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare, - ZnxView, + ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, ZnxView, }, layouts::{Backend, Module, ScratchOwned}, oep::{@@ -49,7 +49,10 @@ + VmpApply<B>
+ VmpApplyAdd<B> + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree - + VecZnxAutomorphismInplace, + + VecZnxAutomorphismInplace + + ZnNormalizeInplace<B> + + ZnFillUniform + + ZnAddNormal, B: Backend + TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B>@@ -75,8 +78,8 @@ let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - LWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk) - | LWECiphertext::keyswitch_scratch_space(module, n, basek, k_lwe_ct, k_lwe_ct, k_ksk), + LWESwitchingKey::encrypt_sk_scratch_space(module, basek, k_ksk) + | LWECiphertext::keyswitch_scratch_space(module, basek, k_lwe_ct, k_lwe_ct, k_ksk), ); let mut sk_lwe_in: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe_in);
@@ -89,9 +89,9 @@
let rows: usize = k_ct.div_ceil(basek * digits); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank) - | GLWEPacker::scratch_space(module, n, basek, k_ct, k_ksk, digits, rank), + GLWECiphertext::encrypt_sk_scratch_space(module, basek, k_ct) + | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_ksk, rank) + | GLWEPacker::scratch_space(module, basek, k_ct, k_ksk, digits, rank), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -87,10 +87,10 @@ let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k()) - | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k()) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, k_autokey, rank) - | GLWECiphertext::trace_inplace_scratch_space(module, n, basek, ct.k(), k_autokey, digits, rank), + GLWECiphertext::encrypt_sk_scratch_space(module, basek, ct.k()) + | GLWECiphertext::decrypt_scratch_space(module, basek, ct.k()) + | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_autokey, rank) + | GLWECiphertext::trace_inplace_scratch_space(module, basek, ct.k(), k_autokey, digits, rank), ); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
@@ -37,19 +37,16 @@
impl<D: DataMut> LWEPlaintext<D> { pub fn encode_i64(&mut self, data: i64, k: usize) { let basek: usize = self.basek(); - self.data - .encode_coeff_i64(basek, 0, k, 0, data, i64::BITS as usize); + self.data.encode_i64(basek, k, data, i64::BITS as usize); } } impl<D: DataRef> LWEPlaintext<D> { pub fn decode_i64(&self, k: usize) -> i64 { - self.data.decode_coeff_i64(self.basek(), 0, k, 0) + self.data.decode_i64(self.basek(), k) } pub fn decode_float(&self) -> Float { - let mut data: Vec<Float> = vec![Float::new(self.k() as u32)]; - self.data.decode_vec_float(self.basek(), 0, &mut data); - data[0].clone() + self.data.decode_float(self.basek()) } }
@@ -5,6 +5,7 @@ mod vec_znx;
mod vec_znx_big; mod vec_znx_dft; mod vmp_pmat; +mod zn; mod znx_base; pub use module::*;@@ -14,4 +15,5 @@ pub use vec_znx::*;
pub use vec_znx_big::*; pub use vec_znx_dft::*; pub use vmp_pmat::*; +pub use zn::*; pub use znx_base::*;
@@ -2,18 +2,18 @@ use crate::layouts::{Backend, ScalarZnxToRef, SvpPPolOwned, SvpPPolToMut, SvpPPolToRef, VecZnxDftToMut, VecZnxDftToRef};
/// Allocates as [crate::layouts::SvpPPol]. pub trait SvpPPolAlloc<B: Backend> { - fn svp_ppol_alloc(&self, n: usize, cols: usize) -> SvpPPolOwned<B>; + fn svp_ppol_alloc(&self, cols: usize) -> SvpPPolOwned<B>; } /// Returns the size in bytes to allocate a [crate::layouts::SvpPPol]. pub trait SvpPPolAllocBytes { - fn svp_ppol_alloc_bytes(&self, n: usize, cols: usize) -> usize; + fn svp_ppol_alloc_bytes(&self, cols: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::MatZnx]. /// User must ensure that bytes is memory aligned and that it length is equal to [SvpPPolAllocBytes]. pub trait SvpPPolFromBytes<B: Backend> { - fn svp_ppol_from_bytes(&self, n: usize, cols: usize, bytes: Vec<u8>) -> SvpPPolOwned<B>; + fn svp_ppol_from_bytes(&self, cols: usize, bytes: Vec<u8>) -> SvpPPolOwned<B>; } /// Prepare a [crate::layouts::ScalarZnx] into an [crate::layouts::SvpPPol].
@@ -7,7 +7,7 @@ };
pub trait VecZnxNormalizeTmpBytes { /// Returns the minimum number of bytes necessary for normalization. - fn vec_znx_normalize_tmp_bytes(&self, n: usize) -> usize; + fn vec_znx_normalize_tmp_bytes(&self) -> usize; } pub trait VecZnxNormalize<B: Backend> {
@@ -7,18 +7,18 @@ };
/// Allocates as [crate::layouts::VecZnxBig]. pub trait VecZnxBigAlloc<B: Backend> { - fn vec_znx_big_alloc(&self, n: usize, cols: usize, size: usize) -> VecZnxBigOwned<B>; + fn vec_znx_big_alloc(&self, cols: usize, size: usize) -> VecZnxBigOwned<B>; } /// Returns the size in bytes to allocate a [crate::layouts::VecZnxBig]. pub trait VecZnxBigAllocBytes { - fn vec_znx_big_alloc_bytes(&self, n: usize, cols: usize, size: usize) -> usize; + fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::VecZnxBig]. /// User must ensure that bytes is memory aligned and that it length is equal to [VecZnxBigAllocBytes]. pub trait VecZnxBigFromBytes<B: Backend> { - fn vec_znx_big_from_bytes(&self, n: usize, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxBigOwned<B>; + fn vec_znx_big_from_bytes(&self, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxBigOwned<B>; } #[allow(clippy::too_many_arguments)]@@ -187,7 +187,7 @@ A: VecZnxBigToMut<B>;
} pub trait VecZnxBigNormalizeTmpBytes { - fn vec_znx_big_normalize_tmp_bytes(&self, n: usize) -> usize; + fn vec_znx_big_normalize_tmp_bytes(&self) -> usize; } pub trait VecZnxBigNormalize<B: Backend> {
@@ -3,19 +3,19 @@ Backend, Data, Scratch, VecZnxBig, VecZnxBigToMut, VecZnxDft, VecZnxDftOwned, VecZnxDftToMut, VecZnxDftToRef, VecZnxToRef,
}; pub trait VecZnxDftAlloc<B: Backend> { - fn vec_znx_dft_alloc(&self, n: usize, cols: usize, size: usize) -> VecZnxDftOwned<B>; + fn vec_znx_dft_alloc(&self, cols: usize, size: usize) -> VecZnxDftOwned<B>; } pub trait VecZnxDftFromBytes<B: Backend> { - fn vec_znx_dft_from_bytes(&self, n: usize, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxDftOwned<B>; + fn vec_znx_dft_from_bytes(&self, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxDftOwned<B>; } pub trait VecZnxDftAllocBytes { - fn vec_znx_dft_alloc_bytes(&self, n: usize, cols: usize, size: usize) -> usize; + fn vec_znx_dft_alloc_bytes(&self, cols: usize, size: usize) -> usize; } pub trait VecZnxDftToVecZnxBigTmpBytes { - fn vec_znx_dft_to_vec_znx_big_tmp_bytes(&self, n: usize) -> usize; + fn vec_znx_dft_to_vec_znx_big_tmp_bytes(&self) -> usize; } pub trait VecZnxDftToVecZnxBig<B: Backend> {
@@ -1,27 +1,19 @@
use crate::layouts::{Backend, MatZnxToRef, Scratch, VecZnxDftToMut, VecZnxDftToRef, VmpPMatOwned, VmpPMatToMut, VmpPMatToRef}; pub trait VmpPMatAlloc<B: Backend> { - fn vmp_pmat_alloc(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned<B>; + fn vmp_pmat_alloc(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned<B>; } pub trait VmpPMatAllocBytes { - fn vmp_pmat_alloc_bytes(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; + fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } pub trait VmpPMatFromBytes<B: Backend> { - fn vmp_pmat_from_bytes( - &self, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - bytes: Vec<u8>, - ) -> VmpPMatOwned<B>; + fn vmp_pmat_from_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: Vec<u8>) -> VmpPMatOwned<B>; } pub trait VmpPrepareTmpBytes { - fn vmp_prepare_tmp_bytes(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; + fn vmp_prepare_tmp_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } pub trait VmpPrepare<B: Backend> {@@ -35,7 +27,6 @@ #[allow(clippy::too_many_arguments)]
pub trait VmpApplyTmpBytes { fn vmp_apply_tmp_bytes( &self, - n: usize, res_size: usize, a_size: usize, b_rows: usize,@@ -81,7 +72,6 @@ #[allow(clippy::too_many_arguments)]
pub trait VmpApplyAddTmpBytes { fn vmp_apply_add_tmp_bytes( &self, - n: usize, res_size: usize, a_size: usize, b_rows: usize,
@@ -0,0 +1,86 @@
+use rand_distr::Distribution; + +use crate::{ + layouts::{Backend, Scratch, ZnToMut}, + source::Source, +}; + +pub trait ZnNormalizeInplace<B: Backend> { + /// Normalizes the selected column of `a`. + fn zn_normalize_inplace<A>(&self, n: usize, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>) + where + A: ZnToMut; +} + +pub trait ZnFillUniform { + /// Fills the first `size` size with uniform values in \[-2^{basek-1}, 2^{basek-1}\] + fn zn_fill_uniform<R>(&self, n: usize, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source) + where + R: ZnToMut; +} + +#[allow(clippy::too_many_arguments)] +pub trait ZnFillDistF64 { + fn zn_fill_dist_f64<R, D: Distribution<f64>>( + &self, + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + dist: D, + bound: f64, + ) where + R: ZnToMut; +} + +#[allow(clippy::too_many_arguments)] +pub trait ZnAddDistF64 { + /// Adds vector sampled according to the provided distribution, scaled by 2^{-k} and bounded to \[-bound, bound\]. + fn zn_add_dist_f64<R, D: Distribution<f64>>( + &self, + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + dist: D, + bound: f64, + ) where + R: ZnToMut; +} + +#[allow(clippy::too_many_arguments)] +pub trait ZnFillNormal { + fn zn_fill_normal<R>( + &self, + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + sigma: f64, + bound: f64, + ) where + R: ZnToMut; +} + +#[allow(clippy::too_many_arguments)] +pub trait ZnAddNormal { + /// Adds a discrete normal vector scaled by 2^{-k} with the provided standard deviation and bounded to \[-bound, bound\]. + fn zn_add_normal<R>( + &self, + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + sigma: f64, + bound: f64, + ) where + R: ZnToMut; +}
@@ -5,3 +5,4 @@ mod vec_znx;
mod vec_znx_big; mod vec_znx_dft; mod vmp_pmat; +mod zn;
@@ -8,8 +8,8 @@ impl<B> SvpPPolFromBytes<B> for Module<B>
where B: Backend + SvpPPolFromBytesImpl<B>, { - fn svp_ppol_from_bytes(&self, n: usize, cols: usize, bytes: Vec<u8>) -> SvpPPolOwned<B> { - B::svp_ppol_from_bytes_impl(n, cols, bytes) + fn svp_ppol_from_bytes(&self, cols: usize, bytes: Vec<u8>) -> SvpPPolOwned<B> { + B::svp_ppol_from_bytes_impl(self.n(), cols, bytes) } }@@ -17,8 +17,8 @@ impl<B> SvpPPolAlloc<B> for Module<B>
where B: Backend + SvpPPolAllocImpl<B>, { - fn svp_ppol_alloc(&self, n: usize, cols: usize) -> SvpPPolOwned<B> { - B::svp_ppol_alloc_impl(n, cols) + fn svp_ppol_alloc(&self, cols: usize) -> SvpPPolOwned<B> { + B::svp_ppol_alloc_impl(self.n(), cols) } }@@ -26,8 +26,8 @@ impl<B> SvpPPolAllocBytes for Module<B>
where B: Backend + SvpPPolAllocBytesImpl<B>, { - fn svp_ppol_alloc_bytes(&self, n: usize, cols: usize) -> usize { - B::svp_ppol_alloc_bytes_impl(n, cols) + fn svp_ppol_alloc_bytes(&self, cols: usize) -> usize { + B::svp_ppol_alloc_bytes_impl(self.n(), cols) } }
@@ -22,8 +22,8 @@ impl<B> VecZnxNormalizeTmpBytes for Module<B>
where B: Backend + VecZnxNormalizeTmpBytesImpl<B>, { - fn vec_znx_normalize_tmp_bytes(&self, n: usize) -> usize { - B::vec_znx_normalize_tmp_bytes_impl(self, n) + fn vec_znx_normalize_tmp_bytes(&self) -> usize { + B::vec_znx_normalize_tmp_bytes_impl(self) } }
@@ -24,8 +24,8 @@ impl<B> VecZnxBigAlloc<B> for Module<B>
where B: Backend + VecZnxBigAllocImpl<B>, { - fn vec_znx_big_alloc(&self, n: usize, cols: usize, size: usize) -> VecZnxBigOwned<B> { - B::vec_znx_big_alloc_impl(n, cols, size) + fn vec_znx_big_alloc(&self, cols: usize, size: usize) -> VecZnxBigOwned<B> { + B::vec_znx_big_alloc_impl(self.n(), cols, size) } }@@ -33,8 +33,8 @@ impl<B> VecZnxBigFromBytes<B> for Module<B>
where B: Backend + VecZnxBigFromBytesImpl<B>, { - fn vec_znx_big_from_bytes(&self, n: usize, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxBigOwned<B> { - B::vec_znx_big_from_bytes_impl(n, cols, size, bytes) + fn vec_znx_big_from_bytes(&self, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxBigOwned<B> { + B::vec_znx_big_from_bytes_impl(self.n(), cols, size, bytes) } }@@ -42,8 +42,8 @@ impl<B> VecZnxBigAllocBytes for Module<B>
where B: Backend + VecZnxBigAllocBytesImpl<B>, { - fn vec_znx_big_alloc_bytes(&self, n: usize, cols: usize, size: usize) -> usize { - B::vec_znx_big_alloc_bytes_impl(n, cols, size) + fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize { + B::vec_znx_big_alloc_bytes_impl(self.n(), cols, size) } }@@ -283,8 +283,8 @@ impl<B> VecZnxBigNormalizeTmpBytes for Module<B>
where B: Backend + VecZnxBigNormalizeTmpBytesImpl<B>, { - fn vec_znx_big_normalize_tmp_bytes(&self, n: usize) -> usize { - B::vec_znx_big_normalize_tmp_bytes_impl(self, n) + fn vec_znx_big_normalize_tmp_bytes(&self) -> usize { + B::vec_znx_big_normalize_tmp_bytes_impl(self) } }
@@ -20,8 +20,8 @@ impl<B> VecZnxDftFromBytes<B> for Module<B>
where B: Backend + VecZnxDftFromBytesImpl<B>, { - fn vec_znx_dft_from_bytes(&self, n: usize, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxDftOwned<B> { - B::vec_znx_dft_from_bytes_impl(n, cols, size, bytes) + fn vec_znx_dft_from_bytes(&self, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxDftOwned<B> { + B::vec_znx_dft_from_bytes_impl(self.n(), cols, size, bytes) } }@@ -29,8 +29,8 @@ impl<B> VecZnxDftAllocBytes for Module<B>
where B: Backend + VecZnxDftAllocBytesImpl<B>, { - fn vec_znx_dft_alloc_bytes(&self, n: usize, cols: usize, size: usize) -> usize { - B::vec_znx_dft_alloc_bytes_impl(n, cols, size) + fn vec_znx_dft_alloc_bytes(&self, cols: usize, size: usize) -> usize { + B::vec_znx_dft_alloc_bytes_impl(self.n(), cols, size) } }@@ -38,8 +38,8 @@ impl<B> VecZnxDftAlloc<B> for Module<B>
where B: Backend + VecZnxDftAllocImpl<B>, { - fn vec_znx_dft_alloc(&self, n: usize, cols: usize, size: usize) -> VecZnxDftOwned<B> { - B::vec_znx_dft_alloc_impl(n, cols, size) + fn vec_znx_dft_alloc(&self, cols: usize, size: usize) -> VecZnxDftOwned<B> { + B::vec_znx_dft_alloc_impl(self.n(), cols, size) } }@@ -47,8 +47,8 @@ impl<B> VecZnxDftToVecZnxBigTmpBytes for Module<B>
where B: Backend + VecZnxDftToVecZnxBigTmpBytesImpl<B>, { - fn vec_znx_dft_to_vec_znx_big_tmp_bytes(&self, n: usize) -> usize { - B::vec_znx_dft_to_vec_znx_big_tmp_bytes_impl(self, n) + fn vec_znx_dft_to_vec_znx_big_tmp_bytes(&self) -> usize { + B::vec_znx_dft_to_vec_znx_big_tmp_bytes_impl(self) } }
@@ -14,8 +14,8 @@ impl<B> VmpPMatAlloc<B> for Module<B>
where B: Backend + VmpPMatAllocImpl<B>, { - fn vmp_pmat_alloc(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned<B> { - B::vmp_pmat_alloc_impl(n, rows, cols_in, cols_out, size) + fn vmp_pmat_alloc(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned<B> { + B::vmp_pmat_alloc_impl(self.n(), rows, cols_in, cols_out, size) } }@@ -23,8 +23,8 @@ impl<B> VmpPMatAllocBytes for Module<B>
where B: Backend + VmpPMatAllocBytesImpl<B>, { - fn vmp_pmat_alloc_bytes(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size) + fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + B::vmp_pmat_alloc_bytes_impl(self.n(), rows, cols_in, cols_out, size) } }@@ -32,16 +32,8 @@ impl<B> VmpPMatFromBytes<B> for Module<B>
where B: Backend + VmpPMatFromBytesImpl<B>, { - fn vmp_pmat_from_bytes( - &self, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - bytes: Vec<u8>, - ) -> VmpPMatOwned<B> { - B::vmp_pmat_from_bytes_impl(n, rows, cols_in, cols_out, size, bytes) + fn vmp_pmat_from_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: Vec<u8>) -> VmpPMatOwned<B> { + B::vmp_pmat_from_bytes_impl(self.n(), rows, cols_in, cols_out, size, bytes) } }@@ -49,8 +41,8 @@ impl<B> VmpPrepareTmpBytes for Module<B>
where B: Backend + VmpPrepareTmpBytesImpl<B>, { - fn vmp_prepare_tmp_bytes(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - B::vmp_prepare_tmp_bytes_impl(self, n, rows, cols_in, cols_out, size) + fn vmp_prepare_tmp_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + B::vmp_prepare_tmp_bytes_impl(self, rows, cols_in, cols_out, size) } }@@ -73,7 +65,6 @@ B: Backend + VmpApplyTmpBytesImpl<B>,
{ fn vmp_apply_tmp_bytes( &self, - n: usize, res_size: usize, a_size: usize, b_rows: usize,@@ -82,7 +73,7 @@ b_cols_out: usize,
b_size: usize, ) -> usize { B::vmp_apply_tmp_bytes_impl( - self, n, res_size, a_size, b_rows, b_cols_in, b_cols_out, b_size, + self, res_size, a_size, b_rows, b_cols_in, b_cols_out, b_size, ) } }@@ -107,7 +98,6 @@ B: Backend + VmpApplyAddTmpBytesImpl<B>,
{ fn vmp_apply_add_tmp_bytes( &self, - n: usize, res_size: usize, a_size: usize, b_rows: usize,@@ -116,7 +106,7 @@ b_cols_out: usize,
b_size: usize, ) -> usize { B::vmp_apply_add_tmp_bytes_impl( - self, n, res_size, a_size, b_rows, b_cols_in, b_cols_out, b_size, + self, res_size, a_size, b_rows, b_cols_in, b_cols_out, b_size, ) } }
@@ -0,0 +1,114 @@
+use crate::{ + api::{ZnAddDistF64, ZnAddNormal, ZnFillDistF64, ZnFillNormal, ZnFillUniform, ZnNormalizeInplace}, + layouts::{Backend, Module, Scratch, ZnToMut}, + oep::{ZnAddDistF64Impl, ZnAddNormalImpl, ZnFillDistF64Impl, ZnFillNormalImpl, ZnFillUniformImpl, ZnNormalizeInplaceImpl}, + source::Source, +}; + +impl<B> ZnNormalizeInplace<B> for Module<B> +where + B: Backend + ZnNormalizeInplaceImpl<B>, +{ + fn zn_normalize_inplace<A>(&self, n: usize, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>) + where + A: ZnToMut, + { + B::zn_normalize_inplace_impl(n, basek, a, a_col, scratch) + } +} + +impl<B> ZnFillUniform for Module<B> +where + B: Backend + ZnFillUniformImpl<B>, +{ + fn zn_fill_uniform<R>(&self, n: usize, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source) + where + R: ZnToMut, + { + B::zn_fill_uniform_impl(n, basek, res, res_col, k, source); + } +} + +impl<B> ZnFillDistF64 for Module<B> +where + B: Backend + ZnFillDistF64Impl<B>, +{ + fn zn_fill_dist_f64<R, D: rand::prelude::Distribution<f64>>( + &self, + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + dist: D, + bound: f64, + ) where + R: ZnToMut, + { + B::zn_fill_dist_f64_impl(n, basek, res, res_col, k, source, dist, bound); + } +} + +impl<B> ZnAddDistF64 for Module<B> +where + B: Backend + ZnAddDistF64Impl<B>, +{ + fn zn_add_dist_f64<R, D: rand::prelude::Distribution<f64>>( + &self, + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + dist: D, + bound: f64, + ) where + R: ZnToMut, + { + B::zn_add_dist_f64_impl(n, basek, res, res_col, k, source, dist, bound); + } +} + +impl<B> ZnFillNormal for Module<B> +where + B: Backend + ZnFillNormalImpl<B>, +{ + fn zn_fill_normal<R>( + &self, + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + sigma: f64, + bound: f64, + ) where + R: ZnToMut, + { + B::zn_fill_normal_impl(n, basek, res, res_col, k, source, sigma, bound); + } +} + +impl<B> ZnAddNormal for Module<B> +where + B: Backend + ZnAddNormalImpl<B>, +{ + fn zn_add_normal<R>( + &self, + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + sigma: f64, + bound: f64, + ) where + R: ZnToMut, + { + B::zn_add_normal_impl(n, basek, res, res_col, k, source, sigma, bound); + } +}
@@ -3,7 +3,7 @@ use rug::{Assign, Float};
use crate::{ api::{ZnxInfos, ZnxView, ZnxViewMut, ZnxZero}, - layouts::{DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef}, + layouts::{DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, Zn, ZnToMut, ZnToRef}, }; impl<D: DataMut> VecZnx<D> {@@ -202,3 +202,90 @@ }
}); } } + +impl<D: DataMut> Zn<D> { + pub fn encode_i64(&mut self, basek: usize, k: usize, data: i64, log_max: usize) { + let size: usize = k.div_ceil(basek); + + #[cfg(debug_assertions)] + { + let a: Zn<&mut [u8]> = self.to_mut(); + assert!( + size <= a.size(), + "invalid argument k.div_ceil(basek)={} > a.size()={}", + size, + a.size() + ); + } + + let k_rem: usize = basek - (k % basek); + let mut a: Zn<&mut [u8]> = self.to_mut(); + (0..a.size()).for_each(|j| a.at_mut(0, j)[0] = 0); + + // If 2^{basek} * 2^{k_rem} < 2^{63}-1, then we can simply copy + // values on the last limb. + // Else we decompose values base2k. + if log_max + k_rem < 63 || k_rem == basek { + a.at_mut(0, size - 1)[0] = data; + } else { + let mask: i64 = (1 << basek) - 1; + let steps: usize = size.min(log_max.div_ceil(basek)); + (size - steps..size) + .rev() + .enumerate() + .for_each(|(j, j_rev)| { + a.at_mut(0, j_rev)[0] = (data >> (j * basek)) & mask; + }) + } + + // Case where prec % k != 0. + if k_rem != basek { + let steps: usize = size.min(log_max.div_ceil(basek)); + (size - steps..size).rev().for_each(|j| { + a.at_mut(0, j)[0] <<= k_rem; + }) + } + } +} + +impl<D: DataRef> Zn<D> { + pub fn decode_i64(&self, basek: usize, k: usize) -> i64 { + let a: Zn<&[u8]> = self.to_ref(); + let size: usize = k.div_ceil(basek); + let mut res: i64 = 0; + let rem: usize = basek - (k % basek); + (0..size).for_each(|j| { + let x: i64 = a.at(0, j)[0]; + if j == size - 1 && rem != basek { + let k_rem: usize = basek - rem; + res = (res << k_rem) + (x >> rem); + } else { + res = (res << basek) + x; + } + }); + res + } + + pub fn decode_float(&self, basek: usize) -> Float { + let a: Zn<&[u8]> = self.to_ref(); + let size: usize = a.size(); + let prec: u32 = (basek * size) as u32; + + // 2^{basek} + let base: Float = Float::with_val(prec, (1 << basek) as f64); + let mut res: Float = Float::with_val(prec, (1 << basek) as f64); + + // y[i] = sum x[j][i] * 2^{-basek*j} + (0..size).for_each(|i| { + if i == 0 { + res.assign(a.at(0, size - i - 1)[0]); + res /= &base; + } else { + res += Float::with_val(prec, a.at(0, size - i - 1)[0]); + res /= &base; + } + }); + + res + } +}
@@ -10,6 +10,7 @@ mod vec_znx;
mod vec_znx_big; mod vec_znx_dft; mod vmp_pmat; +mod zn; pub use mat_znx::*; pub use module::*;@@ -21,6 +22,7 @@ pub use vec_znx::*;
pub use vec_znx_big::*; pub use vec_znx_dft::*; pub use vmp_pmat::*; +pub use zn::*; pub trait Data = PartialEq + Eq + Sized; pub trait DataRef = Data + AsRef<[u8]>;
@@ -0,0 +1,255 @@
+use std::fmt; + +use crate::{ + alloc_aligned, + api::{DataView, DataViewMut, FillUniform, Reset, ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut, ZnxZero}, + layouts::{Data, DataMut, DataRef, ReaderFrom, ToOwnedDeep, WriterTo}, + source::Source, +}; + +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use rand::RngCore; + +#[derive(PartialEq, Eq, Clone, Copy)] +pub struct Zn<D: Data> { + pub data: D, + pub n: usize, + pub cols: usize, + pub size: usize, + pub max_size: usize, +} + +impl<D: DataRef> ToOwnedDeep for Zn<D> { + type Owned = Zn<Vec<u8>>; + fn to_owned_deep(&self) -> Self::Owned { + Zn { + data: self.data.as_ref().to_vec(), + n: self.n, + cols: self.cols, + size: self.size, + max_size: self.max_size, + } + } +} + +impl<D: DataRef> fmt::Debug for Zn<D> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self) + } +} + +impl<D: Data> ZnxInfos for Zn<D> { + fn cols(&self) -> usize { + self.cols + } + + fn rows(&self) -> usize { + 1 + } + + fn n(&self) -> usize { + self.n + } + + fn size(&self) -> usize { + self.size + } +} + +impl<D: Data> ZnxSliceSize for Zn<D> { + fn sl(&self) -> usize { + self.n() * self.cols() + } +} + +impl<D: Data> DataView for Zn<D> { + type D = D; + fn data(&self) -> &Self::D { + &self.data + } +} + +impl<D: Data> DataViewMut for Zn<D> { + fn data_mut(&mut self) -> &mut Self::D { + &mut self.data + } +} + +impl<D: DataRef> ZnxView for Zn<D> { + type Scalar = i64; +} + +impl Zn<Vec<u8>> { + pub fn rsh_scratch_space(n: usize) -> usize { + n * std::mem::size_of::<i64>() + } +} + +impl<D: DataMut> ZnxZero for Zn<D> { + fn zero(&mut self) { + self.raw_mut().fill(0) + } + fn zero_at(&mut self, i: usize, j: usize) { + self.at_mut(i, j).fill(0); + } +} + +impl Zn<Vec<u8>> { + pub fn alloc_bytes(n: usize, cols: usize, size: usize) -> usize { + n * cols * size * size_of::<i64>() + } + + pub fn alloc(n: usize, cols: usize, size: usize) -> Self { + let data: Vec<u8> = alloc_aligned::<u8>(Self::alloc_bytes(n, cols, size)); + Self { + data, + n, + cols, + size, + max_size: size, + } + } + + pub fn from_bytes<Scalar: Sized>(n: usize, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self { + let data: Vec<u8> = bytes.into(); + assert!(data.len() == Self::alloc_bytes(n, cols, size)); + Self { + data, + n, + cols, + size, + max_size: size, + } + } +} + +impl<D: Data> Zn<D> { + pub fn from_data(data: D, n: usize, cols: usize, size: usize) -> Self { + Self { + data, + n, + cols, + size, + max_size: size, + } + } +} + +impl<D: DataRef> fmt::Display for Zn<D> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!( + f, + "Zn(n={}, cols={}, size={})", + self.n, self.cols, self.size + )?; + + for col in 0..self.cols { + writeln!(f, "Column {}:", col)?; + for size in 0..self.size { + let coeffs = self.at(col, size); + write!(f, " Size {}: [", size)?; + + let max_show = 100; + let show_count = coeffs.len().min(max_show); + + for (i, &coeff) in coeffs.iter().take(show_count).enumerate() { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "{}", coeff)?; + } + + if coeffs.len() > max_show { + write!(f, ", ... ({} more)", coeffs.len() - max_show)?; + } + + writeln!(f, "]")?; + } + } + Ok(()) + } +} + +impl<D: DataMut> FillUniform for Zn<D> { + fn fill_uniform(&mut self, source: &mut Source) { + source.fill_bytes(self.data.as_mut()); + } +} + +impl<D: DataMut> Reset for Zn<D> { + fn reset(&mut self) { + self.zero(); + self.n = 0; + self.cols = 0; + self.size = 0; + self.max_size = 0; + } +} + +pub type ZnOwned = Zn<Vec<u8>>; +pub type ZnMut<'a> = Zn<&'a mut [u8]>; +pub type ZnRef<'a> = Zn<&'a [u8]>; + +pub trait ZnToRef { + fn to_ref(&self) -> Zn<&[u8]>; +} + +impl<D: DataRef> ZnToRef for Zn<D> { + fn to_ref(&self) -> Zn<&[u8]> { + Zn { + data: self.data.as_ref(), + n: self.n, + cols: self.cols, + size: self.size, + max_size: self.max_size, + } + } +} + +pub trait ZnToMut { + fn to_mut(&mut self) -> Zn<&mut [u8]>; +} + +impl<D: DataMut> ZnToMut for Zn<D> { + fn to_mut(&mut self) -> Zn<&mut [u8]> { + Zn { + data: self.data.as_mut(), + n: self.n, + cols: self.cols, + size: self.size, + max_size: self.max_size, + } + } +} + +impl<D: DataMut> ReaderFrom for Zn<D> { + fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { + self.n = reader.read_u64::<LittleEndian>()? as usize; + self.cols = reader.read_u64::<LittleEndian>()? as usize; + self.size = reader.read_u64::<LittleEndian>()? as usize; + self.max_size = reader.read_u64::<LittleEndian>()? as usize; + let len: usize = reader.read_u64::<LittleEndian>()? as usize; + let buf: &mut [u8] = self.data.as_mut(); + if buf.len() != len { + return Err(std::io::Error::new( + std::io::ErrorKind::UnexpectedEof, + format!("self.data.len()={} != read len={}", buf.len(), len), + )); + } + reader.read_exact(&mut buf[..len])?; + Ok(()) + } +} + +impl<D: DataRef> WriterTo for Zn<D> { + fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { + writer.write_u64::<LittleEndian>(self.n as u64)?; + writer.write_u64::<LittleEndian>(self.cols as u64)?; + writer.write_u64::<LittleEndian>(self.size as u64)?; + writer.write_u64::<LittleEndian>(self.max_size as u64)?; + let buf: &[u8] = self.data.as_ref(); + writer.write_u64::<LittleEndian>(buf.len() as u64)?; + writer.write_all(buf)?; + Ok(()) + } +}
@@ -5,6 +5,7 @@ mod vec_znx;
mod vec_znx_big; mod vec_znx_dft; mod vmp_pmat; +mod zn; pub use module::*; pub use scratch::*;@@ -13,3 +14,4 @@ pub use vec_znx::*;
pub use vec_znx_big::*; pub use vec_znx_dft::*; pub use vmp_pmat::*; +pub use zn::*;
@@ -10,7 +10,7 @@ /// * See [vec_znx_normalize_base2k_tmp_bytes_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L245C17-L245C55) for reference code.
/// * See [crate::api::VecZnxNormalizeTmpBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxNormalizeTmpBytesImpl<B: Backend> { - fn vec_znx_normalize_tmp_bytes_impl(module: &Module<B>, n: usize) -> usize; + fn vec_znx_normalize_tmp_bytes_impl(module: &Module<B>) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
@@ -263,7 +263,7 @@ /// * See TODO for reference code.
/// * See TODO for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigNormalizeTmpBytesImpl<B: Backend> { - fn vec_znx_big_normalize_tmp_bytes_impl(module: &Module<B>, n: usize) -> usize; + fn vec_znx_big_normalize_tmp_bytes_impl(module: &Module<B>) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
@@ -32,7 +32,7 @@ /// * See TODO for reference code.
/// * See TODO for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftToVecZnxBigTmpBytesImpl<B: Backend> { - fn vec_znx_dft_to_vec_znx_big_tmp_bytes_impl(module: &Module<B>, n: usize) -> usize; + fn vec_znx_dft_to_vec_znx_big_tmp_bytes_impl(module: &Module<B>) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
@@ -38,14 +38,7 @@ /// * See TODO for reference code.
/// * See TODO for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPrepareTmpBytesImpl<B: Backend> { - fn vmp_prepare_tmp_bytes_impl( - module: &Module<B>, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> usize; + fn vmp_prepare_tmp_bytes_impl(module: &Module<B>, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)@@ -67,7 +60,6 @@ /// # Safety [crate::doc::backend_safety] for safety contract.
pub unsafe trait VmpApplyTmpBytesImpl<B: Backend> { fn vmp_apply_tmp_bytes_impl( module: &Module<B>, - n: usize, res_size: usize, a_size: usize, b_rows: usize,@@ -97,7 +89,6 @@ /// # Safety [crate::doc::backend_safety] for safety contract.
pub unsafe trait VmpApplyAddTmpBytesImpl<B: Backend> { fn vmp_apply_add_tmp_bytes_impl( module: &Module<B>, - n: usize, res_size: usize, a_size: usize, b_rows: usize,
@@ -0,0 +1,97 @@
+use rand_distr::Distribution; + +use crate::{ + layouts::{Backend, Scratch, ZnToMut}, + source::Source, +}; + +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See [zn_normalize_base2k_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/zn64.c#L9) for reference code. +/// * See [crate::api::ZnxNormalizeInplace] for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. +pub unsafe trait ZnNormalizeInplaceImpl<B: Backend> { + fn zn_normalize_inplace_impl<A>(n: usize, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>) + where + A: ZnToMut; +} + +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See [crate::api::ZnFillUniform] for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. +pub unsafe trait ZnFillUniformImpl<B: Backend> { + fn zn_fill_uniform_impl<R>(n: usize, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source) + where + R: ZnToMut; +} + +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See [crate::api::ZnFillDistF64] for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. +pub unsafe trait ZnFillDistF64Impl<B: Backend> { + fn zn_fill_dist_f64_impl<R, D: Distribution<f64>>( + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + dist: D, + bound: f64, + ) where + R: ZnToMut; +} + +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See [crate::api::ZnAddDistF64] for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. +pub unsafe trait ZnAddDistF64Impl<B: Backend> { + fn zn_add_dist_f64_impl<R, D: Distribution<f64>>( + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + dist: D, + bound: f64, + ) where + R: ZnToMut; +} + +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See [crate::api::ZnFillNormal] for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. +pub unsafe trait ZnFillNormalImpl<B: Backend> { + fn zn_fill_normal_impl<R>( + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + sigma: f64, + bound: f64, + ) where + R: ZnToMut; +} + +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See [crate::api::ZnAddNormal] for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. +pub unsafe trait ZnAddNormalImpl<B: Backend> { + fn zn_add_normal_impl<R>( + n: usize, + basek: usize, + res: &mut R, + res_col: usize, + k: usize, + source: &mut Source, + sigma: f64, + bound: f64, + ) where + R: ZnToMut; +}
@@ -51,14 +51,13 @@ let mat_cols_out: usize = res_cols;
let mut scratch = ScratchOwned::alloc( module.vmp_apply_tmp_bytes( - n, res_size, a_size, mat_rows, mat_cols_in, mat_cols_out, mat_size, - ) | module.vec_znx_big_normalize_tmp_bytes(n), + ) | module.vec_znx_big_normalize_tmp_bytes(), ); let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(n, a_cols, a_size);@@ -67,10 +66,10 @@ (0..a_cols).for_each(|i| {
a.at_mut(i, a_size - 1)[i + 1] = 1; }); - let mut vmp: VmpPMat<Vec<u8>, B> = module.vmp_pmat_alloc(n, mat_rows, mat_cols_in, mat_cols_out, mat_size); + let mut vmp: VmpPMat<Vec<u8>, B> = module.vmp_pmat_alloc(mat_rows, mat_cols_in, mat_cols_out, mat_size); - let mut c_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(n, mat_cols_out, mat_size); - let mut c_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(n, mat_cols_out, mat_size); + let mut c_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(mat_cols_out, mat_size); + let mut c_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(mat_cols_out, mat_size); let mut mat: MatZnx<Vec<u8>> = MatZnx::alloc(n, mat_rows, mat_cols_in, mat_cols_out, mat_size);@@ -86,7 +85,7 @@ });
module.vmp_prepare(&mut vmp, &mat, scratch.borrow()); - let mut a_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(n, a_cols, a_size); + let mut a_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(a_cols, a_size); (0..a_cols).for_each(|i| { module.vec_znx_dft_from_vec_znx(1, 0, &mut a_dft, i, &a, i); });
@@ -8,7 +8,7 @@ };
use std::time::Instant; use poulpy_hal::{ - api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, ZnxView, ZnxViewMut}, + api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, ZnNormalizeInplace, ZnxView, ZnxViewMut}, layouts::{Module, ScalarZnx, ScratchOwned}, source::Source, };@@ -109,7 +109,7 @@ // LWE plaintext(data * 2^{- (k_lwe_pt - 1)})
pt_lwe.encode_i64(data, k_lwe_pt + 1); // +1 for padding bit // Normalize plaintext to nicely print coefficients - module.vec_znx_normalize_inplace(basek, pt_lwe.data_mut(), 0, scratch.borrow()); + module.zn_normalize_inplace(pt_lwe.n(), basek, pt_lwe.data_mut(), 0, scratch.borrow()); println!("pt_lwe: {}", pt_lwe); // LWE ciphertext
@@ -23,7 +23,6 @@
#[allow(clippy::too_many_arguments)] pub fn cggi_blind_rotate_scratch_space<B: Backend>( module: &Module<B>, - n: usize, block_size: usize, extension_factor: usize, basek: usize,@@ -44,14 +43,14 @@ let brk_size: usize = k_brk.div_ceil(basek);
if block_size > 1 { let cols: usize = rank + 1; - let acc_dft: usize = module.vec_znx_dft_alloc_bytes(n, cols, rows) * extension_factor; - let acc_big: usize = module.vec_znx_big_alloc_bytes(n, 1, brk_size); - let vmp_res: usize = module.vec_znx_dft_alloc_bytes(n, cols, brk_size) * extension_factor; - let vmp_xai: usize = module.vec_znx_dft_alloc_bytes(n, 1, brk_size); + let acc_dft: usize = module.vec_znx_dft_alloc_bytes(cols, rows) * extension_factor; + let acc_big: usize = module.vec_znx_big_alloc_bytes(1, brk_size); + let vmp_res: usize = module.vec_znx_dft_alloc_bytes(cols, brk_size) * extension_factor; + let vmp_xai: usize = module.vec_znx_dft_alloc_bytes(1, brk_size); let acc_dft_add: usize = vmp_res; - let vmp: usize = module.vmp_apply_tmp_bytes(n, brk_size, rows, rows, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) + let vmp: usize = module.vmp_apply_tmp_bytes(brk_size, rows, rows, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) let acc: usize = if extension_factor > 1 { - VecZnx::alloc_bytes(n, cols, k_res.div_ceil(basek)) * extension_factor + VecZnx::alloc_bytes(module.n(), cols, k_res.div_ceil(basek)) * extension_factor } else { 0 };@@ -60,10 +59,10 @@ acc + acc_dft
+ acc_dft_add + vmp_res + vmp_xai - + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes(n) | module.vec_znx_dft_to_vec_znx_big_tmp_bytes(n)))) + + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_dft_to_vec_znx_big_tmp_bytes()))) } else { - GLWECiphertext::bytes_of(n, basek, k_res, rank) - + GLWECiphertext::external_product_scratch_space(module, n, basek, k_res, k_res, k_brk, 1, rank) + GLWECiphertext::bytes_of(module.n(), basek, k_res, rank) + + GLWECiphertext::external_product_scratch_space(module, basek, k_res, k_res, k_brk, 1, rank) } }
@@ -38,11 +38,11 @@ }
} impl BlindRotationKey<Vec<u8>, CGGI> { - pub fn generate_from_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn generate_from_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertext::encrypt_sk_scratch_space(module, n, basek, k, rank) + GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k, rank) } }@@ -108,11 +108,11 @@ impl<B: Backend> BlindRotationKeyPreparedAlloc<B> for BlindRotationKeyPrepared<Vec<u8>, CGGI, B>
where Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>, { - fn alloc(module: &Module<B>, n_glwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self { + fn alloc(module: &Module<B>, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self { let mut data: Vec<GGSWCiphertextPrepared<Vec<u8>, B>> = Vec::with_capacity(n_lwe); (0..n_lwe).for_each(|_| { data.push(GGSWCiphertextPrepared::alloc( - module, n_glwe, basek, k, rows, 1, rank, + module, basek, k, rows, 1, rank, )) }); Self {@@ -139,11 +139,11 @@ _phantom: PhantomData,
} } - pub fn generate_from_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize + pub fn generate_from_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize where Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank) + GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, basek, k, rank) } }
@@ -16,7 +16,7 @@
use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey, utils::set_xai_plus_y}; pub trait BlindRotationKeyPreparedAlloc<B: Backend> { - fn alloc(module: &Module<B>, n_glwe: usize, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self; + fn alloc(module: &Module<B>, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self; } #[derive(PartialEq, Eq)]@@ -74,7 +74,6 @@ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> BlindRotationKeyPrepared<Vec<u8>, BRA, B> { let mut brk: BlindRotationKeyPrepared<Vec<u8>, BRA, B> = BlindRotationKeyPrepared::alloc( module, - self.n(), self.keys.len(), self.basek(), self.k(),@@ -112,7 +111,7 @@ if let Distribution::BinaryBlock(_) = other.dist {
let mut x_pow_a: Vec<SvpPPol<Vec<u8>, B>> = Vec::with_capacity(n << 1); let mut buf: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); (0..n << 1).for_each(|i| { - let mut res: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(n, 1); + let mut res: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(1); set_xai_plus_y(module, i, 0, &mut res, &mut buf); x_pow_a.push(res); });
@@ -22,7 +22,7 @@ pub(crate) drift: usize,
} impl LookUpTable { - pub fn alloc(n: usize, basek: usize, k: usize, extension_factor: usize) -> Self { + pub fn alloc<B: Backend>(module: &Module<B>, basek: usize, k: usize, extension_factor: usize) -> Self { #[cfg(debug_assertions)] { assert!(@@ -34,7 +34,7 @@ }
let size: usize = k.div_ceil(basek); let mut data: Vec<VecZnx<Vec<u8>>> = Vec::with_capacity(extension_factor); (0..extension_factor).for_each(|_| { - data.push(VecZnx::alloc(n, 1, size)); + data.push(VecZnx::alloc(module.n(), 1, size)); }); Self { data,@@ -121,16 +121,6 @@
let drift: usize = step >> 1; // Rotates half the step to the left - module.vec_znx_rotate_inplace(-(drift as i64), &mut lut_full, 0); - - let n_large: usize = lut_full.n(); - - module.vec_znx_normalize_inplace( - self.basek, - &mut lut_full, - 0, - ScratchOwned::alloc(module.vec_znx_normalize_tmp_bytes(n_large)).borrow(), - ); if self.extension_factor() > 1 { (0..self.extension_factor()).for_each(|i| {@@ -142,6 +132,14 @@ });
} else { module.vec_znx_copy(&mut self.data[0], 0, &lut_full, 0); } + + let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(module.vec_znx_normalize_tmp_bytes()); + + self.data.iter_mut().for_each(|a| { + module.vec_znx_normalize_inplace(self.basek, a, 0, scratch.borrow()); + }); + + self.rotate(module, -(drift as i64)); self.drift = drift }
@@ -6,7 +6,8 @@ VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace,
VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftSubABInplace, VecZnxDftToVecZnxBig, VecZnxDftToVecZnxBigConsume, VecZnxDftToVecZnxBigTmpBytes, VecZnxDftZero, VecZnxFillUniform, VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxSub, VecZnxSubABInplace, - VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare, ZnxView, + VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, + ZnNormalizeInplace, ZnxView, }, layouts::{Backend, Module, ScratchOwned}, oep::{@@ -65,7 +66,10 @@ + VecZnxSub
+ VmpPMatAlloc<B> + VmpPrepare<B> + VmpApply<B> - + VmpApplyAdd<B>, + + VmpApplyAdd<B> + + ZnFillUniform + + ZnAddNormal + + ZnNormalizeInplace<B>, B: Backend + VecZnxDftAllocBytesImpl<B> + VecZnxBigAllocBytesImpl<B>@@ -96,7 +100,7 @@ let mut source_xe: Source = Source::new([2u8; 32]);
let mut source_xa: Source = Source::new([1u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::<B>::alloc(BlindRotationKey::generate_from_sk_scratch_space( - module, n, basek, k_brk, rank, + module, basek, k_brk, rank, )); let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);@@ -108,7 +112,6 @@ sk_lwe.fill_binary_block(block_size, &mut source_xs);
let mut scratch_br: ScratchOwned<B> = ScratchOwned::<B>::alloc(cggi_blind_rotate_scratch_space( module, - n, block_size, extension_factor, basek,@@ -148,7 +151,7 @@ .iter_mut()
.enumerate() .for_each(|(i, x)| *x = f(i as i64)); - let mut lut: LookUpTable = LookUpTable::alloc(n, basek, k_lut, extension_factor); + let mut lut: LookUpTable = LookUpTable::alloc(module, basek, k_lut, extension_factor); lut.set(module, &f_vec, log_message_modulus + 1); let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_res, rank);
@@ -13,7 +13,6 @@ where
Module<B>: VecZnxRotateInplace + VecZnxNormalizeInplace<B> + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy, B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, { - let n: usize = module.n(); let basek: usize = 20; let k_lut: usize = 40; let message_modulus: usize = 16;@@ -26,7 +25,7 @@ f.iter_mut()
.enumerate() .for_each(|(i, x)| *x = (i as i64) - 8); - let mut lut: LookUpTable = LookUpTable::alloc(n, basek, k_lut, extension_factor); + let mut lut: LookUpTable = LookUpTable::alloc(module, basek, k_lut, extension_factor); lut.set(module, &f, log_scale); let half_step: i64 = lut.domain_size().div_round(message_modulus << 1) as i64;@@ -49,7 +48,6 @@ where
Module<B>: VecZnxRotateInplace + VecZnxNormalizeInplace<B> + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy, B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, { - let n: usize = module.n(); let basek: usize = 20; let k_lut: usize = 40; let message_modulus: usize = 16;@@ -62,7 +60,7 @@ f.iter_mut()
.enumerate() .for_each(|(i, x)| *x = (i as i64) - 8); - let mut lut: LookUpTable = LookUpTable::alloc(n, basek, k_lut, extension_factor); + let mut lut: LookUpTable = LookUpTable::alloc(module, basek, k_lut, extension_factor); lut.set(module, &f, log_scale); let half_step: i64 = lut.domain_size().div_round(message_modulus << 1) as i64;
@@ -191,7 +191,7 @@ });
} // Lut precision, basically must be able to hold the decomposition power basis of the GGSW - let mut lut: LookUpTable = LookUpTable::alloc(n, basek, basek * rows, extension_factor); + let mut lut: LookUpTable = LookUpTable::alloc(module, basek, basek * rows, extension_factor); lut.set(module, &f, basek * rows); if to_exponent {
@@ -9,7 +9,8 @@ VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxDftAddInplace,
VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftCopy, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxDftToVecZnxBigTmpA, VecZnxFillUniform, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubABInplace, - VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare, ZnxView, ZnxViewMut, + VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, + ZnNormalizeInplace, ZnxView, ZnxViewMut, }, layouts::{Backend, Module, ScalarZnx, ScratchOwned}, oep::{@@ -80,7 +81,10 @@ + VecZnxAutomorphismInplace
+ VecZnxBigSubSmallBInplace<B> + VecZnxBigAllocBytes + VecZnxDftAddInplace<B> - + VecZnxRotate, + + VecZnxRotate + + ZnFillUniform + + ZnAddNormal + + ZnNormalizeInplace<B>, B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>@@ -258,7 +262,10 @@ + VecZnxAutomorphismInplace
+ VecZnxBigSubSmallBInplace<B> + VecZnxBigAllocBytes + VecZnxDftAddInplace<B> - + VecZnxRotate, + + VecZnxRotate + + ZnFillUniform + + ZnAddNormal + + ZnNormalizeInplace<B>, B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>