Skip to content

Commit

Permalink
Add OP-Granite hardfork, limiting bn256Pairing input size
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianBland committed Aug 5, 2024
1 parent 4d0feec commit e695d0f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
14 changes: 14 additions & 0 deletions crates/precompile/src/bn128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ pub mod pair {

const ADDRESS: Address = crate::u64_to_address(8);

pub const GRANITE_MAX_INPUT_SIZE: usize = 112687;
pub const GRANITE: PrecompileWithAddress = PrecompileWithAddress(
ADDRESS,
Precompile::Standard(|input, gas_limit| {
if input.len() > GRANITE_MAX_INPUT_SIZE {
return Err(Error::Bn128PairLength.into());
}
run_pair(input,
ISTANBUL_PAIR_PER_POINT,
ISTANBUL_PAIR_BASE,
gas_limit)
}),
);

pub const ISTANBUL_PAIR_PER_POINT: u64 = 34_000;
pub const ISTANBUL_PAIR_BASE: u64 = 45_000;
pub const ISTANBUL: PrecompileWithAddress = PrecompileWithAddress(
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl PrecompileSpecId {
#[cfg(feature = "optimism")]
BEDROCK | REGOLITH | CANYON => Self::BERLIN,
#[cfg(feature = "optimism")]
ECOTONE | FJORD => Self::CANCUN,
ECOTONE | FJORD | GRANITE => Self::CANCUN,
}
}
}
Expand Down
45 changes: 43 additions & 2 deletions crates/primitives/src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ pub enum SpecId {
CANCUN = 20,
ECOTONE = 21,
FJORD = 22,
PRAGUE = 23,
PRAGUE_EOF = 24,
GRANITE = 23,
PRAGUE = 24,
PRAGUE_EOF = 25,
#[default]
LATEST = u8::MAX,
}
Expand Down Expand Up @@ -120,6 +121,8 @@ impl From<&str> for SpecId {
"Ecotone" => SpecId::ECOTONE,
#[cfg(feature = "optimism")]
"Fjord" => SpecId::FJORD,
#[cfg(feature = "optimism")]
"Granite" => SpecId::GRANITE,
_ => Self::LATEST,
}
}
Expand Down Expand Up @@ -158,6 +161,8 @@ impl From<SpecId> for &'static str {
SpecId::ECOTONE => "Ecotone",
#[cfg(feature = "optimism")]
SpecId::FJORD => "Fjord",
#[cfg(feature = "optimism")]
SpecId::GRANITE => "Granite",
SpecId::LATEST => "Latest",
}
}
Expand Down Expand Up @@ -219,6 +224,8 @@ spec!(CANYON, CanyonSpec);
spec!(ECOTONE, EcotoneSpec);
#[cfg(feature = "optimism")]
spec!(FJORD, FjordSpec);
#[cfg(feature = "optimism")]
spec!(GRANITE, GraniteSpec);

#[cfg(not(feature = "optimism"))]
#[macro_export]
Expand Down Expand Up @@ -378,6 +385,10 @@ macro_rules! spec_to_generic {
use $crate::FjordSpec as SPEC;
$e
}
$crate::SpecId::GRANITE => {
use $crate::GraniteSpec as SPEC;
$e
}
}
}};
}
Expand Down Expand Up @@ -418,6 +429,8 @@ mod tests {
spec_to_generic!(ECOTONE, assert_eq!(SPEC::SPEC_ID, ECOTONE));
#[cfg(feature = "optimism")]
spec_to_generic!(FJORD, assert_eq!(SPEC::SPEC_ID, FJORD));
#[cfg(feature = "optimism")]
spec_to_generic!(GRANITE, assert_eq!(SPEC::SPEC_ID, GRANITE));
spec_to_generic!(PRAGUE, assert_eq!(SPEC::SPEC_ID, PRAGUE));
spec_to_generic!(PRAGUE_EOF, assert_eq!(SPEC::SPEC_ID, PRAGUE_EOF));
spec_to_generic!(LATEST, assert_eq!(SPEC::SPEC_ID, LATEST));
Expand Down Expand Up @@ -540,4 +553,32 @@ mod optimism_tests {
assert!(SpecId::enabled(SpecId::FJORD, SpecId::ECOTONE));
assert!(SpecId::enabled(SpecId::FJORD, SpecId::FJORD));
}

#[test]
fn test_granite_post_merge_hardforks() {
assert!(GraniteSpec::enabled(SpecId::MERGE));
assert!(GraniteSpec::enabled(SpecId::SHANGHAI));
assert!(GraniteSpec::enabled(SpecId::CANCUN));
assert!(!GraniteSpec::enabled(SpecId::LATEST));
assert!(GraniteSpec::enabled(SpecId::BEDROCK));
assert!(GraniteSpec::enabled(SpecId::REGOLITH));
assert!(GraniteSpec::enabled(SpecId::CANYON));
assert!(GraniteSpec::enabled(SpecId::ECOTONE));
assert!(GraniteSpec::enabled(SpecId::FJORD));
assert!(GraniteSpec::enabled(SpecId::GRANITE));
}

#[test]
fn test_granite_post_merge_hardforks_spec_id() {
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::MERGE));
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::SHANGHAI));
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::CANCUN));
assert!(!SpecId::enabled(SpecId::GRANITE, SpecId::LATEST));
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::BEDROCK));
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::REGOLITH));
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::CANYON));
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::ECOTONE));
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::FJORD));
assert!(SpecId::enabled(SpecId::GRANITE, SpecId::GRANITE));
}
}
9 changes: 8 additions & 1 deletion crates/revm/src/optimism/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
Context, ContextPrecompiles, FrameResult,
};
use core::ops::Mul;
use revm_precompile::{secp256r1, PrecompileSpecId};
use revm_precompile::{bn128, secp256r1, PrecompileSpecId};
use std::string::ToString;
use std::sync::Arc;

Expand Down Expand Up @@ -156,6 +156,13 @@ pub fn load_precompiles<SPEC: Spec, EXT, DB: Database>() -> ContextPrecompiles<D
])
}

if SPEC::enabled(SpecId::GRANITE) {
precompiles.extend([
// Restrict bn256Pairing input size
bn128::pair::GRANITE,
])
}

precompiles
}

Expand Down

0 comments on commit e695d0f

Please sign in to comment.