phantom-zone/src/lib.rs
src/lib.rsRaw
1use itertools::{izip, Itertools};
2use num::UnsignedInteger;
3use num_traits::{abs, Zero};
4use rand::CryptoRng;
5use random::{RandomGaussianDist, RandomUniformDist};
6use utils::TryConvertFrom;
7
8mod backend;
9mod decomposer;
10mod lwe;
11mod ntt;
12mod num;
13mod random;
14mod rgsw;
15mod utils;
16pub trait Matrix: AsRef<[Self::R]> {
17 type MatElement;
18 type R: Row<Element = Self::MatElement>;
19
20 fn dimension(&self) -> (usize, usize);
21
22 fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
23 self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
24 }
25
26 fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
27 self.as_ref()[row_idx].as_ref()
28 }
29
30 fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
31 self.as_ref().iter().map(move |r| r)
32 }
33
34 fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
35 &self.as_ref()[row_idx].as_ref()[column_idx]
36 }
37}
38
39pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
40where
41 <Self as Matrix>::R: RowMut,
42{
43 fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
44 self.as_mut()[row_index].as_mut()
45 }
46
47 fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
48 self.as_mut().iter_mut().map(move |r| r)
49 }
50
51 fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
52 self.as_mut()[row_idx].as_mut()[column_idx] = val;
53 }
54
55 fn split_at_row(
56 &mut self,
57 idx: usize,
58 ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
59 self.as_mut().split_at_mut(idx)
60 }
61}
62
63pub trait MatrixEntity: Matrix // where
64// <Self as Matrix>::MatElement: Zero,
65{
66 fn zeros(row: usize, col: usize) -> Self;
67}
68
69pub trait Row: AsRef<[Self::Element]> {
70 type Element;
71}
72
73pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
74
75trait Secret {
76 type Element;
77 fn values(&self) -> &[Self::Element];
78}
79
80impl<T> Matrix for Vec<Vec<T>> {
81 type MatElement = T;
82 type R = Vec<T>;
83
84 fn dimension(&self) -> (usize, usize) {
85 (self.len(), self[0].len())
86 }
87}
88
89impl<T> MatrixMut for Vec<Vec<T>> {}
90
91impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
92 fn zeros(row: usize, col: usize) -> Self {
93 vec![vec![T::zero(); col]; row]
94 }
95}
96
97impl<T> Row for Vec<T> {
98 type Element = T;
99}
100
101impl<T> RowMut for Vec<T> {}