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::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::{
 24    ArithmeticLazyOps, ArithmeticOps, ModInit, ModularOpsU64, ShoupMatrixFMA, VectorOps,
 25};
 26pub use bool::{
 27    aggregate_public_key_shares, aggregate_server_key_shares, gen_client_key, gen_mp_keys_phase1,
 28    gen_mp_keys_phase2, set_mp_seed, set_parameter_set, ParameterSelector,
 29};
 30pub use decomposer::{Decomposer, DecomposerIter, DefaultDecomposer};
 31pub use ntt::{Ntt, NttBackendU64, NttInit};
 32pub use shortint::FheUint8;
 33
 34pub trait Matrix: AsRef<[Self::R]> {
 35    type MatElement;
 36    type R: Row<Element = Self::MatElement>;
 37
 38    fn dimension(&self) -> (usize, usize);
 39
 40    fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
 41        self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
 42    }
 43
 44    fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
 45        self.as_ref()[row_idx].as_ref()
 46    }
 47
 48    fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
 49        self.as_ref().iter().map(move |r| r)
 50    }
 51
 52    fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
 53        &self.as_ref()[row_idx].as_ref()[column_idx]
 54    }
 55
 56    fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
 57        self.as_ref().split_at(idx)
 58    }
 59
 60    /// Does the matrix fit sub-matrix of dimension row x col
 61    fn fits(&self, row: usize, col: usize) -> bool;
 62}
 63
 64pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
 65where
 66    <Self as Matrix>::R: RowMut,
 67{
 68    fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
 69        self.as_mut()[row_index].as_mut()
 70    }
 71
 72    fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
 73        self.as_mut().iter_mut().map(move |r| r)
 74    }
 75
 76    fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
 77        self.as_mut()[row_idx].as_mut()[column_idx] = val;
 78    }
 79
 80    fn split_at_row_mut(
 81        &mut self,
 82        idx: usize,
 83    ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
 84        self.as_mut().split_at_mut(idx)
 85    }
 86}
 87
 88pub trait MatrixEntity: Matrix // where
 89// <Self as Matrix>::MatElement: Zero,
 90{
 91    fn zeros(row: usize, col: usize) -> Self;
 92}
 93
 94pub trait Row: AsRef<[Self::Element]> {
 95    type Element;
 96}
 97
 98pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
 99
100pub trait RowEntity: Row {
101    fn zeros(col: usize) -> Self;
102}
103
104trait Secret {
105    type Element;
106    fn values(&self) -> &[Self::Element];
107}
108
109impl<T> Matrix for Vec<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 &[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> Matrix for &mut [Vec<T>] {
136    type MatElement = T;
137    type R = Vec<T>;
138
139    fn dimension(&self) -> (usize, usize) {
140        (self.len(), self[0].len())
141    }
142
143    fn fits(&self, row: usize, col: usize) -> bool {
144        self.len() >= row && self[0].len() >= col
145    }
146}
147
148impl<T> MatrixMut for Vec<Vec<T>> {}
149impl<T> MatrixMut for &mut [Vec<T>] {}
150
151impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
152    fn zeros(row: usize, col: usize) -> Self {
153        vec![vec![T::zero(); col]; row]
154    }
155}
156
157impl<T> Row for Vec<T> {
158    type Element = T;
159}
160
161impl<T> Row for [T] {
162    type Element = T;
163}
164
165impl<T> RowMut for Vec<T> {}
166
167impl<T: Zero + Clone> RowEntity for Vec<T> {
168    fn zeros(col: usize) -> Self {
169        vec![T::zero(); col]
170    }
171}
172
173pub trait Encryptor<M: ?Sized, C> {
174    fn encrypt(&self, m: &M) -> C;
175}
176
177pub trait Decryptor<M, C> {
178    fn decrypt(&self, c: &C) -> M;
179}
180
181pub trait MultiPartyDecryptor<M, C> {
182    type DecryptionShare;
183
184    fn gen_decryption_share(&self, c: &C) -> Self::DecryptionShare;
185    fn aggregate_decryption_shares(&self, c: &C, shares: &[Self::DecryptionShare]) -> M;
186}