Skip to content

Commit

Permalink
feat(optimism): Isthmus Precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Jan 29, 2025
1 parent 62119ca commit 98b4247
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/optimism/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ all = "warn"
[dependencies]
# revm
revm.workspace = true
primitives.workspace = true
precompile = { workspace = true, features = ["secp256r1"] }
inspector.workspace = true
derive_more.workspace = true
Expand Down
29 changes: 29 additions & 0 deletions crates/optimism/src/bls12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! BLS12-381 precompile with input size limits for Optimism.
use precompile::{
bls12_381, {PrecompileError, PrecompileResult, PrecompileWithAddress},
};
use primitives::Bytes;

pub mod pair {
use super::*;

pub const ISTHMUS_MAX_INPUT_SIZE: usize = 235008;
pub const ISTHMUS: PrecompileWithAddress =
PrecompileWithAddress(precompile::u64_to_address(bls12_381::pairing::ADDRESS), |input, gas_limit| {
run_pair(input, gas_limit)
});

pub fn run_pair(input: &[u8], gas_limit: u64) -> PrecompileResult {
if input.len() > ISTHMUS_MAX_INPUT_SIZE {
return Err(PrecompileError::Other(
"BLS12-381 pairing input is too large".into(),
).into());
}
let input = Bytes::copy_from_slice(input);
bls12_381::pairing::pairing(
&input,
gas_limit,
)
}
}
20 changes: 17 additions & 3 deletions crates/optimism/src/handler/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ impl<CTX, ERROR> OpPrecompileProvider<CTX, ERROR> {
| OpSpecId::CANYON
| OpSpecId::ECOTONE
| OpSpecId::HOLOCENE
| OpSpecId::ISTHMUS,

)) => Self::new(Precompiles::new(spec.into_eth_spec().into())),
OpSpec::Op(OpSpecId::FJORD) => Self::new(fjord()),
OpSpec::Op(OpSpecId::GRANITE)
| OpSpec::Eth(SpecId::PRAGUE | SpecId::OSAKA | SpecId::LATEST) => Self::new(granite()),
OpSpec::Op(OpSpecId::GRANITE) => Self::new(granite()),
OpSpec::Op(OpSpecId::ISTHMUS)
| OpSpec::Eth(SpecId::PRAGUE | SpecId::OSAKA | SpecId::LATEST) => Self::new(isthmus()),
}
}
}
Expand All @@ -91,6 +92,19 @@ pub fn granite() -> &'static Precompiles {
})
}

/// Returns precompiles for Isthmus spec.
pub fn isthmus() -> &'static Precompiles {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Precompiles::cancun().clone();
// Restrict bn256Pairing input size
precompiles.extend([secp256r1::P256VERIFY]);
// Restrict bls12Pairing input size
precompiles.extend([crate::bls12::pair::ISTHMUS]);
Box::new(precompiles)
})
}

impl<CTX, ERROR> PrecompileProvider for OpPrecompileProvider<CTX, ERROR>
where
CTX: CfgGetter,
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate alloc as std;

pub mod api;
pub mod bn128;
pub mod bls12;
pub mod context;
pub mod fast_lz;
pub mod handler;
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/bls12_381/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const INPUT_LENGTH: usize = 384;
/// target field and 0x00 otherwise.
///
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-pairing>
pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult {
pub fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let input_len = input.len();
if input_len == 0 || input_len % INPUT_LENGTH != 0 {
return Err(PrecompileError::Other(format!(
Expand Down

0 comments on commit 98b4247

Please sign in to comment.