Skip to content

Commit

Permalink
adds extra compression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
supinie committed Apr 16, 2024
1 parent b51c159 commit 86ba5b4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/polynomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl<S: State> Default for Poly<S> {
}

impl<S: State> Poly<S> {
pub(crate) fn coeffs(&self) -> &[i16; N] {
&self.coeffs
}

// Sets self to self + x
// The coeffs of self and x should be small enough that no overflow can occur.
// If in doubt, reduce first.
Expand Down
44 changes: 44 additions & 0 deletions src/tests/polynomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(in crate::tests) mod poly_tests {
params::*,
tests::params::params_tests::sec_level_strategy,
};
use more_asserts::assert_le;
use proptest::prelude::*;

pub(in crate::tests) fn new_limited_poly_array() -> impl Strategy<Value = [i16; N]> {
Expand Down Expand Up @@ -139,5 +140,48 @@ pub(in crate::tests) mod poly_tests {

let decompressed_poly = Poly::decompress(&buf[..sec_level.poly_compressed_bytes()], &sec_level).unwrap();
}

#[test]
fn pack_unpack_test(poly in new_poly()) {
let buf = poly.normalise().pack();

let unpacked = Poly::unpack(&buf).unwrap();

assert_eq!(poly.normalise(), unpacked.normalise());
}

#[test]
fn compress_decompress_test(
poly in new_poly(),
sec_level in sec_level_strategy()
) {
let mut buf = [0u8; 160];
let _ = poly
.normalise()
.compress(&mut buf[..sec_level.poly_compressed_bytes()], &sec_level)
.unwrap();

let decompressed = Poly::decompress(&buf[..sec_level.poly_compressed_bytes()], &sec_level).unwrap();

for (original_coeff, new_coeff) in poly
.normalise()
.coeffs()
.iter()
.zip(decompressed.coeffs().iter()) {
if (original_coeff - new_coeff).abs() < 150 {
assert_le!((original_coeff - new_coeff).abs(), 150, "original: {original_coeff}, new: {new_coeff}");
} else {
assert_le!(Q as i16 - (original_coeff - new_coeff).abs(), 150, "original: {original_coeff}, new: {new_coeff}");
}
}
}

// #[test]
// fn write_read_msg_test(poly in new_poly()) {
// let msg = poly.normalise().write_msg().unwrap();
// let new_poly = Poly::read_msg(&msg).unwrap();

// assert_eq!(poly, new_poly);
// }
}
}

0 comments on commit 86ba5b4

Please sign in to comment.