arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/backend/src/implementation/cpu_spqlios/scalar_znx.rs
backend/src/implementation/cpu_spqlios/scalar_znx.rsRaw
  1use crate::{
  2    hal::{
  3        api::{ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut},
  4        layouts::{Backend, Module, ScalarZnx, ScalarZnxOwned, ScalarZnxToMut, ScalarZnxToRef},
  5        oep::{
  6            ScalarZnxAllocBytesImpl, ScalarZnxAllocImpl, ScalarZnxAutomorphismImpl, ScalarZnxAutomorphismInplaceIml,
  7            ScalarZnxFromBytesImpl,
  8        },
  9    },
 10    implementation::cpu_spqlios::{
 11        CPUAVX,
 12        ffi::{module::module_info_t, vec_znx},
 13    },
 14};
 15
 16unsafe impl<B: Backend> ScalarZnxAllocBytesImpl<B> for B
 17where
 18    B: CPUAVX,
 19{
 20    fn scalar_znx_alloc_bytes_impl(n: usize, cols: usize) -> usize {
 21        ScalarZnxOwned::bytes_of(n, cols)
 22    }
 23}
 24
 25unsafe impl<B: Backend> ScalarZnxAllocImpl<B> for B
 26where
 27    B: CPUAVX,
 28{
 29    fn scalar_znx_alloc_impl(n: usize, cols: usize) -> ScalarZnxOwned {
 30        ScalarZnxOwned::new(n, cols)
 31    }
 32}
 33
 34unsafe impl<B: Backend> ScalarZnxFromBytesImpl<B> for B
 35where
 36    B: CPUAVX,
 37{
 38    fn scalar_znx_from_bytes_impl(n: usize, cols: usize, bytes: Vec<u8>) -> ScalarZnxOwned {
 39        ScalarZnxOwned::new_from_bytes(n, cols, bytes)
 40    }
 41}
 42
 43unsafe impl<B: Backend> ScalarZnxAutomorphismImpl<B> for B
 44where
 45    B: CPUAVX,
 46{
 47    fn scalar_znx_automorphism_impl<R, A>(module: &Module<B>, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
 48    where
 49        R: ScalarZnxToMut,
 50        A: ScalarZnxToRef,
 51    {
 52        let a: ScalarZnx<&[u8]> = a.to_ref();
 53        let mut res: ScalarZnx<&mut [u8]> = res.to_mut();
 54        #[cfg(debug_assertions)]
 55        {
 56            assert_eq!(a.n(), module.n());
 57            assert_eq!(res.n(), module.n());
 58        }
 59        unsafe {
 60            vec_znx::vec_znx_automorphism(
 61                module.ptr() as *const module_info_t,
 62                k,
 63                res.at_mut_ptr(res_col, 0),
 64                res.size() as u64,
 65                res.sl() as u64,
 66                a.at_ptr(a_col, 0),
 67                a.size() as u64,
 68                a.sl() as u64,
 69            )
 70        }
 71    }
 72}
 73
 74unsafe impl<B: Backend> ScalarZnxAutomorphismInplaceIml<B> for B
 75where
 76    B: CPUAVX,
 77{
 78    fn scalar_znx_automorphism_inplace_impl<A>(module: &Module<B>, k: i64, a: &mut A, a_col: usize)
 79    where
 80        A: ScalarZnxToMut,
 81    {
 82        let mut a: ScalarZnx<&mut [u8]> = a.to_mut();
 83        #[cfg(debug_assertions)]
 84        {
 85            assert_eq!(a.n(), module.n());
 86        }
 87        unsafe {
 88            vec_znx::vec_znx_automorphism(
 89                module.ptr() as *const module_info_t,
 90                k,
 91                a.at_mut_ptr(a_col, 0),
 92                a.size() as u64,
 93                a.sl() as u64,
 94                a.at_ptr(a_col, 0),
 95                a.size() as u64,
 96                a.sl() as u64,
 97            )
 98        }
 99    }
100}