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 ntt;
 14mod num;
 15mod random;
 16mod rgsw;
 17mod utils;
 18
 19pub trait Matrix: AsRef<[Self::R]> {
 20    type MatElement;
 21    type R: Row<Element = Self::MatElement>;
 22
 23    fn dimension(&self) -> (usize, usize);
 24
 25    fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
 26        self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
 27    }
 28
 29    fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
 30        self.as_ref()[row_idx].as_ref()
 31    }
 32
 33    fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
 34        self.as_ref().iter().map(move |r| r)
 35    }
 36
 37    fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
 38        &self.as_ref()[row_idx].as_ref()[column_idx]
 39    }
 40
 41    fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
 42        self.as_ref().split_at(idx)
 43    }
 44}
 45
 46pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
 47where
 48    <Self as Matrix>::R: RowMut,
 49{
 50    fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
 51        self.as_mut()[row_index].as_mut()
 52    }
 53
 54    fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
 55        self.as_mut().iter_mut().map(move |r| r)
 56    }
 57
 58    fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
 59        self.as_mut()[row_idx].as_mut()[column_idx] = val;
 60    }
 61
 62    fn split_at_row_mut(
 63        &mut self,
 64        idx: usize,
 65    ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
 66        self.as_mut().split_at_mut(idx)
 67    }
 68}
 69
 70pub trait MatrixEntity: Matrix // where
 71// <Self as Matrix>::MatElement: Zero,
 72{
 73    fn zeros(row: usize, col: usize) -> Self;
 74}
 75
 76pub trait Row: AsRef<[Self::Element]> {
 77    type Element;
 78}
 79
 80pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
 81
 82pub trait RowEntity: Row {
 83    fn zeros(col: usize) -> Self;
 84}
 85
 86trait Secret {
 87    type Element;
 88    fn values(&self) -> &[Self::Element];
 89}
 90
 91impl<T> Matrix for Vec<Vec<T>> {
 92    type MatElement = T;
 93    type R = Vec<T>;
 94
 95    fn dimension(&self) -> (usize, usize) {
 96        (self.len(), self[0].len())
 97    }
 98}
 99
100impl<T> Matrix for &[Vec<T>] {
101    type MatElement = T;
102    type R = Vec<T>;
103
104    fn dimension(&self) -> (usize, usize) {
105        (self.len(), self[0].len())
106    }
107}
108
109impl<T> Matrix for &mut [Vec<T>] {
110    type MatElement = T;
111    type R = Vec<T>;
112
113    fn dimension(&self) -> (usize, usize) {
114        (self.len(), self[0].len())
115    }
116}
117
118impl<T> MatrixMut for Vec<Vec<T>> {}
119impl<T> MatrixMut for &mut [Vec<T>] {}
120
121impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
122    fn zeros(row: usize, col: usize) -> Self {
123        vec![vec![T::zero(); col]; row]
124    }
125}
126
127impl<T> Row for Vec<T> {
128    type Element = T;
129}
130
131impl<T> RowMut for Vec<T> {}
132
133impl<T: Zero + Clone> RowEntity for Vec<T> {
134    fn zeros(col: usize) -> Self {
135        vec![T::zero(); col]
136    }
137}