Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
add check for limbs not being less than 2^128
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Sep 8, 2023
1 parent cac232b commit 42c782a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion blackbox_solver/src/barretenberg/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ pub(crate) enum FeatureError {
NoValue,
#[error("Value expected to be i32")]
InvalidI32,
#[error("Value is not a valid grumpkin scalar")]
#[error("Value {scalar_as_hex} is not a valid grumpkin scalar")]
InvalidGrumpkinScalar { scalar_as_hex: String },
#[error("Limb {limb_as_hex} is not less than 2^128")]
InvalidGrumpkinScalarLimb { limb_as_hex: String },
#[error("Could not convert value {value} from i32 to u32")]
InvalidU32 { value: i32, source: std::num::TryFromIntError },
#[error("Could not convert value {value} from i32 to usize")]
Expand Down
12 changes: 12 additions & 0 deletions blackbox_solver/src/barretenberg/wasm/scalar_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ impl ScalarMul for Barretenberg {
let mut bytes = high_16_bytes.to_vec();
bytes.extend_from_slice(&low_16_bytes);

let two_pow_128 = BigUint::from(2u128).pow(128);
if BigUint::from_bytes_be(&low_16_bytes) >= two_pow_128 {
return Err(Error::FromFeature(FeatureError::InvalidGrumpkinScalarLimb {
limb_as_hex: hex::encode(low_16_bytes),
}));
}
if BigUint::from_bytes_be(&high_16_bytes) >= two_pow_128 {
return Err(Error::FromFeature(FeatureError::InvalidGrumpkinScalarLimb {
limb_as_hex: hex::encode(high_16_bytes),
}));
}

// Check if this is smaller than the grumpkin modulus
let grumpkin_integer = BigUint::from_bytes_be(&bytes);
let grumpkin_modulus = BigUint::from_bytes_be(&[
Expand Down

0 comments on commit 42c782a

Please sign in to comment.