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