Skip to content

Commit

Permalink
feat(levm): prague precompiles scaffolding (#1683)
Browse files Browse the repository at this point in the history
**Motivation**

In this PR we add a scaffolding for the new
[precompiles](https://eips.ethereum.org/EIPS/eip-2537) in the fork of
Prague.

**Description**

Create a new array for the new precompiles addresses.
Matches the addresses and dispatches for the precompile function.
  • Loading branch information
tomip01 authored Jan 10, 2025
1 parent 5e2428d commit f393683
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/vm/levm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ num-bigint = "0.4.5"
lambdaworks-math = "0.11.0"
k256 = { version = "0.13.3", features = ["ecdh"] }
kzg-rs = "0.2.3"
bls12_381 = "0.8.0"

[dev-dependencies]
hex.workspace = true
Expand Down
120 changes: 120 additions & 0 deletions crates/vm/levm/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ pub const POINT_EVALUATION_ADDRESS: H160 = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0a,
]);
pub const BLS12_G1ADD_ADDRESS: H160 = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0b,
]);
pub const BLS12_G1MSM_ADDRESS: H160 = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0c,
]);
pub const BLS12_G2ADD_ADDRESS: H160 = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0d,
]);
pub const BLS12_G2MSM_ADDRESS: H160 = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0e,
]);
pub const BLS12_PAIRING_CHECK_ADDRESS: H160 = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f,
]);
pub const BLS12_MAP_FP_TO_G1_ADDRESS: H160 = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x10,
]);
pub const BLS12_MAP_FP2_TO_G2_ADDRESS: H160 = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x11,
]);

pub const PRECOMPILES: [H160; 10] = [
ECRECOVER_ADDRESS,
Expand All @@ -94,13 +122,28 @@ pub const PRECOMPILES: [H160; 10] = [
POINT_EVALUATION_ADDRESS,
];

pub const PRECOMPILES_POST_CANCUN: [H160; 7] = [
BLS12_G1ADD_ADDRESS,
BLS12_G1MSM_ADDRESS,
BLS12_G2ADD_ADDRESS,
BLS12_G2MSM_ADDRESS,
BLS12_PAIRING_CHECK_ADDRESS,
BLS12_MAP_FP_TO_G1_ADDRESS,
BLS12_MAP_FP2_TO_G2_ADDRESS,
];

pub const BLAKE2F_ELEMENT_SIZE: usize = 8;

pub fn is_precompile(callee_address: &Address, spec_id: SpecId) -> bool {
// Cancun specs is the only one that allows point evaluation precompile
if *callee_address == POINT_EVALUATION_ADDRESS && spec_id < SpecId::CANCUN {
return false;
}
// Prague or newers forks should only use this precompiles
// https://eips.ethereum.org/EIPS/eip-2537
if PRECOMPILES_POST_CANCUN.contains(callee_address) && spec_id < SpecId::PRAGUE {
return false;
}

PRECOMPILES.contains(callee_address)
}
Expand Down Expand Up @@ -138,6 +181,27 @@ pub fn execute_precompile(
address if address == POINT_EVALUATION_ADDRESS => {
point_evaluation(&calldata, gas_for_call, consumed_gas)?
}
address if address == BLS12_G1ADD_ADDRESS => {
bls12_g1add(&calldata, gas_for_call, consumed_gas)?
}
address if address == BLS12_G1MSM_ADDRESS => {
bls12_g1msm(&calldata, gas_for_call, consumed_gas)?
}
address if address == BLS12_G2ADD_ADDRESS => {
bls12_g2add(&calldata, gas_for_call, consumed_gas)?
}
address if address == BLS12_G2MSM_ADDRESS => {
bls12_g2msm(&calldata, gas_for_call, consumed_gas)?
}
address if address == BLS12_PAIRING_CHECK_ADDRESS => {
bls12_pairing_check(&calldata, gas_for_call, consumed_gas)?
}
address if address == BLS12_MAP_FP_TO_G1_ADDRESS => {
bls12_map_fp_to_g1(&calldata, gas_for_call, consumed_gas)?
}
address if address == BLS12_MAP_FP2_TO_G2_ADDRESS => {
bls12_map_fp2_tp_g2(&calldata, gas_for_call, consumed_gas)?
}
_ => return Err(VMError::Internal(InternalError::InvalidPrecompileAddress)),
};

Expand Down Expand Up @@ -1082,3 +1146,59 @@ fn point_evaluation(

Ok(Bytes::from(output))
}

pub fn bls12_g1add(
_calldata: &Bytes,
_gas_for_call: u64,
_consumed_gas: &mut u64,
) -> Result<Bytes, VMError> {
Ok(Bytes::new())
}

pub fn bls12_g1msm(
_calldata: &Bytes,
_gas_for_call: u64,
_consumed_gas: &mut u64,
) -> Result<Bytes, VMError> {
Ok(Bytes::new())
}

pub fn bls12_g2add(
_calldata: &Bytes,
_gas_for_call: u64,
_consumed_gas: &mut u64,
) -> Result<Bytes, VMError> {
Ok(Bytes::new())
}

pub fn bls12_g2msm(
_calldata: &Bytes,
_gas_for_call: u64,
_consumed_gas: &mut u64,
) -> Result<Bytes, VMError> {
Ok(Bytes::new())
}

pub fn bls12_pairing_check(
_calldata: &Bytes,
_gas_for_call: u64,
_consumed_gas: &mut u64,
) -> Result<Bytes, VMError> {
Ok(Bytes::new())
}

pub fn bls12_map_fp_to_g1(
_calldata: &Bytes,
_gas_for_call: u64,
_consumed_gas: &mut u64,
) -> Result<Bytes, VMError> {
Ok(Bytes::new())
}

pub fn bls12_map_fp2_tp_g2(
_calldata: &Bytes,
_gas_for_call: u64,
_consumed_gas: &mut u64,
) -> Result<Bytes, VMError> {
Ok(Bytes::new())
}

0 comments on commit f393683

Please sign in to comment.