arnaucube
arnaucube/phantom-zonePublic Self-hosted repository
phantom-zone/src/lib.rs
src/lib.rsRaw
  1use std::{iter::Once, sync::OnceLock};
  2
  3use itertools::{izip, Itertools};
  4use num::UnsignedInteger;
  5use num_traits::{abs, Zero};
  6use rand::CryptoRng;
  7use utils::TryConvertFrom1;
  8
  9mod backend;
 10mod bool;
 11mod decomposer;
 12mod lwe;
 13mod multi_party;
 14mod noise;
 15mod ntt;
 16mod num;
 17mod pbs;
 18mod random;
 19mod rgsw;
 20mod shortint;
 21mod utils;
 22
 23pub use ntt::{Ntt, NttBackendU64, NttInit};
 24
 25pub trait Matrix: AsRef<[Self::R]> {
 26    type MatElement;
 27    type R: Row<Element = Self::MatElement>;
 28
 29    fn dimension(&self) -> (usize, usize);
 30
 31    fn get_row(&self, row_idx: usize) -> impl Iterator<Item = &Self::MatElement> {
 32        self.as_ref()[row_idx].as_ref().iter().map(move |r| r)
 33    }
 34
 35    fn get_row_slice(&self, row_idx: usize) -> &[Self::MatElement] {
 36        self.as_ref()[row_idx].as_ref()
 37    }
 38
 39    fn iter_rows(&self) -> impl Iterator<Item = &Self::R> {
 40        self.as_ref().iter().map(move |r| r)
 41    }
 42
 43    fn get(&self, row_idx: usize, column_idx: usize) -> &Self::MatElement {
 44        &self.as_ref()[row_idx].as_ref()[column_idx]
 45    }
 46
 47    fn split_at_row(&self, idx: usize) -> (&[<Self as Matrix>::R], &[<Self as Matrix>::R]) {
 48        self.as_ref().split_at(idx)
 49    }
 50
 51    /// Does the matrix fit sub-matrix of dimension row x col
 52    fn fits(&self, row: usize, col: usize) -> bool;
 53}
 54
 55pub trait MatrixMut: Matrix + AsMut<[<Self as Matrix>::R]>
 56where
 57    <Self as Matrix>::R: RowMut,
 58{
 59    fn get_row_mut(&mut self, row_index: usize) -> &mut [Self::MatElement] {
 60        self.as_mut()[row_index].as_mut()
 61    }
 62
 63    fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Self::R> {
 64        self.as_mut().iter_mut().map(move |r| r)
 65    }
 66
 67    fn set(&mut self, row_idx: usize, column_idx: usize, val: <Self as Matrix>::MatElement) {
 68        self.as_mut()[row_idx].as_mut()[column_idx] = val;
 69    }
 70
 71    fn split_at_row_mut(
 72        &mut self,
 73        idx: usize,
 74    ) -> (&mut [<Self as Matrix>::R], &mut [<Self as Matrix>::R]) {
 75        self.as_mut().split_at_mut(idx)
 76    }
 77}
 78
 79pub trait MatrixEntity: Matrix // where
 80// <Self as Matrix>::MatElement: Zero,
 81{
 82    fn zeros(row: usize, col: usize) -> Self;
 83}
 84
 85pub trait Row: AsRef<[Self::Element]> {
 86    type Element;
 87}
 88
 89pub trait RowMut: Row + AsMut<[<Self as Row>::Element]> {}
 90
 91pub trait RowEntity: Row {
 92    fn zeros(col: usize) -> Self;
 93}
 94
 95trait Secret {
 96    type Element;
 97    fn values(&self) -> &[Self::Element];
 98}
 99
100impl<T> Matrix for Vec<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    fn fits(&self, row: usize, col: usize) -> bool {
109        self.len() >= row && self[0].len() >= col
110    }
111}
112
113impl<T> Matrix for &[Vec<T>] {
114    type MatElement = T;
115    type R = Vec<T>;
116
117    fn dimension(&self) -> (usize, usize) {
118        (self.len(), self[0].len())
119    }
120
121    fn fits(&self, row: usize, col: usize) -> bool {
122        self.len() >= row && self[0].len() >= col
123    }
124}
125
126impl<T> Matrix for &mut [Vec<T>] {
127    type MatElement = T;
128    type R = Vec<T>;
129
130    fn dimension(&self) -> (usize, usize) {
131        (self.len(), self[0].len())
132    }
133
134    fn fits(&self, row: usize, col: usize) -> bool {
135        self.len() >= row && self[0].len() >= col
136    }
137}
138
139impl<T> MatrixMut for Vec<Vec<T>> {}
140impl<T> MatrixMut for &mut [Vec<T>] {}
141
142impl<T: Zero + Clone> MatrixEntity for Vec<Vec<T>> {
143    fn zeros(row: usize, col: usize) -> Self {
144        vec![vec![T::zero(); col]; row]
145    }
146}
147
148impl<T> Row for Vec<T> {
149    type Element = T;
150}
151
152impl<T> Row for [T] {
153    type Element = T;
154}
155
156impl<T> RowMut for Vec<T> {}
157
158impl<T: Zero + Clone> RowEntity for Vec<T> {
159    fn zeros(col: usize) -> Self {
160        vec![T::zero(); col]
161    }
162}
163
164trait Encryptor<M: ?Sized, C> {
165    fn encrypt(&self, m: &M) -> C;
166}
167
168trait Decryptor<M, C> {
169    fn decrypt(&self, c: &C) -> M;
170}
171
172trait MultiPartyDecryptor<M, C> {
173    type DecryptionShare;
174
175    fn gen_decryption_share(&self, c: &C) -> Self::DecryptionShare;
176    fn aggregate_decryption_shares(&self, c: &C, shares: &[Self::DecryptionShare]) -> M;
177}