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