diff --git a/crates/precompile/src/blake2.rs b/crates/precompile/src/blake2.rs index 80d78a9bf9..0bbc470ff6 100644 --- a/crates/precompile/src/blake2.rs +++ b/crates/precompile/src/blake2.rs @@ -1,11 +1,11 @@ -use crate::{Error, PrecompileAddress, StandardPrecompileFn}; +use crate::{Error, PrecompileWithAddress, StandardPrecompileFn}; use crate::{Precompile, PrecompileResult}; use core::convert::TryInto; const F_ROUND: u64 = 1; const INPUT_LENGTH: usize = 213; -pub const FUN: PrecompileAddress = PrecompileAddress( +pub const FUN: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(9), Precompile::Standard(run as StandardPrecompileFn), ); diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index 914817cc01..fc60d29100 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -1,11 +1,11 @@ -use crate::{primitives::U256, Address, Error, Precompile, PrecompileAddress, PrecompileResult}; +use crate::{primitives::U256, Address, Error, Precompile, PrecompileWithAddress, PrecompileResult}; use alloc::vec::Vec; pub mod add { use super::*; const ADDRESS: Address = crate::u64_to_address(6); - pub const ISTANBUL: PrecompileAddress = PrecompileAddress( + pub const ISTANBUL: PrecompileWithAddress = PrecompileWithAddress( ADDRESS, Precompile::Standard(|input: &[u8], target_gas: u64| -> PrecompileResult { if 150 > target_gas { @@ -15,7 +15,7 @@ pub mod add { }), ); - pub const BYZANTIUM: PrecompileAddress = PrecompileAddress( + pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( ADDRESS, Precompile::Standard(|input: &[u8], target_gas: u64| -> PrecompileResult { if 500 > target_gas { @@ -29,7 +29,7 @@ pub mod add { pub mod mul { use super::*; const ADDRESS: Address = crate::u64_to_address(7); - pub const ISTANBUL: PrecompileAddress = PrecompileAddress( + pub const ISTANBUL: PrecompileWithAddress = PrecompileWithAddress( ADDRESS, Precompile::Standard(|input: &[u8], gas_limit: u64| -> PrecompileResult { if 6_000 > gas_limit { @@ -39,7 +39,7 @@ pub mod mul { }), ); - pub const BYZANTIUM: PrecompileAddress = PrecompileAddress( + pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( ADDRESS, Precompile::Standard(|input: &[u8], gas_limit: u64| -> PrecompileResult { if 40_000 > gas_limit { @@ -56,7 +56,7 @@ pub mod pair { const ISTANBUL_PAIR_PER_POINT: u64 = 34_000; const ISTANBUL_PAIR_BASE: u64 = 45_000; - pub const ISTANBUL: PrecompileAddress = PrecompileAddress( + pub const ISTANBUL: PrecompileWithAddress = PrecompileWithAddress( ADDRESS, Precompile::Standard(|input: &[u8], target_gas: u64| -> PrecompileResult { super::run_pair( @@ -70,7 +70,7 @@ pub mod pair { const BYZANTIUM_PAIR_PER_POINT: u64 = 80_000; const BYZANTIUM_PAIR_BASE: u64 = 100_000; - pub const BYZANTIUM: PrecompileAddress = PrecompileAddress( + pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( ADDRESS, Precompile::Standard(|input: &[u8], target_gas: u64| -> PrecompileResult { super::run_pair( diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index c750507f78..7b678b896b 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -1,12 +1,12 @@ use super::calc_linear_cost_u32; -use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn}; +use crate::{Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn}; use sha2::*; -pub const SHA256: PrecompileAddress = PrecompileAddress( +pub const SHA256: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(2), Precompile::Standard(sha256_run as StandardPrecompileFn), ); -pub const RIPEMD160: PrecompileAddress = PrecompileAddress( +pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(3), Precompile::Standard(ripemd160_run as StandardPrecompileFn), ); diff --git a/crates/precompile/src/identity.rs b/crates/precompile/src/identity.rs index 7f54931a17..fc28edd873 100644 --- a/crates/precompile/src/identity.rs +++ b/crates/precompile/src/identity.rs @@ -1,7 +1,7 @@ use super::calc_linear_cost_u32; -use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn}; +use crate::{Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn}; -pub const FUN: PrecompileAddress = PrecompileAddress( +pub const FUN: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(4), Precompile::Standard(identity_run as StandardPrecompileFn), ); diff --git a/crates/precompile/src/kzg_point_evaluation.rs b/crates/precompile/src/kzg_point_evaluation.rs index a6674cefde..323e80afa5 100644 --- a/crates/precompile/src/kzg_point_evaluation.rs +++ b/crates/precompile/src/kzg_point_evaluation.rs @@ -1,9 +1,9 @@ -use crate::{Address, Error, Precompile, PrecompileAddress, PrecompileResult}; +use crate::{Address, Error, Precompile, PrecompileWithAddress, PrecompileResult}; use c_kzg::{Bytes32, Bytes48, KzgProof, KzgSettings}; use revm_primitives::{hex_literal::hex, Env}; use sha2::{Digest, Sha256}; -pub const POINT_EVALUATION: PrecompileAddress = PrecompileAddress(ADDRESS, Precompile::Env(run)); +pub const POINT_EVALUATION: PrecompileWithAddress = PrecompileWithAddress(ADDRESS, Precompile::Env(run)); const ADDRESS: Address = crate::u64_to_address(0x0A); const GAS_COST: u64 = 50_000; diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index c1b9accafc..70034bfd0b 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -61,7 +61,7 @@ impl PrecompileOutput { } #[derive(Clone, Debug)] pub struct Precompiles { - pub fun: HashMap, + pub inner: Vec, } impl Default for Precompiles { @@ -86,10 +86,10 @@ impl fmt::Debug for Precompile { } #[derive(Clone, Debug)] -pub struct PrecompileAddress(Address, Precompile); +pub struct PrecompileWithAddress(Address, Precompile); -impl From for (Address, Precompile) { - fn from(value: PrecompileAddress) -> Self { +impl From for (Address, Precompile) { + fn from(value: PrecompileWithAddress) -> Self { (value.0, value.1) } } @@ -131,16 +131,14 @@ impl Precompiles { pub fn homestead() -> &'static Self { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { - let fun = [ + let inner = vec![ secp256k1::ECRECOVER, hash::SHA256, hash::RIPEMD160, identity::FUN, - ] - .into_iter() - .map(From::from) - .collect(); - Box::new(Self { fun }) + ]; + inner.sort_unstable_by_key(|i| i.0); + Box::new(Self { inner }) }) } @@ -148,19 +146,16 @@ impl Precompiles { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { let mut precompiles = Box::new(Self::homestead().clone()); - precompiles.fun.extend( - [ - // EIP-196: Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128. - // EIP-197: Precompiled contracts for optimal ate pairing check on the elliptic curve alt_bn128. - bn128::add::BYZANTIUM, - bn128::mul::BYZANTIUM, - bn128::pair::BYZANTIUM, - // EIP-198: Big integer modular exponentiation. - modexp::BYZANTIUM, - ] - .into_iter() - .map(From::from), - ); + precompiles.inner.extend([ + // EIP-196: Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128. + // EIP-197: Precompiled contracts for optimal ate pairing check on the elliptic curve alt_bn128. + bn128::add::BYZANTIUM, + bn128::mul::BYZANTIUM, + bn128::pair::BYZANTIUM, + // EIP-198: Big integer modular exponentiation. + modexp::BYZANTIUM, + ]); + precompiles.inner.sort_unstable_by_key(|i| i.0); precompiles }) } @@ -169,18 +164,15 @@ impl Precompiles { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { let mut precompiles = Box::new(Self::byzantium().clone()); - precompiles.fun.extend( - [ - // EIP-152: Add BLAKE2 compression function `F` precompile. - blake2::FUN, - // EIP-1108: Reduce alt_bn128 precompile gas costs. - bn128::add::ISTANBUL, - bn128::mul::ISTANBUL, - bn128::pair::ISTANBUL, - ] - .into_iter() - .map(From::from), - ); + precompiles.inner.extend([ + // EIP-152: Add BLAKE2 compression function `F` precompile. + blake2::FUN, + // EIP-1108: Reduce alt_bn128 precompile gas costs. + bn128::add::ISTANBUL, + bn128::mul::ISTANBUL, + bn128::pair::ISTANBUL, + ]); + precompiles.inner.sort_unstable_by_key(|i| i.0); precompiles }) } @@ -189,14 +181,11 @@ impl Precompiles { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { let mut precompiles = Box::new(Self::istanbul().clone()); - precompiles.fun.extend( - [ - // EIP-2565: ModExp Gas Cost. - modexp::BERLIN, - ] - .into_iter() - .map(From::from), - ); + precompiles.inner.extend([ + // EIP-2565: ModExp Gas Cost. + modexp::BERLIN, + ]); + precompiles.inner.sort_unstable_by_key(|i| i.0); precompiles }) } @@ -209,7 +198,7 @@ impl Precompiles { #[cfg(feature = "c-kzg")] { let mut precompiles = Box::new(Self::berlin().clone()); - precompiles.fun.extend( + precompiles.inner.extend( [ // EIP-4844: Shard Blob Transactions kzg_point_evaluation::POINT_EVALUATION, @@ -242,11 +231,11 @@ impl Precompiles { } pub fn addresses(&self) -> impl IntoIterator { - self.fun.keys() + self.inner.iter().map(|i| &i.0) } pub fn contains(&self, address: &Address) -> bool { - self.fun.contains_key(address) + self.inner.binary_search_by_key(address, |i| i.0) } pub fn get(&self, address: &Address) -> Option { diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index 2f90f0cb6b..e5555fc0e9 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -1,18 +1,18 @@ use crate::{ primitives::U256, utilities::{get_right_padded, get_right_padded_vec, left_padding, left_padding_vec}, - Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn, + Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn, }; use alloc::vec::Vec; use aurora_engine_modexp::modexp; use core::cmp::{max, min}; -pub const BYZANTIUM: PrecompileAddress = PrecompileAddress( +pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(5), Precompile::Standard(byzantium_run as StandardPrecompileFn), ); -pub const BERLIN: PrecompileAddress = PrecompileAddress( +pub const BERLIN: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(5), Precompile::Standard(berlin_run as StandardPrecompileFn), ); diff --git a/crates/precompile/src/secp256k1.rs b/crates/precompile/src/secp256k1.rs index 33246faf2b..80e13d788a 100644 --- a/crates/precompile/src/secp256k1.rs +++ b/crates/precompile/src/secp256k1.rs @@ -1,8 +1,8 @@ -use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn}; +use crate::{Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn}; use alloc::vec::Vec; use core::cmp::min; -pub const ECRECOVER: PrecompileAddress = PrecompileAddress( +pub const ECRECOVER: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(1), Precompile::Standard(ec_recover_run as StandardPrecompileFn), ); diff --git a/documentation/src/crates/precompile.md b/documentation/src/crates/precompile.md index 334594cfe9..fdcfd26ca6 100644 --- a/documentation/src/crates/precompile.md +++ b/documentation/src/crates/precompile.md @@ -19,7 +19,7 @@ Types and Constants: - `Log`: Represents an Ethereum log, with an address, a list of topics, and associated data. - `Precompiles`: A collection of precompiled contracts available in a particular hard fork of Ethereum. - `Precompile`: Represents a precompiled contract, which can either be a standard Ethereum precompile, or a custom precompile. -- `PrecompileAddress`: Associates a precompiled contract with its address. +- `PrecompileWithAddress`: Associates a precompiled contract with its address. - `SpecId`: An enumeration representing different hard fork specifications in Ethereum, such as Homestead, Byzantium, Istanbul, Berlin, and Latest. Functions: