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::{ModInit, ModularOpsU64, VectorOps};
 24pub use ntt::{Ntt, NttBackendU64, NttInit};
 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
 96trait Secret {
 97    type Element;
 98    fn values(&self) -> &[Self::Element];
 99}
100
101impl<T> Matrix for Vec<Vec<T>> {
102    type MatElement = T;
103    type R = Vec<T>;
104
105    fn dimension(&self) -> (usize, usize) {
106        (self.len(), self[0].len())
107    }
108
109    fn fits(&self, row: usize, col: usize) -> bool {
110        self.len() >= row && self[0].len() >= col
111    }
112}
113
114impl<T> Matrix for &[Vec<T>] {
115    type MatElement = T;
116    type R = Vec<T>;
117
118    fn dimension(&self) -> (usize, usize) {
119        (self.len(), self[0].len())
120    }
121
122    fn fits(&self, row: usize, col: usize) -> bool {
123        self.len() >= row && self[0].len() >= col
124    }
125}
126
127impl<T> Matrix for &mut [Vec<T>] {
128    type MatElement = T;
129    type R = Vec<T>;
130
131    fn dimension(&self) -> (usize, usize) {
132        (self.len(), self[0].len())
133    }
134
135    fn fits(&self, row: usize, col: usize) -> bool {
136        self.len() >= row && self[0].len() >= col
137    }
138}
139
140impl<T> MatrixMut for Vec<Vec<T>> {}
141impl<T> MatrixMut for &mut [Vec<T>] {}
142
143impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
144    fn zeros(row: usize, col: usize) -> Self {
145        vec![vec![T::zero(); col]; row]
146    }
147}
148
149impl<T> Row for Vec<T> {
150    type Element = T;
151}
152
153impl<T> Row for [T] {
154    type Element = T;
155}
156
157impl<T> RowMut for Vec<T> {}
158
159impl<T: Zero + Clone> RowEntity for Vec<T> {
160    fn zeros(col: usize) -> Self {
161        vec![T::zero(); col]
162    }
163}
164
165trait Encryptor<M: ?Sized, C> {
166    fn encrypt(&self, m: &M) -> C;
167}
168
169trait Decryptor<M, C> {
170    fn decrypt(&self, c: &C) -> M;
171}
172
173trait MultiPartyDecryptor<M, C> {
174    type DecryptionShare;
175
176    fn gen_decryption_share(&self, c: &C) -> Self::DecryptionShare;
177    fn aggregate_decryption_shares(&self, c: &C, shares: &[Self::DecryptionShare]) -> M;
178}