Skip to content

Commit

Permalink
add const to Compress trait and more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobkaufmann committed Mar 7, 2024
1 parent a524463 commit 32f21ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<const N: usize> Blob<N> {
let degree = (N as u128).to_be_bytes();

let mut comm = [0u8; Commitment::BYTES];
let _ = commitment
commitment
.compress(comm.as_mut_slice())
.expect("sufficient buffer len");

Expand Down
26 changes: 20 additions & 6 deletions src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,25 @@ impl From<ECGroupError> for Error {
///
/// github.com/zkcrypto/pairing/blob/34aa52b0f7bef705917252ea63e5a13fa01af551/src/bls12_381/README.md
pub trait Compress {
fn compress(&self, buf: impl AsMut<[u8]>) -> Result<usize, &'static str>;
/// The length in bytes of the compressed representation of `self`.
const COMPRESSED: usize;

/// Compresses `self` into `buf`.
///
/// # Errors
///
/// Compression will fail if the length of `buf` is less than `Self::COMPRESSED`.
fn compress(&self, buf: impl AsMut<[u8]>) -> Result<(), &'static str>;
}

/// A data structure that can be deserialized from the compressed format defined by Zcash.
///
/// github.com/zkcrypto/pairing/blob/34aa52b0f7bef705917252ea63e5a13fa01af551/src/bls12_381/README.md
pub trait Decompress: Sized {
/// The error that can occur upon decompression.
type Error;

/// Decompresses `compressed` into `Self`.
fn decompress(compressed: impl AsRef<[u8]>) -> Result<Self, Self::Error>;
}

Expand Down Expand Up @@ -493,14 +503,16 @@ impl Neg for P1 {
}

impl Compress for P1 {
fn compress(&self, mut buf: impl AsMut<[u8]>) -> Result<usize, &'static str> {
if buf.as_mut().len() < Self::BYTES {
const COMPRESSED: usize = Self::BYTES;

fn compress(&self, mut buf: impl AsMut<[u8]>) -> Result<(), &'static str> {
if buf.as_mut().len() < Self::COMPRESSED {
return Err("insufficient buffer length");
}
unsafe {
blst_p1_compress(buf.as_mut().as_mut_ptr(), &self.element);
}
Ok(Self::BYTES)
Ok(())
}
}

Expand Down Expand Up @@ -611,14 +623,16 @@ impl Mul<Fr> for P2 {
}

impl Compress for P2 {
fn compress(&self, mut buf: impl AsMut<[u8]>) -> Result<usize, &'static str> {
const COMPRESSED: usize = Self::BYTES;

fn compress(&self, mut buf: impl AsMut<[u8]>) -> Result<(), &'static str> {
if buf.as_mut().len() < Self::BYTES {
return Err("insufficient buffer length");
}
unsafe {
blst_p2_compress(buf.as_mut().as_mut_ptr(), &self.element);
}
Ok(Self::BYTES)
Ok(())
}
}

Expand Down

0 comments on commit 32f21ec

Please sign in to comment.