diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index d2860444381..8d1eee7b76a 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -46,14 +46,13 @@ dev = [ "optional_eip3607", "optional_gas_refund", ] +ethersdb = ["tokio", "futures", "ethers-providers", "ethers-core", "hex"] +k256 = ["revm_precompiles/k256_ecrecover"] memory_limit = [] no_gas_measuring = [] optional_block_gas_limit = [] optional_eip3607 = [] optional_gas_refund = [] -std = ["bytes/std", "num_enum/std", "rlp/std"] secp256k1 = ["revm_precompiles/secp256k1"] -k256 = ["revm_precompiles/k256_ecrecover"] -web3db = [] -ethersdb = ["tokio", "futures", "ethers-providers", "ethers-core", "hex"] -with-serde = ["serde", "hex", "hex/serde", "hashbrown/serde"] +std = ["bytes/std", "num_enum/std", "rlp/std"] +with-serde = ["serde", "hex", "hex/serde", "hashbrown/serde", "ruint/serde"] diff --git a/crates/revm_precompiles/src/lib.rs b/crates/revm_precompiles/src/lib.rs index 4b1523a2adf..fc79b0183c6 100644 --- a/crates/revm_precompiles/src/lib.rs +++ b/crates/revm_precompiles/src/lib.rs @@ -112,6 +112,7 @@ impl Precompiles { static INSTANCE: OnceCell = OnceCell::new(); INSTANCE.get_or_init(|| { let fun = vec![ + #[cfg(any(feature = "k256_ecrecover", feature = "secp256k1"))] secp256k1::ECRECOVER, hash::SHA256, hash::RIPEMD160, diff --git a/crates/revm_precompiles/src/secp256k1.rs b/crates/revm_precompiles/src/secp256k1.rs index 66e2d7ed92f..d6dfee26a49 100644 --- a/crates/revm_precompiles/src/secp256k1.rs +++ b/crates/revm_precompiles/src/secp256k1.rs @@ -1,14 +1,45 @@ +#[cfg(any(feature = "k256_ecrecover", feature = "secp256k1"))] use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn}; -use alloc::vec::Vec; -use core::cmp::min; - -const ECRECOVER_BASE: u64 = 3_000; +#[cfg(any(feature = "k256_ecrecover", feature = "secp256k1"))] pub const ECRECOVER: PrecompileAddress = PrecompileAddress( crate::u64_to_b160(1), Precompile::Standard(ec_recover_run as StandardPrecompileFn), ); +#[cfg(any(feature = "k256_ecrecover", feature = "secp256k1"))] +fn ec_recover_run(i: &[u8], target_gas: u64) -> PrecompileResult { + use alloc::vec::Vec; + use core::cmp::min; + + const ECRECOVER_BASE: u64 = 3_000; + + if ECRECOVER_BASE > target_gas { + return Err(Error::OutOfGas); + } + let mut input = [0u8; 128]; + input[..min(i.len(), 128)].copy_from_slice(&i[..min(i.len(), 128)]); + + let mut msg = [0u8; 32]; + let mut sig = [0u8; 65]; + + msg[0..32].copy_from_slice(&input[0..32]); + sig[0..32].copy_from_slice(&input[64..96]); + sig[32..64].copy_from_slice(&input[96..128]); + + if input[32..63] != [0u8; 31] || !matches!(input[63], 27 | 28) { + return Ok((ECRECOVER_BASE, Vec::new())); + } + + sig[64] = input[63] - 27; + + let out = secp256k1::ecrecover(&sig, &msg) + .map(Vec::from) + .unwrap_or_default(); + + Ok((ECRECOVER_BASE, out)) +} + #[cfg(feature = "k256_ecrecover")] #[allow(clippy::module_inception)] mod secp256k1 { @@ -58,30 +89,3 @@ mod secp256k1 { Ok(hash) } } - -fn ec_recover_run(i: &[u8], target_gas: u64) -> PrecompileResult { - if ECRECOVER_BASE > target_gas { - return Err(Error::OutOfGas); - } - let mut input = [0u8; 128]; - input[..min(i.len(), 128)].copy_from_slice(&i[..min(i.len(), 128)]); - - let mut msg = [0u8; 32]; - let mut sig = [0u8; 65]; - - msg[0..32].copy_from_slice(&input[0..32]); - sig[0..32].copy_from_slice(&input[64..96]); - sig[32..64].copy_from_slice(&input[96..128]); - - if input[32..63] != [0u8; 31] || !matches!(input[63], 27 | 28) { - return Ok((ECRECOVER_BASE, Vec::new())); - } - - sig[64] = input[63] - 27; - - let out = secp256k1::ecrecover(&sig, &msg) - .map(Vec::from) - .unwrap_or_default(); - - Ok((ECRECOVER_BASE, out)) -}