Skip to content

Commit

Permalink
feat: optional poseidon codehash feature gate (#13)
Browse files Browse the repository at this point in the history
* fix clippy

* fix alloydb

* add scroll-poseidon-codehash gate

* disable ethereum tests prague_suite before #12 fix
  • Loading branch information
lightsing committed Aug 7, 2024
1 parent 2a3e0a5 commit 9e445b2
Show file tree
Hide file tree
Showing 33 changed files with 180 additions and 167 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ jobs:
fail-fast: false
matrix:
rust: [ "stable", "beta", "nightly" ]
flags: [ "--no-default-features", "", "--features=\"all, scroll\"", "--features=\"all, optimism\"" ]
flags: [
"--no-default-features",
"",
"--features=\"scroll\"",
"--features=\"scroll, scroll-poseidon-codehash\"",
"--features=\"all, scroll\"",
"--features=\"all, scroll, scroll-poseidon-codehash\"",
"--features=\"optimism\""
"--features=\"all, optimism\""
]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
Expand All @@ -38,7 +47,7 @@ jobs:
strategy:
fail-fast: false
matrix:
features: ["", "optimism"]
features: [ "", "optimism" ]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
Expand All @@ -53,7 +62,7 @@ jobs:
strategy:
fail-fast: false
matrix:
features: ["", "serde", "std"]
features: [ "", "serde", "std" ]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ethereum-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ jobs:
ethtests/EIPTests/StateTests/stEIP1153-transientStorage/ \
ethtests/EIPTests/StateTests/stEIP4844-blobtransactions/ \
ethtests/EIPTests/StateTests/stEIP2537/ \
tests/eof_suite/state_tests \
tests/prague_suite/state_tests
tests/eof_suite/state_tests # \
# tests/prague_suite/state_tests
5 changes: 3 additions & 2 deletions bins/revm-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
bytes = "1.6"
hex = "0.4"
revm = { path = "../../crates/revm", version = "12.1.0", default-features=false }
revm = { path = "../../crates/revm", version = "12.1.0", default-features = false }
microbench = "0.5"
alloy-sol-macro = "0.7.7"
alloy-sol-types = "0.7.7"
Expand All @@ -28,4 +28,5 @@ name = "transfer"
name = "burntpix"

[features]
scroll = ["revm/scroll"]
scroll = ["revm/scroll"]
scroll-poseidon-codehash = ["scroll", "revm/scroll-poseidon-codehash"]
6 changes: 3 additions & 3 deletions bins/revm-test/src/bin/burntpix/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ fn try_from_hex_to_u32(hex: &str) -> eyre::Result<u32> {

fn insert_account_info(cache_db: &mut CacheDB<EmptyDB>, addr: Address, code: Bytes) {
let keccak256_code_hash = hex::encode(keccak256(&code));
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
let poseidon_code_hash = hex::encode(revm::primitives::poseidon(&code));
#[cfg(not(feature = "scroll"))]
#[cfg(not(feature = "scroll-poseidon-codehash"))]
let account_info = AccountInfo::new(
U256::from(0),
0,
B256::from_str(&keccak256_code_hash).unwrap(),
Bytecode::new_raw(code),
);
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
let account_info = AccountInfo::new(
U256::from(0),
0,
Expand Down
3 changes: 2 additions & 1 deletion bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ walkdir = "2.5"
k256 = { version = "0.13.3", features = ["ecdsa"] }

[features]
scroll = ["revm/scroll"]
scroll = ["revm/scroll"]
scroll-poseidon-codehash = ["scroll", "revm/scroll-poseidon-codehash"]
8 changes: 4 additions & 4 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,18 @@ pub fn execute_test_suite(
#[cfg(feature = "scroll")]
let code_size = info.code.len();
let keccak_code_hash = keccak256(&info.code);
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
let poseidon_code_hash = revm::primitives::poseidon(&info.code);
let bytecode = Bytecode::new_raw(info.code);
let acc_info = revm::primitives::AccountInfo {
balance: info.balance,
#[cfg(feature = "scroll")]
code_size,
#[cfg(not(feature = "scroll"))]
#[cfg(not(feature = "scroll-poseidon-codehash"))]
code_hash: keccak_code_hash,
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
code_hash: poseidon_code_hash,
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
keccak_code_hash,
code: Some(bytecode),
nonce: info.nonce,
Expand Down
4 changes: 4 additions & 0 deletions crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ scroll-default-handler = [
negate-scroll-default-handler = [
"revm-primitives/negate-scroll-default-handler",
]
scroll-poseidon-codehash = [
"scroll",
"revm-primitives/scroll-poseidon-codehash",
]

dev = [
"memory_limit",
Expand Down
3 changes: 1 addition & 2 deletions crates/interpreter/src/function_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ impl FunctionStack {

/// Pops a frame from the stack and sets current_code_idx to the popped frame's idx.
pub fn pop(&mut self) -> Option<FunctionReturnFrame> {
self.return_stack.pop().map(|frame| {
self.return_stack.pop().inspect(|frame| {
self.current_code_idx = frame.idx;
frame
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub const fn selfdestruct_cost(spec_id: SpecId, res: SelfDestructResult) -> u64
/// * Account access gas. after berlin it can be cold or warm.
/// * Transfer value gas. If value is transferred and balance of target account is updated.
/// * If account is not existing and needs to be created. After Spurious dragon
/// this is only accounted if value is transferred.
/// this is only accounted if value is transferred.
#[inline]
pub const fn call_cost(
spec_id: SpecId,
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait Host {
/// Get code size of `address` and if the account is cold.
fn code_size(&mut self, address: Address) -> Option<(usize, bool)>;

#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
/// Get keccak code hash of `address` and if the account is cold.
fn keccak_code_hash(&mut self, address: Address) -> Option<(B256, bool)>;

Expand Down
11 changes: 5 additions & 6 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use std::vec::Vec;

use super::LoadAccountResult;

#[cfg(feature = "scroll")]
use revm_primitives::POSEIDON_EMPTY;

/// A dummy [Host] implementation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DummyHost {
Expand Down Expand Up @@ -74,19 +71,21 @@ impl Host for DummyHost {
}

#[inline]
#[cfg(not(feature = "scroll"))]
#[cfg(not(feature = "scroll-poseidon-codehash"))]
fn code_hash(&mut self, _address: Address) -> Option<(B256, bool)> {
Some((KECCAK_EMPTY, false))
}

#[inline]
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
fn code_hash(&mut self, _address: Address) -> Option<(B256, bool)> {
use revm_primitives::POSEIDON_EMPTY;

Some((POSEIDON_EMPTY, false))
}

#[inline]
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
fn keccak_code_hash(&mut self, _address: Address) -> Option<(B256, bool)> {
Some((KECCAK_EMPTY, false))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ pub fn extcodehash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
check!(interpreter, CONSTANTINOPLE);
pop_address!(interpreter, address);

#[cfg(not(feature = "scroll"))]
#[cfg(not(feature = "scroll-poseidon-codehash"))]
let result = host.code_hash(address);
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
let result = host.keccak_code_hash(address);

let Some((code_hash, is_cold)) = result else {
Expand Down
1 change: 1 addition & 0 deletions crates/precompile/src/bls12_381/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const INPUT_LENGTH: usize = 384;
/// following structure:
/// * 128 bytes of G1 point encoding
/// * 256 bytes of G2 point encoding
///
/// Each point is expected to be in the subgroup of order q.
/// Output is 32 bytes where first 31 bytes are equal to 0x00 and the last byte
/// is 0x01 if pairing result is equal to the multiplicative identity in a pairing
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/bls12_381/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ pub(super) fn remove_padding(input: &[u8]) -> Result<&[u8; FP_LENGTH], Precompil
///
/// From [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537):
/// * A scalar for the multiplication operation is encoded as 32 bytes by performing BigEndian
/// encoding of the corresponding (unsigned) integer.
/// encoding of the corresponding (unsigned) integer.
///
/// We do not check that the scalar is a canonical Fr element, because the EIP specifies:
/// * The corresponding integer is not required to be less than or equal than main subgroup order
/// `q`.
/// `q`.
pub(super) fn extract_scalar_input(input: &[u8]) -> Result<blst_scalar, PrecompileError> {
if input.len() != SCALAR_LENGTH {
return Err(PrecompileError::Other(format!(
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ optimism = []
optimism-default-handler = ["optimism"]
negate-optimism-default-handler = []

scroll = ["halo2curves", "poseidon-base"]
scroll = []
# Scroll default handler enabled Scroll handler register by default in EvmBuilder.
scroll-default-handler = ["scroll"]
negate-scroll-default-handler = []
scroll-poseidon-codehash = ["scroll", "halo2curves", "poseidon-base"]

dev = [
"memory_limit",
Expand Down
35 changes: 17 additions & 18 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pub use legacy::{JumpTable, LegacyAnalyzedBytecode};
use std::sync::Arc;

use crate::{keccak256, Bytes, B256, KECCAK_EMPTY};
#[cfg(feature = "scroll")]
use crate::{poseidon, POSEIDON_EMPTY};

/// State of the [`Bytecode`] analysis.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -47,27 +45,28 @@ impl Bytecode {
}

/// Calculate hash of the bytecode.
#[cfg(not(feature = "scroll"))]
pub fn hash_slow(&self) -> B256 {
if self.is_empty() {
KECCAK_EMPTY
} else {
keccak256(self.original_byte_slice())
}
}
cfg_if::cfg_if! {
if #[cfg(not(feature = "scroll-poseidon-codehash"))] {
if self.is_empty() {
KECCAK_EMPTY
} else {
keccak256(self.original_byte_slice())
}
} else {
use crate::{poseidon, POSEIDON_EMPTY};

/// Calculate poseidon hash of the bytecode.
#[cfg(feature = "scroll")]
pub fn poseidon_hash_slow(&self) -> B256 {
if self.is_empty() {
POSEIDON_EMPTY
} else {
poseidon(self.original_byte_slice())
if self.is_empty() {
POSEIDON_EMPTY
} else {
poseidon(self.original_byte_slice())
}
}
}
}

/// Calculate hash of the bytecode.
#[cfg(feature = "scroll")]
/// Calculate keccak hash of the bytecode.
#[cfg(feature = "scroll-poseidon-codehash")]
pub fn keccak_hash_slow(&self) -> B256 {
if self.is_empty() {
KECCAK_EMPTY
Expand Down
11 changes: 5 additions & 6 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ use core::hash::Hash;
use std::boxed::Box;
use std::vec::Vec;

#[cfg(not(feature = "scroll"))]
use crate::KECCAK_EMPTY;
#[cfg(feature = "scroll")]
use crate::POSEIDON_EMPTY;

/// EVM environment configuration.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -233,11 +228,15 @@ impl Env {
// This EIP is introduced after london but there was no collision in past
// so we can leave it enabled always
cfg_if::cfg_if! {
if #[cfg(not(feature = "scroll"))] {
if #[cfg(not(feature = "scroll-poseidon-codehash"))] {
use crate::KECCAK_EMPTY;

if !self.cfg.is_eip3607_disabled() && account.info.code_hash != KECCAK_EMPTY {
return Err(InvalidTransaction::RejectCallerWithCode);
}
} else {
use crate::POSEIDON_EMPTY;

if !self.cfg.is_eip3607_disabled() && account.info.code_hash != POSEIDON_EMPTY {
return Err(InvalidTransaction::RejectCallerWithCode);
}
Expand Down
1 change: 1 addition & 0 deletions crates/primitives/src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub enum SpecId {
/// Curie update introduces:
/// - Support `EIP-1559` transactions.
/// - Support the `BASEFEE`, `MCOPY`, `TLOAD`, `TSTORE` opcodes.
///
/// Although the Curie update include new opcodes in Cancun, the most important change
/// `EIP-4844` is not included. So we sort it before Cancun.
CURIE = 19,
Expand Down
Loading

0 comments on commit 9e445b2

Please sign in to comment.