Skip to content

Commit

Permalink
feat: Add feature-gating in prover code based on target network (#598)
Browse files Browse the repository at this point in the history
* WIP

* Tweak

* Final tweaks and test

* Fix

* Clippy

* Add feature-gating

* feat: Feature-gate specific bits of the prover for pos and cdk

* Add default eth_mainnet

* fix: update CI

* Pacify mighty clippy

* Use eth_mainnet feature directly in KERNEL

* Comment

* Add missing fix from feat/feature_gat_chain

* Clippy

* Missing worker config

* Apply suggestion for typed transactions

Co-authored-by: Hamy Ratoanina <[email protected]>

* Misc
  • Loading branch information
Nashtare authored Sep 6, 2024
1 parent 7f7ce75 commit 223a0d9
Show file tree
Hide file tree
Showing 45 changed files with 551 additions and 383 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ jobs:
run: cargo fmt --all --check

- name: Run cargo clippy
run: cargo clippy --all-features --all-targets -- -D warnings -A incomplete-features
run: cargo clippy --all-targets -- -D warnings -A incomplete-features

- name: Run cargo clippy (with `cdk_erigon` flag)
run: cargo clippy --all-features --all-targets --features cdk_erigon -- -D warnings -A incomplete-features
run: cargo clippy --all-targets --no-default-features --features cdk_erigon -- -D warnings -A incomplete-features

- name: Rustdoc
run: cargo doc --all
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ vergen = { version = "9.0.0", features = ["build", "rustc"] }
winnow = "0.6.13"

# local dependencies
evm_arithmetization = { path = "evm_arithmetization", version = "0.4.0" }
evm_arithmetization = { path = "evm_arithmetization", version = "0.4.0", default-features = false }
mpt_trie = { path = "mpt_trie", version = "0.4.1" }
proof_gen = { path = "proof_gen", version = "0.4.0" }
proof_gen = { path = "proof_gen", version = "0.4.0", default-features = false }
smt_trie = { path = "smt_trie", version = "0.1.1" }
trace_decoder = { path = "trace_decoder", version = "0.6.0" }
trace_decoder = { path = "trace_decoder", version = "0.6.0", default-features = false }
zk_evm_common = { path = "common", version = "0.1.0" }
zk_evm_proc_macro = { path = "proc_macro", version = "0.1.0" }

# zero-bin related dependencies
ops = { path = "zero_bin/ops" }
prover = { path = "zero_bin/prover" }
rpc = { path = "zero_bin/rpc" }
zero_bin_common = { path = "zero_bin/common" }
ops = { path = "zero_bin/ops", default-features = false }
prover = { path = "zero_bin/prover", default-features = false }
rpc = { path = "zero_bin/rpc", default-features = false }
zero_bin_common = { path = "zero_bin/common", default-features = false }

# plonky2-related dependencies
plonky2 = { git = "https://github.com/0xPolygonZero/plonky2.git", rev = "dc77c77f2b06500e16ad4d7f1c2b057903602eed" }
Expand Down
14 changes: 5 additions & 9 deletions evm_arithmetization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ hex-literal = { workspace = true }
itertools = { workspace = true }
keccak-hash = { workspace = true }
log = { workspace = true }
plonky2_maybe_rayon = { workspace = true }
plonky2_maybe_rayon = { workspace = true, features = ["parallel"] }
num = { workspace = true }
num-bigint = { workspace = true }
once_cell = { workspace = true }
pest = { workspace = true }
pest_derive = { workspace = true }
plonky2 = { workspace = true }
plonky2 = { workspace = true, features = ["parallel"] }
plonky2_util = { workspace = true }
starky = { workspace = true }
starky = { workspace = true, features = ["parallel"] }
rand = { workspace = true }
rand_chacha = { workspace = true }
rlp = { workspace = true }
Expand All @@ -57,15 +57,11 @@ hex = { workspace = true }
ripemd = { workspace = true }

[features]
default = ["parallel"]
default = ["eth_mainnet"]
asmtools = ["hex"]
parallel = [
"plonky2/parallel",
"plonky2_maybe_rayon/parallel",
"starky/parallel",
]
polygon_pos = []
cdk_erigon = ["smt_trie"]
eth_mainnet = []

[[bin]]
name = "assemble"
Expand Down
19 changes: 15 additions & 4 deletions evm_arithmetization/src/cpu/kernel/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ use super::assembler::{assemble, Kernel};
use crate::cpu::kernel::constants::evm_constants;
use crate::cpu::kernel::parser::parse;

pub const NUMBER_KERNEL_FILES: usize = if cfg!(feature = "cdk_erigon") {
159
pub const NUMBER_KERNEL_FILES: usize = if cfg!(feature = "eth_mainnet") {
157
} else if cfg!(feature = "cdk_erigon") {
156
} else if cfg!(feature = "polygon_pos") {
155
} else {
158
// unreachable
0
};

pub static KERNEL_FILES: [&str; NUMBER_KERNEL_FILES] = [
Expand Down Expand Up @@ -60,8 +65,9 @@ pub static KERNEL_FILES: [&str; NUMBER_KERNEL_FILES] = [
include_str!("asm/core/precompiles/bn_mul.asm"),
include_str!("asm/core/precompiles/snarkv.asm"),
include_str!("asm/core/precompiles/blake2_f.asm"),
#[cfg(feature = "eth_mainnet")]
include_str!("asm/core/precompiles/kzg_peval.asm"),
include_str!("asm/curve/bls381/util.asm"),
// include_str!("asm/curve/bls381/util.asm"),
include_str!("asm/curve/bn254/curve_arithmetic/constants.asm"),
include_str!("asm/curve/bn254/curve_arithmetic/curve_add.asm"),
include_str!("asm/curve/bn254/curve_arithmetic/curve_mul.asm"),
Expand Down Expand Up @@ -168,6 +174,7 @@ pub static KERNEL_FILES: [&str; NUMBER_KERNEL_FILES] = [
include_str!("asm/transactions/type_0.asm"),
include_str!("asm/transactions/type_1.asm"),
include_str!("asm/transactions/type_2.asm"),
#[cfg(feature = "eth_mainnet")]
include_str!("asm/transactions/type_3.asm"),
include_str!("asm/util/assertions.asm"),
include_str!("asm/util/basic_macros.asm"),
Expand All @@ -186,6 +193,10 @@ pub(crate) fn combined_kernel_from_files<const N: usize>(files: [&str; N]) -> Ke
let mut active_features = HashSet::new();
if cfg!(feature = "cdk_erigon") {
active_features.insert("cdk_erigon");
} else if cfg!(feature = "polygon_pos") {
active_features.insert("polygon_pos");
} else {
active_features.insert("eth_mainnet");
}

let parsed_files = files
Expand Down
13 changes: 4 additions & 9 deletions evm_arithmetization/src/cpu/kernel/asm/beacon_roots.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
///
/// *NOTE*: This will panic if one of the provided timestamps is zero.

/// Pre-stack: (empty)
/// Post-stack: (empty)
global set_beacon_root:
#[cfg(feature = cdk_erigon)]
{
PUSH pre_block_execution
}
#[cfg(not(feature = cdk_erigon))]
{
PUSH txn_loop
}

// stack: (empty)
PUSH txn_loop
%timestamp
// stack: timestamp, retdest
PUSH @HISTORY_BUFFER_LENGTH
Expand Down
12 changes: 10 additions & 2 deletions evm_arithmetization/src/cpu/kernel/asm/core/exception.asm
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,16 @@ min_stack_len_for_opcode:
BYTES 0 // 0x46, CHAINID
BYTES 0 // 0x47, SELFBALANCE
BYTES 0 // 0x48, BASEFEE
BYTES 1 // 0x49, BLOBHASH
BYTES 0 // 0x4a, BLOBBASEFEE
#[cfg(feature = eth_mainnet)]
{
BYTES 1 // 0x49, BLOBHASH
BYTES 0 // 0x4a, BLOBBASEFEE
}
#[cfg(not(feature = eth_mainnet))]
{
BYTES 0 // 0x49, BLOBHASH is only active on Ethereum mainnet
BYTES 0 // 0x4a, BLOBBASEFEE is only active on Ethereum mainnet
}
%rep 5 // 0x4b-0x4f, invalid
BYTES 0
%endrep
Expand Down
11 changes: 9 additions & 2 deletions evm_arithmetization/src/cpu/kernel/asm/core/precompiles/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ global handle_precompiles:
DUP1 %eq_const(@BN_ADD) %jumpi(precompile_bn_add)
DUP1 %eq_const(@BN_MUL) %jumpi(precompile_bn_mul)
DUP1 %eq_const(@SNARKV) %jumpi(precompile_snarkv)
DUP1 %eq_const(@BLAKE2_F) %jumpi(precompile_blake2_f)
%eq_const(@KZG_PEVAL) %jumpi(precompile_kzg_peval)
#[cfg(feature = eth_mainnet)]
{
DUP1 %eq_const(@BLAKE2_F) %jumpi(precompile_blake2_f)
%eq_const(@KZG_PEVAL) %jumpi(precompile_kzg_peval)
}
#[cfg(not(feature = eth_mainnet))]
{
%eq_const(@BLAKE2_F) %jumpi(precompile_blake2_f)
}
// stack: retdest
JUMP

Expand Down
12 changes: 10 additions & 2 deletions evm_arithmetization/src/cpu/kernel/asm/core/syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,16 @@ global syscall_jumptable:
JUMPTABLE sys_chainid
JUMPTABLE sys_selfbalance
JUMPTABLE sys_basefee
JUMPTABLE sys_blobhash
JUMPTABLE sys_blobbasefee
#[cfg(feature = eth_mainnet)]
{
JUMPTABLE sys_blobhash
JUMPTABLE sys_blobbasefee
}
#[cfg(not(feature = eth_mainnet))]
{
JUMPTABLE panic // BLOBHASH is only active on Ethereum mainnet
JUMPTABLE panic // BLOBBASEFEE is only active on Ethereum mainnet
}
%rep 5
JUMPTABLE panic // 0x4b-0x4f are invalid opcodes
%endrep
Expand Down
23 changes: 18 additions & 5 deletions evm_arithmetization/src/cpu/kernel/asm/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,20 @@ global start_txns:
%mload_global_metadata(@GLOBAL_METADATA_BLOCK_GAS_USED_BEFORE)
// stack: init_gas_used, txn_counter, num_nibbles, txn_nb

// If txn_idx == 0, update the beacon_root and exit roots.
%mload_global_metadata(@GLOBAL_METADATA_TXN_NUMBER_BEFORE)
ISZERO
%jumpi(set_beacon_root)
#[cfg(feature = eth_mainnet)]
{
// If txn_idx == 0, update the beacon_root for Ethereum mainnet.
%mload_global_metadata(@GLOBAL_METADATA_TXN_NUMBER_BEFORE)
ISZERO
%jumpi(set_beacon_root)
}
#[cfg(feature = cdk_erigon)]
{
// If txn_idx == 0, perform pre-state execution for CDK erigon.
%mload_global_metadata(@GLOBAL_METADATA_TXN_NUMBER_BEFORE)
ISZERO
%jumpi(pre_block_execution)
}

// stack: init_gas_used, txn_counter, num_nibbles, txn_nb
global txn_loop:
Expand Down Expand Up @@ -255,5 +265,8 @@ global check_final_state_trie:
PUSH 0 %mstore_txn_field(@TXN_FIELD_CHAIN_ID_PRESENT)
PUSH 0 %mstore_txn_field(@TXN_FIELD_TO)

%reset_blob_versioned_hashes
#[cfg(feature = eth_mainnet)]
{
%reset_blob_versioned_hashes
}
%endmacro
86 changes: 45 additions & 41 deletions evm_arithmetization/src/cpu/kernel/asm/memory/metadata.asm
Original file line number Diff line number Diff line change
Expand Up @@ -277,47 +277,51 @@ global sys_basefee:
SWAP1
EXIT_KERNEL

global sys_blobhash:
// stack: kexit_info, index
%charge_gas_const(@GAS_HASH_OPCODE)
// stack: kexit_info, index
%blobhash
// stack: blobhash, kexit_info
SWAP1
EXIT_KERNEL

%macro blobhash
// stack: kexit_info, index
SWAP1
// stack: index, kexit_info
%mload_global_metadata(@GLOBAL_METADATA_BLOB_VERSIONED_HASHES_LEN)
DUP2
LT ISZERO // == GE
// stack: index >= len, index, kexit_info
%jumpi(%%index_too_big)
PUSH @SEGMENT_TXN_BLOB_VERSIONED_HASHES
%build_kernel_address
// stack: read_addr, kexit_info
MLOAD_GENERAL
%jump(%%end)
%%index_too_big:
// The index is larger than the list, just push 0.
// stack: index, kexit_info
POP
PUSH 0
// stack: 0, kexit_info
%%end:
// stack: blobhash, kexit_info
%endmacro

global sys_blobbasefee:
// stack: kexit_info
%charge_gas_const(@GAS_BASE)
// stack: kexit_info
PROVER_INPUT(blobbasefee)
// stack: blobbasefee, kexit_info
SWAP1
EXIT_KERNEL
/// Blob-related macros are only available for Ethereum mainnet.
#[cfg(feature = eth_mainnet)]
{
global sys_blobhash:
// stack: kexit_info, index
%charge_gas_const(@GAS_HASH_OPCODE)
// stack: kexit_info, index
%blobhash
// stack: blobhash, kexit_info
SWAP1
EXIT_KERNEL

%macro blobhash
// stack: kexit_info, index
SWAP1
// stack: index, kexit_info
%mload_global_metadata(@GLOBAL_METADATA_BLOB_VERSIONED_HASHES_LEN)
DUP2
LT ISZERO // == GE
// stack: index >= len, index, kexit_info
%jumpi(%%index_too_big)
PUSH @SEGMENT_TXN_BLOB_VERSIONED_HASHES
%build_kernel_address
// stack: read_addr, kexit_info
MLOAD_GENERAL
%jump(%%end)
%%index_too_big:
// The index is larger than the list, just push 0.
// stack: index, kexit_info
POP
PUSH 0
// stack: 0, kexit_info
%%end:
// stack: blobhash, kexit_info
%endmacro

global sys_blobbasefee:
// stack: kexit_info
%charge_gas_const(@GAS_BASE)
// stack: kexit_info
PROVER_INPUT(blobbasefee)
// stack: blobbasefee, kexit_info
SWAP1
EXIT_KERNEL
}

global sys_blockhash:
// stack: kexit_info, block_number
Expand Down
Loading

0 comments on commit 223a0d9

Please sign in to comment.