From 42c782a719f60b15fefe1b1e11d36b9b257f4be7 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Fri, 8 Sep 2023 22:00:11 +0000 Subject: [PATCH] add check for limbs not being less than 2^128 --- blackbox_solver/src/barretenberg/wasm/mod.rs | 4 +++- blackbox_solver/src/barretenberg/wasm/scalar_mul.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/blackbox_solver/src/barretenberg/wasm/mod.rs b/blackbox_solver/src/barretenberg/wasm/mod.rs index 75ca52de..03d9712d 100644 --- a/blackbox_solver/src/barretenberg/wasm/mod.rs +++ b/blackbox_solver/src/barretenberg/wasm/mod.rs @@ -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")] diff --git a/blackbox_solver/src/barretenberg/wasm/scalar_mul.rs b/blackbox_solver/src/barretenberg/wasm/scalar_mul.rs index c7a30f7c..cabb189e 100644 --- a/blackbox_solver/src/barretenberg/wasm/scalar_mul.rs +++ b/blackbox_solver/src/barretenberg/wasm/scalar_mul.rs @@ -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(&[