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 all 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
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
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/g1_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 7 additions & 6 deletions crates/precompile/src/bls12_381/g1_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/g1_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/g2_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 7 additions & 6 deletions crates/precompile/src/bls12_381/g2_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
"G2MSM input length should be multiple of {}, was {}",
g2_mul::INPUT_LENGTH,
input_len
)));
)).into());
}

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

let mut g2_points: Vec<blst_p2> = Vec::with_capacity(k);
Expand All @@ -52,7 +52,7 @@ pub(super) fn g2_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_g2_input(slice, true)?;
let p0_aff = &extract_g2_input(slice, true).unwrap();

let mut p0 = blst_p2::default();
// SAFETY: p0 and p0_aff are blst values.
Expand All @@ -64,14 +64,15 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
&extract_scalar_input(
&input[i * g2_mul::INPUT_LENGTH + G2_INPUT_ITEM_LENGTH
..i * g2_mul::INPUT_LENGTH + G2_INPUT_ITEM_LENGTH + SCALAR_LENGTH],
)?
)
.unwrap()
.b,
);
}

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

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

let out = encode_g2_point(&multiexp_aff);
Ok((required_gas, out))
PrecompileResult::ok(required_gas, out)
}
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/g2_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ pub(super) const INPUT_LENGTH: usize = 288;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-multiplication>
pub(super) fn g2_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!(
"G2MUL 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_g2_input(&input[..G2_INPUT_ITEM_LENGTH], true)?;
let p0_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH], true).unwrap();
let mut p0 = blst_p2::default();
// SAFETY: p0 and p0_aff are blst values.
unsafe { blst_p2_from_affine(&mut p0, p0_aff) };

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

let mut p = blst_p2::default();
// SAFETY: input_scalar0.b has fixed size, p and p0 are blst values.
Expand All @@ -52,5 +52,5 @@ pub(super) fn g2_mul(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)
}
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/map_fp2_to_g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ const BASE_GAS_FEE: u64 = 75000;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-mapping-fp2-element-to-g2-point>
pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

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

let input_p0_x = remove_padding(&input[..PADDED_FP_LENGTH])?;
let input_p0_y = remove_padding(&input[PADDED_FP_LENGTH..PADDED_FP2_LENGTH])?;
let input_p0_x = remove_padding(&input[..PADDED_FP_LENGTH]).unwrap();
let input_p0_y = remove_padding(&input[PADDED_FP_LENGTH..PADDED_FP2_LENGTH]).unwrap();

let mut fp2 = blst_fp2::default();
let mut fp_x = blst_fp::default();
Expand All @@ -56,5 +56,5 @@ pub(super) fn map_fp2_to_g2(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)
}
8 changes: 4 additions & 4 deletions crates/precompile/src/bls12_381/map_fp_to_g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ const MAP_FP_TO_G1_BASE: u64 = 5500;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-mapping-fp-element-to-g1-point>
pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if MAP_FP_TO_G1_BASE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

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

let input_p0 = remove_padding(input)?;
let input_p0 = remove_padding(input).unwrap();

let mut fp = blst_fp::default();

Expand All @@ -48,5 +48,5 @@ pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut p_aff, &p) };

let out = encode_g1_point(&p_aff);
Ok((MAP_FP_TO_G1_BASE, out))
PrecompileResult::ok(MAP_FP_TO_G1_BASE, out)
}
Loading
Loading