Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(optimism): Isthmus Precompiles #2038

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions crates/optimism/src/bls12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! 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)
}
}
21 changes: 17 additions & 4 deletions crates/optimism/src/handler/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ impl<CTX, ERROR> OpPrecompileProvider<CTX, ERROR> {
| OpSpecId::REGOLITH
| OpSpecId::CANYON
| OpSpecId::ECOTONE
| OpSpecId::HOLOCENE
| OpSpecId::ISTHMUS,
| OpSpecId::HOLOCENE,
)) => 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 +91,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 @@ -6,6 +6,7 @@
extern crate alloc as std;

pub mod api;
pub mod bls12;
pub mod bn128;
pub mod context;
pub mod fast_lz;
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
Loading