Skip to content

Commit

Permalink
add insert_precompiles function
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Mar 20, 2024
1 parent 21e3d06 commit e008ab3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 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/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ alphanet-instructions.workspace = true
reth.workspace = true
reth-node-api.workspace = true
reth-node-optimism.workspace = true
revm-precompile.workspace = true

[lints]
workspace = true
31 changes: 14 additions & 17 deletions crates/node/src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
use alphanet_instructions::eip3074;
use alphanet_precompile::{
bls12_381::{
BLS12_G1ADD, BLS12_G1MUL, BLS12_G1MULTIEXP, BLS12_G2ADD, BLS12_G2MUL, BLS12_G2MULTIEXP,
BLS12_MAP_FP2_TO_G2, BLS12_MAP_FP_TO_G1, BLS12_PAIRING,
},
secp256r1::P256VERIFY,
};
use alphanet_precompile::{bls12_381, secp256r1};
use reth::{
primitives::{
revm::{config::revm_spec, env::fill_op_tx_env},
Expand All @@ -19,13 +13,23 @@ use reth::{
},
};
use reth_node_api::{ConfigureEvm, ConfigureEvmEnv};
use revm_precompile::PrecompileWithAddress;
use std::sync::Arc;

/// Custom EVM configuration
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct AlphaNetEvmConfig;

fn insert_precompiles<I>(precompiles: &mut Precompiles, precompiles_with_address: I)
where
I: Iterator<Item = PrecompileWithAddress>,
{
for precompile_with_address in precompiles_with_address {
precompiles.inner.insert(precompile_with_address.0, precompile_with_address.1);
}
}

impl AlphaNetEvmConfig {
/// Sets the precompiles to the EVM handler
///
Expand All @@ -43,16 +47,9 @@ impl AlphaNetEvmConfig {
// install the precompiles
handler.pre_execution.load_precompiles = Arc::new(move || {
let mut precompiles = Precompiles::new(PrecompileSpecId::from_spec_id(spec_id)).clone();
precompiles.inner.insert(P256VERIFY.0, P256VERIFY.1);
precompiles.inner.insert(BLS12_G1ADD.0, BLS12_G1ADD.1);
precompiles.inner.insert(BLS12_G1MUL.0, BLS12_G1MUL.1);
precompiles.inner.insert(BLS12_G1MULTIEXP.0, BLS12_G1MULTIEXP.1);
precompiles.inner.insert(BLS12_G2ADD.0, BLS12_G2ADD.1);
precompiles.inner.insert(BLS12_G2MUL.0, BLS12_G2MUL.1);
precompiles.inner.insert(BLS12_G2MULTIEXP.0, BLS12_G2MULTIEXP.1);
precompiles.inner.insert(BLS12_PAIRING.0, BLS12_PAIRING.1);
precompiles.inner.insert(BLS12_MAP_FP_TO_G1.0, BLS12_MAP_FP_TO_G1.1);
precompiles.inner.insert(BLS12_MAP_FP2_TO_G2.0, BLS12_MAP_FP2_TO_G2.1);
insert_precompiles(&mut precompiles, secp256r1::precompiles());
insert_precompiles(&mut precompiles, bls12_381::precompiles());

precompiles.into()
});
}
Expand Down
34 changes: 25 additions & 9 deletions crates/precompile/src/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,24 @@ const PADDED_INPUT_LENGTH: usize = 64;
const PADDING_LEGTH: usize = 16;
const FP_CONCAT_LENGTH: usize = 96;

/// bls12381 precompiles
pub fn precompiles() -> impl Iterator<Item = PrecompileWithAddress> {
[
BLS12_G1ADD,
BLS12_G1MUL,
BLS12_G1MULTIEXP,
BLS12_G2ADD,
BLS12_G2MUL,
BLS12_G2MULTIEXP,
BLS12_PAIRING,
BLS12_MAP_FP_TO_G1,
BLS12_MAP_FP2_TO_G2,
]
.into_iter()
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile.
pub const BLS12_G1ADD: PrecompileWithAddress =
const BLS12_G1ADD: PrecompileWithAddress =
PrecompileWithAddress(crate::u64_to_address(BLS12_G1ADD_ADDRESS), Precompile::Standard(g1_add));

// Removes zeros with which the precompile inputs are left padded to 64 bytes.
Expand Down Expand Up @@ -137,7 +153,7 @@ fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MUL precompile.
pub const BLS12_G1MUL: PrecompileWithAddress =
const BLS12_G1MUL: PrecompileWithAddress =
PrecompileWithAddress(crate::u64_to_address(BLS12_G1MUL_ADDRESS), Precompile::Standard(g1_mul));

fn g1_mul(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
Expand All @@ -150,7 +166,7 @@ fn g1_mul(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MULTIEXP precompile.
pub const BLS12_G1MULTIEXP: PrecompileWithAddress = PrecompileWithAddress(
const BLS12_G1MULTIEXP: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(BLS12_G1MULTIEXP_ADDRESS),
Precompile::Standard(g1_multiexp),
);
Expand All @@ -166,7 +182,7 @@ fn g1_multiexp(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2ADD precompile.
pub const BLS12_G2ADD: PrecompileWithAddress =
const BLS12_G2ADD: PrecompileWithAddress =
PrecompileWithAddress(crate::u64_to_address(BLS12_G2ADD_ADDRESS), Precompile::Standard(g2_add));

fn g2_add(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
Expand All @@ -179,7 +195,7 @@ fn g2_add(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MUL precompile.
pub const BLS12_G2MUL: PrecompileWithAddress =
const BLS12_G2MUL: PrecompileWithAddress =
PrecompileWithAddress(crate::u64_to_address(BLS12_G2MUL_ADDRESS), Precompile::Standard(g2_mul));

fn g2_mul(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
Expand All @@ -192,7 +208,7 @@ fn g2_mul(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MULTIEXP precompile.
pub const BLS12_G2MULTIEXP: PrecompileWithAddress = PrecompileWithAddress(
const BLS12_G2MULTIEXP: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(BLS12_G2MULTIEXP_ADDRESS),
Precompile::Standard(g2_multiexp),
);
Expand All @@ -208,7 +224,7 @@ fn g2_multiexp(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile.
pub const BLS12_PAIRING: PrecompileWithAddress = PrecompileWithAddress(
const BLS12_PAIRING: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(BLS12_PAIRING_ADDRESS),
Precompile::Standard(pairing),
);
Expand All @@ -224,7 +240,7 @@ fn pairing(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP_TO_G1 precompile.
pub const BLS12_MAP_FP_TO_G1: PrecompileWithAddress = PrecompileWithAddress(
const BLS12_MAP_FP_TO_G1: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(BLS12_MAP_FP_TO_G1_ADDRESS),
Precompile::Standard(map_fp_to_g1),
);
Expand All @@ -240,7 +256,7 @@ fn map_fp_to_g1(_input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP2_TO_G2 precompile.
pub const BLS12_MAP_FP2_TO_G2: PrecompileWithAddress = PrecompileWithAddress(
const BLS12_MAP_FP2_TO_G2: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(BLS12_MAP_FP2_TO_G2_ADDRESS),
Precompile::Standard(map_fp2_to_g2),
);
Expand Down
7 changes: 6 additions & 1 deletion crates/precompile/src/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ use p256::ecdsa::{signature::hazmat::PrehashVerifier, Signature, VerifyingKey};
use revm_precompile::{Precompile, PrecompileWithAddress};
use revm_primitives::{Bytes, PrecompileError, PrecompileResult, B256};

/// secp256r1 precompiles
pub fn precompiles() -> impl Iterator<Item = PrecompileWithAddress> {
[P256VERIFY].into_iter()
}

/// [EIP-7212](https://eips.ethereum.org/EIPS/eip-7212#specification) secp256r1 precompile.
pub const P256VERIFY: PrecompileWithAddress = PrecompileWithAddress(
const P256VERIFY: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(P256VERIFY_ADDRESS),
Precompile::Standard(p256_verify),
);
Expand Down

0 comments on commit e008ab3

Please sign in to comment.