Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
supinie committed Apr 9, 2024
1 parent 9dbd5f0 commit 20f10a8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 39 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ mod vectors;
mod tests {
// mod buffer;
mod field_operations;
// mod indcpa;
// mod matrix;
// mod ntt;
// mod indcpa;
// mod matrix;
// mod ntt;
mod params;
// mod polynomials;
// mod sample;
// mod vectors;
// mod sample;
// mod vectors;
}
50 changes: 30 additions & 20 deletions src/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
errors::CrystalsError,
params::{SecurityLevel, K},
polynomials::{Normalised, Poly, State, Unnormalised},
vectors::PolyVec
polynomials::{Normalised, Poly, State, Unnormalised},
vectors::PolyVec,
};
use tinyvec::{array_vec, ArrayVec};

Expand All @@ -22,14 +22,17 @@ impl<S: State> Matrix<S> {
}
}


impl Matrix<Normalised> {
// Create a new, empty matrix
fn new(k: K) -> Self {
let polyvecs = match k {
K::Two => array_vec!([PolyVec<Normalised>; 4] => PolyVec::new(k), PolyVec::new(k)),
K::Three => array_vec!([PolyVec<Normalised>; 4] => PolyVec::new(k), PolyVec::new(k), PolyVec::new(k)),
K::Four => array_vec!([PolyVec<Normalised>; 4] => PolyVec::new(k), PolyVec::new(k), PolyVec::new(k), PolyVec::new(k))
K::Three => {
array_vec!([PolyVec<Normalised>; 4] => PolyVec::new(k), PolyVec::new(k), PolyVec::new(k))
}
K::Four => {
array_vec!([PolyVec<Normalised>; 4] => PolyVec::new(k), PolyVec::new(k), PolyVec::new(k), PolyVec::new(k))
}
};

Self {
Expand All @@ -44,36 +47,43 @@ impl Matrix<Unnormalised> {
let mut polyvecs = ArrayVec::<[PolyVec<Unnormalised>; 4]>::new();
if transpose {
for i in 0..sec_level.into() {
let row: Result<ArrayVec<[Poly<Unnormalised>; 4]>, CrystalsError> = (0..sec_level.into()).map(|j| {
#[allow(clippy::cast_possible_truncation)] // we know that max i, j is 4
Poly::derive_uniform(seed, i as u8, j as u8)
}).collect();

let row: Result<ArrayVec<[Poly<Unnormalised>; 4]>, CrystalsError> = (0..sec_level
.into())
.map(|j| {
#[allow(clippy::cast_possible_truncation)] // we know that max i, j is 4
Poly::derive_uniform(seed, i as u8, j as u8)
})
.collect();

let arr_vec = row?;

let polyvec = PolyVec::from(arr_vec)?;
polyvecs.push(polyvec);
};
}
} else {
for i in 0..sec_level.into() {
let row: Result<ArrayVec<[Poly<Unnormalised>; 4]>, CrystalsError> = (0..sec_level.into()).map(|j| {
#[allow(clippy::cast_possible_truncation)] // we know that max i, j is 4
Poly::derive_uniform(seed, j as u8, i as u8)
}).collect();

let row: Result<ArrayVec<[Poly<Unnormalised>; 4]>, CrystalsError> = (0..sec_level
.into())
.map(|j| {
#[allow(clippy::cast_possible_truncation)] // we know that max i, j is 4
Poly::derive_uniform(seed, j as u8, i as u8)
})
.collect();

let arr_vec = row?;

let polyvec = PolyVec::from(arr_vec)?;
polyvecs.push(polyvec);
};
}
}

Ok(Self { polyvecs, sec_level })

Ok(Self {
polyvecs,
sec_level,
})
}
}


// pub type Mat512 = [PolyVec512; 2];
// pub type Mat768 = [PolyVec768; 3];
// pub type Mat1024 = [PolyVec1024; 4];
Expand Down
4 changes: 2 additions & 2 deletions src/polynomials/sample.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
errors::CrystalsError,
params::{Eta, N, Q, SYMBYTES},
polynomials::{Noise, Poly, Unnormalised}
polynomials::{Noise, Poly, Unnormalised},
};
use byteorder::{ByteOrder, LittleEndian};
use rand_core::{CryptoRng, Error, RngCore};
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Poly<Unnormalised> {
// coefficients are reduced, but not normalised (close to normal)
pub(crate) fn derive_uniform(seed: &[u8], x: u8, y: u8) -> Result<Self, CrystalsError> {
if seed.len() != 32 {
return Err(CrystalsError::InvalidSeedLength(seed.len(), 32))
return Err(CrystalsError::InvalidSeedLength(seed.len(), 32));
}
let seed_suffix = [x, y];
let mut buf = [0u8; 168];
Expand Down
30 changes: 18 additions & 12 deletions src/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use crate::{
errors::{CrystalsError, PackingError},
params::{SecurityLevel, K, POLYBYTES},
polynomials::{decompress_to_poly, unpack_to_poly, Noise, Normalised, Poly, State, Unnormalised},
polynomials::{
decompress_to_poly, unpack_to_poly, Noise, Normalised, Poly, State, Unnormalised,
},
};
use tinyvec::{array_vec, ArrayVec};

Expand All @@ -24,12 +26,16 @@ impl<S: State> PolyVec<S> {
&self.polynomials.as_slice()[..self.sec_level.into()]
}

pub(crate) fn from(polynomials: ArrayVec<[Poly<S>; 4]>) -> Result<Self, CrystalsError> {
K::try_from(polynomials.len())
.map_or_else(
|_| Err(CrystalsError::InternalError()),
|sec_level| Ok(Self { polynomials, sec_level })
)
pub(crate) fn from(polynomials: ArrayVec<[Poly<S>; 4]>) -> Result<Self, CrystalsError> {
K::try_from(polynomials.len()).map_or_else(
|_| Err(CrystalsError::InternalError()),
|sec_level| {
Ok(Self {
polynomials,
sec_level,
})
},
)
}

// Add two polyvecs pointwise.
Expand Down Expand Up @@ -145,10 +151,9 @@ impl PolyVec<Normalised> {
Ok(())
}


// buf should be of length k * poly_compressed_bytes
// compresses the polyvec poly-wise into the buffer
fn compress(&self, buf:&mut [u8]) -> Result<(), PackingError> {
fn compress(&self, buf: &mut [u8]) -> Result<(), PackingError> {
let bytes_len = self.sec_level().poly_compressed_bytes();
if buf.len() != self.polynomials.len() * bytes_len {
return Err(CrystalsError::IncorrectBufferLength(
Expand All @@ -157,11 +162,12 @@ impl PolyVec<Normalised> {
)
.into());
}

let _ = buf.chunks_mut(bytes_len)

let _ = buf
.chunks_mut(bytes_len)
.zip(self.polynomials.iter())
.map(|(buf_chunk, poly)| poly.compress(buf_chunk, &self.sec_level()));

Ok(())
}
}
Expand Down

0 comments on commit 20f10a8

Please sign in to comment.