Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add feature-gating in prover code based on target network #598

Merged
merged 19 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ jobs:
run: cargo fmt --all --check

- name: Run cargo clippy
run: cargo clippy --all-features --all-targets -- -D warnings -A incomplete-features
atanmarko marked this conversation as resolved.
Show resolved Hide resolved
run: cargo clippy --all-targets -- -D warnings -A incomplete-features

- name: Rustdoc
run: cargo doc --all
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 @@ -56,15 +56,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 = []
eth_mainnet = []

[[bin]]
name = "assemble"
Expand Down
9 changes: 2 additions & 7 deletions evm_arithmetization/benches/fibonacci_25m_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use evm_arithmetization::generation::{GenerationInputs, TrieInputs};
use evm_arithmetization::proof::{BlockHashes, BlockMetadata, TrieRoots};
use evm_arithmetization::prover::testing::simulate_execution;
use evm_arithmetization::testing_utils::{
beacon_roots_account_nibbles, beacon_roots_contract_from_storage, ger_account_nibbles,
beacon_roots_account_nibbles, beacon_roots_contract_from_storage,
preinitialized_state_and_storage_tries, update_beacon_roots_account_storage,
GLOBAL_EXIT_ROOT_ACCOUNT,
};
use evm_arithmetization::Node;
use hex_literal::hex;
Expand Down Expand Up @@ -148,10 +147,6 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs> {
beacon_roots_account_nibbles(),
rlp::encode(&beacon_roots_account).to_vec(),
)?;
expected_state_trie_after.insert(
ger_account_nibbles(),
rlp::encode(&GLOBAL_EXIT_ROOT_ACCOUNT).to_vec(),
)?;

