Skip to content

Commit ba5c7f5

Browse files
committed
Merge branch 'main' into feat/openvm
# Conflicts: # src/precompile/blake2.rs # src/precompile/bn128.rs # src/precompile/hash.rs # src/precompile/modexp.rs
2 parents aa8cfdc + 02737d1 commit ba5c7f5

File tree

4 files changed

+36
-70
lines changed

4 files changed

+36
-70
lines changed

src/precompile/blake2.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
use super::precompile_not_implemented;
22

3-
use revm::{precompile::PrecompileWithAddress, primitives::Address};
4-
5-
// CONSTANTS
6-
// ================================================================================================
3+
use revm::{
4+
precompile::{blake2, PrecompileWithAddress},
5+
primitives::Address,
6+
};
77

88
/// The BLAKE2 precompile address.
9-
const ADDRESS: Address = revm::precompile::blake2::FUN.0;
10-
11-
// BLAKE2 PRECOMPILE
12-
// ================================================================================================
9+
pub const ADDRESS: Address = blake2::FUN.0;
1310

14-
/// The shanghai BLAKE2 precompile implementation with address.
11+
/// The BLAKE2 precompile is not implemented in the SHANGHAI hardfork.
1512
///
1613
/// This precompile is not implemented and will return `PrecompileError::Other("Precompile not
1714
/// implemented".into())`.

