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    /// Does the matrix fit sub-matrix of dimension row x col
 47    fn fits(&self, row: usize, col: usize) -> bool;
 48}
 49
 50pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
 51where
 52    <Self as Matrix>::R: RowMut,
 53{
 54    fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
 55        self.as_mut()[row_index].as_mut()
 56    }
 57
 58    fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
 59        self.as_mut().iter_mut().map(move |r| r)
 60    }
 61
 62    fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
 63        self.as_mut()[row_idx].as_mut()[column_idx] = val;
 64    }
 65
 66    fn split_at_row_mut(
 67        &mut self,
 68        idx: usize,
 69    ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
 70        self.as_mut().split_at_mut(idx)
 71    }
 72}
 73
 74pub trait MatrixEntity: Matrix // where
 75// <Self as Matrix>::MatElement: Zero,
 76{
 77    fn zeros(row: usize, col: usize) -> Self;
 78}
 79
 80pub trait Row: AsRef<[Self::Element]> {
 81    type Element;
 82}
 83
 84pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
 85
 86pub trait RowEntity: Row {
 87    fn zeros(col: usize) -> Self;
 88}
 89
 90trait Secret {
 91    type Element;
 92    fn values(&self) -> &[Self::Element];
 93}
 94
 95impl<T> Matrix for Vec<Vec<T>> {
 96    type MatElement = T;
 97    type R = Vec<T>;
 98
 99    fn dimension(&self) -> (usize, usize) {
100        (self.len(), self[0].len())
101    }
102
103    fn fits(&self, row: usize, col: usize) -> bool {
104        self.len() >= row && self[0].len() >= col
105    }
106}
107
108impl<T> Matrix for &[Vec<T>] {
109    type MatElement = T;
110    type R = Vec<T>;
111
112    fn dimension(&self) -> (usize, usize) {
113        (self.len(), self[0].len())
114    }
115
116    fn fits(&self, row: usize, col: usize) -> bool {
117        self.len() >= row && self[0].len() >= col
118    }
119}
120
121impl<T> Matrix for &mut [Vec<T>] {
122    type MatElement = T;
123    type R = Vec<T>;
124
125    fn dimension(&self) -> (usize, usize) {
126        (self.len(), self[0].len())
127    }
128
129    fn fits(&self, row: usize, col: usize) -> bool {
130        self.len() >= row && self[0].len() >= col
131    }
132}
133
134impl<T> MatrixMut for Vec<Vec<T>> {}
135impl<T> MatrixMut for &mut [Vec<T>] {}
136
137impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
138    fn zeros(row: usize, col: usize) -> Self {
139        vec![vec![T::zero(); col]; row]
140    }
141}
142
143impl<T> Row for Vec<T> {
144    type Element = T;
145}
146
147impl<T> RowMut for Vec<T> {}
148
149impl<T: Zero + Clone> RowEntity for Vec<T> {
150    fn zeros(col: usize) -> Self {
151        vec![T::zero(); col]
152    }
153}