phantom-zone/src/lib.rs
src/lib.rsRaw
1use num_traits::Zero;
2
3mod backend;
4mod bool;
5mod decomposer;
6mod lwe;
7mod multi_party;
8mod ntt;
9mod pbs;
10mod random;
11mod rgsw;
12#[cfg(any(feature = "interactive_mp", feature = "non_interactive_mp"))]
13mod shortint;
14mod utils;
15
16pub use backend::{
17 ArithmeticLazyOps, ArithmeticOps, ModInit, ModularOpsU64, ShoupMatrixFMA, VectorOps,
18};
19
20pub use bool::*;
21pub use ntt::{Ntt, NttBackendU64, NttInit};
22#[cfg(any(feature = "interactive_mp", feature = "non_interactive_mp"))]
23pub use shortint::{div_zero_error_flag, reset_error_flags, FheUint8};
24
25pub use decomposer::{Decomposer, DecomposerIter, DefaultDecomposer};
26
27pub trait Matrix: AsRef<[Self::R]> {
28 type MatElement;
29 type R: Row<Element = Self::MatElement>;
30
31 /// (Rows, Cols)
32 fn dimension(&self) -> (usize, usize);
33
34 fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
35 self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
36 }
37
38 fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
39 self.as_ref()[row_idx].as_ref()
40 }
41
42 fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
43 self.as_ref().iter().map(move |r| r)
44 }
45
46 fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
47 &self.as_ref()[row_idx].as_ref()[column_idx]
48 }
49
50 fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
51 self.as_ref().split_at(idx)
52 }
53
54 /// Does the matrix fit sub-matrix of dimension row x col
55 fn fits(&self, row: usize, col: usize) -> bool;
56}
57
58pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
59where
60 <Self as Matrix>::R: RowMut,
61{
62 fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
63 self.as_mut()[row_index].as_mut()
64 }
65
66 fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
67 self.as_mut().iter_mut().map(move |r| r)
68 }
69
70 fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
71 self.as_mut()[row_idx].as_mut()[column_idx] = val;
72 }
73
74 fn split_at_row_mut(
75 &mut self,
76 idx: usize,
77 ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
78 self.as_mut().split_at_mut(idx)
79 }
80}
81
82pub trait MatrixEntity: Matrix // where
83// <Self as Matrix>::MatElement: Zero,
84{
85 fn zeros(row: usize, col: usize) -> Self;
86}
87
88pub trait Row: AsRef<[Self::Element]> {
89 type Element;
90}
91
92pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
93
94pub trait RowEntity: Row {
95 fn zeros(col: usize) -> Self;
96}
97
98impl<T> Matrix for Vec<Vec<T>> {
99 type MatElement = T;
100 type R = Vec<T>;
101
102 fn dimension(&self) -> (usize, usize) {
103 (self.len(), self[0].len())
104 }
105
106 fn fits(&self, row: usize, col: usize) -> bool {
107 self.len() >= row && self[0].len() >= col
108 }
109}
110
111impl<T> Matrix for &[Vec<T>] {
112 type MatElement = T;
113 type R = Vec<T>;
114
115 fn dimension(&self) -> (usize, usize) {
116 (self.len(), self[0].len())
117 }
118
119 fn fits(&self, row: usize, col: usize) -> bool {
120 self.len() >= row && self[0].len() >= col
121 }
122}
123
124impl<T> Matrix for &mut [Vec<T>] {
125 type MatElement = T;
126 type R = Vec<T>;
127
128 fn dimension(&self) -> (usize, usize) {
129 (self.len(), self[0].len())
130 }
131
132 fn fits(&self, row: usize, col: usize) -> bool {
133 self.len() >= row && self[0].len() >= col
134 }
135}
136
137impl<T> MatrixMut for Vec<Vec<T>> {}
138impl<T> MatrixMut for &mut [Vec<T>] {}
139
140impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
141 fn zeros(row: usize, col: usize) -> Self {
142 vec![vec![T::zero(); col]; row]
143 }
144}
145
146impl<T> Row for Vec<T> {
147 type Element = T;
148}
149
150impl<T> Row for [T] {
151 type Element = T;
152}
153
154impl<T> RowMut for Vec<T> {}
155
156impl<T: Zero + Clone> RowEntity for Vec<T> {
157 fn zeros(col: usize) -> Self {
158 vec![T::zero(); col]
159 }
160}
161
162pub trait Encryptor<M: ?Sized, C> {
163 fn encrypt(&self, m: &M) -> C;
164}
165
166pub trait Decryptor<M, C> {
167 fn decrypt(&self, c: &C) -> M;
168}
169
170pub trait MultiPartyDecryptor<M, C> {
171 type DecryptionShare;
172
173 fn gen_decryption_share(&self, c: &C) -> Self::DecryptionShare;
174 fn aggregate_decryption_shares(&self, c: &C, shares: &[Self::DecryptionShare]) -> M;
175}
176
177pub trait KeySwitchWithId<C> {
178 fn key_switch(&self, user_id: usize) -> C;
179}
180
181pub trait SampleExtractor<R> {
182 /// Extract ciphertext at `index`
183 fn extract_at(&self, index: usize) -> R;
184 /// Extract all ciphertexts
185 fn extract_all(&self) -> Vec<R>;
186 /// Extract first `how_many` ciphertexts
187 fn extract_many(&self, how_many: usize) -> Vec<R>;
188}
189
190trait Encoder<F, T> {
191 fn encode(&self, v: F) -> T;
192}
193
194trait SizeInBitsWithLogModulus {
195 /// Returns size of `Self` containing several elements mod Q where
196 /// 2^{log_modulus-1} < Q <= `2^log_modulus`
197 fn size(&self, log_modulus: usize) -> usize;
198}
199
200impl SizeInBitsWithLogModulus for Vec<Vec<u64>> {
201 fn size(&self, log_modulus: usize) -> usize {
202 let mut total = 0;
203 self.iter().for_each(|r| total += log_modulus * r.len());
204 total
205 }
206}
207
208impl SizeInBitsWithLogModulus for Vec<u64> {
209 fn size(&self, log_modulus: usize) -> usize {
210 self.len() * log_modulus
211 }
212}