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