Skip to content

Commit

Permalink
fix: compiler errors related to feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Dec 16, 2022
1 parent 2cb9910 commit 08dfa6c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
9 changes: 4 additions & 5 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions crates/revm_precompiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Precompiles {
static INSTANCE: OnceCell<Precompiles> = OnceCell::new();
INSTANCE.get_or_init(|| {
let fun = vec![
#[cfg(any(feature = "k256_ecrecover", feature = "secp256k1"))]
secp256k1::ECRECOVER,
hash::SHA256,
hash::RIPEMD160,
Expand Down
66 changes: 35 additions & 31 deletions crates/revm_precompiles/src/secp256k1.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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))
}

0 comments on commit 08dfa6c

Please sign in to comment.