Skip to content

Commit

Permalink
Load fjord precompiles via optimism handler register
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianBland committed May 24, 2024
1 parent 48fb6c7 commit 9c04e78
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
6 changes: 4 additions & 2 deletions crates/precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ secp256k1 = { version = "0.29.0", default-features = false, features = [
blst = { version = "0.3.11", optional = true }

# p256verify precompile
p256 = { version = "0.13.2", default-features = false, features = ["ecdsa"] }
p256 = { version = "0.13.2", optional = true, default-features = false, features = ["ecdsa"] }

[dev-dependencies]
criterion = { version = "0.5" }
Expand All @@ -70,7 +70,7 @@ std = [
hashbrown = ["revm-primitives/hashbrown"]
asm-keccak = ["revm-primitives/asm-keccak"]

optimism = ["revm-primitives/optimism"]
optimism = ["revm-primitives/optimism", "secp256r1"]
# Optimism default handler enabled Optimism handler register by default in EvmBuilder.
optimism-default-handler = [
"optimism",
Expand All @@ -80,6 +80,8 @@ negate-optimism-default-handler = [
"revm-primitives/negate-optimism-default-handler",
]

secp256r1 = ["dep:p256"]

# These libraries may not work on all no_std platforms as they depend on C.

# Enables the KZG point evaluation precompile.
Expand Down
10 changes: 3 additions & 7 deletions crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod identity;
pub mod kzg_point_evaluation;
pub mod modexp;
pub mod secp256k1;
#[cfg(feature = "secp256r1")]
pub mod secp256r1;
pub mod utilities;

Expand Down Expand Up @@ -66,8 +67,6 @@ impl Precompiles {
PrecompileSpecId::ISTANBUL => Self::istanbul(),
PrecompileSpecId::BERLIN => Self::berlin(),
PrecompileSpecId::CANCUN => Self::cancun(),
#[cfg(feature = "optimism")]
PrecompileSpecId::FJORD => Self::fjord(),
PrecompileSpecId::PRAGUE => Self::prague(),
PrecompileSpecId::LATEST => Self::latest(),
}
Expand Down Expand Up @@ -161,6 +160,7 @@ impl Precompiles {
}

/// Returns precompiles for Fjord spec.
#[cfg(feature = "optimism")]
pub fn fjord() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
Expand Down Expand Up @@ -266,8 +266,6 @@ pub enum PrecompileSpecId {
ISTANBUL,
BERLIN,
CANCUN,
#[cfg(feature = "optimism")]
FJORD,
PRAGUE,
LATEST,
}
Expand All @@ -289,9 +287,7 @@ impl PrecompileSpecId {
#[cfg(feature = "optimism")]
BEDROCK | REGOLITH | CANYON => Self::BERLIN,
#[cfg(feature = "optimism")]
ECOTONE => Self::CANCUN,
#[cfg(feature = "optimism")]
FJORD => Self::FJORD,
ECOTONE | FJORD => Self::CANCUN,
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/precompile/src/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use revm_primitives::{Bytes, PrecompileError, PrecompileResult, B256};
const P256VERIFY_BASE: u64 = 3_450;

/// Returns the secp256r1 precompile with its address.

pub fn precompiles() -> impl Iterator<Item = PrecompileWithAddress> {
[P256VERIFY].into_iter()
}
Expand All @@ -32,7 +31,7 @@ pub const P256VERIFY: PrecompileWithAddress =
/// | signed message hash | r | s | public key x | public key y |
/// | :-----------------: | :-: | :-: | :----------: | :----------: |
/// | 32 | 32 | 32 | 32 | 32 |
fn p256_verify(input: &Bytes, gas_limit: u64) -> PrecompileResult {
pub fn p256_verify(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if P256VERIFY_BASE > gas_limit {
return Err(PrecompileError::OutOfGas);
}
Expand All @@ -42,7 +41,7 @@ fn p256_verify(input: &Bytes, gas_limit: u64) -> PrecompileResult {

/// Returns `Some(())` if the signature included in the input byte slice is
/// valid, `None` otherwise.
fn verify_impl(input: &[u8]) -> Option<()> {
pub fn verify_impl(input: &[u8]) -> Option<()> {
if input.len() < 160 {
return None;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/revm/src/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod handler_register;
mod l1block;

pub use handler_register::{
deduct_caller, end, last_frame_return, load_accounts, optimism_handle_register, output,
reward_beneficiary, validate_env, validate_tx_against_state,
deduct_caller, end, last_frame_return, load_accounts, load_precompiles,
optimism_handle_register, output, reward_beneficiary, validate_env, validate_tx_against_state,
};
pub use l1block::{L1BlockInfo, BASE_FEE_RECIPIENT, L1_BLOCK_CONTRACT, L1_FEE_RECIPIENT};
20 changes: 19 additions & 1 deletion crates/revm/src/optimism/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use crate::{
db::Database, spec_to_generic, Account, EVMError, Env, ExecutionResult, HaltReason,
HashMap, InvalidTransaction, ResultAndState, Spec, SpecId, SpecId::REGOLITH, U256,
},
Context, FrameResult,
Context, ContextPrecompiles, FrameResult,
};
use core::ops::Mul;
use revm_precompile::{secp256r1, PrecompileSpecId, Precompiles};
use std::string::ToString;
use std::sync::Arc;

Expand All @@ -23,6 +24,8 @@ pub fn optimism_handle_register<DB: Database, EXT>(handler: &mut EvmHandler<'_,
handler.validation.env = Arc::new(validate_env::<SPEC, DB>);
// Validate transaction against state.
handler.validation.tx_against_state = Arc::new(validate_tx_against_state::<SPEC, EXT, DB>);
// Load additional precompiles for the given chain spec.
handler.pre_execution.load_precompiles = Arc::new(load_precompiles::<SPEC, EXT, DB>);
// load l1 data
handler.pre_execution.load_accounts = Arc::new(load_accounts::<SPEC, EXT, DB>);
// An estimated batch cost is charged from the caller and added to L1 Fee Vault.
Expand Down Expand Up @@ -137,6 +140,21 @@ pub fn last_frame_return<SPEC: Spec, EXT, DB: Database>(
Ok(())
}

/// Load precompiles for Optimism chain.
#[inline]
pub fn load_precompiles<SPEC: Spec, EXT, DB: Database>() -> ContextPrecompiles<DB> {
let mut precompiles = Precompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)).clone();

if SPEC::enabled(SpecId::FJORD) {
precompiles.extend([
// EIP-7212: secp256r1 P256verify
secp256r1::P256VERIFY,
])
}

precompiles.into()
}

/// Load account (make them warm) and l1 data from database.
#[inline]
pub fn load_accounts<SPEC: Spec, EXT, DB: Database>(
Expand Down

0 comments on commit 9c04e78

Please sign in to comment.