arnaucube
arnaucube/phantom-zonePublic Self-hosted repository
phantom-zone/src/lib.rs
src/lib.rsRaw
  1use itertools::{izip, Itertools};
  2use num::UnsignedInteger;
  3use num_traits::{abs, Zero};
  4use rand::CryptoRng;
  5use random::{RandomGaussianDist, RandomUniformDist};
  6use utils::TryConvertFrom;
  7
  8mod backend;
  9mod bool;
 10mod decomposer;
 11mod lwe;
 12mod multi_party;
 13mod noise;
 14mod ntt;
 15mod num;
 16mod random;
 17mod rgsw;
 18mod utils;
 19
 20pub trait Matrix: AsRef<[Self::R]> {
 21    type MatElement;
 22    type R: Row<Element = Self::MatElement>;
 23
 24    fn dimension(&self) -> (usize, usize);
 25
 26    fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
 27        self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
 28    }
 29
 30    fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
 31        self.as_ref()[row_idx].as_ref()
 32    }
 33
 34    fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
 35        self.as_ref().iter().map(move |r| r)
 36    }
 37
 38    fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
 39        &self.as_ref()[row_idx].as_ref()[column_idx]
 40    }
 41
 42    fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
 43        self.as_ref().split_at(idx)
 44    }
 45}
 46
 47pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
 48where
 49    <Self as Matrix>::R: RowMut,
 50{
 51    fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
 52        self.as_mut()[row_index].as_mut()
 53    }
 54
 55    fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
 56        self.as_mut().iter_mut().map(move |r| r)
 57    }
 58
 59    fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
 60        self.as_mut()[row_idx].as_mut()[column_idx] = val;
 61    }
 62
 63    fn split_at_row_mut(
 64        &mut self,
 65        idx: usize,
 66    ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
 67        self.as_mut().split_at_mut(idx)
 68    }
 69}
 70
 71pub trait MatrixEntity: Matrix // where
 72// <Self as Matrix>::MatElement: Zero,
 73{
 74    fn zeros(row: usize, col: usize) -> Self;
 75}
 76
 77pub trait Row: AsRef<[Self::Element]> {
 78    type Element;
 79}
 80
 81pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
 82
 83pub trait RowEntity: Row {
 84    fn zeros(col: usize) -> Self;
 85}
 86
 87trait Secret {
 88    type Element;
 89    fn values(&self) -> &[Self::Element];
 90}
 91
 92impl<T> Matrix for Vec<Vec<T>> {
 93    type MatElement = T;
 94    type R = Vec<T>;
 95
 96    fn dimension(&self) -> (usize, usize) {
 97        (self.len(), self[0].len())
 98    }
 99}
100
101impl<T> Matrix for &[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
110impl<T> Matrix for &mut [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
119impl<T> MatrixMut for Vec<Vec<T>> {}
120impl<T> MatrixMut for &mut [Vec<T>] {}
121
122impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
123    fn zeros(row: usize, col: usize) -> Self {
124        vec![vec![T::zero(); col]; row]
125    }
126}
127
128impl<T> Row for Vec<T> {
129    type Element = T;
130}
131
132impl<T> RowMut for Vec<T> {}
133
134impl<T: Zero + Clone> RowEntity for Vec<T> {
135    fn zeros(col: usize) -> Self {
136        vec![T::zero(); col]
137    }
138}