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 random;
 18mod rgsw;
 19mod shortint;
 20mod utils;
 21
 22pub trait Matrix: AsRef<[Self::R]> {
 23    type MatElement;
 24    type R: Row<Element = Self::MatElement>;
 25
 26    fn dimension(&self) -> (usize, usize);
 27
 28    fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
 29        self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
 30    }
 31
 32    fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
 33        self.as_ref()[row_idx].as_ref()
 34    }
 35
 36    fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
 37        self.as_ref().iter().map(move |r| r)
 38    }
 39
 40    fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
 41        &self.as_ref()[row_idx].as_ref()[column_idx]
 42    }
 43
 44    fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
 45        self.as_ref().split_at(idx)
 46    }
 47
 48    /// Does the matrix fit sub-matrix of dimension row x col
 49    fn fits(&self, row: usize, col: usize) -> bool;
 50}
 51
 52pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
 53where
 54    <Self as Matrix>::R: RowMut,
 55{
 56    fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
 57        self.as_mut()[row_index].as_mut()
 58    }
 59
 60    fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
 61        self.as_mut().iter_mut().map(move |r| r)
 62    }
 63
 64    fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
 65        self.as_mut()[row_idx].as_mut()[column_idx] = val;
 66    }
 67
 68    fn split_at_row_mut(
 69        &mut self,
 70        idx: usize,
 71    ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
 72        self.as_mut().split_at_mut(idx)
 73    }
 74}
 75
 76pub trait MatrixEntity: Matrix // where
 77// <Self as Matrix>::MatElement: Zero,
 78{
 79    fn zeros(row: usize, col: usize) -> Self;
 80}
 81
 82pub trait Row: AsRef<[Self::Element]> {
 83    type Element;
 84}
 85
 86pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
 87
 88pub trait RowEntity: Row {
 89    fn zeros(col: usize) -> Self;
 90}
 91
 92trait Secret {
 93    type Element;
 94    fn values(&self) -> &[Self::Element];
 95}
 96
 97impl<T> Matrix for Vec<Vec<T>> {
 98    type MatElement = T;
 99    type R = Vec<T>;
100
101    fn dimension(&self) -> (usize, usize) {
102        (self.len(), self[0].len())
103    }
104
105    fn fits(&self, row: usize, col: usize) -> bool {
106        self.len() >= row && self[0].len() >= col
107    }
108}
109
110impl<T> Matrix for &[Vec<T>] {
111    type MatElement = T;
112    type R = Vec<T>;
113
114    fn dimension(&self) -> (usize, usize) {
115        (self.len(), self[0].len())
116    }
117
118    fn fits(&self, row: usize, col: usize) -> bool {
119        self.len() >= row && self[0].len() >= col
120    }
121}
122
123impl<T> Matrix for &mut [Vec<T>] {
124    type MatElement = T;
125    type R = Vec<T>;
126
127    fn dimension(&self) -> (usize, usize) {
128        (self.len(), self[0].len())
129    }
130
131    fn fits(&self, row: usize, col: usize) -> bool {
132        self.len() >= row && self[0].len() >= col
133    }
134}
135
136impl<T> MatrixMut for Vec<Vec<T>> {}
137impl<T> MatrixMut for &mut [Vec<T>] {}
138
139impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
140    fn zeros(row: usize, col: usize) -> Self {
141        vec![vec![T::zero(); col]; row]
142    }
143}
144
145impl<T> Row for Vec<T> {
146    type Element = T;
147}
148
149impl<T> Row for [T] {
150    type Element = T;
151}
152
153impl<T> RowMut for Vec<T> {}
154
155impl<T: Zero + Clone> RowEntity for Vec<T> {
156    fn zeros(col: usize) -> Self {
157        vec![T::zero(); col]
158    }
159}
160
161trait Encryptor<M: ?Sized, C> {
162    fn encrypt(&self, m: &M) -> C;
163}
164
165trait Decryptor<M, C> {
166    fn decrypt(&self, c: &C) -> M;
167}
168
169trait MultiPartyDecryptor<M, C> {
170    type DecryptionShare;
171
172    fn gen_decryption_share(&self, c: &C) -> Self::DecryptionShare;
173    fn aggregate_decryption_shares(&self, c: &C, shares: &[Self::DecryptionShare]) -> M;
174}