Skip to content

Commit

Permalink
fix: NIF panicked when message wasn't 32 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Apr 22, 2024
1 parent 32dd2d3 commit 1ec70ee
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions native/bls_nif/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ fn sign<'env>(
private_key: Binary,
message: Binary,
) -> Result<Binary<'env>, String> {
if message.len() != 32 {
return Err(format!("Message must be 32 bytes long"));
}
let sk = match SecretKey::deserialize(private_key.as_slice()) {
Ok(sk) => sk,
Err(e) => return Err(format!("{:?}", e)),
Expand Down Expand Up @@ -51,6 +54,9 @@ fn aggregate<'env>(env: Env<'env>, signatures: Vec<Binary>) -> Result<Binary<'en

#[rustler::nif]
fn verify<'env>(public_key: Binary, message: Binary, signature: Binary) -> Result<bool, String> {
if message.len() != 32 {
return Err(format!("Message must be 32 bytes long"));
}
let sig = Signature::deserialize(signature.as_slice()).map_err(|err| format!("{:?}", err))?;
let pubkey =
PublicKey::deserialize(public_key.as_slice()).map_err(|err| format!("{:?}", err))?;
Expand Down Expand Up @@ -86,6 +92,9 @@ fn fast_aggregate_verify<'env>(
message: Binary,
signature: Binary,
) -> Result<bool, String> {
if message.len() != 32 {
return Err(format!("Message must be 32 bytes long"));
}
let aggregate_sig = AggregateSignature::deserialize(signature.as_slice())
.map_err(|err| format!("{:?}", err))?;
let pubkeys_result = public_keys
Expand All @@ -104,6 +113,9 @@ fn eth_fast_aggregate_verify<'env>(
message: Binary,
signature: Binary,
) -> Result<bool, String> {
if message.len() != 32 {
return Err(format!("Message must be 32 bytes long"));
}
let aggregate_sig = AggregateSignature::deserialize(signature.as_slice())
.map_err(|err| format!("{:?}", err))?;
let pubkeys_result = public_keys
Expand Down

0 comments on commit 1ec70ee

Please sign in to comment.