Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Error if CKZG or BLS features are disabled, but are called #1448

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions crates/precompile/src/blake2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress};
use revm_primitives::Bytes;
use revm_primitives::{Bytes, PrecompileErrors};

const F_ROUND: u64 = 1;
const INPUT_LENGTH: usize = 213;
Expand All @@ -14,20 +14,20 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let input = &input[..];

if input.len() != INPUT_LENGTH {
return Err(Error::Blake2WrongLength);
return Err(Error::Blake2WrongLength.into());
}

let f = match input[212] {
1 => true,
0 => false,
_ => return Err(Error::Blake2WrongFinalIndicatorFlag),
_ => return Err(Error::Blake2WrongFinalIndicatorFlag.into()),
};

// rounds 4 bytes
let rounds = u32::from_be_bytes(input[..4].try_into().unwrap()) as usize;
let gas_used = rounds as u64 * F_ROUND;
if gas_used > gas_limit {
return Err(Error::OutOfGas);
return Err(Error::OutOfGas.into());
}

let mut h = [0u64; 8];
Expand All @@ -51,7 +51,7 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
out[i..i + 8].copy_from_slice(&h.to_le_bytes());
}

Ok((gas_used, out.into()))
PrecompileResult::ok(gas_used, out.into())
}

pub mod algo {
Expand Down
41 changes: 24 additions & 17 deletions crates/precompile/src/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,35 @@ mod test {

for vector in test_vectors.0 {
let test_name = format!("{file_name}/{}", vector.name);
let input = Bytes::from_hex(vector.input.clone()).unwrap_or_else(|e| {
let input = Bytes::from_hex(&vector.input).unwrap_or_else(|e| {
panic!(
"could not deserialize input {} as hex in {test_name}: {e}",
&vector.input
"could not deserialize input {} as hex in {}: {}",
vector.input, test_name, e
)
});
let target_gas: u64 = 30_000_000;
let res = precompile(&input, target_gas);
if vector.error.unwrap_or_default() {
assert!(res.is_err(), "expected error didn't happen in {test_name}");
} else {
let (actual_gas, actual_output) =
res.unwrap_or_else(|e| panic!("precompile call failed for {test_name}: {e}"));
assert_eq!(
vector.gas, actual_gas,
"expected gas: {}, actual gas: {} in {test_name}",
vector.gas, actual_gas
);
let expected_output = Bytes::from_hex(vector.expected).unwrap();
assert_eq!(
expected_output, actual_output,
"expected output: {expected_output}, actual output: {actual_output} in {test_name}");
match res {
PrecompileResult::Error { .. } if vector.error.unwrap_or_default() => {
// Test passed, it was expected to fail
}
PrecompileResult::Ok {
gas_used: actual_gas,
output: actual_output,
} => {
assert_eq!(
vector.gas, actual_gas,
"expected gas: {}, actual gas: {} in {}",
vector.gas, actual_gas, test_name
);
let expected_output = Bytes::from_hex(&vector.expected).unwrap();
assert_eq!(
expected_output, actual_output,
"expected output: {:?}, actual output: {:?} in {}",
expected_output, actual_output, test_name
);
}
_ => panic!("unexpected result in {}, vector: {:?}", test_name, vector),
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/precompile/src/bls12_381/g1_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{u64_to_address, PrecompileWithAddress};
use blst::{
blst_p1, blst_p1_add_or_double_affine, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine,
};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -23,21 +23,21 @@ const INPUT_LENGTH: usize = 256;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g1-addition>
pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
"G1ADD input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
)).into());
}

// NB: There is no subgroup check for the G1 addition precompile.
//
// So we set the subgroup checks here to `false`
let a_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], false)?;
let b_aff = &extract_g1_input(&input[G1_INPUT_ITEM_LENGTH..], false)?;
let a_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], false).unwrap();
let b_aff = &extract_g1_input(&input[G1_INPUT_ITEM_LENGTH..], false).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why unwrap here?

Copy link
Author

@rupam-04 rupam-04 May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I use expect, then what panic msg should I give? Or should I use something else altogether?


let mut b = blst_p1::default();
// SAFETY: b and b_aff are blst values.
Expand All @@ -52,5 +52,5 @@ pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut p_aff, &p) };

