use crate::FFT64Ref; use poulpy_hal::{ api::{TakeSlice, VecZnxBigAutomorphismInplaceTmpBytes, VecZnxBigNormalizeTmpBytes}, layouts::{ Backend, Module, Scratch, VecZnx, VecZnxBig, VecZnxBigOwned, VecZnxBigToMut, VecZnxBigToRef, VecZnxToMut, VecZnxToRef, ZnxInfos, ZnxView, ZnxViewMut, }, oep::{ TakeSliceImpl, VecZnxBigAddImpl, VecZnxBigAddInplaceImpl, VecZnxBigAddNormalImpl, VecZnxBigAddSmallImpl, VecZnxBigAddSmallInplaceImpl, VecZnxBigAllocBytesImpl, VecZnxBigAllocImpl, VecZnxBigAutomorphismImpl, VecZnxBigAutomorphismInplaceImpl, VecZnxBigAutomorphismInplaceTmpBytesImpl, VecZnxBigFromBytesImpl, VecZnxBigFromSmallImpl, VecZnxBigNegateImpl, VecZnxBigNegateInplaceImpl, VecZnxBigNormalizeImpl, VecZnxBigNormalizeTmpBytesImpl, VecZnxBigSubImpl, VecZnxBigSubInplaceImpl, VecZnxBigSubNegateInplaceImpl, VecZnxBigSubSmallAImpl, VecZnxBigSubSmallBImpl, VecZnxBigSubSmallInplaceImpl, VecZnxBigSubSmallNegateInplaceImpl, }, reference::{ fft64::vec_znx_big::{ vec_znx_big_add, vec_znx_big_add_inplace, vec_znx_big_add_normal_ref, vec_znx_big_add_small, vec_znx_big_add_small_inplace, vec_znx_big_automorphism, vec_znx_big_automorphism_inplace, vec_znx_big_automorphism_inplace_tmp_bytes, vec_znx_big_negate, vec_znx_big_negate_inplace, vec_znx_big_normalize, vec_znx_big_normalize_tmp_bytes, vec_znx_big_sub, vec_znx_big_sub_inplace, vec_znx_big_sub_negate_inplace, vec_znx_big_sub_small_a, vec_znx_big_sub_small_a_inplace, vec_znx_big_sub_small_b, vec_znx_big_sub_small_b_inplace, }, znx::{znx_copy_ref, znx_zero_ref}, }, source::Source, }; unsafe impl VecZnxBigAllocBytesImpl for FFT64Ref { fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::() } } unsafe impl VecZnxBigAllocImpl for FFT64Ref { fn vec_znx_big_alloc_impl(n: usize, cols: usize, size: usize) -> VecZnxBigOwned { VecZnxBig::alloc(n, cols, size) } } unsafe impl VecZnxBigFromBytesImpl for FFT64Ref { fn vec_znx_big_from_bytes_impl(n: usize, cols: usize, size: usize, bytes: Vec) -> VecZnxBigOwned { VecZnxBig::from_bytes(n, cols, size, bytes) } } unsafe impl VecZnxBigFromSmallImpl for FFT64Ref { fn vec_znx_big_from_small_impl(res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxToRef, { let mut res: VecZnxBig<&mut [u8], FFT64Ref> = res.to_mut(); let a: VecZnx<&[u8]> = a.to_ref(); #[cfg(debug_assertions)] { assert_eq!(res.n(), a.n()); } let res_size: usize = res.size(); let a_size: usize = a.size(); let min_size: usize = res_size.min(a_size); for j in 0..min_size { znx_copy_ref(res.at_mut(res_col, j), a.at(a_col, j)); } for j in min_size..res_size { znx_zero_ref(res.at_mut(res_col, j)); } } } unsafe impl VecZnxBigAddNormalImpl for FFT64Ref { fn add_normal_impl>( _module: &Module, base2k: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source, sigma: f64, bound: f64, ) { vec_znx_big_add_normal_ref(base2k, res, res_col, k, sigma, bound, source); } } unsafe impl VecZnxBigAddImpl for FFT64Ref { /// 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, { vec_znx_big_add(res, res_col, a, a_col, b, b_col); } } unsafe impl VecZnxBigAddInplaceImpl for FFT64Ref { /// 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, { vec_znx_big_add_inplace(res, res_col, a, a_col); } } unsafe impl VecZnxBigAddSmallImpl for FFT64Ref { /// 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, { vec_znx_big_add_small(res, res_col, a, a_col, b, b_col); } } unsafe impl VecZnxBigAddSmallInplaceImpl for FFT64Ref { /// 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, { vec_znx_big_add_small_inplace(res, res_col, a, a_col); } } unsafe impl VecZnxBigSubImpl for FFT64Ref { /// 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, { vec_znx_big_sub(res, res_col, a, a_col, b, b_col); } } unsafe impl VecZnxBigSubInplaceImpl for FFT64Ref { /// Subtracts `a` from `b` and stores the result on `b`. fn vec_znx_big_sub_inplace_impl(_module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxBigToRef, { vec_znx_big_sub_inplace(res, res_col, a, a_col); } } unsafe impl VecZnxBigSubNegateInplaceImpl for FFT64Ref { /// Subtracts `b` from `a` and stores the result on `b`. fn vec_znx_big_sub_negate_inplace_impl(_module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxBigToRef, { vec_znx_big_sub_negate_inplace(res, res_col, a, a_col); } } unsafe impl VecZnxBigSubSmallAImpl for FFT64Ref { /// 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, { vec_znx_big_sub_small_a(res, res_col, a, a_col, b, b_col); } } unsafe impl VecZnxBigSubSmallInplaceImpl for FFT64Ref { /// Subtracts `a` from `res` and stores the result on `res`. fn vec_znx_big_sub_small_inplace_impl(_module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxToRef, { vec_znx_big_sub_small_a_inplace(res, res_col, a, a_col); } } unsafe impl VecZnxBigSubSmallBImpl for FFT64Ref { /// 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, { vec_znx_big_sub_small_b(res, res_col, a, a_col, b, b_col); } } unsafe impl VecZnxBigSubSmallNegateInplaceImpl for FFT64Ref { /// Subtracts `res` from `a` and stores the result on `res`. fn vec_znx_big_sub_small_negate_inplace_impl(_module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxToRef, { vec_znx_big_sub_small_b_inplace(res, res_col, a, a_col); } } unsafe impl VecZnxBigNegateImpl for FFT64Ref { fn vec_znx_big_negate_impl(_module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxBigToRef, { vec_znx_big_negate(res, res_col, a, a_col); } } unsafe impl VecZnxBigNegateInplaceImpl for FFT64Ref { fn vec_znx_big_negate_inplace_impl(_module: &Module, res: &mut R, res_col: usize) where R: VecZnxBigToMut, { vec_znx_big_negate_inplace(res, res_col); } } unsafe impl VecZnxBigNormalizeTmpBytesImpl for FFT64Ref { fn vec_znx_big_normalize_tmp_bytes_impl(module: &Module) -> usize { vec_znx_big_normalize_tmp_bytes(module.n()) } } unsafe impl VecZnxBigNormalizeImpl for FFT64Ref where Self: TakeSliceImpl, { fn vec_znx_big_normalize_impl( module: &Module, res: &mut R, res_base2k: usize, res_offset: i64, res_col: usize, a: &A, a_base2k: usize, a_col: usize, scratch: &mut Scratch, ) where R: VecZnxToMut, A: VecZnxBigToRef, { let (carry, _) = scratch.take_slice(module.vec_znx_big_normalize_tmp_bytes() / size_of::()); vec_znx_big_normalize(res, res_base2k, res_offset, res_col, a, a_base2k, a_col, carry); } } unsafe impl VecZnxBigAutomorphismImpl for FFT64Ref { /// Applies the automorphism X^i -> X^ik on `a` and stores the result on `b`. fn vec_znx_big_automorphism_impl(_module: &Module, p: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxBigToMut, A: VecZnxBigToRef, { vec_znx_big_automorphism(p, res, res_col, a, a_col); } } unsafe impl VecZnxBigAutomorphismInplaceTmpBytesImpl for FFT64Ref { fn vec_znx_big_automorphism_inplace_tmp_bytes_impl(module: &Module) -> usize { vec_znx_big_automorphism_inplace_tmp_bytes(module.n()) } } unsafe impl VecZnxBigAutomorphismInplaceImpl for FFT64Ref where Module: VecZnxBigAutomorphismInplaceTmpBytes, { /// Applies the automorphism X^i -> X^ik on `a` and stores the result on `a`. fn vec_znx_big_automorphism_inplace_impl( module: &Module, p: i64, res: &mut R, res_col: usize, scratch: &mut Scratch, ) where R: VecZnxBigToMut, { let (tmp, _) = scratch.take_slice(module.vec_znx_big_automorphism_inplace_tmp_bytes() / size_of::()); vec_znx_big_automorphism_inplace(p, res, res_col, tmp); } }