arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-hal/src/layouts/znx_base.rs
poulpy-hal/src/layouts/znx_base.rsRaw
  1use std::fmt::{Debug, Display};
  2
  3use crate::{
  4    layouts::{Backend, Data, DataMut, DataRef},
  5    source::Source,
  6};
  7use bytemuck::Pod;
  8use rand_distr::num_traits::Zero;
  9
 10pub trait ZnxInfos {
 11    /// Returns the ring degree of the polynomials.
 12    fn n(&self) -> usize;
 13
 14    /// Returns the base two logarithm of the ring dimension of the polynomials.
 15    fn log_n(&self) -> usize {
 16        (usize::BITS - (self.n() - 1).leading_zeros()) as _
 17    }
 18
 19    /// Returns the number of rows.
 20    fn rows(&self) -> usize;
 21
 22    /// Returns the number of polynomials in each row.
 23    fn cols(&self) -> usize;
 24
 25    /// Returns the number of size per polynomial.
 26    fn size(&self) -> usize;
 27
 28    /// Returns the total number of small polynomials.
 29    fn poly_count(&self) -> usize {
 30        self.rows() * self.cols() * self.size()
 31    }
 32}
 33
 34pub trait ZnxSliceSizeImpl<B: Backend> {
 35    fn slice_size(&self) -> usize;
 36}
 37
 38pub trait ZnxSliceSize {
 39    /// Returns the slice size, which is the offset between
 40    /// two size of the same column.
 41    fn sl(&self) -> usize;
 42}
 43
 44pub trait DataView {
 45    type D: Data;
 46    fn data(&self) -> &Self::D;
 47}
 48
 49pub trait DataViewMut: DataView {
 50    fn data_mut(&mut self) -> &mut Self::D;
 51}
 52
 53pub trait ZnxView: ZnxInfos + DataView<D: DataRef> {
 54    type Scalar: Copy + Zero + Display + Debug + Pod;
 55
 56    /// Returns a non-mutable pointer to the underlying coefficients array.
 57    fn as_ptr(&self) -> *const Self::Scalar {
 58        self.data().as_ref().as_ptr() as *const Self::Scalar
 59    }
 60
 61    /// Returns a non-mutable reference to the entire underlying coefficient array.
 62    fn raw(&self) -> &[Self::Scalar] {
 63        unsafe { std::slice::from_raw_parts(self.as_ptr(), self.n() * self.poly_count()) }
 64    }
 65
 66    /// Returns a non-mutable pointer starting at the j-th small polynomial of the i-th column.
 67    fn at_ptr(&self, i: usize, j: usize) -> *const Self::Scalar {
 68        assert!(i < self.cols(), "cols: {} >= self.cols(): {}", i, self.cols());
 69        assert!(j < self.size(), "size: {} >= self.size(): {}", j, self.size());
 70        let offset: usize = self.n() * (j * self.cols() + i);
 71        unsafe { self.as_ptr().add(offset) }
 72    }
 73
 74    /// Returns non-mutable reference to the (i, j)-th small polynomial.
 75    fn at(&self, i: usize, j: usize) -> &[Self::Scalar] {
 76        unsafe { std::slice::from_raw_parts(self.at_ptr(i, j), self.n()) }
 77    }
 78}
 79
 80pub trait ZnxViewMut: ZnxView + DataViewMut<D: DataMut> {
 81    /// Returns a mutable pointer to the underlying coefficients array.
 82    fn as_mut_ptr(&mut self) -> *mut Self::Scalar {
 83        self.data_mut().as_mut().as_mut_ptr() as *mut Self::Scalar
 84    }
 85
 86    /// Returns a mutable reference to the entire underlying coefficient array.
 87    fn raw_mut(&mut self) -> &mut [Self::Scalar] {
 88        unsafe { std::slice::from_raw_parts_mut(self.as_mut_ptr(), self.n() * self.poly_count()) }
 89    }
 90
 91    /// Returns a mutable pointer starting at the j-th small polynomial of the i-th column.
 92    fn at_mut_ptr(&mut self, i: usize, j: usize) -> *mut Self::Scalar {
 93        assert!(i < self.cols(), "cols: {} >= self.cols(): {}", i, self.cols());
 94        assert!(j < self.size(), "size: {} >= self.size(): {}", j, self.size());
 95        let offset: usize = self.n() * (j * self.cols() + i);
 96        unsafe { self.as_mut_ptr().add(offset) }
 97    }
 98
 99    /// Returns mutable reference to the (i, j)-th small polynomial.
100    fn at_mut(&mut self, i: usize, j: usize) -> &mut [Self::Scalar] {
101        unsafe { std::slice::from_raw_parts_mut(self.at_mut_ptr(i, j), self.n()) }
102    }
103}
104
105//(Jay)Note: Can't provide blanket impl. of ZnxView because Scalar is not known
106impl<T> ZnxViewMut for T where T: ZnxView + DataViewMut<D: DataMut> {}
107
108pub trait ZnxZero
109where
110    Self: Sized,
111{
112    fn zero(&mut self);
113    fn zero_at(&mut self, i: usize, j: usize);
114}
115
116pub trait FillUniform {
117    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source);
118}