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