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