let receipt_0 = LegacyReceiptRlp {
status: false,
Expand Down Expand Up @@ -186,7 +181,7 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs> {
checkpoint_state_trie_root: H256(hex!(
"fe07ff6d1ab215df17884b89112ccf2373597285a56c5902150313ad1a53ee57"
)),
global_exit_roots: vec![],
ger_data: None,
block_metadata,
txn_number_before: 0.into(),
gas_used_before: 0.into(),
Expand Down
19 changes: 16 additions & 3 deletions evm_arithmetization/src/cpu/kernel/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ use super::assembler::{assemble, Kernel};
use crate::cpu::kernel::constants::evm_constants;
use crate::cpu::kernel::parser::parse;

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

pub static KERNEL_FILES: [&str; NUMBER_KERNEL_FILES] = [
"global jumped_to_0: PANIC",
Expand Down Expand Up @@ -56,8 +62,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"),
atanmarko marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -164,6 +171,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 @@ -172,7 +180,8 @@ pub static KERNEL_FILES: [&str; NUMBER_KERNEL_FILES] = [
include_str!("asm/account_code.asm"),
include_str!("asm/balance.asm"),
include_str!("asm/bloom_filter.asm"),
include_str!("asm/global_exit_root.asm"),
#[cfg(feature = "cdk_erigon")]
include_str!("asm/cdk_pre_execution.asm"),
];

pub static KERNEL: Lazy<Kernel> = Lazy::new(combined_kernel);
Expand All @@ -181,6 +190,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
5 changes: 4 additions & 1 deletion evm_arithmetization/src/cpu/kernel/asm/beacon_roots.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
///
/// *NOTE*: This will panic if one of the provided timestamps is zero.

/// Pre-stack: (empty)
/// Post-stack: (empty)
global set_beacon_root:
PUSH set_global_exit_roots
// stack: (empty)
PUSH txn_loop
%timestamp
// stack: timestamp, retdest
PUSH @HISTORY_BUFFER_LENGTH
Expand Down
118 changes: 118 additions & 0 deletions evm_arithmetization/src/cpu/kernel/asm/cdk_pre_execution.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/// CDK-Erigon pre-block execution logic.
/// Reference implementation: `cdk-erigon/core/state/intra_block_state_zkevm.go`.
/// This currently supports the Etrog upgrade.

/// Pre-stack: (empty)
/// Post-stack: (empty)
global pre_block_execution:
// stack: (empty)
PUSH txn_loop
// stack: retdest
PUSH @ADDRESS_SCALABLE_L2
%is_non_existent
%jumpi(create_scalable_l2_account)

global update_scalable_block_number:
// stack: retdest
%blocknumber
PUSH @LAST_BLOCK_STORAGE_POS
// stack: last_block_slot, block_number, retdest
%write_scalable_storage
// stack: retdest

// Check timestamp
PUSH @ADDRESS_SCALABLE_L2_STATE_KEY
PUSH @TIMESTAMP_STORAGE_POS
%read_storage_linked_list_w_state_key
// stack: old_timestamp, retdest
%timestamp
GT %jumpi(update_scalable_timestamp)

global update_scalable_prev_block_root_hash:
// stack: retdest
%mload_global_metadata(@GLOBAL_METADATA_STATE_TRIE_DIGEST_BEFORE)
// stack: prev_block_root, retdest
PUSH @STATE_ROOT_STORAGE_POS
PUSH 1 %blocknumber SUB
// stack: block_number - 1, STATE_ROOT_STORAGE_POS, prev_block_root, retdest
PUSH @SEGMENT_KERNEL_GENERAL
// stack: addr, block_number - 1, STATE_ROOT_STORAGE_POS, prev_block_root, retdest
MSTORE_32BYTES_32
// stack: addr, STATE_ROOT_STORAGE_POS, prev_block_root, retdest
MSTORE_32BYTES_32
// stack: addr, prev_block_root, retdest
POP
// stack: prev_block_root, retdest
PUSH 64 PUSH @SEGMENT_KERNEL_GENERAL
// stack: addr, len, prev_block_root, retdest
KECCAK_GENERAL
// stack: slot, prev_block_root, retdest
%write_scalable_storage
// stack: retdest

global update_scalable_l1blockhash:
// stack: retdest
PROVER_INPUT(ger)
// stack: l1blockhash?, retdest
DUP1 %eq_const(@U256_MAX) %jumpi(skip_and_exit)
// stack: l1blockhash, retdest
PUSH @GLOBAL_EXIT_ROOT_STORAGE_POS
PROVER_INPUT(ger)
// stack: root, GLOBAL_EXIT_ROOT_STORAGE_POS, l1blockhash, retdest
PUSH @SEGMENT_KERNEL_GENERAL
// stack: addr, root, GLOBAL_EXIT_ROOT_STORAGE_POS, l1blockhash, retdest
MSTORE_32BYTES_32
// stack: addr, GLOBAL_EXIT_ROOT_STORAGE_POS, l1blockhash, retdest
MSTORE_32BYTES_32
// stack: addr, l1blockhash, retdest
POP
// stack: l1blockhash, retdest
PUSH 64 PUSH @SEGMENT_KERNEL_GENERAL
// stack: addr, len, l1blockhash, retdest
KECCAK_GENERAL
// stack: slot, l1blockhash, retdest
%slot_to_storage_key
// stack: storage_key, l1blockhash, retdest
PUSH @GLOBAL_EXIT_ROOT_MANAGER_L2_STATE_KEY
// stack: state_key, storage_key, l1blockhash, retdest
%insert_slot_with_value_from_keys
// stack: retdest
JUMP

skip_and_exit:
// stack: null, retdest
POP
JUMP

global update_scalable_timestamp:
// stack: retdest
%timestamp
PUSH @TIMESTAMP_STORAGE_POS
// stack: timestamp_slot, timestamp, retdest
%write_scalable_storage
%jump(update_scalable_prev_block_root_hash)

global create_scalable_l2_account:
// stack: (empty)
PUSH update_scalable_block_number
// stack: retdest
%get_trie_data_size // pointer to new account we're about to create
// stack: new_account_ptr, retdest
PUSH 0 %append_to_trie_data // nonce
PUSH 0 %append_to_trie_data // balance
PUSH 0 %append_to_trie_data // storage root pointer
PUSH @EMPTY_STRING_HASH %append_to_trie_data // code hash
// stack: new_account_ptr, retdest
PUSH @ADDRESS_SCALABLE_L2_STATE_KEY
// stack: key, new_account_ptr, retdest
%jump(mpt_insert_state_trie)

%macro write_scalable_storage
// stack: slot, value
%slot_to_storage_key
// stack: storage_key, value
PUSH @ADDRESS_SCALABLE_L2_STATE_KEY
// stack: state_key, storage_key, value
%insert_slot_with_value_from_keys
// stack: (empty)
%endmacro
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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think an invalid instruction will be caught elsewhere anyway, so we don't need to make a distinction here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems weird to me to have BYTES 1 lying around for an instruction that doesn't exist. Even though as you point this would get caught elsewhere, I think we'd rather not have invalid Kernel bits if we have workarounds

{
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
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
48 changes: 0 additions & 48 deletions evm_arithmetization/src/cpu/kernel/asm/global_exit_root.asm

This file was deleted.

Loading
Loading