Skip to content

Commit b7142de

Browse files
committed
fix: limit size to max pairing input size given 20M gas
1 parent 7b5e5fb commit b7142de

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

Cargo.lock

+38-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/client/src/precompiles/bls12.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use revm::{
1919
primitives::PrecompileOutput,
2020
};
2121

22+
/// The max pairing size for BLS12-381 input given a 20M gas limit.
23+
const BLS12_MAX_PAIRING_SIZE_ISTHMUS: usize = 235_008;
24+
2225
/// The address of the BLS12-381 pairing check precompile.
2326
const BLS12_PAIRING_CHECK: Address = address!("0x000000000000000000000000000000000000000f");
2427

@@ -32,8 +35,8 @@ const PAIRING_MULTIPLIER_BASE: u64 = 32600;
3235
const PAIRING_OFFSET_BASE: u64 = 37700;
3336

3437
/// The address of the BLS12-381 pairing precompile.
35-
pub(crate) const FPVM_BLS12_PAIRING: PrecompileWithAddress =
36-
PrecompileWithAddress(BLS12_PAIRING_CHECK, Precompile::Standard(fpvm_bls12_pairing));
38+
pub(crate) const FPVM_BLS12_PAIRING_ISTHMUS: PrecompileWithAddress =
39+
PrecompileWithAddress(BLS12_PAIRING_CHECK, Precompile::Standard(fpvm_bls12_pairing_isthmus));
3740

3841
/// Performs an FPVM-accelerated BLS12-381 pairing check.
3942
fn fpvm_bls12_pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult {
@@ -91,11 +94,33 @@ fn fpvm_bls12_pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult {
9194
Ok(PrecompileOutput::new(required_gas, result_data.into()))
9295
}
9396

97+
/// Performs an FPVM-accelerated `bls12` pairing check precompile call
98+
/// after the Isthmus Hardfork.
99+
fn fpvm_bls12_pairing_isthmus(input: &Bytes, gas_limit: u64) -> PrecompileResult {
100+
if input.len() > BLS12_MAX_PAIRING_SIZE_ISTHMUS {
101+
return Err(PrecompileError::Other(alloc::format!(
102+
"Pairing input length must be at most {}",
103+
BLS12_MAX_PAIRING_SIZE_ISTHMUS
104+
))
105+
.into());
106+
}
107+
108+
fpvm_bls12_pairing(input, gas_limit)
109+
}
110+
94111
#[cfg(test)]
95112
mod tests {
96113
use super::*;
97114
use alloc::vec;
98115

116+
#[test]
117+
fn test_fpvm_bls12_pairing_isthmus_max_bytes() {
118+
let input = Bytes::from(vec![0u8; BLS12_MAX_PAIRING_SIZE_ISTHMUS + 1]);
119+
let gas_limit = PAIRING_MULTIPLIER_BASE;
120+
let err = PrecompileError::Other("Pairing input length must be at most 235008".to_string());
121+
assert_eq!(fpvm_bls12_pairing_isthmus(&input, gas_limit), Err(err.into()));
122+
}
123+
99124
#[test]
100125
fn test_fpvm_bls12_offset() {
101126
let input = Bytes::from(vec![0u8; INPUT_LENGTH + 1]);

bin/client/src/precompiles/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub(crate) fn fpvm_handle_register<F, H>(
3434

3535
// Extend with FPVM-accelerated precompiles
3636
let override_precompiles = [
37-
bls12::FPVM_BLS12_PAIRING,
3837
ecrecover::FPVM_ECRECOVER,
3938
bn128_pair::FPVM_ECPAIRING,
4039
kzg_point_eval::FPVM_KZG_POINT_EVAL,
@@ -45,6 +44,10 @@ pub(crate) fn fpvm_handle_register<F, H>(
4544
ctx_precompiles.extend([bn128_pair::FPVM_ECPAIRING_GRANITE]);
4645
}
4746

47+
if spec_id.is_enabled_in(SpecId::ISTANBUL) {
48+
ctx_precompiles.extend([bls12::FPVM_BLS12_PAIRING_ISTHMUS]);
49+
}
50+
4851
ctx_precompiles
4952
});
5053
}

0 commit comments

Comments
 (0)