src/precompile/bn128/mod.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use revm::precompile::{
2-
bn128::{ADD_INPUT_LEN, MUL_INPUT_LEN, PAIR_ELEMENT_LEN},
2+
bn128::{self, ADD_INPUT_LEN, MUL_INPUT_LEN, PAIR_ELEMENT_LEN},
33
utilities::{bool_to_bytes32, right_pad},
44
PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress,
55
};
@@ -32,7 +32,7 @@ cfg_if::cfg_if! {
3232
pub mod add {
3333
use super::*;
3434

35-
pub use revm::precompile::bn128::add::{ADDRESS, ISTANBUL_ADD_GAS_COST};
35+
pub use bn128::add::{ADDRESS, ISTANBUL_ADD_GAS_COST};
3636

3737
/// Bn128 add precompile with ISTANBUL gas rules
3838
pub const ISTANBUL: PrecompileWithAddress =
@@ -45,7 +45,7 @@ pub mod add {
4545
pub mod mul {
4646
use super::*;
4747

48-
pub use revm::precompile::bn128::mul::{ADDRESS, ISTANBUL_MUL_GAS_COST};
48+
pub use bn128::mul::{ADDRESS, ISTANBUL_MUL_GAS_COST};
4949

5050
/// Bn128 mul precompile with ISTANBUL gas rules
5151
pub const ISTANBUL: PrecompileWithAddress =
@@ -57,42 +57,30 @@ pub mod mul {
5757
pub mod pair {
5858
use super::*;
5959

60-
pub use revm::precompile::bn128::pair::{ADDRESS, ISTANBUL_PAIR_BASE, ISTANBUL_PAIR_PER_POINT};
61-
62-
// CONSTANTS
63-
// --------------------------------------------------------------------------------------------
60+
pub use bn128::pair::{ADDRESS, ISTANBUL_PAIR_BASE, ISTANBUL_PAIR_PER_POINT};
6461

6562
/// The number of pairing inputs per pairing operation. If the inputs provided to the precompile
6663
/// call are < 4, we append (G1::infinity, G2::generator) until we have the required no. of
6764
/// inputs.
68-
const N_PAIRING_PER_OP: usize = 4;
69-
70-
/// The number of bytes taken to represent a pair (G1, G2).
71-
const N_BYTES_PER_PAIR: usize = 192;
72-
73-
// BN128 PAIRING PRECOMPILE
74-
// --------------------------------------------------------------------------------------------
65+
const BERNOULLI_LEN_LIMIT: usize = 4;
7566

76-
/// The BN128 PAIRING precompile with address.
67+
/// The Bn128 pair precompile with BERNOULLI input rules.
7768
pub const BERNOULLI: PrecompileWithAddress = PrecompileWithAddress(ADDRESS, bernoulli_run);
7869

79-
/// The bernoulli BN128 PAIRING precompile implementation.
70+
/// The bernoulli Bn128 pair precompile implementation.
8071
///
8172
/// # Errors
8273
/// - `PrecompileError::Other("BN128PairingInputOverflow: input overflow".into())` if the input
8374
/// length is greater than 768 bytes.
8475
fn bernoulli_run(input: &[u8], gas_limit: u64) -> PrecompileResult {
85-
if input.len() > N_PAIRING_PER_OP * N_BYTES_PER_PAIR {
76+
if input.len() > BERNOULLI_LEN_LIMIT * PAIR_ELEMENT_LEN {
8677
return Err(PrecompileError::Other("BN128PairingInputOverflow: input overflow".into()));
8778
}
8879
run_pair(input, ISTANBUL_PAIR_PER_POINT, ISTANBUL_PAIR_BASE, gas_limit)
8980
}
9081

91-
/// The BN128 PAIRING precompile with address.
92-
pub const FEYNMAN: PrecompileWithAddress =
93-
PrecompileWithAddress(ADDRESS, |input, gas_limit| {
94-
run_pair(input, ISTANBUL_PAIR_PER_POINT, ISTANBUL_PAIR_BASE, gas_limit)
95-
});
82+
/// The Bn128 pair precompile in FEYNMAN hardfork.
83+
pub const FEYNMAN: PrecompileWithAddress = bn128::pair::ISTANBUL;
9684
}
9785

9886
// Copied from https://github.com/bluealloy/revm/blob/v75/crates/precompile/src/bn128.rs Under MIT License

src/precompile/hash.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
use super::precompile_not_implemented;
22

3-
use revm::{precompile::PrecompileWithAddress, primitives::Address};
3+
use revm::{
4+
precompile::{hash, PrecompileResult, PrecompileWithAddress},
5+
primitives::Address,
6+
};
47

58
pub mod sha256 {
69
use super::*;
7-
use revm::precompile::PrecompileResult;
810

9-
// CONSTANTS
10-
// ------------------------------------------------------------------------------------------------
11+
/// SHA-256 precompile address
12+
pub const ADDRESS: Address = hash::SHA256.0;
1113

12-
/// The SHA256 precompile address.
13-
pub const ADDRESS: Address = revm::precompile::hash::SHA256.0;
14-
15-
// SHA256 SHANGHAI PRECOMPILE
16-
// --------------------------------------------------------------------------------------------
17-
18-
/// The shanghai SHA256 precompile implementation with address.
14+
/// The SHA256 precompile is not implemented in the Shanghai hardfork.
1915
pub const SHANGHAI: PrecompileWithAddress = precompile_not_implemented(ADDRESS);
2016

21-
// SHA256 BERNOULLI PRECOMPILE
22-
// --------------------------------------------------------------------------------------------
23-
2417
/// The bernoulli SHA256 precompile implementation with address.
2518
pub const BERNOULLI: PrecompileWithAddress = PrecompileWithAddress(ADDRESS, run);
2619

@@ -30,13 +23,13 @@ pub mod sha256 {
3023
use revm::precompile::{calc_linear_cost_u32, PrecompileError, PrecompileOutput};
3124
let cost = calc_linear_cost_u32(input.len(), 60, 12);
3225
if cost > gas_limit {
33-
Err(PrecompileError::OutOfGas)
26+
Err(PrecompileError::OutOfGas)
3427
} else {
3528
let output = openvm_sha2::sha256(input);
3629
Ok(PrecompileOutput::new(cost, output.to_vec().into()))
3730
}
3831
} else {
39-
revm::precompile::hash::sha256_run(input, gas_limit)
32+
hash::sha256_run(input, gas_limit)
4033
}
4134
}
4235
}
@@ -45,16 +38,10 @@ pub mod sha256 {
4538
pub mod ripemd160 {
4639
use super::*;
4740

48-
// CONSTANTS
49-
// --------------------------------------------------------------------------------------------
50-
5141
/// The RIPEMD160 precompile address.
52-
const ADDRESS: Address = revm::precompile::hash::RIPEMD160.0;
53-
54-
// RIPEMD160 SHANGHAI PRECOMPILE
55-
// --------------------------------------------------------------------------------------------
42+
pub const ADDRESS: Address = hash::RIPEMD160.0;
5643

57-
/// The shanghai RIPEMD160 precompile implementation with address.
44+
/// The shanghai RIPEMD160 precompile is not implemented in the Shanghai hardfork.
5845
///
5946
/// This precompile is not implemented and will return `PrecompileError::Other("Precompile not
6047
/// implemented".into())`.

src/precompile/modexp.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
use revm::{
22
precompile::{
3-
modexp::{berlin_gas_calc, run_inner},
3+
modexp::{self, berlin_gas_calc, run_inner},
44
utilities::right_pad_with_offset,
55
PrecompileError, PrecompileResult, PrecompileWithAddress,
66
},
77
primitives::{Address, U256},
88
};
99

10-
// CONSTANTS
11-
// ================================================================================================
12-
1310
/// The MODEXP precompile address.
14-
const ADDRESS: Address = revm::precompile::modexp::OSAKA.0;
15-
16-
/// The maximum length of the input for the MODEXP precompile.
17-
const SCROLL_LEN_LIMIT: U256 = U256::from_limbs([32, 0, 0, 0]);
11+
pub const ADDRESS: Address = modexp::BYZANTIUM.0;
1812

19-
// MODEXP PRECOMPILE
20-
// ================================================================================================
13+
/// The maximum length of the input for the MODEXP precompile in BERNOULLI hardfork.
14+
pub const BERNOULLI_LEN_LIMIT: U256 = U256::from_limbs([32, 0, 0, 0]);
2115

22-
/// The bernoulli MODEXP precompile implementation with address.
16+
/// The MODEXP precompile with BERNOULLI length limit rule.
2317
pub const BERNOULLI: PrecompileWithAddress = PrecompileWithAddress(ADDRESS, bernoulli_run);
2418

2519
/// The bernoulli MODEXP precompile implementation.
@@ -31,19 +25,19 @@ pub const BERNOULLI: PrecompileWithAddress = PrecompileWithAddress(ADDRESS, bern
3125
/// length is greater than 32 bytes.
3226
/// - `PrecompileError::Other("ModexpModOverflow: modexp mod overflow".into())` if the modulus
3327
/// length is greater than 32 bytes.
34-
fn bernoulli_run(input: &[u8], gas_limit: u64) -> PrecompileResult {
28+
pub fn bernoulli_run(input: &[u8], gas_limit: u64) -> PrecompileResult {
3529
let base_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 0).into_owned());
3630
let exp_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 32).into_owned());
3731
let mod_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 64).into_owned());
3832

3933
// modexp temporarily only accepts inputs of 32 bytes (256 bits) or less
40-
if base_len > SCROLL_LEN_LIMIT {
34+
if base_len > BERNOULLI_LEN_LIMIT {
4135
return Err(PrecompileError::Other("ModexpBaseOverflow: modexp base overflow".into()));
4236
}
43-
if exp_len > SCROLL_LEN_LIMIT {
37+
if exp_len > BERNOULLI_LEN_LIMIT {
4438
return Err(PrecompileError::Other("ModexpExpOverflow: modexp exp overflow".into()));
4539
}
46-
if mod_len > SCROLL_LEN_LIMIT {
40+
if mod_len > BERNOULLI_LEN_LIMIT {
4741
return Err(PrecompileError::Other("ModexpModOverflow: modexp mod overflow".into()));
4842
}
4943

0 commit comments

Comments
 (0)