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