Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/handler/src/precompile_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
CallInput::Bytes(bytes) => bytes.0.iter().as_slice(),
};

match (*precompile)(input_bytes, gas_limit) {
match precompile.execute(input_bytes, gas_limit) {
Ok(output) => {
let underflow = result.gas.record_cost(output.gas_used);
assert!(underflow, "Gas underflow is not possible");
Expand Down
20 changes: 9 additions & 11 deletions crates/op-revm/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use revm::{
handler::{EthPrecompiles, PrecompileProvider},
interpreter::{InputsImpl, InterpreterResult},
precompile::{
self, bn254, secp256r1, PrecompileError, PrecompileResult, PrecompileWithAddress,
self, bn254, secp256r1, Precompile, PrecompileError, PrecompileId, PrecompileResult,
Precompiles,
},
primitives::{hardfork::SpecId, Address, OnceLock},
Expand Down Expand Up @@ -144,10 +144,8 @@ pub mod bn254_pair {
/// Max input size for the bn254 pair precompile.
pub const GRANITE_MAX_INPUT_SIZE: usize = 112687;
/// Bn254 pair precompile.
pub const GRANITE: PrecompileWithAddress =
PrecompileWithAddress(bn254::pair::ADDRESS, |input, gas_limit| {
run_pair(input, gas_limit)
});
pub const GRANITE: Precompile =
Precompile::new(PrecompileId::Bn254Pairing, bn254::pair::ADDRESS, run_pair);

/// Run the bn254 pair precompile with Optimism input limit.
pub fn run_pair(input: &[u8], gas_limit: u64) -> PrecompileResult {
Expand Down Expand Up @@ -179,14 +177,14 @@ pub mod bls12_381 {
pub const ISTHMUS_PAIRING_MAX_INPUT_SIZE: usize = 235008;

/// G1 msm precompile.
pub const ISTHMUS_G1_MSM: PrecompileWithAddress =
PrecompileWithAddress(G1_MSM_ADDRESS, run_g1_msm);
pub const ISTHMUS_G1_MSM: Precompile =
Precompile::new(PrecompileId::Bls12G1Msm, G1_MSM_ADDRESS, run_g1_msm);
/// G2 msm precompile.
pub const ISTHMUS_G2_MSM: PrecompileWithAddress =
PrecompileWithAddress(G2_MSM_ADDRESS, run_g2_msm);
pub const ISTHMUS_G2_MSM: Precompile =
Precompile::new(PrecompileId::Bls12G2Msm, G2_MSM_ADDRESS, run_g2_msm);
/// Pairing precompile.
pub const ISTHMUS_PAIRING: PrecompileWithAddress =
PrecompileWithAddress(PAIRING_ADDRESS, run_pair);
pub const ISTHMUS_PAIRING: Precompile =
Precompile::new(PrecompileId::Bls12Pairing, PAIRING_ADDRESS, run_pair);

/// Run the g1 msm precompile with Optimism input limit.
pub fn run_g1_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
Expand Down
6 changes: 4 additions & 2 deletions crates/precompile/src/blake2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//! Blake2 precompile. More details in [`run`]

use crate::{crypto, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};

const F_ROUND: u64 = 1;
const INPUT_LENGTH: usize = 213;

/// Blake2 precompile
pub const FUN: PrecompileWithAddress = PrecompileWithAddress(crate::u64_to_address(9), run);
pub const FUN: Precompile = Precompile::new(PrecompileId::Blake2F, crate::u64_to_address(9), run);

/// reference: <https://eips.ethereum.org/EIPS/eip-152>
/// input format:
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/bls12_381.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! BLS12-381 precompiles added in [`EIP-2537`](https://eips.ethereum.org/EIPS/eip-2537)
//! For more details check modules for each precompile.
use crate::PrecompileWithAddress;
use crate::Precompile;

#[allow(dead_code)]
pub(crate) mod arkworks;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub mod pairing;
mod utils;

/// Returns the BLS12-381 precompiles with their addresses.
pub fn precompiles() -> impl Iterator<Item = PrecompileWithAddress> {
pub fn precompiles() -> impl Iterator<Item = Precompile> {
[
g1_add::PRECOMPILE,
g1_msm::PRECOMPILE,
Expand Down
7 changes: 5 additions & 2 deletions crates/precompile/src/bls12_381/g1_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ use super::utils::{pad_g1_point, remove_g1_padding};
use crate::bls12_381_const::{
G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, PADDED_G1_LENGTH,
};
use crate::{crypto, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile.
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G1_ADD_ADDRESS, g1_add);
pub const PRECOMPILE: Precompile =
Precompile::new(PrecompileId::Bls12G1Add, G1_ADD_ADDRESS, g1_add);

/// G1 addition call expects `256` bytes as an input that is interpreted as byte
/// concatenation of two G1 points (`128` bytes each).
Expand Down
7 changes: 5 additions & 2 deletions crates/precompile/src/bls12_381/g1_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use crate::bls12_381_const::{
PADDED_G1_LENGTH, SCALAR_LENGTH,
};
use crate::bls12_381_utils::msm_required_gas;
use crate::{crypto, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MSM precompile.
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G1_MSM_ADDRESS, g1_msm);
pub const PRECOMPILE: Precompile =
Precompile::new(PrecompileId::Bls12G1Msm, G1_MSM_ADDRESS, g1_msm);

/// Implements EIP-2537 G1MSM precompile.
/// G1 multi-scalar-multiplication call expects `160*k` bytes as an input that is interpreted
Expand Down
7 changes: 5 additions & 2 deletions crates/precompile/src/bls12_381/g2_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ use super::utils::{pad_g2_point, remove_g2_padding};
use crate::bls12_381_const::{
G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH, PADDED_G2_LENGTH,
};
use crate::{crypto, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2ADD precompile.
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G2_ADD_ADDRESS, g2_add);
pub const PRECOMPILE: Precompile =
Precompile::new(PrecompileId::Bls12G2Add, G2_ADD_ADDRESS, g2_add);

/// G2 addition call expects `512` bytes as an input that is interpreted as byte
/// concatenation of two G2 points (`256` bytes each).
Expand Down
7 changes: 5 additions & 2 deletions crates/precompile/src/bls12_381/g2_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use crate::bls12_381_const::{
PADDED_G2_LENGTH, SCALAR_LENGTH,
};
use crate::bls12_381_utils::msm_required_gas;
use crate::{crypto, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MSM precompile.
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G2_MSM_ADDRESS, g2_msm);
pub const PRECOMPILE: Precompile =
Precompile::new(PrecompileId::Bls12G2Msm, G2_MSM_ADDRESS, g2_msm);

/// Implements EIP-2537 G2MSM precompile.
/// G2 multi-scalar-multiplication call expects `288*k` bytes as an input that is interpreted
Expand Down
12 changes: 8 additions & 4 deletions crates/precompile/src/bls12_381/map_fp2_to_g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ use super::utils::{pad_g2_point, remove_fp_padding};
use crate::bls12_381_const::{
MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH,
};
use crate::{crypto, PrecompileWithAddress};
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP2_TO_G2 precompile.
pub const PRECOMPILE: PrecompileWithAddress =
PrecompileWithAddress(MAP_FP2_TO_G2_ADDRESS, map_fp2_to_g2);
pub const PRECOMPILE: Precompile = Precompile::new(
PrecompileId::Bls12MapFp2ToGp2,
MAP_FP2_TO_G2_ADDRESS,
map_fp2_to_g2,
);

/// Field-to-curve call expects 128 bytes as an input that is interpreted as
/// an element of Fp2. Output of this call is 256 bytes and is an encoded G2
Expand Down
11 changes: 8 additions & 3 deletions crates/precompile/src/bls12_381/map_fp_to_g1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
//! BLS12-381 map fp to g1 precompile. More details in [`map_fp_to_g1`]
use super::utils::{pad_g1_point, remove_fp_padding};
use crate::bls12_381_const::{MAP_FP_TO_G1_ADDRESS, MAP_FP_TO_G1_BASE_GAS_FEE, PADDED_FP_LENGTH};
use crate::{crypto, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP_TO_G1 precompile.
pub const PRECOMPILE: PrecompileWithAddress =
PrecompileWithAddress(MAP_FP_TO_G1_ADDRESS, map_fp_to_g1);
pub const PRECOMPILE: Precompile = Precompile::new(
PrecompileId::Bls12MapFpToGp1,
MAP_FP_TO_G1_ADDRESS,
map_fp_to_g1,
);

/// Field-to-curve call expects 64 bytes as an input that is interpreted as an
/// element of Fp. Output of this call is 128 bytes and is an encoded G1 point.
Expand Down
7 changes: 5 additions & 2 deletions crates/precompile/src/bls12_381/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use crate::bls12_381_const::{
PADDED_G1_LENGTH, PADDED_G2_LENGTH, PAIRING_ADDRESS, PAIRING_INPUT_LENGTH,
PAIRING_MULTIPLIER_BASE, PAIRING_OFFSET_BASE,
};
use crate::{crypto, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};
use primitives::B256;
use std::vec::Vec;

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile.
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(PAIRING_ADDRESS, pairing);
pub const PRECOMPILE: Precompile =
Precompile::new(PrecompileId::Bls12Pairing, PAIRING_ADDRESS, pairing);

/// Pairing call expects 384*k (k being a positive integer) bytes as an inputs
/// that is interpreted as byte concatenation of k slices. Each slice has the
Expand Down
26 changes: 13 additions & 13 deletions crates/precompile/src/bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
crypto,
utilities::{bool_to_bytes32, right_pad},
Address, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress,
Address, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};
use std::vec::Vec;

Expand All @@ -29,17 +29,17 @@ pub mod add {
pub const ISTANBUL_ADD_GAS_COST: u64 = 150;

/// Bn254 add precompile with ISTANBUL gas rules
pub const ISTANBUL: PrecompileWithAddress =
PrecompileWithAddress(ADDRESS, |input, gas_limit| {
pub const ISTANBUL: Precompile =
Precompile::new(PrecompileId::Bn254Add, ADDRESS, |input, gas_limit| {
run_add(input, ISTANBUL_ADD_GAS_COST, gas_limit)
});

/// Bn254 add precompile with BYZANTIUM gas rules
pub const BYZANTIUM_ADD_GAS_COST: u64 = 500;

/// Bn254 add precompile with BYZANTIUM gas rules
pub const BYZANTIUM: PrecompileWithAddress =
PrecompileWithAddress(ADDRESS, |input, gas_limit| {
pub const BYZANTIUM: Precompile =
Precompile::new(PrecompileId::Bn254Add, ADDRESS, |input, gas_limit| {
run_add(input, BYZANTIUM_ADD_GAS_COST, gas_limit)
});
}
Expand All @@ -55,17 +55,17 @@ pub mod mul {
pub const ISTANBUL_MUL_GAS_COST: u64 = 6_000;

/// Bn254 mul precompile with ISTANBUL gas rules
pub const ISTANBUL: PrecompileWithAddress =
PrecompileWithAddress(ADDRESS, |input, gas_limit| {
pub const ISTANBUL: Precompile =
Precompile::new(PrecompileId::Bn254Mul, ADDRESS, |input, gas_limit| {
run_mul(input, ISTANBUL_MUL_GAS_COST, gas_limit)
});

/// Bn254 mul precompile with BYZANTIUM gas rules
pub const BYZANTIUM_MUL_GAS_COST: u64 = 40_000;

/// Bn254 mul precompile with BYZANTIUM gas rules
pub const BYZANTIUM: PrecompileWithAddress =
PrecompileWithAddress(ADDRESS, |input, gas_limit| {
pub const BYZANTIUM: Precompile =
Precompile::new(PrecompileId::Bn254Mul, ADDRESS, |input, gas_limit| {
run_mul(input, BYZANTIUM_MUL_GAS_COST, gas_limit)
});
}
Expand All @@ -84,8 +84,8 @@ pub mod pair {
pub const ISTANBUL_PAIR_BASE: u64 = 45_000;

/// Bn254 pair precompile with ISTANBUL gas rules
pub const ISTANBUL: PrecompileWithAddress =
PrecompileWithAddress(ADDRESS, |input, gas_limit| {
pub const ISTANBUL: Precompile =
Precompile::new(PrecompileId::Bn254Pairing, ADDRESS, |input, gas_limit| {
run_pair(
input,
ISTANBUL_PAIR_PER_POINT,
Expand All @@ -101,8 +101,8 @@ pub mod pair {
pub const BYZANTIUM_PAIR_BASE: u64 = 100_000;

/// Bn254 pair precompile with BYZANTIUM gas rules
pub const BYZANTIUM: PrecompileWithAddress =
PrecompileWithAddress(ADDRESS, |input, gas_limit| {
pub const BYZANTIUM: Precompile =
Precompile::new(PrecompileId::Bn254Pairing, ADDRESS, |input, gas_limit| {
run_pair(
input,
BYZANTIUM_PAIR_PER_POINT,
Expand Down
15 changes: 10 additions & 5 deletions crates/precompile/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
//! Hash precompiles, it contains SHA-256 and RIPEMD-160 hash precompiles
//! More details in [`sha256_run`] and [`ripemd160_run`]
use super::calc_linear_cost_u32;
use crate::{crypto, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
use crate::{
crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
};

/// SHA-256 precompile
pub const SHA256: PrecompileWithAddress =
PrecompileWithAddress(crate::u64_to_address(2), sha256_run);
pub const SHA256: Precompile =
Precompile::new(PrecompileId::Sha256, crate::u64_to_address(2), sha256_run);

/// RIPEMD-160 precompile
pub const RIPEMD160: PrecompileWithAddress =
PrecompileWithAddress(crate::u64_to_address(3), ripemd160_run);
pub const RIPEMD160: Precompile = Precompile::new(
PrecompileId::Ripemd160,
crate::u64_to_address(3),
ripemd160_run,
);

/// Computes the SHA-256 hash of the input data
///
Expand Down
Loading
Loading