Skip to content

Commit

Permalink
change viz for reduce_mod_uniform_buffer (#811)
Browse files Browse the repository at this point in the history
* change viz for reduce_mod_uniform_buffer

* expose safe return on result scalar
  • Loading branch information
joyqvq authored Aug 16, 2024
1 parent 2c593b9 commit 0e944fc
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions fastcrypto/src/groups/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,27 @@ impl ScalarType for Scalar {
/// The input buffer must be at least 48 bytes long to ensure that there is only negligible bias in
/// the output.
pub(crate) fn reduce_mod_uniform_buffer(buffer: &[u8]) -> Scalar {
assert!(buffer.len() >= 48);
match buffer_to_scalar_mod_r(buffer) {
Ok(scalar) => scalar,
Err(_) => panic!("Invalid input length"),
}
}

/// Similar to `reduce_mod_uniform_buffer`, returns a result of scalar, and does not panic on invalid length.
pub fn buffer_to_scalar_mod_r(buffer: &[u8]) -> FastCryptoResult<Scalar> {
if buffer.len() < 48 {
return Err(FastCryptoError::InputTooShort(48));
}
if buffer.len() > 64 {
return Err(FastCryptoError::InputTooLong(64));
}
let mut ret = blst_fr::default();
let mut tmp = blst_scalar::default();
unsafe {
blst_scalar_from_be_bytes(&mut tmp, buffer.as_ptr(), buffer.len());
blst_fr_from_scalar(&mut ret, &tmp);
}
Scalar(ret)
Ok(Scalar(ret))
}

impl FiatShamirChallenge for Scalar {
Expand Down

0 comments on commit 0e944fc

Please sign in to comment.