From c878ea52ff7f260fd54220b44c9fd09bf8bfdc41 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Fri, 24 May 2024 02:32:52 +0530 Subject: [PATCH 1/6] change ```PrecompileResult``` to enum --- crates/precompile/src/blake2.rs | 8 +- crates/precompile/src/bls12_381.rs | 52 ++--- crates/precompile/src/bls12_381/g1_add.rs | 10 +- crates/precompile/src/bls12_381/g1_msm.rs | 12 +- crates/precompile/src/bls12_381/g1_mul.rs | 10 +- crates/precompile/src/bls12_381/g2_add.rs | 10 +- crates/precompile/src/bls12_381/g2_msm.rs | 12 +- crates/precompile/src/bls12_381/g2_mul.rs | 10 +- .../precompile/src/bls12_381/map_fp2_to_g2.rs | 10 +- .../precompile/src/bls12_381/map_fp_to_g1.rs | 8 +- crates/precompile/src/bls12_381/pairing.rs | 10 +- crates/precompile/src/bn128.rs | 190 ++++++++++++------ crates/precompile/src/hash.rs | 8 +- crates/precompile/src/identity.rs | 4 +- crates/precompile/src/kzg_point_evaluation.rs | 22 +- crates/precompile/src/modexp.rs | 62 +++--- crates/precompile/src/secp256k1.rs | 6 +- crates/primitives/src/precompile.rs | 36 +++- crates/revm/src/builder.rs | 2 +- crates/revm/src/context/evm_context.rs | 12 +- 20 files changed, 310 insertions(+), 184 deletions(-) diff --git a/crates/precompile/src/blake2.rs b/crates/precompile/src/blake2.rs index 057bba74d4..40c3169a9e 100644 --- a/crates/precompile/src/blake2.rs +++ b/crates/precompile/src/blake2.rs @@ -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 PrecompileResult::err(Error::Blake2WrongLength); } let f = match input[212] { 1 => true, 0 => false, - _ => return Err(Error::Blake2WrongFinalIndicatorFlag), + _ => return PrecompileResult::err(Error::Blake2WrongFinalIndicatorFlag), }; // 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 PrecompileResult::err(Error::OutOfGas); } let mut h = [0u64; 8]; @@ -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 { diff --git a/crates/precompile/src/bls12_381.rs b/crates/precompile/src/bls12_381.rs index fd5daae00a..3203d89b7f 100644 --- a/crates/precompile/src/bls12_381.rs +++ b/crates/precompile/src/bls12_381.rs @@ -77,39 +77,43 @@ mod test { #[case::map_fp_to_g1(map_fp_to_g1::map_fp_to_g1, "map_fp_to_G1_bls.json")] #[case::map_fp2_to_g2(map_fp2_to_g2::map_fp2_to_g2, "map_fp2_to_G2_bls.json")] fn test_bls( - #[case] precompile: fn(input: &Bytes, gas_limit: u64) -> PrecompileResult, - #[case] file_name: &str, - ) { - let test_vectors = load_test_vectors(format!("test-vectors/{file_name}")) - .unwrap_or_else(|e| panic!("Failed to load test vectors from {file_name}: {e}")); + #[case] precompile: fn(input: &Bytes, gas_limit: u64) -> PrecompileResult, + #[case] file_name: &str, +) { + let test_vectors = load_test_vectors(format!("test-vectors/{}", file_name)) + .unwrap_or_else(|e| panic!("Failed to load test vectors from {}: {}", file_name, e)); - 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| { - panic!( - "could not deserialize input {} as hex in {test_name}: {e}", - &vector.input - ) - }); - 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}")); + for vector in test_vectors.0 { + let test_name = format!("{}/{}", file_name, vector.name); + let input = Bytes::from_hex(&vector.input).unwrap_or_else(|e| { + panic!( + "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); + 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 {test_name}", - vector.gas, actual_gas + "expected gas: {}, actual gas: {} in {}", + vector.gas, actual_gas, test_name ); - let expected_output = Bytes::from_hex(vector.expected).unwrap(); + 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}"); + "expected output: {:?}, actual output: {:?} in {}", + expected_output, actual_output, test_name + ); } + _ => panic!("unexpected result in {}", test_name), } } +} #[rstest] #[case::g1_empty(0, g1_mul::BASE_GAS_FEE, 0)] diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index 9fed002dfc..e24a4274fe 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -23,11 +23,11 @@ const INPUT_LENGTH: usize = 256; /// See also: pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas); + return PrecompileResult::err(PrecompileError::OutOfGas); } if input.len() != INPUT_LENGTH { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "G1ADD input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); @@ -36,8 +36,8 @@ pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { // 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(); let mut b = blst_p1::default(); // SAFETY: b and b_aff are blst values. @@ -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) } diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index c8d086abb6..7e37ec5bbc 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -25,7 +25,7 @@ pub const ADDRESS: u64 = 0x0d; pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % g1_mul::INPUT_LENGTH != 0 { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "G1MSM input length should be multiple of {}, was {}", g1_mul::INPUT_LENGTH, input_len @@ -35,7 +35,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { 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 PrecompileResult::err(PrecompileError::OutOfGas); } let mut g1_points: Vec = Vec::with_capacity(k); @@ -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(); let mut p0 = blst_p1::default(); // SAFETY: p0 and p0_aff are blst values. @@ -64,14 +64,14 @@ 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() .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); @@ -82,5 +82,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) } diff --git a/crates/precompile/src/bls12_381/g1_mul.rs b/crates/precompile/src/bls12_381/g1_mul.rs index 305458e236..9feaf3896b 100644 --- a/crates/precompile/src/bls12_381/g1_mul.rs +++ b/crates/precompile/src/bls12_381/g1_mul.rs @@ -25,10 +25,10 @@ pub(super) const INPUT_LENGTH: usize = 160; /// See also: pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas); + return PrecompileResult::err(PrecompileError::OutOfGas); } if input.len() != INPUT_LENGTH { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "G1MUL input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); @@ -37,12 +37,12 @@ pub(super) fn g1_mul(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(&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. @@ -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) } diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index a8f5b85a8e..67ef9626f5 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -24,11 +24,11 @@ const INPUT_LENGTH: usize = 512; /// See also pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas); + return PrecompileResult::err(PrecompileError::OutOfGas); } if input.len() != INPUT_LENGTH { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "G2ADD input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); @@ -37,8 +37,8 @@ pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { // 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. @@ -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) } diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 85f8208fde..9772975a56 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -25,7 +25,7 @@ pub const ADDRESS: u64 = 0x10; pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % g2_mul::INPUT_LENGTH != 0 { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "G2MSM input length should be multiple of {}, was {}", g2_mul::INPUT_LENGTH, input_len @@ -35,7 +35,7 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { 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 PrecompileResult::err(PrecompileError::OutOfGas); } let mut g2_points: Vec = Vec::with_capacity(k); @@ -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. @@ -64,14 +64,14 @@ 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); @@ -82,5 +82,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) } diff --git a/crates/precompile/src/bls12_381/g2_mul.rs b/crates/precompile/src/bls12_381/g2_mul.rs index 62cb903e9b..6691c292b4 100644 --- a/crates/precompile/src/bls12_381/g2_mul.rs +++ b/crates/precompile/src/bls12_381/g2_mul.rs @@ -25,10 +25,10 @@ pub(super) const INPUT_LENGTH: usize = 288; /// See also: pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas); + return PrecompileResult::err(PrecompileError::OutOfGas); } if input.len() != INPUT_LENGTH { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "G2MUL input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); @@ -37,12 +37,12 @@ pub(super) fn g2_mul(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(&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. @@ -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) } diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 51a90ac313..1855ebdeba 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -23,18 +23,18 @@ const BASE_GAS_FEE: u64 = 75000; /// See also: pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas); + return PrecompileResult::err(PrecompileError::OutOfGas); } if input.len() != PADDED_FP2_LENGTH { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}", input.len() ))); } - 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(); @@ -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) } diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index a6d9f48ecb..45a31f1966 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -21,17 +21,17 @@ const MAP_FP_TO_G1_BASE: u64 = 5500; /// See also: 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 PrecompileResult::err(PrecompileError::OutOfGas); } if input.len() != PADDED_FP_LENGTH { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "MAP_FP_TO_G1 input should be {PADDED_FP_LENGTH} bytes, was {}", input.len() ))); } - let input_p0 = remove_padding(input)?; + let input_p0 = remove_padding(input).unwrap(); let mut fp = blst_fp::default(); @@ -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) } diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index 2a699a0859..d22926f705 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -32,7 +32,7 @@ const INPUT_LENGTH: usize = 384; pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % INPUT_LENGTH != 0 { - return Err(PrecompileError::Other(format!( + return PrecompileResult::err(PrecompileError::Other(format!( "Pairing input length should be multiple of {INPUT_LENGTH}, was {input_len}" ))); } @@ -40,7 +40,7 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let k = input_len / INPUT_LENGTH; let required_gas: u64 = PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_OFFSET_BASE; if required_gas > gas_limit { - return Err(PrecompileError::OutOfGas); + return PrecompileResult::err(PrecompileError::OutOfGas); } // accumulator for the fp12 multiplications of the miller loops. @@ -52,7 +52,7 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let p1_aff = &extract_g1_input( &input[i * INPUT_LENGTH..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH], true, - )?; + ).unwrap(); // NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check. // @@ -61,7 +61,7 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { &input[i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH ..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + G2_INPUT_ITEM_LENGTH], true, - )?; + ).unwrap(); if i > 0 { // after the first slice (i>0) we use cur_ml to store the current @@ -98,5 +98,5 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { result = 1; } } - Ok((required_gas, B256::with_last_byte(result).into())) + PrecompileResult::ok(required_gas, B256::with_last_byte(result).into()) } diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index ce08da7994..60ef77a8ec 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -1,6 +1,6 @@ use crate::{ utilities::{bool_to_bytes32, right_pad}, - Address, Error, Precompile, PrecompileResult, PrecompileWithAddress, + Address, Error, Precompile, PrecompileResult, PrecompileWithAddress }; use bn::{AffineG1, AffineG2, Fq, Fq2, Group, Gt, G1, G2}; @@ -122,30 +122,39 @@ pub fn new_g1_point(px: Fq, py: Fq) -> Result { pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult { if gas_cost > gas_limit { - return Err(Error::OutOfGas); + return PrecompileResult::err(Error::OutOfGas); } let input = right_pad::(input); - let p1 = read_point(&input[..64])?; - let p2 = read_point(&input[64..])?; + let p1 = match read_point(&input[..64]) { + Ok(point) => point, + Err(_) => return PrecompileResult::err(Error::Bn128FieldPointNotAMember), + }; + let p2 = match read_point(&input[64..]) { + Ok(point) => point, + Err(_) => return PrecompileResult::err(Error::Bn128FieldPointNotAMember), + }; let mut output = [0u8; 64]; if let Some(sum) = AffineG1::from_jacobian(p1 + p2) { sum.x().to_big_endian(&mut output[..32]).unwrap(); sum.y().to_big_endian(&mut output[32..]).unwrap(); } - Ok((gas_cost, output.into())) + PrecompileResult::ok(gas_cost, output.into()) } pub fn run_mul(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult { if gas_cost > gas_limit { - return Err(Error::OutOfGas); + return PrecompileResult::err(Error::OutOfGas); } let input = right_pad::(input); - let p = read_point(&input[..64])?; + let p = match read_point(&input[..64]) { + Ok(point) => point, + Err(_) => return PrecompileResult::err(Error::Bn128FieldPointNotAMember), + }; // `Fr::from_slice` can only fail when the length is not 32. let fr = bn::Fr::from_slice(&input[64..96]).unwrap(); @@ -155,7 +164,7 @@ pub fn run_mul(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult mul.x().to_big_endian(&mut output[..32]).unwrap(); mul.y().to_big_endian(&mut output[32..]).unwrap(); } - Ok((gas_cost, output.into())) + PrecompileResult::ok(gas_cost, output.into()) } pub fn run_pair( @@ -166,11 +175,11 @@ pub fn run_pair( ) -> PrecompileResult { let gas_used = (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost; if gas_used > gas_limit { - return Err(Error::OutOfGas); + return PrecompileResult::err(Error::OutOfGas); } if input.len() % PAIR_ELEMENT_LEN != 0 { - return Err(Error::Bn128PairLength); + return PrecompileResult::err(Error::Bn128PairLength); } let success = if input.is_empty() { @@ -186,23 +195,47 @@ pub fn run_pair( // SAFETY: We're reading `6 * 32 == PAIR_ELEMENT_LEN` bytes from `input[idx..]` // per iteration. This is guaranteed to be in-bounds. let slice = unsafe { input.get_unchecked(start..start + 32) }; - Fq::from_slice(slice).map_err(|_| Error::Bn128FieldPointNotAMember) + Fq::from_slice(slice).map_err(|_| PrecompileResult::err(Error::Bn128FieldPointNotAMember)) + }; + let ax = match read_fq_at(0) { + Ok(ax) => ax, + Err(err) => return err, + }; + let ay = match read_fq_at(1) { + Ok(ay) => ay, + Err(err) => return err, + }; + let bay = match read_fq_at(2) { + Ok(bay) => bay, + Err(err) => return err, + }; + let bax = match read_fq_at(3) { + Ok(bax) => bax, + Err(err) => return err, + }; + let bby = match read_fq_at(4) { + Ok(bby) => bby, + Err(err) => return err, + }; + let bbx = match read_fq_at(5) { + Ok(bbx) => bbx, + Err(err) => return err, + }; + + let a = match new_g1_point(ax, ay) { + Ok(a) => a, + Err(_) => return PrecompileResult::err(Error::Bn128AffineGFailedToCreate), }; - let ax = read_fq_at(0)?; - let ay = read_fq_at(1)?; - let bay = read_fq_at(2)?; - let bax = read_fq_at(3)?; - let bby = read_fq_at(4)?; - let bbx = read_fq_at(5)?; - - let a = new_g1_point(ax, ay)?; let b = { let ba = Fq2::new(bax, bay); let bb = Fq2::new(bbx, bby); if ba.is_zero() && bb.is_zero() { G2::zero() } else { - G2::from(AffineG2::new(ba, bb).map_err(|_| Error::Bn128AffineGFailedToCreate)?) + match AffineG2::new(ba, bb) { + Ok(affine_g2) => G2::from(affine_g2), + Err(_) => return PrecompileResult::err(Error::Bn128AffineGFailedToCreate), + } } }; @@ -211,7 +244,7 @@ pub fn run_pair( mul == Gt::one() }; - Ok((gas_used, bool_to_bytes32(success))) + PrecompileResult::ok(gas_used, bool_to_bytes32(success)) } #[cfg(test)] @@ -219,6 +252,7 @@ mod tests { use crate::bn128::add::BYZANTIUM_ADD_GAS_COST; use crate::bn128::mul::BYZANTIUM_MUL_GAS_COST; use crate::bn128::pair::{BYZANTIUM_PAIR_BASE, BYZANTIUM_PAIR_PER_POINT}; + use crate::PrecompileError; use revm_primitives::hex; use super::*; @@ -240,8 +274,12 @@ mod tests { ) .unwrap(); - let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); - assert_eq!(res, expected); + let run_result = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); + match run_result { + PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), + PrecompileResult::Error { error_type } => panic!("run_add failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("run_add failed with fatal error: {:?}", msg), + } // zero sum test let input = hex::decode( @@ -259,8 +297,12 @@ mod tests { ) .unwrap(); - let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); - assert_eq!(res, expected); + let run_result = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); + match run_result { + PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), + PrecompileResult::Error { error_type } => panic!("run_add failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("run_add failed with fatal error: {:?}", msg), + } // out of gas test let input = hex::decode( @@ -273,8 +315,11 @@ mod tests { .unwrap(); let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 499); - println!("{:?}", res); - assert!(matches!(res, Err(Error::OutOfGas))); + match res { + PrecompileResult::Ok { .. } => panic!("Expected an OutOfGas error, but got Ok"), + PrecompileResult::Error { error_type } => assert_eq!(error_type, Error::OutOfGas), + PrecompileResult::FatalError { msg } => panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg), + } // no input test let input = [0u8; 0]; @@ -285,8 +330,12 @@ mod tests { ) .unwrap(); - let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); - assert_eq!(res, expected); + let run_result = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); + match run_result { + PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), + PrecompileResult::Error { error_type } => panic!("run_add failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("run_add failed with fatal error: {:?}", msg), + } // point not on curve fail let input = hex::decode( @@ -299,7 +348,11 @@ mod tests { .unwrap(); let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); - assert!(matches!(res, Err(Error::Bn128AffineGFailedToCreate))); + match res { + PrecompileResult::Ok { .. } => panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok"), + PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::Bn128AffineGFailedToCreate), + PrecompileResult::FatalError { msg } => panic!("Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", msg), + } } #[test] @@ -318,8 +371,12 @@ mod tests { ) .unwrap(); - let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); - assert_eq!(res, expected); + let run_result = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); + match run_result { + PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), + PrecompileResult::Error { error_type } => panic!("run_mul failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("run_mul failed with fatal error: {:?}", msg), + } // out of gas test let input = hex::decode( @@ -331,7 +388,11 @@ mod tests { .unwrap(); let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 39_999); - assert!(matches!(res, Err(Error::OutOfGas))); + match res { + PrecompileResult::Ok { .. } => panic!("Expected an OutOfGas error, but got Ok"), + PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::OutOfGas), + PrecompileResult::FatalError { msg } => panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg), + } // zero multiplication test let input = hex::decode( @@ -348,8 +409,12 @@ mod tests { ) .unwrap(); - let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); - assert_eq!(res, expected); + let run_result = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); + match run_result { + PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), + PrecompileResult::Error { error_type } => panic!("run_mul failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("run_mul failed with fatal error: {:?}", msg), + } // no input test let input = [0u8; 0]; @@ -360,8 +425,12 @@ mod tests { ) .unwrap(); - let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); - assert_eq!(res, expected); + let run_result = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); + match run_result { + PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), + PrecompileResult::Error { error_type } => panic!("run_mul failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("run_mul failed with fatal error: {:?}", msg), + } // point not on curve fail let input = hex::decode( @@ -373,7 +442,11 @@ mod tests { .unwrap(); let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); - assert!(matches!(res, Err(Error::Bn128AffineGFailedToCreate))); + match res { + PrecompileResult::Ok { .. } => panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok"), + PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::Bn128AffineGFailedToCreate), + PrecompileResult::FatalError { msg } => panic!("Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", msg), + } } #[test] @@ -425,13 +498,12 @@ mod tests { ) .unwrap(); - let res = run_pair( - &input, - BYZANTIUM_PAIR_PER_POINT, - BYZANTIUM_PAIR_BASE, - 259_999, - ); - assert!(matches!(res, Err(Error::OutOfGas))); + let run_result = run_pair(&input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, 259_999); + match run_result { + PrecompileResult::Ok { .. } => panic!("Expected an OutOfGas error, but got Ok"), + PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::OutOfGas), + PrecompileResult::FatalError { msg } => panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg), + } // no input test let input = [0u8; 0]; @@ -447,7 +519,7 @@ mod tests { ) .unwrap(); assert_eq!(res, expected); - + // point not on curve fail let input = hex::decode( "\ @@ -460,13 +532,12 @@ mod tests { ) .unwrap(); - let res = run_pair( - &input, - BYZANTIUM_PAIR_PER_POINT, - BYZANTIUM_PAIR_BASE, - 260_000, - ); - assert!(matches!(res, Err(Error::Bn128AffineGFailedToCreate))); + let run_result = run_pair(&input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, 260_000); + match run_result { + PrecompileResult::Ok { .. } => panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok"), + PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::Bn128AffineGFailedToCreate), + PrecompileResult::FatalError { msg } => panic!("Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", msg), + } // invalid input length let input = hex::decode( @@ -478,12 +549,11 @@ mod tests { ) .unwrap(); - let res = run_pair( - &input, - BYZANTIUM_PAIR_PER_POINT, - BYZANTIUM_PAIR_BASE, - 260_000, - ); - assert!(matches!(res, Err(Error::Bn128PairLength))); + let run_result = run_pair(&input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, 260_000); + match run_result { + PrecompileResult::Ok { .. } => panic!("Expected a Bn128PairLength error, but got Ok"), + PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::Bn128PairLength), + PrecompileResult::FatalError { msg } => panic!("Expected a Bn128PairLength error, but got FatalError: {:?}", msg), + } } } diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index 9fc6a8ae49..2ac972a2d0 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -17,10 +17,10 @@ pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( pub fn sha256_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let cost = calc_linear_cost_u32(input.len(), 60, 12); if cost > gas_limit { - Err(Error::OutOfGas) + PrecompileResult::err(Error::OutOfGas) } else { let output = sha2::Sha256::digest(input); - Ok((cost, output.to_vec().into())) + PrecompileResult::ok(cost, output.to_vec().into()) } } @@ -30,13 +30,13 @@ pub fn sha256_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { pub fn ripemd160_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let gas_used = calc_linear_cost_u32(input.len(), 600, 120); if gas_used > gas_limit { - Err(Error::OutOfGas) + PrecompileResult::err(Error::OutOfGas) } else { let mut hasher = ripemd::Ripemd160::new(); hasher.update(input); let mut output = [0u8; 32]; hasher.finalize_into((&mut output[12..]).into()); - Ok((gas_used, output.to_vec().into())) + PrecompileResult::ok(gas_used, output.to_vec().into()) } } diff --git a/crates/precompile/src/identity.rs b/crates/precompile/src/identity.rs index 85722ea810..11e7b17fb4 100644 --- a/crates/precompile/src/identity.rs +++ b/crates/precompile/src/identity.rs @@ -17,7 +17,7 @@ pub const IDENTITY_PER_WORD: u64 = 3; pub fn identity_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let gas_used = calc_linear_cost_u32(input.len(), IDENTITY_BASE, IDENTITY_PER_WORD); if gas_used > gas_limit { - return Err(Error::OutOfGas); + return PrecompileResult::err(Error::OutOfGas); } - Ok((gas_used, input.clone())) + PrecompileResult::ok(gas_used, input.clone()) } diff --git a/crates/precompile/src/kzg_point_evaluation.rs b/crates/precompile/src/kzg_point_evaluation.rs index 5790186a99..130a6335be 100644 --- a/crates/precompile/src/kzg_point_evaluation.rs +++ b/crates/precompile/src/kzg_point_evaluation.rs @@ -26,19 +26,19 @@ pub const RETURN_VALUE: &[u8; 64] = &hex!( /// with z and y being padded 32 byte big endian values pub fn run(input: &Bytes, gas_limit: u64, env: &Env) -> PrecompileResult { if gas_limit < GAS_COST { - return Err(Error::OutOfGas); + return PrecompileResult::err(Error::OutOfGas); } // Verify input length. if input.len() != 192 { - return Err(Error::BlobInvalidInputLength); + return PrecompileResult::err(Error::BlobInvalidInputLength); } // Verify commitment matches versioned_hash let versioned_hash = &input[..32]; let commitment = &input[96..144]; if kzg_to_versioned_hash(commitment) != versioned_hash { - return Err(Error::BlobMismatchedVersion); + return PrecompileResult::err(Error::BlobMismatchedVersion); } // Verify KZG proof with z and y in big endian format @@ -47,11 +47,11 @@ pub fn run(input: &Bytes, gas_limit: u64, env: &Env) -> PrecompileResult { let y = as_bytes32(&input[64..96]); let proof = as_bytes48(&input[144..192]); if !verify_kzg_proof(commitment, z, y, proof, env.cfg.kzg_settings.get()) { - return Err(Error::BlobVerifyKzgProofFailed); + return PrecompileResult::err(Error::BlobVerifyKzgProofFailed); } // Return FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS as padded 32 byte big endian values - Ok((GAS_COST, RETURN_VALUE.into())) + PrecompileResult::ok(GAS_COST, RETURN_VALUE.into()) } /// `VERSIONED_HASH_VERSION_KZG ++ sha256(commitment)[1..]` @@ -113,8 +113,14 @@ mod tests { let expected_output = hex!("000000000000000000000000000000000000000000000000000000000000100073eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"); let gas = 50000; let env = Env::default(); - let (actual_gas, actual_output) = run(&input.into(), gas, &env).unwrap(); - assert_eq!(actual_gas, gas); - assert_eq!(actual_output[..], expected_output); + let run_result = run(&input.into(), gas, &env); + match run_result { + PrecompileResult::Ok { gas_used, output } => { + assert_eq!(gas_used, gas); + assert_eq!(output[..], expected_output); + }, + PrecompileResult::Error { error_type } => panic!("run failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("run failed with fatal error: {:?}", msg), + } } } diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index a55b445912..6579130098 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -50,7 +50,7 @@ where { // If there is no minimum gas, return error. if min_gas > gas_limit { - return Err(Error::OutOfGas); + return PrecompileResult::err(Error::OutOfGas); } // The format of input is: @@ -66,20 +66,20 @@ where // cast base and modulus to usize, it does not make sense to handle larger values let Ok(base_len) = usize::try_from(base_len) else { - return Err(Error::ModexpBaseOverflow); + return PrecompileResult::err(Error::ModexpBaseOverflow); }; let Ok(mod_len) = usize::try_from(mod_len) else { - return Err(Error::ModexpModOverflow); + return PrecompileResult::err(Error::ModexpModOverflow); }; // Handle a special case when both the base and mod length are zero. if base_len == 0 && mod_len == 0 { - return Ok((min_gas, Bytes::new())); + return PrecompileResult::ok(min_gas, Bytes::new()); } // Cast exponent length to usize, since it does not make sense to handle larger values. let Ok(exp_len) = usize::try_from(exp_len) else { - return Err(Error::ModexpModOverflow); + return PrecompileResult::err(Error::ModexpModOverflow); }; // Used to extract ADJUSTED_EXPONENT_LENGTH. @@ -99,7 +99,7 @@ where // Check if we have enough gas. let gas_cost = calc_gas(base_len as u64, exp_len as u64, mod_len as u64, &exp_highp); if gas_cost > gas_limit { - return Err(Error::OutOfGas); + return PrecompileResult::err(Error::OutOfGas); } // Padding is needed if the input does not contain all 3 values. @@ -113,7 +113,7 @@ where let output = modexp(base, exponent, modulus); // left pad the result to modulus length. bytes will always by less or equal to modulus length. - Ok((gas_cost, left_pad_vec(&output, mod_len).into_owned().into())) + PrecompileResult::ok(gas_cost, left_pad_vec(&output, mod_len).into_owned().into()) } pub fn byzantium_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_highp: &U256) -> u64 { @@ -347,14 +347,16 @@ mod tests { fn test_byzantium_modexp_gas() { for (test, &test_gas) in TESTS.iter().zip(BYZANTIUM_GAS.iter()) { let input = hex::decode(test.input).unwrap().into(); - let res = byzantium_run(&input, 100_000_000).unwrap(); - let expected = hex::decode(test.expected).unwrap(); - assert_eq!( - res.0, test_gas, - "used gas not matching for test: {}", - test.name - ); - assert_eq!(res.1, expected, "test:{}", test.name); + let run_result = byzantium_run(&input, 100_000_000); + match run_result { + PrecompileResult::Ok { gas_used, output } => { + let expected = hex::decode(test.expected).unwrap(); + assert_eq!(gas_used, test_gas, "used gas not matching for test: {}", test.name); + assert_eq!(output, expected, "test:{}", test.name); + }, + PrecompileResult::Error { error_type } => panic!("byzantium_run failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("byzantium_run failed with fatal error: {:?}", msg), + } } } @@ -362,21 +364,29 @@ mod tests { fn test_berlin_modexp_gas() { for (test, &test_gas) in TESTS.iter().zip(BERLIN_GAS.iter()) { let input = hex::decode(test.input).unwrap().into(); - let res = berlin_run(&input, 100_000_000).unwrap(); - let expected = hex::decode(test.expected).unwrap(); - assert_eq!( - res.0, test_gas, - "used gas not matching for test: {}", - test.name - ); - assert_eq!(res.1, expected, "test:{}", test.name); + let run_result = berlin_run(&input, 100_000_000); + match run_result { + PrecompileResult::Ok { gas_used, output } => { + let expected = hex::decode(test.expected).unwrap(); + assert_eq!(gas_used, test_gas, "used gas not matching for test: {}", test.name); + assert_eq!(output, expected, "test:{}", test.name); + }, + PrecompileResult::Error { error_type } => panic!("berlin_run failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("berlin_run failed with fatal error: {:?}", msg), + } } } #[test] fn test_berlin_modexp_empty_input() { - let res = berlin_run(&Bytes::new(), 100_000).unwrap(); - let expected: Vec = Vec::new(); - assert_eq!(res.1, expected) + let run_result = berlin_run(&Bytes::new(), 100_000); + match run_result { + PrecompileResult::Ok { gas_used: _, output } => { + let expected: Vec = Vec::new(); + assert_eq!(output, expected); + }, + PrecompileResult::Error { error_type } => panic!("berlin_run failed with error: {:?}", error_type), + PrecompileResult::FatalError { msg } => panic!("berlin_run failed with fatal error: {:?}", msg), + } } } diff --git a/crates/precompile/src/secp256k1.rs b/crates/precompile/src/secp256k1.rs index 4ad079e1e7..27ae6200d9 100644 --- a/crates/precompile/src/secp256k1.rs +++ b/crates/precompile/src/secp256k1.rs @@ -70,14 +70,14 @@ pub fn ec_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { const ECRECOVER_BASE: u64 = 3_000; if ECRECOVER_BASE > gas_limit { - return Err(Error::OutOfGas); + return PrecompileResult::err(Error::OutOfGas); } let input = right_pad::<128>(input); // `v` must be a 32-byte big-endian integer equal to 27 or 28. if !(input[32..63].iter().all(|&b| b == 0) && matches!(input[63], 27 | 28)) { - return Ok((ECRECOVER_BASE, Bytes::new())); + return PrecompileResult::ok(ECRECOVER_BASE, Bytes::new()); } let msg = <&B256>::try_from(&input[0..32]).unwrap(); @@ -87,5 +87,5 @@ pub fn ec_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let out = secp256k1::ecrecover(sig, recid, msg) .map(|o| o.to_vec().into()) .unwrap_or_default(); - Ok((ECRECOVER_BASE, out)) + PrecompileResult::ok(ECRECOVER_BASE, out) } diff --git a/crates/primitives/src/precompile.rs b/crates/primitives/src/precompile.rs index e585377537..5802d58b86 100644 --- a/crates/primitives/src/precompile.rs +++ b/crates/primitives/src/precompile.rs @@ -6,7 +6,39 @@ use std::{boxed::Box, string::String, sync::Arc}; /// A precompile operation result. /// /// Returns either `Ok((gas_used, return_bytes))` or `Err(error)`. -pub type PrecompileResult = Result<(u64, Bytes), PrecompileError>; +pub enum PrecompileResult { + Ok { + gas_used: u64, + output: Bytes, + }, + Error { + error_type: PrecompileError, + }, + FatalError { + msg: String, + } +} + +impl PrecompileResult { + pub fn ok(gas_used: u64, output: Bytes) -> Self { + Self::Ok { gas_used, output } + } + + pub fn err(error_type: PrecompileError) -> Self { + Self::Error { error_type } + } + + pub fn fatal_error(msg: impl Into) -> Self { + Self::FatalError { msg: msg.into() } + } + + pub fn unwrap(self) -> (u64, Bytes) { + match self { + Self::Ok { gas_used, output } => (gas_used, output), + _ => panic!("called `PrecompileResult::unwrap()` on an `Error` value"), + } + } +} pub type StandardPrecompileFn = fn(&Bytes, u64) -> PrecompileResult; pub type EnvPrecompileFn = fn(&Bytes, u64, env: &Env) -> PrecompileResult; @@ -175,7 +207,7 @@ mod test { _gas_price: u64, _env: &Env, ) -> PrecompileResult { - PrecompileResult::Err(PrecompileError::OutOfGas) + PrecompileResult::err(PrecompileError::OutOfGas) } } diff --git a/crates/revm/src/builder.rs b/crates/revm/src/builder.rs index 4b2a81ec4f..b9d9a03656 100644 --- a/crates/revm/src/builder.rs +++ b/crates/revm/src/builder.rs @@ -614,7 +614,7 @@ mod test { _gas_price: u64, _context: &mut InnerEvmContext, ) -> PrecompileResult { - Ok((10, Bytes::new())) + PrecompileResult::ok(10, Bytes::new()) } } diff --git a/crates/revm/src/context/evm_context.rs b/crates/revm/src/context/evm_context.rs index b92d711da8..c560584e93 100644 --- a/crates/revm/src/context/evm_context.rs +++ b/crates/revm/src/context/evm_context.rs @@ -1,4 +1,5 @@ use revm_interpreter::CallValue; +use revm_precompile::PrecompileResult; use super::inner_evm_context::InnerEvmContext; use crate::{ @@ -119,21 +120,24 @@ impl EvmContext { }; match out { - Ok((gas_used, data)) => { + PrecompileResult::Ok { gas_used, output } => { if result.gas.record_cost(gas_used) { result.result = InstructionResult::Return; - result.output = data; + result.output = output; } else { result.result = InstructionResult::PrecompileOOG; } } - Err(e) => { - result.result = if e == crate::precompile::Error::OutOfGas { + PrecompileResult::Error { error_type } => { + result.result = if error_type == crate::precompile::Error::OutOfGas { InstructionResult::PrecompileOOG } else { InstructionResult::PrecompileError }; } + PrecompileResult::FatalError { msg: _ } => { + result.result = InstructionResult::FatalExternalError; + } } Some(result) } From 69b65f81d5a8731520908fe27ad600baa66086cd Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Fri, 24 May 2024 02:56:42 +0530 Subject: [PATCH 2/6] fixed tests and fmt --- crates/precompile/src/bls12_381.rs | 69 +++++---- crates/precompile/src/bls12_381/g1_msm.rs | 3 +- crates/precompile/src/bls12_381/g2_msm.rs | 3 +- crates/precompile/src/bls12_381/pairing.rs | 6 +- crates/precompile/src/bn128.rs | 144 +++++++++++------- crates/precompile/src/kzg_point_evaluation.rs | 10 +- crates/precompile/src/modexp.rs | 47 ++++-- crates/primitives/src/precompile.rs | 15 +- 8 files changed, 176 insertions(+), 121 deletions(-) diff --git a/crates/precompile/src/bls12_381.rs b/crates/precompile/src/bls12_381.rs index 3203d89b7f..52cd760272 100644 --- a/crates/precompile/src/bls12_381.rs +++ b/crates/precompile/src/bls12_381.rs @@ -77,43 +77,46 @@ mod test { #[case::map_fp_to_g1(map_fp_to_g1::map_fp_to_g1, "map_fp_to_G1_bls.json")] #[case::map_fp2_to_g2(map_fp2_to_g2::map_fp2_to_g2, "map_fp2_to_G2_bls.json")] fn test_bls( - #[case] precompile: fn(input: &Bytes, gas_limit: u64) -> PrecompileResult, - #[case] file_name: &str, -) { - let test_vectors = load_test_vectors(format!("test-vectors/{}", file_name)) - .unwrap_or_else(|e| panic!("Failed to load test vectors from {}: {}", file_name, e)); + #[case] precompile: fn(input: &Bytes, gas_limit: u64) -> PrecompileResult, + #[case] file_name: &str, + ) { + let test_vectors = load_test_vectors(format!("test-vectors/{}", file_name)) + .unwrap_or_else(|e| panic!("Failed to load test vectors from {}: {}", file_name, e)); - for vector in test_vectors.0 { - let test_name = format!("{}/{}", file_name, vector.name); - let input = Bytes::from_hex(&vector.input).unwrap_or_else(|e| { - panic!( - "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); - 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 - ); + for vector in test_vectors.0 { + let test_name = format!("{}/{}", file_name, vector.name); + let input = Bytes::from_hex(&vector.input).unwrap_or_else(|e| { + panic!( + "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); + 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 {}", test_name), } - _ => panic!("unexpected result in {}", test_name), } } -} #[rstest] #[case::g1_empty(0, g1_mul::BASE_GAS_FEE, 0)] diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index 7e37ec5bbc..e6171893af 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -64,7 +64,8 @@ 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() + ) + .unwrap() .b, ); } diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 9772975a56..919e5355d3 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -64,7 +64,8 @@ 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() + ) + .unwrap() .b, ); } diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index d22926f705..a0574155e2 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -52,7 +52,8 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let p1_aff = &extract_g1_input( &input[i * INPUT_LENGTH..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH], true, - ).unwrap(); + ) + .unwrap(); // NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check. // @@ -61,7 +62,8 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { &input[i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH ..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + G2_INPUT_ITEM_LENGTH], true, - ).unwrap(); + ) + .unwrap(); if i > 0 { // after the first slice (i>0) we use cur_ml to store the current diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index 60ef77a8ec..889d38e4d9 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -1,6 +1,6 @@ use crate::{ utilities::{bool_to_bytes32, right_pad}, - Address, Error, Precompile, PrecompileResult, PrecompileWithAddress + Address, Error, Precompile, PrecompileResult, PrecompileWithAddress, }; use bn::{AffineG1, AffineG2, Fq, Fq2, Group, Gt, G1, G2}; @@ -195,7 +195,8 @@ pub fn run_pair( // SAFETY: We're reading `6 * 32 == PAIR_ELEMENT_LEN` bytes from `input[idx..]` // per iteration. This is guaranteed to be in-bounds. let slice = unsafe { input.get_unchecked(start..start + 32) }; - Fq::from_slice(slice).map_err(|_| PrecompileResult::err(Error::Bn128FieldPointNotAMember)) + Fq::from_slice(slice) + .map_err(|_| PrecompileResult::err(Error::Bn128FieldPointNotAMember)) }; let ax = match read_fq_at(0) { Ok(ax) => ax, @@ -274,12 +275,8 @@ mod tests { ) .unwrap(); - let run_result = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); - match run_result { - PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), - PrecompileResult::Error { error_type } => panic!("run_add failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("run_add failed with fatal error: {:?}", msg), - } + let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); + assert_eq!(res, expected); // zero sum test let input = hex::decode( @@ -297,12 +294,8 @@ mod tests { ) .unwrap(); - let run_result = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); - match run_result { - PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), - PrecompileResult::Error { error_type } => panic!("run_add failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("run_add failed with fatal error: {:?}", msg), - } + let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); + assert_eq!(res, expected); // out of gas test let input = hex::decode( @@ -318,7 +311,9 @@ mod tests { match res { PrecompileResult::Ok { .. } => panic!("Expected an OutOfGas error, but got Ok"), PrecompileResult::Error { error_type } => assert_eq!(error_type, Error::OutOfGas), - PrecompileResult::FatalError { msg } => panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg), + PrecompileResult::FatalError { msg } => { + panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg) + } } // no input test @@ -330,12 +325,8 @@ mod tests { ) .unwrap(); - let run_result = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); - match run_result { - PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), - PrecompileResult::Error { error_type } => panic!("run_add failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("run_add failed with fatal error: {:?}", msg), - } + let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); + assert_eq!(res, expected); // point not on curve fail let input = hex::decode( @@ -349,9 +340,16 @@ mod tests { let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); match res { - PrecompileResult::Ok { .. } => panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok"), - PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::Bn128AffineGFailedToCreate), - PrecompileResult::FatalError { msg } => panic!("Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", msg), + PrecompileResult::Ok { .. } => { + panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok") + } + PrecompileResult::Error { error_type } => { + assert_eq!(error_type, PrecompileError::Bn128FieldPointNotAMember) + } + PrecompileResult::FatalError { msg } => panic!( + "Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", + msg + ), } } @@ -371,12 +369,8 @@ mod tests { ) .unwrap(); - let run_result = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); - match run_result { - PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), - PrecompileResult::Error { error_type } => panic!("run_mul failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("run_mul failed with fatal error: {:?}", msg), - } + let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); + assert_eq!(res, expected); // out of gas test let input = hex::decode( @@ -390,8 +384,12 @@ mod tests { let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 39_999); match res { PrecompileResult::Ok { .. } => panic!("Expected an OutOfGas error, but got Ok"), - PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::OutOfGas), - PrecompileResult::FatalError { msg } => panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg), + PrecompileResult::Error { error_type } => { + assert_eq!(error_type, PrecompileError::OutOfGas) + } + PrecompileResult::FatalError { msg } => { + panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg) + } } // zero multiplication test @@ -409,12 +407,8 @@ mod tests { ) .unwrap(); - let run_result = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); - match run_result { - PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), - PrecompileResult::Error { error_type } => panic!("run_mul failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("run_mul failed with fatal error: {:?}", msg), - } + let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); + assert_eq!(res, expected); // no input test let input = [0u8; 0]; @@ -425,12 +419,8 @@ mod tests { ) .unwrap(); - let run_result = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); - match run_result { - PrecompileResult::Ok { output, .. } => assert_eq!(output, expected), - PrecompileResult::Error { error_type } => panic!("run_mul failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("run_mul failed with fatal error: {:?}", msg), - } + let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); + assert_eq!(res, expected); // point not on curve fail let input = hex::decode( @@ -443,9 +433,16 @@ mod tests { let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); match res { - PrecompileResult::Ok { .. } => panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok"), - PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::Bn128AffineGFailedToCreate), - PrecompileResult::FatalError { msg } => panic!("Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", msg), + PrecompileResult::Ok { .. } => { + panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok") + } + PrecompileResult::Error { error_type } => { + assert_eq!(error_type, PrecompileError::Bn128FieldPointNotAMember) + } + PrecompileResult::FatalError { msg } => panic!( + "Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", + msg + ), } } @@ -498,11 +495,20 @@ mod tests { ) .unwrap(); - let run_result = run_pair(&input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, 259_999); + let run_result = run_pair( + &input, + BYZANTIUM_PAIR_PER_POINT, + BYZANTIUM_PAIR_BASE, + 259_999, + ); match run_result { PrecompileResult::Ok { .. } => panic!("Expected an OutOfGas error, but got Ok"), - PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::OutOfGas), - PrecompileResult::FatalError { msg } => panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg), + PrecompileResult::Error { error_type } => { + assert_eq!(error_type, PrecompileError::OutOfGas) + } + PrecompileResult::FatalError { msg } => { + panic!("Expected an OutOfGas error, but got FatalError: {:?}", msg) + } } // no input test @@ -519,7 +525,7 @@ mod tests { ) .unwrap(); assert_eq!(res, expected); - + // point not on curve fail let input = hex::decode( "\ @@ -532,11 +538,23 @@ mod tests { ) .unwrap(); - let run_result = run_pair(&input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, 260_000); + let run_result = run_pair( + &input, + BYZANTIUM_PAIR_PER_POINT, + BYZANTIUM_PAIR_BASE, + 260_000, + ); match run_result { - PrecompileResult::Ok { .. } => panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok"), - PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::Bn128AffineGFailedToCreate), - PrecompileResult::FatalError { msg } => panic!("Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", msg), + PrecompileResult::Ok { .. } => { + panic!("Expected a Bn128AffineGFailedToCreate error, but got Ok") + } + PrecompileResult::Error { error_type } => { + assert_eq!(error_type, PrecompileError::Bn128AffineGFailedToCreate) + } + PrecompileResult::FatalError { msg } => panic!( + "Expected a Bn128AffineGFailedToCreate error, but got FatalError: {:?}", + msg + ), } // invalid input length @@ -549,11 +567,21 @@ mod tests { ) .unwrap(); - let run_result = run_pair(&input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, 260_000); + let run_result = run_pair( + &input, + BYZANTIUM_PAIR_PER_POINT, + BYZANTIUM_PAIR_BASE, + 260_000, + ); match run_result { PrecompileResult::Ok { .. } => panic!("Expected a Bn128PairLength error, but got Ok"), - PrecompileResult::Error { error_type } => assert_eq!(error_type, PrecompileError::Bn128PairLength), - PrecompileResult::FatalError { msg } => panic!("Expected a Bn128PairLength error, but got FatalError: {:?}", msg), + PrecompileResult::Error { error_type } => { + assert_eq!(error_type, PrecompileError::Bn128PairLength) + } + PrecompileResult::FatalError { msg } => panic!( + "Expected a Bn128PairLength error, but got FatalError: {:?}", + msg + ), } } } diff --git a/crates/precompile/src/kzg_point_evaluation.rs b/crates/precompile/src/kzg_point_evaluation.rs index 130a6335be..3570d07e80 100644 --- a/crates/precompile/src/kzg_point_evaluation.rs +++ b/crates/precompile/src/kzg_point_evaluation.rs @@ -118,9 +118,13 @@ mod tests { PrecompileResult::Ok { gas_used, output } => { assert_eq!(gas_used, gas); assert_eq!(output[..], expected_output); - }, - PrecompileResult::Error { error_type } => panic!("run failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("run failed with fatal error: {:?}", msg), + } + PrecompileResult::Error { error_type } => { + panic!("run failed with error: {:?}", error_type) + } + PrecompileResult::FatalError { msg } => { + panic!("run failed with fatal error: {:?}", msg) + } } } } diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index 6579130098..69bfe96937 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -351,11 +351,19 @@ mod tests { match run_result { PrecompileResult::Ok { gas_used, output } => { let expected = hex::decode(test.expected).unwrap(); - assert_eq!(gas_used, test_gas, "used gas not matching for test: {}", test.name); + assert_eq!( + gas_used, test_gas, + "used gas not matching for test: {}", + test.name + ); assert_eq!(output, expected, "test:{}", test.name); - }, - PrecompileResult::Error { error_type } => panic!("byzantium_run failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("byzantium_run failed with fatal error: {:?}", msg), + } + PrecompileResult::Error { error_type } => { + panic!("byzantium_run failed with error: {:?}", error_type) + } + PrecompileResult::FatalError { msg } => { + panic!("byzantium_run failed with fatal error: {:?}", msg) + } } } } @@ -368,11 +376,19 @@ mod tests { match run_result { PrecompileResult::Ok { gas_used, output } => { let expected = hex::decode(test.expected).unwrap(); - assert_eq!(gas_used, test_gas, "used gas not matching for test: {}", test.name); + assert_eq!( + gas_used, test_gas, + "used gas not matching for test: {}", + test.name + ); assert_eq!(output, expected, "test:{}", test.name); - }, - PrecompileResult::Error { error_type } => panic!("berlin_run failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("berlin_run failed with fatal error: {:?}", msg), + } + PrecompileResult::Error { error_type } => { + panic!("berlin_run failed with error: {:?}", error_type) + } + PrecompileResult::FatalError { msg } => { + panic!("berlin_run failed with fatal error: {:?}", msg) + } } } } @@ -381,12 +397,19 @@ mod tests { fn test_berlin_modexp_empty_input() { let run_result = berlin_run(&Bytes::new(), 100_000); match run_result { - PrecompileResult::Ok { gas_used: _, output } => { + PrecompileResult::Ok { + gas_used: _, + output, + } => { let expected: Vec = Vec::new(); assert_eq!(output, expected); - }, - PrecompileResult::Error { error_type } => panic!("berlin_run failed with error: {:?}", error_type), - PrecompileResult::FatalError { msg } => panic!("berlin_run failed with fatal error: {:?}", msg), + } + PrecompileResult::Error { error_type } => { + panic!("berlin_run failed with error: {:?}", error_type) + } + PrecompileResult::FatalError { msg } => { + panic!("berlin_run failed with fatal error: {:?}", msg) + } } } } diff --git a/crates/primitives/src/precompile.rs b/crates/primitives/src/precompile.rs index 5802d58b86..dc4d8ceadb 100644 --- a/crates/primitives/src/precompile.rs +++ b/crates/primitives/src/precompile.rs @@ -7,16 +7,9 @@ use std::{boxed::Box, string::String, sync::Arc}; /// /// Returns either `Ok((gas_used, return_bytes))` or `Err(error)`. pub enum PrecompileResult { - Ok { - gas_used: u64, - output: Bytes, - }, - Error { - error_type: PrecompileError, - }, - FatalError { - msg: String, - } + Ok { gas_used: u64, output: Bytes }, + Error { error_type: PrecompileError }, + FatalError { msg: String }, } impl PrecompileResult { @@ -31,7 +24,7 @@ impl PrecompileResult { pub fn fatal_error(msg: impl Into) -> Self { Self::FatalError { msg: msg.into() } } - + pub fn unwrap(self) -> (u64, Bytes) { match self { Self::Ok { gas_used, output } => (gas_used, output), From 3380df7246a7dffa54001f351fc375576b536fc0 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Fri, 24 May 2024 22:54:50 +0530 Subject: [PATCH 3/6] define ```PrecompileResult``` as ```Result``` --- Cargo.lock | 46 +++++++++++++++++++++++++++-- crates/precompile/src/bls12_381.rs | 8 ++--- crates/primitives/Cargo.toml | 1 + crates/primitives/src/precompile.rs | 36 ++++++++++------------ 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6914d008c..9114dfe3a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2943,7 +2943,7 @@ dependencies = [ "indicatif", "reqwest 0.12.4", "revm-interpreter", - "revm-precompile", + "revm-precompile 7.0.0", "serde", "serde_json", "tokio", @@ -2956,7 +2956,7 @@ dependencies = [ "bincode", "paste", "phf", - "revm-primitives", + "revm-primitives 4.0.0", "serde", "serde_json", "walkdir", @@ -2974,7 +2974,7 @@ dependencies = [ "k256", "once_cell", "rand", - "revm-primitives", + "revm-primitives 4.0.0", "ripemd", "rstest", "secp256k1", @@ -2985,9 +2985,49 @@ dependencies = [ "substrate-bn", ] +[[package]] +name = "revm-precompile" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc8af9aa737eef0509a50d9f3cc1a631557a00ef2e70a3aa8a75d9ee0ed275bb" +dependencies = [ + "aurora-engine-modexp", + "blst", + "c-kzg", + "k256", + "once_cell", + "revm-primitives 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ripemd", + "secp256k1", + "sha2", + "substrate-bn", +] + +[[package]] +name = "revm-primitives" +version = "4.0.0" +dependencies = [ + "alloy-primitives", + "auto_impl", + "bitflags 2.5.0", + "bitvec", + "c-kzg", + "cfg-if", + "derive_more", + "dyn-clone", + "enumn", + "hashbrown", + "hex", + "once_cell", + "revm-precompile 7.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", +] + [[package]] name = "revm-primitives" version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9bf5d465e64b697da6a111cb19e798b5b2ebb18e5faf2ad48e9e8d47c64add2" dependencies = [ "alloy-primitives", "auto_impl", diff --git a/crates/precompile/src/bls12_381.rs b/crates/precompile/src/bls12_381.rs index 52cd760272..2068b6ecbe 100644 --- a/crates/precompile/src/bls12_381.rs +++ b/crates/precompile/src/bls12_381.rs @@ -80,11 +80,11 @@ mod test { #[case] precompile: fn(input: &Bytes, gas_limit: u64) -> PrecompileResult, #[case] file_name: &str, ) { - let test_vectors = load_test_vectors(format!("test-vectors/{}", file_name)) - .unwrap_or_else(|e| panic!("Failed to load test vectors from {}: {}", file_name, e)); + let test_vectors = load_test_vectors(format!("test-vectors/{file_name}")) + .unwrap_or_else(|e| panic!("Failed to load test vectors from {file_name}: {e}")); for vector in test_vectors.0 { - let test_name = format!("{}/{}", file_name, vector.name); + let test_name = format!("{file_name}/{}", vector.name); let input = Bytes::from_hex(&vector.input).unwrap_or_else(|e| { panic!( "could not deserialize input {} as hex in {}: {}", @@ -113,7 +113,7 @@ mod test { expected_output, actual_output, test_name ); } - _ => panic!("unexpected result in {}", test_name), + _ => panic!("unexpected result in {}, vector: {:?}", test_name, vector), } } } diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index a9d009d2e4..93ca6cfb29 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -29,6 +29,7 @@ hashbrown = "0.14" auto_impl = "1.2" bitvec = { version = "1", default-features = false, features = ["alloc"] } bitflags = { version = "2.5.0", default-features = false } +revm-precompile = "7.0.0" # For setting the CfgEnv KZGSettings. Enabled by c-kzg flag. c-kzg = { version = "1.0.2", default-features = false, optional = true } diff --git a/crates/primitives/src/precompile.rs b/crates/primitives/src/precompile.rs index dc4d8ceadb..45ed1f3183 100644 --- a/crates/primitives/src/precompile.rs +++ b/crates/primitives/src/precompile.rs @@ -2,34 +2,28 @@ use crate::{Bytes, Env}; use core::fmt; use dyn_clone::DynClone; use std::{boxed::Box, string::String, sync::Arc}; +use revm_precompile::PrecompileOutput; /// A precompile operation result. /// /// Returns either `Ok((gas_used, return_bytes))` or `Err(error)`. -pub enum PrecompileResult { - Ok { gas_used: u64, output: Bytes }, - Error { error_type: PrecompileError }, - FatalError { msg: String }, +pub enum PrecompileErrors { + Error(PrecompileError), + Fatal { + msg: String + }, } -impl PrecompileResult { - pub fn ok(gas_used: u64, output: Bytes) -> Self { - Self::Ok { gas_used, output } - } - - pub fn err(error_type: PrecompileError) -> Self { - Self::Error { error_type } - } +pub type PrecompileResult = Result; - pub fn fatal_error(msg: impl Into) -> Self { - Self::FatalError { msg: msg.into() } +impl PrecompileErrors { + pub fn err(err: PrecompileError) -> PrecompileResult { + Err(PrecompileErrors::Error(err)) } - - pub fn unwrap(self) -> (u64, Bytes) { - match self { - Self::Ok { gas_used, output } => (gas_used, output), - _ => panic!("called `PrecompileResult::unwrap()` on an `Error` value"), - } + pub fn fatal_error(msg: impl Into) -> PrecompileResult { + Err(PrecompileErrors::Fatal { + msg: msg.into() + }) } } @@ -200,7 +194,7 @@ mod test { _gas_price: u64, _env: &Env, ) -> PrecompileResult { - PrecompileResult::err(PrecompileError::OutOfGas) + PrecompileErrors::err(PrecompileError::OutOfGas) } } From 328ea2527cad8e9a1ee08cbc3c9e7aac6ce119f9 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Fri, 24 May 2024 23:08:00 +0530 Subject: [PATCH 4/6] replace ```PrecompileResult::err``` with ```PrecompileErrors::err``` --- crates/precompile/src/blake2.rs | 8 +++---- crates/precompile/src/bls12_381/g1_add.rs | 6 +++--- crates/precompile/src/bls12_381/g1_msm.rs | 6 +++--- crates/precompile/src/bls12_381/g1_mul.rs | 6 +++--- crates/precompile/src/bls12_381/g2_add.rs | 6 +++--- crates/precompile/src/bls12_381/g2_msm.rs | 6 +++--- crates/precompile/src/bls12_381/g2_mul.rs | 6 +++--- .../precompile/src/bls12_381/map_fp2_to_g2.rs | 6 +++--- .../precompile/src/bls12_381/map_fp_to_g1.rs | 6 +++--- crates/precompile/src/bls12_381/pairing.rs | 6 +++--- crates/precompile/src/bn128.rs | 21 ++++++++++--------- crates/precompile/src/hash.rs | 6 +++--- crates/precompile/src/identity.rs | 4 ++-- crates/precompile/src/kzg_point_evaluation.rs | 10 ++++----- crates/precompile/src/modexp.rs | 12 +++++------ crates/precompile/src/secp256k1.rs | 4 ++-- 16 files changed, 60 insertions(+), 59 deletions(-) diff --git a/crates/precompile/src/blake2.rs b/crates/precompile/src/blake2.rs index 40c3169a9e..d71171333c 100644 --- a/crates/precompile/src/blake2.rs +++ b/crates/precompile/src/blake2.rs @@ -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; @@ -14,20 +14,20 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input = &input[..]; if input.len() != INPUT_LENGTH { - return PrecompileResult::err(Error::Blake2WrongLength); + return PrecompileErrors::err(Error::Blake2WrongLength); } let f = match input[212] { 1 => true, 0 => false, - _ => return PrecompileResult::err(Error::Blake2WrongFinalIndicatorFlag), + _ => return PrecompileErrors::err(Error::Blake2WrongFinalIndicatorFlag), }; // 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 PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } let mut h = [0u64; 8]; diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index e24a4274fe..db84d6697a 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -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 = @@ -23,11 +23,11 @@ const INPUT_LENGTH: usize = 256; /// See also: pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } if input.len() != INPUT_LENGTH { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "G1ADD input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index e6171893af..9d960d3c42 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -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 = @@ -25,7 +25,7 @@ pub const ADDRESS: u64 = 0x0d; pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % g1_mul::INPUT_LENGTH != 0 { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "G1MSM input length should be multiple of {}, was {}", g1_mul::INPUT_LENGTH, input_len @@ -35,7 +35,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { 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 PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } let mut g1_points: Vec = Vec::with_capacity(k); diff --git a/crates/precompile/src/bls12_381/g1_mul.rs b/crates/precompile/src/bls12_381/g1_mul.rs index 9feaf3896b..af9c091fd1 100644 --- a/crates/precompile/src/bls12_381/g1_mul.rs +++ b/crates/precompile/src/bls12_381/g1_mul.rs @@ -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 = @@ -25,10 +25,10 @@ pub(super) const INPUT_LENGTH: usize = 160; /// See also: pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } if input.len() != INPUT_LENGTH { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "G1MUL input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index 67ef9626f5..676d227222 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -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 = @@ -24,11 +24,11 @@ const INPUT_LENGTH: usize = 512; /// See also pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } if input.len() != INPUT_LENGTH { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "G2ADD input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 919e5355d3..4c4427de9d 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -6,7 +6,7 @@ use super::{ }; use crate::{u64_to_address, PrecompileWithAddress}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_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_G2MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -25,7 +25,7 @@ pub const ADDRESS: u64 = 0x10; pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % g2_mul::INPUT_LENGTH != 0 { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "G2MSM input length should be multiple of {}, was {}", g2_mul::INPUT_LENGTH, input_len @@ -35,7 +35,7 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { 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 PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } let mut g2_points: Vec = Vec::with_capacity(k); diff --git a/crates/precompile/src/bls12_381/g2_mul.rs b/crates/precompile/src/bls12_381/g2_mul.rs index 6691c292b4..11e9cbf69f 100644 --- a/crates/precompile/src/bls12_381/g2_mul.rs +++ b/crates/precompile/src/bls12_381/g2_mul.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{u64_to_address, PrecompileWithAddress}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_mult, 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_G2MUL precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -25,10 +25,10 @@ pub(super) const INPUT_LENGTH: usize = 288; /// See also: pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } if input.len() != INPUT_LENGTH { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "G2MUL input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 1855ebdeba..8d45c9b685 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -7,7 +7,7 @@ use blst::{ blst_fp, blst_fp2, blst_fp_from_bendian, blst_map_to_g2, blst_p2, blst_p2_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_MAP_FP2_TO_G2 precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -23,11 +23,11 @@ const BASE_GAS_FEE: u64 = 75000; /// See also: pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } if input.len() != PADDED_FP2_LENGTH { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}", input.len() ))); diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index 45a31f1966..32e7af8f92 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -6,7 +6,7 @@ use crate::{u64_to_address, PrecompileWithAddress}; use blst::{ blst_fp, blst_fp_from_bendian, blst_map_to_g1, blst_p1, blst_p1_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_MAP_FP_TO_G1 precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -21,11 +21,11 @@ const MAP_FP_TO_G1_BASE: u64 = 5500; /// See also: pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult { if MAP_FP_TO_G1_BASE > gas_limit { - return PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } if input.len() != PADDED_FP_LENGTH { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "MAP_FP_TO_G1 input should be {PADDED_FP_LENGTH} bytes, was {}", input.len() ))); diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index a0574155e2..de2ac4f253 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{u64_to_address, PrecompileWithAddress}; use blst::{blst_final_exp, blst_fp12, blst_fp12_is_one, blst_fp12_mul, blst_miller_loop}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult, B256}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult, B256}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -32,7 +32,7 @@ const INPUT_LENGTH: usize = 384; pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % INPUT_LENGTH != 0 { - return PrecompileResult::err(PrecompileError::Other(format!( + return PrecompileErrors::err(PrecompileError::Other(format!( "Pairing input length should be multiple of {INPUT_LENGTH}, was {input_len}" ))); } @@ -40,7 +40,7 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let k = input_len / INPUT_LENGTH; let required_gas: u64 = PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_OFFSET_BASE; if required_gas > gas_limit { - return PrecompileResult::err(PrecompileError::OutOfGas); + return PrecompileErrors::err(PrecompileError::OutOfGas); } // accumulator for the fp12 multiplications of the miller loops. diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index 889d38e4d9..d7f997442f 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -3,6 +3,7 @@ use crate::{ Address, Error, Precompile, PrecompileResult, PrecompileWithAddress, }; use bn::{AffineG1, AffineG2, Fq, Fq2, Group, Gt, G1, G2}; +use revm_primitives::PrecompileErrors; pub mod add { use super::*; @@ -122,18 +123,18 @@ pub fn new_g1_point(px: Fq, py: Fq) -> Result { pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult { if gas_cost > gas_limit { - return PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } let input = right_pad::(input); let p1 = match read_point(&input[..64]) { Ok(point) => point, - Err(_) => return PrecompileResult::err(Error::Bn128FieldPointNotAMember), + Err(_) => return PrecompileErrors::err(Error::Bn128FieldPointNotAMember), }; let p2 = match read_point(&input[64..]) { Ok(point) => point, - Err(_) => return PrecompileResult::err(Error::Bn128FieldPointNotAMember), + Err(_) => return PrecompileErrors::err(Error::Bn128FieldPointNotAMember), }; let mut output = [0u8; 64]; @@ -146,14 +147,14 @@ pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult pub fn run_mul(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult { if gas_cost > gas_limit { - return PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } let input = right_pad::(input); let p = match read_point(&input[..64]) { Ok(point) => point, - Err(_) => return PrecompileResult::err(Error::Bn128FieldPointNotAMember), + Err(_) => return PrecompileErrors::err(Error::Bn128FieldPointNotAMember), }; // `Fr::from_slice` can only fail when the length is not 32. @@ -175,11 +176,11 @@ pub fn run_pair( ) -> PrecompileResult { let gas_used = (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost; if gas_used > gas_limit { - return PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } if input.len() % PAIR_ELEMENT_LEN != 0 { - return PrecompileResult::err(Error::Bn128PairLength); + return PrecompileErrors::err(Error::Bn128PairLength); } let success = if input.is_empty() { @@ -196,7 +197,7 @@ pub fn run_pair( // per iteration. This is guaranteed to be in-bounds. let slice = unsafe { input.get_unchecked(start..start + 32) }; Fq::from_slice(slice) - .map_err(|_| PrecompileResult::err(Error::Bn128FieldPointNotAMember)) + .map_err(|_| PrecompileErrors::err(Error::Bn128FieldPointNotAMember)) }; let ax = match read_fq_at(0) { Ok(ax) => ax, @@ -225,7 +226,7 @@ pub fn run_pair( let a = match new_g1_point(ax, ay) { Ok(a) => a, - Err(_) => return PrecompileResult::err(Error::Bn128AffineGFailedToCreate), + Err(_) => return PrecompileErrors::err(Error::Bn128AffineGFailedToCreate), }; let b = { let ba = Fq2::new(bax, bay); @@ -235,7 +236,7 @@ pub fn run_pair( } else { match AffineG2::new(ba, bb) { Ok(affine_g2) => G2::from(affine_g2), - Err(_) => return PrecompileResult::err(Error::Bn128AffineGFailedToCreate), + Err(_) => return PrecompileErrors::err(Error::Bn128AffineGFailedToCreate), } } }; diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index 2ac972a2d0..00ea74cd26 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -1,6 +1,6 @@ use super::calc_linear_cost_u32; use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress}; -use revm_primitives::Bytes; +use revm_primitives::{Bytes, PrecompileErrors}; use sha2::Digest; pub const SHA256: PrecompileWithAddress = @@ -17,7 +17,7 @@ pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( pub fn sha256_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let cost = calc_linear_cost_u32(input.len(), 60, 12); if cost > gas_limit { - PrecompileResult::err(Error::OutOfGas) + PrecompileErrors::err(Error::OutOfGas) } else { let output = sha2::Sha256::digest(input); PrecompileResult::ok(cost, output.to_vec().into()) @@ -30,7 +30,7 @@ pub fn sha256_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { pub fn ripemd160_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let gas_used = calc_linear_cost_u32(input.len(), 600, 120); if gas_used > gas_limit { - PrecompileResult::err(Error::OutOfGas) + PrecompileErrors::err(Error::OutOfGas) } else { let mut hasher = ripemd::Ripemd160::new(); hasher.update(input); diff --git a/crates/precompile/src/identity.rs b/crates/precompile/src/identity.rs index 11e7b17fb4..be3410e0df 100644 --- a/crates/precompile/src/identity.rs +++ b/crates/precompile/src/identity.rs @@ -1,6 +1,6 @@ use super::calc_linear_cost_u32; use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress}; -use revm_primitives::Bytes; +use revm_primitives::{Bytes, PrecompileErrors}; pub const FUN: PrecompileWithAddress = PrecompileWithAddress(crate::u64_to_address(4), Precompile::Standard(identity_run)); @@ -17,7 +17,7 @@ pub const IDENTITY_PER_WORD: u64 = 3; pub fn identity_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let gas_used = calc_linear_cost_u32(input.len(), IDENTITY_BASE, IDENTITY_PER_WORD); if gas_used > gas_limit { - return PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } PrecompileResult::ok(gas_used, input.clone()) } diff --git a/crates/precompile/src/kzg_point_evaluation.rs b/crates/precompile/src/kzg_point_evaluation.rs index 3570d07e80..c16b9faed0 100644 --- a/crates/precompile/src/kzg_point_evaluation.rs +++ b/crates/precompile/src/kzg_point_evaluation.rs @@ -1,6 +1,6 @@ use crate::{Address, Error, Precompile, PrecompileResult, PrecompileWithAddress}; use c_kzg::{Bytes32, Bytes48, KzgProof, KzgSettings}; -use revm_primitives::{hex_literal::hex, Bytes, Env}; +use revm_primitives::{hex_literal::hex, Bytes, Env, PrecompileErrors}; use sha2::{Digest, Sha256}; pub const POINT_EVALUATION: PrecompileWithAddress = @@ -26,19 +26,19 @@ pub const RETURN_VALUE: &[u8; 64] = &hex!( /// with z and y being padded 32 byte big endian values pub fn run(input: &Bytes, gas_limit: u64, env: &Env) -> PrecompileResult { if gas_limit < GAS_COST { - return PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } // Verify input length. if input.len() != 192 { - return PrecompileResult::err(Error::BlobInvalidInputLength); + return PrecompileErrors::err(Error::BlobInvalidInputLength); } // Verify commitment matches versioned_hash let versioned_hash = &input[..32]; let commitment = &input[96..144]; if kzg_to_versioned_hash(commitment) != versioned_hash { - return PrecompileResult::err(Error::BlobMismatchedVersion); + return PrecompileErrors::err(Error::BlobMismatchedVersion); } // Verify KZG proof with z and y in big endian format @@ -47,7 +47,7 @@ pub fn run(input: &Bytes, gas_limit: u64, env: &Env) -> PrecompileResult { let y = as_bytes32(&input[64..96]); let proof = as_bytes48(&input[144..192]); if !verify_kzg_proof(commitment, z, y, proof, env.cfg.kzg_settings.get()) { - return PrecompileResult::err(Error::BlobVerifyKzgProofFailed); + return PrecompileErrors::err(Error::BlobVerifyKzgProofFailed); } // Return FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS as padded 32 byte big endian values diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index 69bfe96937..542dcf2e2f 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -5,7 +5,7 @@ use crate::{ }; use aurora_engine_modexp::modexp; use core::cmp::{max, min}; -use revm_primitives::Bytes; +use revm_primitives::{Bytes, PrecompileErrors}; pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(5), @@ -50,7 +50,7 @@ where { // If there is no minimum gas, return error. if min_gas > gas_limit { - return PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } // The format of input is: @@ -66,10 +66,10 @@ where // cast base and modulus to usize, it does not make sense to handle larger values let Ok(base_len) = usize::try_from(base_len) else { - return PrecompileResult::err(Error::ModexpBaseOverflow); + return PrecompileErrors::err(Error::ModexpBaseOverflow); }; let Ok(mod_len) = usize::try_from(mod_len) else { - return PrecompileResult::err(Error::ModexpModOverflow); + return PrecompileErrors::err(Error::ModexpModOverflow); }; // Handle a special case when both the base and mod length are zero. @@ -79,7 +79,7 @@ where // Cast exponent length to usize, since it does not make sense to handle larger values. let Ok(exp_len) = usize::try_from(exp_len) else { - return PrecompileResult::err(Error::ModexpModOverflow); + return PrecompileErrors::err(Error::ModexpModOverflow); }; // Used to extract ADJUSTED_EXPONENT_LENGTH. @@ -99,7 +99,7 @@ where // Check if we have enough gas. let gas_cost = calc_gas(base_len as u64, exp_len as u64, mod_len as u64, &exp_highp); if gas_cost > gas_limit { - return PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } // Padding is needed if the input does not contain all 3 values. diff --git a/crates/precompile/src/secp256k1.rs b/crates/precompile/src/secp256k1.rs index 27ae6200d9..c3aba6facc 100644 --- a/crates/precompile/src/secp256k1.rs +++ b/crates/precompile/src/secp256k1.rs @@ -1,5 +1,5 @@ use crate::{utilities::right_pad, Error, Precompile, PrecompileResult, PrecompileWithAddress}; -use revm_primitives::{alloy_primitives::B512, Bytes, B256}; +use revm_primitives::{alloy_primitives::B512, Bytes, PrecompileErrors, B256}; pub const ECRECOVER: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(1), @@ -70,7 +70,7 @@ pub fn ec_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { const ECRECOVER_BASE: u64 = 3_000; if ECRECOVER_BASE > gas_limit { - return PrecompileResult::err(Error::OutOfGas); + return PrecompileErrors::err(Error::OutOfGas); } let input = right_pad::<128>(input); From 953cff535da70cfde60b11c2c2270ad137e59dc6 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Sun, 26 May 2024 01:03:01 +0530 Subject: [PATCH 5/6] implement ```From``` for ```PrecompileErrors``` --- crates/precompile/src/blake2.rs | 6 +-- crates/precompile/src/bls12_381/g1_add.rs | 6 +-- crates/precompile/src/bls12_381/g1_msm.rs | 6 +-- crates/precompile/src/bls12_381/g1_mul.rs | 6 +-- crates/precompile/src/bls12_381/g2_add.rs | 6 +-- crates/precompile/src/bls12_381/g2_msm.rs | 6 +-- crates/precompile/src/bls12_381/g2_mul.rs | 6 +-- .../precompile/src/bls12_381/map_fp2_to_g2.rs | 6 +-- .../precompile/src/bls12_381/map_fp_to_g1.rs | 6 +-- crates/precompile/src/bls12_381/pairing.rs | 6 +-- crates/precompile/src/bn128.rs | 40 +++++++++---------- crates/precompile/src/hash.rs | 4 +- crates/precompile/src/identity.rs | 2 +- crates/precompile/src/kzg_point_evaluation.rs | 8 ++-- crates/precompile/src/modexp.rs | 10 ++--- crates/precompile/src/secp256k1.rs | 2 +- crates/primitives/src/precompile.rs | 9 ++++- 17 files changed, 71 insertions(+), 64 deletions(-) diff --git a/crates/precompile/src/blake2.rs b/crates/precompile/src/blake2.rs index d71171333c..2cf60d7f65 100644 --- a/crates/precompile/src/blake2.rs +++ b/crates/precompile/src/blake2.rs @@ -14,20 +14,20 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input = &input[..]; if input.len() != INPUT_LENGTH { - return PrecompileErrors::err(Error::Blake2WrongLength); + return Err(Error::Blake2WrongLength.into()); } let f = match input[212] { 1 => true, 0 => false, - _ => return PrecompileErrors::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 PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas.into()); } let mut h = [0u64; 8]; diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index db84d6697a..77b2008ae0 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -23,14 +23,14 @@ const INPUT_LENGTH: usize = 256; /// See also: pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } if input.len() != INPUT_LENGTH { - return PrecompileErrors::err(PrecompileError::Other(format!( + 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. diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index 9d960d3c42..14b4b16a55 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -25,17 +25,17 @@ pub const ADDRESS: u64 = 0x0d; pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % g1_mul::INPUT_LENGTH != 0 { - return PrecompileErrors::err(PrecompileError::Other(format!( + return Err(PrecompileError::Other(format!( "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 PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } let mut g1_points: Vec = Vec::with_capacity(k); diff --git a/crates/precompile/src/bls12_381/g1_mul.rs b/crates/precompile/src/bls12_381/g1_mul.rs index af9c091fd1..7af43e1896 100644 --- a/crates/precompile/src/bls12_381/g1_mul.rs +++ b/crates/precompile/src/bls12_381/g1_mul.rs @@ -25,13 +25,13 @@ pub(super) const INPUT_LENGTH: usize = 160; /// See also: pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } if input.len() != INPUT_LENGTH { - return PrecompileErrors::err(PrecompileError::Other(format!( + 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. diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index 676d227222..c92e77bc0b 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -24,14 +24,14 @@ const INPUT_LENGTH: usize = 512; /// See also pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } if input.len() != INPUT_LENGTH { - return PrecompileErrors::err(PrecompileError::Other(format!( + 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. diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 4c4427de9d..701baef058 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -25,17 +25,17 @@ pub const ADDRESS: u64 = 0x10; pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % g2_mul::INPUT_LENGTH != 0 { - return PrecompileErrors::err(PrecompileError::Other(format!( + return Err(PrecompileError::Other(format!( "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 PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } let mut g2_points: Vec = Vec::with_capacity(k); diff --git a/crates/precompile/src/bls12_381/g2_mul.rs b/crates/precompile/src/bls12_381/g2_mul.rs index 11e9cbf69f..362e8078c3 100644 --- a/crates/precompile/src/bls12_381/g2_mul.rs +++ b/crates/precompile/src/bls12_381/g2_mul.rs @@ -25,13 +25,13 @@ pub(super) const INPUT_LENGTH: usize = 288; /// See also: pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } if input.len() != INPUT_LENGTH { - return PrecompileErrors::err(PrecompileError::Other(format!( + 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. diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 8d45c9b685..6d21053fb8 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -23,14 +23,14 @@ const BASE_GAS_FEE: u64 = 75000; /// See also: pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { if BASE_GAS_FEE > gas_limit { - return PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } if input.len() != PADDED_FP2_LENGTH { - return PrecompileErrors::err(PrecompileError::Other(format!( + 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]).unwrap(); diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index 32e7af8f92..9e6fb6ccd3 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -21,14 +21,14 @@ const MAP_FP_TO_G1_BASE: u64 = 5500; /// See also: pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult { if MAP_FP_TO_G1_BASE > gas_limit { - return PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } if input.len() != PADDED_FP_LENGTH { - return PrecompileErrors::err(PrecompileError::Other(format!( + 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).unwrap(); diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index de2ac4f253..728f8d2d78 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -32,15 +32,15 @@ const INPUT_LENGTH: usize = 384; pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); if input_len == 0 || input_len % INPUT_LENGTH != 0 { - return PrecompileErrors::err(PrecompileError::Other(format!( + return Err(PrecompileError::Other(format!( "Pairing input length should be multiple of {INPUT_LENGTH}, was {input_len}" - ))); + )).into()); } let k = input_len / INPUT_LENGTH; let required_gas: u64 = PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_OFFSET_BASE; if required_gas > gas_limit { - return PrecompileErrors::err(PrecompileError::OutOfGas); + return Err(PrecompileError::OutOfGas.into()); } // accumulator for the fp12 multiplications of the miller loops. diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index d7f997442f..bc14005fc0 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -123,18 +123,18 @@ pub fn new_g1_point(px: Fq, py: Fq) -> Result { pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult { if gas_cost > gas_limit { - return PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas.into()); } let input = right_pad::(input); let p1 = match read_point(&input[..64]) { Ok(point) => point, - Err(_) => return PrecompileErrors::err(Error::Bn128FieldPointNotAMember), + Err(_) => return Err(Error::Bn128FieldPointNotAMember.into()), }; let p2 = match read_point(&input[64..]) { Ok(point) => point, - Err(_) => return PrecompileErrors::err(Error::Bn128FieldPointNotAMember), + Err(_) => return Err(Error::Bn128FieldPointNotAMember.into()), }; let mut output = [0u8; 64]; @@ -147,14 +147,14 @@ pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult pub fn run_mul(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult { if gas_cost > gas_limit { - return PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas.into()); } let input = right_pad::(input); let p = match read_point(&input[..64]) { Ok(point) => point, - Err(_) => return PrecompileErrors::err(Error::Bn128FieldPointNotAMember), + Err(_) => return Err(Error::Bn128FieldPointNotAMember.into()), }; // `Fr::from_slice` can only fail when the length is not 32. @@ -176,11 +176,11 @@ pub fn run_pair( ) -> PrecompileResult { let gas_used = (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost; if gas_used > gas_limit { - return PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas.into()); } if input.len() % PAIR_ELEMENT_LEN != 0 { - return PrecompileErrors::err(Error::Bn128PairLength); + return Err(Error::Bn128PairLength.into()); } let success = if input.is_empty() { @@ -197,7 +197,7 @@ pub fn run_pair( // per iteration. This is guaranteed to be in-bounds. let slice = unsafe { input.get_unchecked(start..start + 32) }; Fq::from_slice(slice) - .map_err(|_| PrecompileErrors::err(Error::Bn128FieldPointNotAMember)) + .map_err(|_| Err(Error::Bn128FieldPointNotAMember.into())) }; let ax = match read_fq_at(0) { Ok(ax) => ax, @@ -226,7 +226,7 @@ pub fn run_pair( let a = match new_g1_point(ax, ay) { Ok(a) => a, - Err(_) => return PrecompileErrors::err(Error::Bn128AffineGFailedToCreate), + Err(_) => return Err(Error::Bn128AffineGFailedToCreate.into()), }; let b = { let ba = Fq2::new(bax, bay); @@ -236,7 +236,7 @@ pub fn run_pair( } else { match AffineG2::new(ba, bb) { Ok(affine_g2) => G2::from(affine_g2), - Err(_) => return PrecompileErrors::err(Error::Bn128AffineGFailedToCreate), + Err(_) => return Err(Error::Bn128AffineGFailedToCreate.into()), } } }; @@ -276,7 +276,7 @@ mod tests { ) .unwrap(); - let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); + let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap().output; assert_eq!(res, expected); // zero sum test @@ -295,7 +295,7 @@ mod tests { ) .unwrap(); - let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); + let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap().output; assert_eq!(res, expected); // out of gas test @@ -326,7 +326,7 @@ mod tests { ) .unwrap(); - let (_, res) = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap(); + let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500).unwrap().output; assert_eq!(res, expected); // point not on curve fail @@ -370,7 +370,7 @@ mod tests { ) .unwrap(); - let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); + let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap().output; assert_eq!(res, expected); // out of gas test @@ -408,7 +408,7 @@ mod tests { ) .unwrap(); - let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); + let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap().output; assert_eq!(res, expected); // no input test @@ -420,7 +420,7 @@ mod tests { ) .unwrap(); - let (_, res) = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap(); + let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000).unwrap().output; assert_eq!(res, expected); // point not on curve fail @@ -469,13 +469,13 @@ mod tests { hex::decode("0000000000000000000000000000000000000000000000000000000000000001") .unwrap(); - let (_, res) = run_pair( + let res = run_pair( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, 260_000, ) - .unwrap(); + .unwrap().output; assert_eq!(res, expected); // out of gas test @@ -518,13 +518,13 @@ mod tests { hex::decode("0000000000000000000000000000000000000000000000000000000000000001") .unwrap(); - let (_, res) = run_pair( + let res = run_pair( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, 260_000, ) - .unwrap(); + .unwrap().output; assert_eq!(res, expected); // point not on curve fail diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index 00ea74cd26..316f6a25db 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -17,7 +17,7 @@ pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( pub fn sha256_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let cost = calc_linear_cost_u32(input.len(), 60, 12); if cost > gas_limit { - PrecompileErrors::err(Error::OutOfGas) + Err(Error::OutOfGas.into()) } else { let output = sha2::Sha256::digest(input); PrecompileResult::ok(cost, output.to_vec().into()) @@ -30,7 +30,7 @@ pub fn sha256_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { pub fn ripemd160_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let gas_used = calc_linear_cost_u32(input.len(), 600, 120); if gas_used > gas_limit { - PrecompileErrors::err(Error::OutOfGas) + Err(Error::OutOfGas.into()) } else { let mut hasher = ripemd::Ripemd160::new(); hasher.update(input); diff --git a/crates/precompile/src/identity.rs b/crates/precompile/src/identity.rs index be3410e0df..2e303fd626 100644 --- a/crates/precompile/src/identity.rs +++ b/crates/precompile/src/identity.rs @@ -17,7 +17,7 @@ pub const IDENTITY_PER_WORD: u64 = 3; pub fn identity_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let gas_used = calc_linear_cost_u32(input.len(), IDENTITY_BASE, IDENTITY_PER_WORD); if gas_used > gas_limit { - return PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas); } PrecompileResult::ok(gas_used, input.clone()) } diff --git a/crates/precompile/src/kzg_point_evaluation.rs b/crates/precompile/src/kzg_point_evaluation.rs index c16b9faed0..fb67dc93a8 100644 --- a/crates/precompile/src/kzg_point_evaluation.rs +++ b/crates/precompile/src/kzg_point_evaluation.rs @@ -26,19 +26,19 @@ pub const RETURN_VALUE: &[u8; 64] = &hex!( /// with z and y being padded 32 byte big endian values pub fn run(input: &Bytes, gas_limit: u64, env: &Env) -> PrecompileResult { if gas_limit < GAS_COST { - return PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas.into()); } // Verify input length. if input.len() != 192 { - return PrecompileErrors::err(Error::BlobInvalidInputLength); + return Err(Error::BlobInvalidInputLength.into()); } // Verify commitment matches versioned_hash let versioned_hash = &input[..32]; let commitment = &input[96..144]; if kzg_to_versioned_hash(commitment) != versioned_hash { - return PrecompileErrors::err(Error::BlobMismatchedVersion); + return Err(Error::BlobMismatchedVersion.into()); } // Verify KZG proof with z and y in big endian format @@ -47,7 +47,7 @@ pub fn run(input: &Bytes, gas_limit: u64, env: &Env) -> PrecompileResult { let y = as_bytes32(&input[64..96]); let proof = as_bytes48(&input[144..192]); if !verify_kzg_proof(commitment, z, y, proof, env.cfg.kzg_settings.get()) { - return PrecompileErrors::err(Error::BlobVerifyKzgProofFailed); + return Err(Error::BlobVerifyKzgProofFailed.into()); } // Return FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS as padded 32 byte big endian values diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index 542dcf2e2f..95ef2c68db 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -50,7 +50,7 @@ where { // If there is no minimum gas, return error. if min_gas > gas_limit { - return PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas.into()); } // The format of input is: @@ -66,10 +66,10 @@ where // cast base and modulus to usize, it does not make sense to handle larger values let Ok(base_len) = usize::try_from(base_len) else { - return PrecompileErrors::err(Error::ModexpBaseOverflow); + return Err(Error::ModexpBaseOverflow.into()); }; let Ok(mod_len) = usize::try_from(mod_len) else { - return PrecompileErrors::err(Error::ModexpModOverflow); + return Err(Error::ModexpModOverflow.into()); }; // Handle a special case when both the base and mod length are zero. @@ -79,7 +79,7 @@ where // Cast exponent length to usize, since it does not make sense to handle larger values. let Ok(exp_len) = usize::try_from(exp_len) else { - return PrecompileErrors::err(Error::ModexpModOverflow); + return Err(Error::ModexpModOverflow.into()); }; // Used to extract ADJUSTED_EXPONENT_LENGTH. @@ -99,7 +99,7 @@ where // Check if we have enough gas. let gas_cost = calc_gas(base_len as u64, exp_len as u64, mod_len as u64, &exp_highp); if gas_cost > gas_limit { - return PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas.into()); } // Padding is needed if the input does not contain all 3 values. diff --git a/crates/precompile/src/secp256k1.rs b/crates/precompile/src/secp256k1.rs index c3aba6facc..bc41eae38f 100644 --- a/crates/precompile/src/secp256k1.rs +++ b/crates/precompile/src/secp256k1.rs @@ -70,7 +70,7 @@ pub fn ec_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { const ECRECOVER_BASE: u64 = 3_000; if ECRECOVER_BASE > gas_limit { - return PrecompileErrors::err(Error::OutOfGas); + return Err(Error::OutOfGas.into()); } let input = right_pad::<128>(input); diff --git a/crates/primitives/src/precompile.rs b/crates/primitives/src/precompile.rs index 45ed1f3183..ba014e73ea 100644 --- a/crates/primitives/src/precompile.rs +++ b/crates/primitives/src/precompile.rs @@ -7,6 +7,7 @@ use revm_precompile::PrecompileOutput; /// A precompile operation result. /// /// Returns either `Ok((gas_used, return_bytes))` or `Err(error)`. +#[derive(Debug)] pub enum PrecompileErrors { Error(PrecompileError), Fatal { @@ -27,6 +28,12 @@ impl PrecompileErrors { } } +impl From for PrecompileErrors { + fn from(err: PrecompileError) -> Self { + PrecompileErrors::Error(err) + } +} + pub type StandardPrecompileFn = fn(&Bytes, u64) -> PrecompileResult; pub type EnvPrecompileFn = fn(&Bytes, u64, env: &Env) -> PrecompileResult; @@ -194,7 +201,7 @@ mod test { _gas_price: u64, _env: &Env, ) -> PrecompileResult { - PrecompileErrors::err(PrecompileError::OutOfGas) + Err(PrecompileError::OutOfGas.into()) } } From 4bf5bb3197ac0a44abbe7cebc98036a20e3831bd Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Sun, 26 May 2024 15:34:15 +0530 Subject: [PATCH 6/6] drop ```revm-precompile``` from dependencies --- Cargo.lock | 46 ++----------------- crates/precompile/src/bls12_381/g1_add.rs | 2 +- crates/precompile/src/bls12_381/g1_msm.rs | 2 +- crates/precompile/src/bls12_381/g1_mul.rs | 2 +- crates/precompile/src/bls12_381/g2_add.rs | 2 +- crates/precompile/src/bls12_381/g2_msm.rs | 2 +- crates/precompile/src/bls12_381/g2_mul.rs | 2 +- .../precompile/src/bls12_381/map_fp2_to_g2.rs | 2 +- .../precompile/src/bls12_381/map_fp_to_g1.rs | 2 +- crates/precompile/src/bls12_381/pairing.rs | 2 +- crates/precompile/src/secp256k1.rs | 2 +- crates/primitives/Cargo.toml | 1 - 12 files changed, 13 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9114dfe3a7..e6914d008c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2943,7 +2943,7 @@ dependencies = [ "indicatif", "reqwest 0.12.4", "revm-interpreter", - "revm-precompile 7.0.0", + "revm-precompile", "serde", "serde_json", "tokio", @@ -2956,7 +2956,7 @@ dependencies = [ "bincode", "paste", "phf", - "revm-primitives 4.0.0", + "revm-primitives", "serde", "serde_json", "walkdir", @@ -2974,7 +2974,7 @@ dependencies = [ "k256", "once_cell", "rand", - "revm-primitives 4.0.0", + "revm-primitives", "ripemd", "rstest", "secp256k1", @@ -2985,49 +2985,9 @@ dependencies = [ "substrate-bn", ] -[[package]] -name = "revm-precompile" -version = "7.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc8af9aa737eef0509a50d9f3cc1a631557a00ef2e70a3aa8a75d9ee0ed275bb" -dependencies = [ - "aurora-engine-modexp", - "blst", - "c-kzg", - "k256", - "once_cell", - "revm-primitives 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "ripemd", - "secp256k1", - "sha2", - "substrate-bn", -] - -[[package]] -name = "revm-primitives" -version = "4.0.0" -dependencies = [ - "alloy-primitives", - "auto_impl", - "bitflags 2.5.0", - "bitvec", - "c-kzg", - "cfg-if", - "derive_more", - "dyn-clone", - "enumn", - "hashbrown", - "hex", - "once_cell", - "revm-precompile 7.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde", -] - [[package]] name = "revm-primitives" version = "4.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9bf5d465e64b697da6a111cb19e798b5b2ebb18e5faf2ad48e9e8d47c64add2" dependencies = [ "alloy-primitives", "auto_impl", diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index 77b2008ae0..d7390eabbd 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -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, PrecompileErrors, PrecompileResult}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index 14b4b16a55..5bcd9095b3 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -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, PrecompileErrors, PrecompileResult}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/bls12_381/g1_mul.rs b/crates/precompile/src/bls12_381/g1_mul.rs index 7af43e1896..14430aead5 100644 --- a/crates/precompile/src/bls12_381/g1_mul.rs +++ b/crates/precompile/src/bls12_381/g1_mul.rs @@ -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, PrecompileErrors, PrecompileResult}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MUL precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index c92e77bc0b..6005dc6673 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -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, PrecompileErrors, PrecompileResult}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2ADD precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 701baef058..cc9db92923 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -6,7 +6,7 @@ use super::{ }; use crate::{u64_to_address, PrecompileWithAddress}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/bls12_381/g2_mul.rs b/crates/precompile/src/bls12_381/g2_mul.rs index 362e8078c3..f285c5c8aa 100644 --- a/crates/precompile/src/bls12_381/g2_mul.rs +++ b/crates/precompile/src/bls12_381/g2_mul.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{u64_to_address, PrecompileWithAddress}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_mult, blst_p2_to_affine}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MUL precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 6d21053fb8..afcdcbb22a 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -7,7 +7,7 @@ use blst::{ blst_fp, blst_fp2, blst_fp_from_bendian, blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine, }; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP2_TO_G2 precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index 9e6fb6ccd3..19e194c8cf 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -6,7 +6,7 @@ use crate::{u64_to_address, PrecompileWithAddress}; use blst::{ blst_fp, blst_fp_from_bendian, blst_map_to_g1, blst_p1, blst_p1_affine, blst_p1_to_affine, }; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP_TO_G1 precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index 728f8d2d78..24b8ce7c29 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{u64_to_address, PrecompileWithAddress}; use blst::{blst_final_exp, blst_fp12, blst_fp12_is_one, blst_fp12_mul, blst_miller_loop}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileErrors, PrecompileResult, B256}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult, B256}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile. pub const PRECOMPILE: PrecompileWithAddress = diff --git a/crates/precompile/src/secp256k1.rs b/crates/precompile/src/secp256k1.rs index bc41eae38f..9b16f635c4 100644 --- a/crates/precompile/src/secp256k1.rs +++ b/crates/precompile/src/secp256k1.rs @@ -1,5 +1,5 @@ use crate::{utilities::right_pad, Error, Precompile, PrecompileResult, PrecompileWithAddress}; -use revm_primitives::{alloy_primitives::B512, Bytes, PrecompileErrors, B256}; +use revm_primitives::{alloy_primitives::B512, Bytes, B256}; pub const ECRECOVER: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(1), diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 93ca6cfb29..a9d009d2e4 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -29,7 +29,6 @@ hashbrown = "0.14" auto_impl = "1.2" bitvec = { version = "1", default-features = false, features = ["alloc"] } bitflags = { version = "2.5.0", default-features = false } -revm-precompile = "7.0.0" # For setting the CfgEnv KZGSettings. Enabled by c-kzg flag. c-kzg = { version = "1.0.2", default-features = false, optional = true }