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