poulpy/base2k/src/commons.rs
base2k/src/commons.rsRaw
1use crate::{Backend, Module};
2
3pub trait ZnxInfos {
4 /// Returns the ring degree of the polynomials.
5 fn n(&self) -> usize;
6
7 /// Returns the base two logarithm of the ring dimension of the polynomials.
8 fn log_n(&self) -> usize;
9
10 /// Returns the number of rows.
11 fn rows(&self) -> usize;
12
13 /// Returns the number of polynomials in each row.
14 fn cols(&self) -> usize;
15
16 /// Returns the number of limbs per polynomial.
17 fn limbs(&self) -> usize;
18
19 /// Returns the total number of small polynomials.
20 fn poly_count(&self) -> usize;
21}
22
23pub trait ZnxBase<B: Backend> {
24 type Scalar;
25 fn new(module: &Module<B>, cols: usize, limbs: usize) -> Self;
26 fn from_bytes(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self;
27 fn from_bytes_borrow(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self;
28 fn bytes_of(module: &Module<B>, cols: usize, limbs: usize) -> usize;
29}
30
31pub trait ZnxLayout: ZnxInfos {
32 type Scalar;
33
34 /// Returns a non-mutable pointer to the underlying coefficients array.
35 fn as_ptr(&self) -> *const Self::Scalar;
36
37 /// Returns a mutable pointer to the underlying coefficients array.
38 fn as_mut_ptr(&mut self) -> *mut Self::Scalar;
39
40 /// Returns a non-mutable reference to the entire underlying coefficient array.
41 fn raw(&self) -> &[Self::Scalar] {
42 unsafe { std::slice::from_raw_parts(self.as_ptr(), self.n() * self.poly_count()) }
43 }
44
45 /// Returns a mutable reference to the entire underlying coefficient array.
46 fn raw_mut(&mut self) -> &mut [Self::Scalar] {
47 unsafe { std::slice::from_raw_parts_mut(self.as_mut_ptr(), self.n() * self.poly_count()) }
48 }
49
50 /// Returns a non-mutable pointer starting at the (i, j)-th small polynomial.
51 fn at_ptr(&self, i: usize, j: usize) -> *const Self::Scalar {
52 #[cfg(debug_assertions)]
53 {
54 assert!(i < self.cols());
55 assert!(j < self.limbs());
56 }
57 let offset = self.n() * (j * self.cols() + i);
58 unsafe { self.as_ptr().add(offset) }
59 }
60
61 /// Returns a mutable pointer starting at the (i, j)-th small polynomial.
62 fn at_mut_ptr(&mut self, i: usize, j: usize) -> *mut Self::Scalar {
63 #[cfg(debug_assertions)]
64 {
65 assert!(i < self.cols());
66 assert!(j < self.limbs());
67 }
68 let offset = self.n() * (j * self.cols() + i);
69 unsafe { self.as_mut_ptr().add(offset) }
70 }
71
72 /// Returns non-mutable reference to the (i, j)-th small polynomial.
73 fn at_poly(&self, i: usize, j: usize) -> &[Self::Scalar] {
74 unsafe { std::slice::from_raw_parts(self.at_ptr(i, j), self.n()) }
75 }
76
77 /// Returns mutable reference to the (i, j)-th small polynomial.
78 fn at_poly_mut(&mut self, i: usize, j: usize) -> &mut [Self::Scalar] {
79 unsafe { std::slice::from_raw_parts_mut(self.at_mut_ptr(i, j), self.n()) }
80 }
81
82 /// Returns non-mutable reference to the i-th limb.
83 fn at_limb(&self, j: usize) -> &[Self::Scalar] {
84 unsafe { std::slice::from_raw_parts(self.at_ptr(0, j), self.n() * self.cols()) }
85 }
86
87 /// Returns mutable reference to the i-th limb.
88 fn at_limb_mut(&mut self, j: usize) -> &mut [Self::Scalar] {
89 unsafe { std::slice::from_raw_parts_mut(self.at_mut_ptr(0, j), self.n() * self.cols()) }
90 }
91}