phantom-zone/src/lib.rs
src/lib.rsRaw
1use itertools::{izip, Itertools};
2use num::UnsignedInteger;
3use num_traits::{abs, Zero};
4use rand::CryptoRng;
5use utils::TryConvertFrom1;
6
7mod backend;
8mod bool;
9mod decomposer;
10mod lwe;
11mod multi_party;
12mod noise;
13mod ntt;
14mod num;
15mod random;
16mod rgsw;
17mod shortint;
18mod utils;
19
20pub trait Matrix: AsRef<[Self::R]> {
21 type MatElement;
22 type R: Row<Element = Self::MatElement>;
23
24 fn dimension(&self) -> (usize, usize);
25
26 fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
27 self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
28 }
29
30 fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
31 self.as_ref()[row_idx].as_ref()
32 }
33
34 fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
35 self.as_ref().iter().map(move |r| r)
36 }
37
38 fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
39 &self.as_ref()[row_idx].as_ref()[column_idx]
40 }
41
42 fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
43 self.as_ref().split_at(idx)
44 }
45
46 /// Does the matrix fit sub-matrix of dimension row x col
47 fn fits(&self, row: usize, col: usize) -> bool;
48}
49
50pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
51where
52 <Self as Matrix>::R: RowMut,
53{
54 fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
55 self.as_mut()[row_index].as_mut()
56 }
57
58 fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
59 self.as_mut().iter_mut().map(move |r| r)
60 }
61
62 fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
63 self.as_mut()[row_idx].as_mut()[column_idx] = val;
64 }
65
66 fn split_at_row_mut(
67 &mut self,
68 idx: usize,
69 ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
70 self.as_mut().split_at_mut(idx)
71 }
72}
73
74pub trait MatrixEntity: Matrix // where
75// <Self as Matrix>::MatElement: Zero,
76{
77 fn zeros(row: usize, col: usize) -> Self;
78}
79
80pub trait Row: AsRef<[Self::Element]> {
81 type Element;
82}
83
84pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
85
86pub trait RowEntity: Row {
87 fn zeros(col: usize) -> Self;
88}
89
90trait Secret {
91 type Element;
92 fn values(&self) -> &[Self::Element];
93}
94
95impl<T> Matrix for Vec<Vec<T>> {
96 type MatElement = T;
97 type R = Vec<T>;
98
99 fn dimension(&self) -> (usize, usize) {
100 (self.len(), self[0].len())
101 }
102
103 fn fits(&self, row: usize, col: usize) -> bool {
104 self.len() >= row && self[0].len() >= col
105 }
106}
107
108impl<T> Matrix for &[Vec<T>] {
109 type MatElement = T;
110 type R = Vec<T>;
111
112 fn dimension(&self) -> (usize, usize) {
113 (self.len(), self[0].len())
114 }
115
116 fn fits(&self, row: usize, col: usize) -> bool {
117 self.len() >= row && self[0].len() >= col
118 }
119}
120
121impl<T> Matrix for &mut [Vec<T>] {
122 type MatElement = T;
123 type R = Vec<T>;
124
125 fn dimension(&self) -> (usize, usize) {
126 (self.len(), self[0].len())
127 }
128
129 fn fits(&self, row: usize, col: usize) -> bool {
130 self.len() >= row && self[0].len() >= col
131 }
132}
133
134impl<T> MatrixMut for Vec<Vec<T>> {}
135impl<T> MatrixMut for &mut [Vec<T>] {}
136
137impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
138 fn zeros(row: usize, col: usize) -> Self {
139 vec![vec![T::zero(); col]; row]
140 }
141}
142
143impl<T> Row for Vec<T> {
144 type Element = T;
145}
146
147impl<T> Row for [T] {
148 type Element = T;
149}
150
151impl<T> RowMut for Vec<T> {}
152
153impl<T: Zero + Clone> RowEntity for Vec<T> {
154 fn zeros(col: usize) -> Self {
155 vec![T::zero(); col]
156 }
157}
158
159trait Encryptor<M, C> {
160 fn encrypt(&self, m: &M) -> C;
161}
162
163trait Decryptor<M, C: ?Sized> {
164 fn decrypt(&self, c: &C) -> M;
165}