use itertools::{izip, Itertools}; use num::UnsignedInteger; use num_traits::{abs, Zero}; use rand::CryptoRng; use random::{RandomGaussianDist, RandomUniformDist}; use utils::TryConvertFrom; mod backend; mod bool; mod decomposer; mod lwe; mod ntt; mod num; mod random; mod rgsw; mod utils; pub trait Matrix: AsRef<[Self::R]> { type MatElement; type R: Row; fn dimension(&self) -> (usize, usize); fn get_row(&self, row_idx: usize) -> impl Iterator { self.as_ref()[row_idx].as_ref().iter().map(move |r| r) } fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] { self.as_ref()[row_idx].as_ref() } fn iter_rows(&self) -> impl Iterator { self.as_ref().iter().map(move |r| r) } fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement { &self.as_ref()[row_idx].as_ref()[column_idx] } fn split_at_row(&self, idx: usize) -> (&[::R], &[::R]) { self.as_ref().split_at(idx) } } pub trait MatrixMut: Matrix + AsMut<[::R]> where ::R: RowMut, { fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] { self.as_mut()[row_index].as_mut() } fn iter_rows_mut(&mut self) -> impl Iterator { self.as_mut().iter_mut().map(move |r| r) } fn set(&mut self, row_idx: usize, column_idx: usize, val: ::MatElement) { self.as_mut()[row_idx].as_mut()[column_idx] = val; } fn split_at_row_mut( &mut self, idx: usize, ) -> (&mut [::R], &mut [::R]) { self.as_mut().split_at_mut(idx) } } pub trait MatrixEntity: Matrix // where // ::MatElement: Zero, { fn zeros(row: usize, col: usize) -> Self; } pub trait Row: AsRef<[Self::Element]> { type Element; } pub trait RowMut: Row + AsMut<[::Element]> {} pub trait RowEntity: Row { fn zeros(col: usize) -> Self; } trait Secret { type Element; fn values(&self) -> &[Self::Element]; } impl Matrix for Vec> { type MatElement = T; type R = Vec; fn dimension(&self) -> (usize, usize) { (self.len(), self[0].len()) } } impl Matrix for &[Vec] { type MatElement = T; type R = Vec; fn dimension(&self) -> (usize, usize) { (self.len(), self[0].len()) } } impl Matrix for &mut [Vec] { type MatElement = T; type R = Vec; fn dimension(&self) -> (usize, usize) { (self.len(), self[0].len()) } } impl MatrixMut for Vec> {} impl MatrixMut for &mut [Vec] {} impl MatrixEntity for Vec> { fn zeros(row: usize, col: usize) -> Self { vec![vec![T::zero(); col]; row] } } impl Row for Vec { type Element = T; } impl RowMut for Vec {} impl RowEntity for Vec { fn zeros(col: usize) -> Self { vec![T::zero(); col] } }