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 bool;
10mod decomposer;
11mod lwe;
12mod ntt;
13mod num;
14mod random;
15mod rgsw;
16mod utils;
17pub trait Matrix: AsRef<[Self::R]> {
18 type MatElement;
19 type R: Row<Element = Self::MatElement>;
20
21 fn dimension(&self) -> (usize, usize);
22
23 fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
24 self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
25 }
26
27 fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
28 self.as_ref()[row_idx].as_ref()
29 }
30
31 fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
32 self.as_ref().iter().map(move |r| r)
33 }
34
35 fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
36 &self.as_ref()[row_idx].as_ref()[column_idx]
37 }
38
39 fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
40 self.as_ref().split_at(idx)
41 }
42}
43
44pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
45where
46 <Self as Matrix>::R: RowMut,
47{
48 fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
49 self.as_mut()[row_index].as_mut()
50 }
51
52 fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
53 self.as_mut().iter_mut().map(move |r| r)
54 }
55
56 fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
57 self.as_mut()[row_idx].as_mut()[column_idx] = val;
58 }
59
60 fn split_at_row_mut(
61 &mut self,
62 idx: usize,
63 ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
64 self.as_mut().split_at_mut(idx)
65 }
66}
67
68pub trait MatrixEntity: Matrix // where
69// <Self as Matrix>::MatElement: Zero,
70{
71 fn zeros(row: usize, col: usize) -> Self;
72}
73
74pub trait Row: AsRef<[Self::Element]> {
75 type Element;
76}
77
78pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
79
80trait Secret {
81 type Element;
82 fn values(&self) -> &[Self::Element];
83}
84
85impl<T> Matrix for Vec<Vec<T>> {
86 type MatElement = T;
87 type R = Vec<T>;
88
89 fn dimension(&self) -> (usize, usize) {
90 (self.len(), self[0].len())
91 }
92}
93
94impl<T> Matrix for &[Vec<T>] {
95 type MatElement = T;
96 type R = Vec<T>;
97
98 fn dimension(&self) -> (usize, usize) {
99 (self.len(), self[0].len())
100 }
101}
102
103impl<T> Matrix for &mut [Vec<T>] {
104 type MatElement = T;
105 type R = Vec<T>;
106
107 fn dimension(&self) -> (usize, usize) {
108 (self.len(), self[0].len())
109 }
110}
111
112impl<T> MatrixMut for Vec<Vec<T>> {}
113impl<T> MatrixMut for &mut [Vec<T>] {}
114
115impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
116 fn zeros(row: usize, col: usize) -> Self {
117 vec![vec![T::zero(); col]; row]
118 }
119}
120
121impl<T> Row for Vec<T> {
122 type Element = T;
123}
124
125impl<T> RowMut for Vec<T> {}