use std::fmt; use rand_distr::{Distribution, Normal}; use crate::{ hal::{ api::{ TakeSlice, VecZnxBigAddDistF64, VecZnxBigFillDistF64, VecZnxBigNormalizeTmpBytes, ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut, }, layouts::{ Data, DataRef, Module, Scratch, VecZnx, VecZnxBig, VecZnxBigBytesOf, VecZnxBigOwned, VecZnxBigToMut, VecZnxBigToRef, VecZnxToMut, VecZnxToRef, }, oep::{ VecZnxBigAddDistF64Impl, VecZnxBigAddImpl, VecZnxBigAddInplaceImpl, VecZnxBigAddNormalImpl, VecZnxBigAddSmallImpl, VecZnxBigAddSmallInplaceImpl, VecZnxBigAllocBytesImpl, VecZnxBigAllocImpl, VecZnxBigAutomorphismImpl, VecZnxBigAutomorphismInplaceImpl, VecZnxBigFillDistF64Impl, VecZnxBigFillNormalImpl, VecZnxBigFromBytesImpl, VecZnxBigNegateInplaceImpl, VecZnxBigNormalizeImpl, VecZnxBigNormalizeTmpBytesImpl, VecZnxBigSubABInplaceImpl, VecZnxBigSubBAInplaceImpl, VecZnxBigSubImpl, VecZnxBigSubSmallAImpl, VecZnxBigSubSmallAInplaceImpl, VecZnxBigSubSmallBImpl, VecZnxBigSubSmallBInplaceImpl, }, source::Source, }, implementation::cpu_spqlios::{ffi::vec_znx, module_fft64::FFT64}, }; const VEC_ZNX_BIG_FFT64_WORDSIZE: usize = 1; impl ZnxView for VecZnxBig { type Scalar = i64; } impl VecZnxBigBytesOf for VecZnxBig { fn bytes_of(n: usize, cols: usize, size: usize) -> usize { VEC_ZNX_BIG_FFT64_WORDSIZE * n * cols * size * size_of::() } } impl ZnxSliceSize for VecZnxBig { fn sl(&self) -> usize { VEC_ZNX_BIG_FFT64_WORDSIZE * self.n() * self.cols() } } unsafe impl VecZnxBigAllocImpl for FFT64 { fn vec_znx_big_alloc_impl(n: usize, cols: usize, size: usize) -> VecZnxBigOwned { VecZnxBig::, FFT64>::new(n, cols, size) } } unsafe impl VecZnxBigFromBytesImpl for FFT64 { fn vec_znx_big_from_bytes_impl(n: usize, cols: usize, size: usize, bytes: Vec) -> VecZnxBigOwned { VecZnxBig::, FFT64>::new_from_bytes(n, cols, size, bytes) } } unsafe impl VecZnxBigAllocBytesImpl for FFT64 { fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { VecZnxBig::, FFT64>::bytes_of(n, cols, size) } } unsafe impl VecZnxBigAddDistF64Impl for FFT64 { fn add_dist_f64_impl, D: Distribution>( _module: &Module, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source, dist: D, bound: f64, ) { let mut res: VecZnxBig<&mut [u8], FFT64> = 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 { res.at_mut(res_col, limb).iter_mut().for_each(|x| { let mut dist_f64: f64 = dist.sample(source); while dist_f64.abs() > bound { dist_f64 = dist.sample(source) } *x += (dist_f64.round() as i64) << basek_rem; }); } else { res.at_mut(res_col, limb).iter_mut().for_each(|x| { let mut dist_f64: f64 = dist.sample(source); while dist_f64.abs() > bound { dist_f64 = dist.sample(source) } *x += dist_f64.round() as i64 }); } } } unsafe impl VecZnxBigAddNormalImpl for FFT64 { fn add_normal_impl>( module: &Module, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source, sigma: f64, bound: f64, ) { module.vec_znx_big_add_dist_f64( basek, res, res_col, k, source, Normal::new(0.0, sigma).unwrap(), bound, ); } } unsafe impl VecZnxBigFillDistF64Impl for FFT64 { fn fill_dist_f64_impl, D: Distribution>( _module: &Module, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source, dist: D, bound: f64, ) { let mut res: VecZnxBig<&mut [u8], FFT64> = 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 { res.at_mut(res_col, limb).iter_mut().for_each(|x| { let mut dist_f64: f64 = dist.sample(source); while dist_f64.abs() > bound { dist_f64 = dist.sample(source) } *x = (dist_f64.round() as i64) << basek_rem; }); } else { res.at_mut(res_col, limb).iter_mut().for_each(|x| { let mut dist_f64: f64 = dist.sample(source); while dist_f64.abs() > bound { dist_f64 = dist.sample(source) } *x = dist_f64.round() as i64 }); } } } unsafe impl VecZnxBigFillNormalImpl for FFT64 { fn fill_normal_impl>( module: &Module, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source, sigma: f64, bound: f64, ) { module.vec_znx_big_fill_dist_f64( basek, res, res_col, k, source, Normal::new(0.0, sigma).unwrap(), bound, ); } } unsafe impl VecZnxBigAddImpl for FFT64 { /// Adds `a` to `b` and stores the result on `c`. fn vec_znx_big_add_impl( module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize, ) where R: VecZnxBigToMut, A: VecZnxBigToRef, B: VecZnxBigToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let b: VecZnxBig<&[u8], FFT64> = b.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); assert_eq!(b.n(), res.n()); assert_ne!(a.as_ptr(), b.as_ptr()); } unsafe { vec_znx::vec_znx_add( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, b.at_ptr(b_col, 0), b.size() as u64, b.sl() as u64, ) } } } unsafe impl VecZnxBigAddInplaceImpl for FFT64 { /// Adds `a` to `b` and stores the result on `b`. fn vec_znx_big_add_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxBigToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); } unsafe { vec_znx::vec_znx_add( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, res.at_ptr(res_col, 0), res.size() as u64, res.sl() as u64, ) } } } unsafe impl VecZnxBigAddSmallImpl for FFT64 { /// Adds `a` to `b` and stores the result on `c`. fn vec_znx_big_add_small_impl( module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize, ) where R: VecZnxBigToMut, A: VecZnxBigToRef, B: VecZnxToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let b: VecZnx<&[u8]> = b.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); assert_eq!(b.n(), res.n()); assert_ne!(a.as_ptr(), b.as_ptr()); } unsafe { vec_znx::vec_znx_add( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, b.at_ptr(b_col, 0), b.size() as u64, b.sl() as u64, ) } } } unsafe impl VecZnxBigAddSmallInplaceImpl for FFT64 { /// Adds `a` to `b` and stores the result on `b`. fn vec_znx_big_add_small_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxToRef, { let a: VecZnx<&[u8]> = a.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); } unsafe { vec_znx::vec_znx_add( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, res.at_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, ) } } } unsafe impl VecZnxBigSubImpl for FFT64 { /// Subtracts `a` to `b` and stores the result on `c`. fn vec_znx_big_sub_impl( module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize, ) where R: VecZnxBigToMut, A: VecZnxBigToRef, B: VecZnxBigToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let b: VecZnxBig<&[u8], FFT64> = b.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); assert_eq!(b.n(), res.n()); assert_ne!(a.as_ptr(), b.as_ptr()); } unsafe { vec_znx::vec_znx_sub( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, b.at_ptr(b_col, 0), b.size() as u64, b.sl() as u64, ) } } } unsafe impl VecZnxBigSubABInplaceImpl for FFT64 { /// Subtracts `a` from `b` and stores the result on `b`. fn vec_znx_big_sub_ab_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxBigToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); } unsafe { vec_znx::vec_znx_sub( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, res.at_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, ) } } } unsafe impl VecZnxBigSubBAInplaceImpl for FFT64 { /// Subtracts `b` from `a` and stores the result on `b`. fn vec_znx_big_sub_ba_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxBigToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); } unsafe { vec_znx::vec_znx_sub( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, res.at_ptr(res_col, 0), res.size() as u64, res.sl() as u64, ) } } } unsafe impl VecZnxBigSubSmallAImpl for FFT64 { /// Subtracts `b` from `a` and stores the result on `c`. fn vec_znx_big_sub_small_a_impl( module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize, ) where R: VecZnxBigToMut, A: VecZnxToRef, B: VecZnxBigToRef, { let a: VecZnx<&[u8]> = a.to_ref(); let b: VecZnxBig<&[u8], FFT64> = b.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); assert_eq!(b.n(), res.n()); assert_ne!(a.as_ptr(), b.as_ptr()); } unsafe { vec_znx::vec_znx_sub( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, b.at_ptr(b_col, 0), b.size() as u64, b.sl() as u64, ) } } } unsafe impl VecZnxBigSubSmallAInplaceImpl for FFT64 { /// Subtracts `a` from `res` and stores the result on `res`. fn vec_znx_big_sub_small_a_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxToRef, { let a: VecZnx<&[u8]> = a.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); } unsafe { vec_znx::vec_znx_sub( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, res.at_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, ) } } } unsafe impl VecZnxBigSubSmallBImpl for FFT64 { /// Subtracts `b` from `a` and stores the result on `c`. fn vec_znx_big_sub_small_b_impl( module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize, ) where R: VecZnxBigToMut, A: VecZnxBigToRef, B: VecZnxToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let b: VecZnx<&[u8]> = b.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); assert_eq!(b.n(), res.n()); assert_ne!(a.as_ptr(), b.as_ptr()); } unsafe { vec_znx::vec_znx_sub( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, b.at_ptr(b_col, 0), b.size() as u64, b.sl() as u64, ) } } } unsafe impl VecZnxBigSubSmallBInplaceImpl for FFT64 { /// Subtracts `res` from `a` and stores the result on `res`. fn vec_znx_big_sub_small_b_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxToRef, { let a: VecZnx<&[u8]> = a.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); } unsafe { vec_znx::vec_znx_sub( module.ptr(), res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, res.at_ptr(res_col, 0), res.size() as u64, res.sl() as u64, ) } } } unsafe impl VecZnxBigNegateInplaceImpl for FFT64 { fn vec_znx_big_negate_inplace_impl(module: &Module, a: &mut A, a_col: usize) where A: VecZnxBigToMut, { let mut a: VecZnxBig<&mut [u8], FFT64> = a.to_mut(); unsafe { vec_znx::vec_znx_negate( module.ptr(), 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, ) } } } unsafe impl VecZnxBigNormalizeTmpBytesImpl for FFT64 { fn vec_znx_big_normalize_tmp_bytes_impl(module: &Module, n: usize) -> usize { unsafe { vec_znx::vec_znx_normalize_base2k_tmp_bytes(module.ptr(), n as u64) as usize } } } unsafe impl VecZnxBigNormalizeImpl for FFT64 { fn vec_znx_big_normalize_impl( module: &Module, basek: usize, res: &mut R, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch, ) where R: VecZnxToMut, A: VecZnxBigToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let mut res: VecZnx<&mut [u8]> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(res.n(), a.n()); } let (tmp_bytes, _) = scratch.take_slice(module.vec_znx_big_normalize_tmp_bytes(a.n())); 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, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, tmp_bytes.as_mut_ptr(), ); } } } unsafe impl VecZnxBigAutomorphismImpl for FFT64 { /// Applies the automorphism X^i -> X^ik on `a` and stores the result on `b`. fn vec_znx_big_automorphism_impl(module: &Module, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxBigToRef, { let a: VecZnxBig<&[u8], FFT64> = a.to_ref(); let mut res: VecZnxBig<&mut [u8], FFT64> = res.to_mut(); #[cfg(debug_assertions)] { assert_eq!(a.n(), res.n()); } unsafe { vec_znx::vec_znx_automorphism( module.ptr(), k, res.at_mut_ptr(res_col, 0), res.size() as u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, ) } } } unsafe impl VecZnxBigAutomorphismInplaceImpl for FFT64 { /// Applies the automorphism X^i -> X^ik on `a` and stores the result on `a`. fn vec_znx_big_automorphism_inplace_impl(module: &Module, k: i64, a: &mut A, a_col: usize) where A: VecZnxBigToMut, { let mut a: VecZnxBig<&mut [u8], FFT64> = a.to_mut(); unsafe { vec_znx::vec_znx_automorphism( module.ptr(), k, 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, ) } } } impl fmt::Display for VecZnxBig { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!( f, "VecZnxBig(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(()) } }