let out = encode_g1_point(&p_aff);
Ok((BASE_GAS_FEE, out))
PrecompileResult::ok(BASE_GAS_FEE, out)
}
15 changes: 8 additions & 7 deletions crates/precompile/src/bls12_381/g1_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
};
use crate::{u64_to_address, PrecompileWithAddress};
use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MSM precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -29,13 +29,13 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
"G1MSM input length should be multiple of {}, was {}",
g1_mul::INPUT_LENGTH,
input_len
)));
)).into());
}

let k = input_len / g1_mul::INPUT_LENGTH;
let required_gas = msm_required_gas(k, g1_mul::BASE_GAS_FEE);
if required_gas > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

let mut g1_points: Vec<blst_p1> = Vec::with_capacity(k);
Expand All @@ -53,7 +53,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
// NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
//
// So we set the subgroup_check flag to `true`
let p0_aff = &extract_g1_input(slice, true)?;
let p0_aff = &extract_g1_input(slice, true).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why unwrap?


let mut p0 = blst_p1::default();
// SAFETY: p0 and p0_aff are blst values.
Expand All @@ -64,14 +64,15 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
&extract_scalar_input(
&input[i * g1_mul::INPUT_LENGTH + G1_INPUT_ITEM_LENGTH
..i * g1_mul::INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + SCALAR_LENGTH],
)?
)
.unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap?

.b,
);
}

// return infinity point if all points are infinity
if g1_points.is_empty() {
return Ok((required_gas, [0; 128].into()));
return PrecompileResult::ok(required_gas, [0; 128].into());
}

let points = p1_affines::from(&g1_points);
Expand All @@ -82,5 +83,5 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut multiexp_aff, &multiexp) };

let out = encode_g1_point(&multiexp_aff);
Ok((required_gas, out))
PrecompileResult::ok(required_gas, out)
}
12 changes: 6 additions & 6 deletions crates/precompile/src/bls12_381/g1_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
};
use crate::{u64_to_address, PrecompileWithAddress};
use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_mult, blst_p1_to_affine};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MUL precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -25,24 +25,24 @@ pub(super) const INPUT_LENGTH: usize = 160;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g1-multiplication>
pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}
if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
"G1MUL input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
)).into());
}

// NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
//
// So we set the subgroup_check flag to `true`
let p0_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], true)?;
let p0_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], true).unwrap();
let mut p0 = blst_p1::default();
// SAFETY: p0 and p0_aff are blst values.
unsafe { blst_p1_from_affine(&mut p0, p0_aff) };

let input_scalar0 = extract_scalar_input(&input[G1_INPUT_ITEM_LENGTH..])?;
let input_scalar0 = extract_scalar_input(&input[G1_INPUT_ITEM_LENGTH..]).unwrap();

let mut p = blst_p1::default();
// SAFETY: input_scalar0.b has fixed size, p and p0 are blst values.
Expand All @@ -52,5 +52,5 @@ pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut p_aff, &p) };

let out = encode_g1_point(&p_aff);
Ok((BASE_GAS_FEE, out))
PrecompileResult::ok(BASE_GAS_FEE, out)
}
12 changes: 6 additions & 6 deletions crates/precompile/src/bls12_381/g2_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{u64_to_address, PrecompileWithAddress};
use blst::{
blst_p2, blst_p2_add_or_double_affine, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine,
};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2ADD precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -24,21 +24,21 @@ const INPUT_LENGTH: usize = 512;
/// See also <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-addition>
pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
"G2ADD input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
)).into());
}

// NB: There is no subgroup check for the G2 addition precompile.
//
// So we set the subgroup checks here to `false`
let a_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH], false)?;
let b_aff = &extract_g2_input(&input[G2_INPUT_ITEM_LENGTH..], false)?;
let a_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH], false).unwrap();
let b_aff = &extract_g2_input(&input[G2_INPUT_ITEM_LENGTH..], false).unwrap();

let mut b = blst_p2::default();
// SAFETY: b and b_aff are blst values.
Expand All @@ -53,5 +53,5 @@ pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p2_to_affine(&mut p_aff, &p) };

let out = encode_g2_point(&p_aff);
Ok((BASE_GAS_FEE, out))
PrecompileResult::ok(BASE_GAS_FEE, out)
}
Loading
Loading