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 utils;
18
19pub trait Matrix: AsRef<[Self::R]> {
20 type MatElement;
21 type R: Row<Element = Self::MatElement>;
22
23 fn dimension(&self) -> (usize, usize);
24
25 fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
26 self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
27 }
28
29 fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
30 self.as_ref()[row_idx].as_ref()
31 }
32
33 fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
34 self.as_ref().iter().map(move |r| r)
35 }
36
37 fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
38 &self.as_ref()[row_idx].as_ref()[column_idx]
39 }
40
41 fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
42 self.as_ref().split_at(idx)
43 }
44
45 /// Does the matrix fit sub-matrix of dimension row x col
46 fn fits(&self, row: usize, col: usize) -> bool;
47}
48
49pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
50where
51 <Self as Matrix>::R: RowMut,
52{
53 fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
54 self.as_mut()[row_index].as_mut()
55 }
56
57 fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
58 self.as_mut().iter_mut().map(move |r| r)
59 }
60
61 fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
62 self.as_mut()[row_idx].as_mut()[column_idx] = val;
63 }
64
65 fn split_at_row_mut(
66 &mut self,
67 idx: usize,
68 ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
69 self.as_mut().split_at_mut(idx)
70 }
71}
72
73pub trait MatrixEntity: Matrix // where
74// <Self as Matrix>::MatElement: Zero,
75{
76 fn zeros(row: usize, col: usize) -> Self;
77}
78
79pub trait Row: AsRef<[Self::Element]> {
80 type Element;
81}
82
83pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
84
85pub trait RowEntity: Row {
86 fn zeros(col: usize) -> Self;
87}
88
89trait Secret {
90 type Element;
91 fn values(&self) -> &[Self::Element];
92}
93
94impl<T> Matrix for Vec<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 fn fits(&self, row: usize, col: usize) -> bool {
103 self.len() >= row && self[0].len() >= col
104 }
105}
106
107impl<T> Matrix for &[Vec<T>] {
108 type MatElement = T;
109 type R = Vec<T>;
110
111 fn dimension(&self) -> (usize, usize) {
112 (self.len(), self[0].len())
113 }
114
115 fn fits(&self, row: usize, col: usize) -> bool {
116 self.len() >= row && self[0].len() >= col
117 }
118}
119
120impl<T> Matrix for &mut [Vec<T>] {
121 type MatElement = T;
122 type R = Vec<T>;
123
124 fn dimension(&self) -> (usize, usize) {
125 (self.len(), self[0].len())
126 }
127
128 fn fits(&self, row: usize, col: usize) -> bool {
129 self.len() >= row && self[0].len() >= col
130 }
131}
132
133impl<T> MatrixMut for Vec<Vec<T>> {}
134impl<T> MatrixMut for &mut [Vec<T>] {}
135
136impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
137 fn zeros(row: usize, col: usize) -> Self {
138 vec![vec![T::zero(); col]; row]
139 }
140}
141
142impl<T> Row for Vec<T> {
143 type Element = T;
144}
145
146impl<T> RowMut for Vec<T> {}
147
148impl<T: Zero + Clone> RowEntity for Vec<T> {
149 fn zeros(col: usize) -> Self {
150 vec![T::zero(); col]
151 }
152}