Skip to content

Commit

Permalink
fixes polyvec decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
supinie committed Apr 16, 2024
1 parent 3986bef commit a066a85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,25 @@ mod vec_tests {

let output = poly_vec.normalise().compress(&mut buf[..end]).unwrap();
}

#[test]
fn unpack_test(poly_vec in new_poly_vec()) {
let mut buf = [0u8; 4 * POLYBYTES]; // max buf length
let k: usize = poly_vec.sec_level().k().into();

let _result = poly_vec.normalise().pack(&mut buf[..k * POLYBYTES]).unwrap();

let unpacked = PolyVec::unpack(&buf[..k * POLYBYTES]).unwrap();
}

#[test]
fn decompress_test(poly_vec in new_poly_vec()) {
let mut buf = [0u8; 4 * 160]; // max poly_vec_compressed_bytes
let end = poly_vec.sec_level().poly_vec_compressed_bytes();

let _result = poly_vec.normalise().compress(&mut buf[..end]).unwrap();

let decompressed = PolyVec::decompress(&buf[..end]).unwrap();
}
}
}
17 changes: 9 additions & 8 deletions src/vectors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// use core::num::TryFromIntError;
use crate::{
errors::{CrystalsError, PackingError},
params::{Eta, SecurityLevel, K, N, POLYBYTES},
Expand Down Expand Up @@ -170,8 +169,6 @@ impl PolyVec<Normalised> {
let _ = buf
.chunks_mut(self.sec_level().poly_compressed_bytes())
.zip(self.polynomials.iter())
// This is applying it to the buf_chunk not the buf
// .map(|(buf_chunk, poly)| poly.compress(buf_chunk, &self.sec_level()));
.for_each(|(buf_chunk, poly)| { let _ = poly.compress(buf_chunk, &self.sec_level()); } );

Ok(())
Expand Down Expand Up @@ -201,12 +198,16 @@ impl PolyVec<Normalised> {
}

// Decompress a given buffer into a polyvec.
// The buffer should be of length poly_compressed_bytes.
// The buffer should be of length poly_vec_compressed_bytes.
// If the length of the buffer is incorrect, the operation can still succeed provided it is a valid
// multiple of POLYBYTES, and will result in a polyvec of incorrect security level.
// poly_vec_compressed_bytes, and will result in a polyvec of incorrect security level.
pub(crate) fn decompress(buf: &[u8]) -> Result<Self, PackingError> {
let k = K::try_from(buf.len() / POLYBYTES)?;
let sec_level = SecurityLevel::new(k);
let sec_level = match buf.len() {
256 => Ok(SecurityLevel::new(K::Two)),
384 => Ok(SecurityLevel::new(K::Three)),
640 => Ok(SecurityLevel::new(K::Four)),
_ => Err(PackingError::Crystals(CrystalsError::IncorrectBufferLength(buf.len(), 0)))
}?;

let polyvec_result = buf
.chunks(sec_level.poly_compressed_bytes())
Expand All @@ -216,7 +217,7 @@ impl PolyVec<Normalised> {
match polyvec_result {
Ok(polynomials) => Ok(Self {
polynomials,
sec_level: k,
sec_level: sec_level.k(),
}),
Err(err) => Err(err),
}
Expand Down

0 comments on commit a066a85

Please sign in to comment.