From 364cd45725c08183a3d9647adb7129917a596cb1 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Thu, 6 Jun 2024 10:00:34 -0400 Subject: [PATCH 1/5] Update with latest plonky2 changes --- Cargo.toml | 9 +++--- evm_arithmetization/src/all_stark.rs | 4 +++ .../src/cpu/kernel/tests/bignum/mod.rs | 4 +-- evm_arithmetization/src/cpu/kernel/utils.rs | 3 +- .../src/fixed_recursive_verifier.rs | 31 ++++++++++--------- .../src/keccak_sponge/keccak_sponge_stark.rs | 3 +- evm_arithmetization/src/logic.rs | 3 +- evm_arithmetization/src/verifier.rs | 17 +++++----- 8 files changed, 39 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 61db35cd5..88bb44fe9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,11 +20,10 @@ serde_json = "1.0.96" thiserror = "1.0.49" # plonky2-related dependencies -plonky2 = "0.2.2" -plonky2_maybe_rayon = "0.2.0" -plonky2_util = "0.2.0" -starky = "0.4.0" - +plonky2 = { git = "https://github.com/0xPolygonZero/plonky2", branch = "main" } +plonky2_maybe_rayon = { git = "https://github.com/0xPolygonZero/plonky2", branch = "main" } +plonky2_util = { git = "https://github.com/0xPolygonZero/plonky2", branch = "main" } +starky = { git = "https://github.com/0xPolygonZero/plonky2", branch = "main" } [workspace.package] edition = "2021" diff --git a/evm_arithmetization/src/all_stark.rs b/evm_arithmetization/src/all_stark.rs index f8422b300..9ad3eea52 100644 --- a/evm_arithmetization/src/all_stark.rs +++ b/evm_arithmetization/src/all_stark.rs @@ -123,6 +123,10 @@ pub(crate) fn all_cross_table_lookups() -> Vec> { ] } +/// Position of the Memory CTL within the vector of CTLs returned by +/// `all_cross_table_lookups()`. +pub(crate) const MEMORY_CTL_INDEX: usize = Table::Memory as usize; + /// `CrossTableLookup` for `ArithmeticStark`, to connect it with the `Cpu` /// module. fn ctl_arithmetic() -> CrossTableLookup { diff --git a/evm_arithmetization/src/cpu/kernel/tests/bignum/mod.rs b/evm_arithmetization/src/cpu/kernel/tests/bignum/mod.rs index c18ad5f76..68a5e00c0 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/bignum/mod.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/bignum/mod.rs @@ -6,10 +6,10 @@ use std::path::PathBuf; use anyhow::Result; use ethereum_types::U256; use itertools::Itertools; +use num::integer::div_ceil; use num::{BigUint, One, Zero}; use num_bigint::RandBigInt; use plonky2::field::goldilocks_field::GoldilocksField as F; -use plonky2_util::ceil_div_usize; use rand::Rng; use crate::cpu::kernel::aggregator::KERNEL; @@ -90,7 +90,7 @@ fn max_bignum(bit_size: usize) -> BigUint { } fn bignum_len(a: &BigUint) -> usize { - ceil_div_usize(a.bits() as usize, BIGNUM_LIMB_BITS) + (a.bits() as usize).div_ceil(BIGNUM_LIMB_BITS) } fn run_test(fn_label: &str, memory: Vec, stack: Vec) -> Result<(Vec, Vec)> { diff --git a/evm_arithmetization/src/cpu/kernel/utils.rs b/evm_arithmetization/src/cpu/kernel/utils.rs index adda086e8..082086d17 100644 --- a/evm_arithmetization/src/cpu/kernel/utils.rs +++ b/evm_arithmetization/src/cpu/kernel/utils.rs @@ -1,7 +1,6 @@ use core::fmt::Debug; use ethereum_types::U256; -use plonky2_util::ceil_div_usize; /// Enumerate the length `W` windows of `vec`, and run `maybe_replace` on each /// one. @@ -28,7 +27,7 @@ where } pub(crate) fn u256_to_trimmed_be_bytes(u256: &U256) -> Vec { - let num_bytes = ceil_div_usize(u256.bits(), 8); + let num_bytes = u256.bits().div_ceil(8); // `byte` is little-endian, so we manually reverse it. (0..num_bytes).rev().map(|i| u256.byte(i)).collect() } diff --git a/evm_arithmetization/src/fixed_recursive_verifier.rs b/evm_arithmetization/src/fixed_recursive_verifier.rs index ed62014f5..98ee74a35 100644 --- a/evm_arithmetization/src/fixed_recursive_verifier.rs +++ b/evm_arithmetization/src/fixed_recursive_verifier.rs @@ -35,7 +35,7 @@ use starky::lookup::{get_grand_product_challenge_set_target, GrandProductChallen use starky::proof::StarkProofWithMetadata; use starky::stark::Stark; -use crate::all_stark::{all_cross_table_lookups, AllStark, Table, NUM_TABLES}; +use crate::all_stark::{all_cross_table_lookups, AllStark, Table, MEMORY_CTL_INDEX, NUM_TABLES}; use crate::generation::GenerationInputs; use crate::get_challenges::observe_public_values_target; use crate::proof::{ @@ -587,26 +587,27 @@ where // Extra sums to add to the looked last value. // Only necessary for the Memory values. - let mut extra_looking_sums = - vec![vec![builder.zero(); stark_config.num_challenges]; NUM_TABLES]; - - // Memory - extra_looking_sums[*Table::Memory] = (0..stark_config.num_challenges) - .map(|c| { - get_memory_extra_looking_sum_circuit( - &mut builder, - &public_values, - ctl_challenges.challenges[c], - ) - }) - .collect_vec(); + let mut extra_looking_sums = HashMap::new(); + + extra_looking_sums.insert( + MEMORY_CTL_INDEX, + (0..stark_config.num_challenges) + .map(|c| { + get_memory_extra_looking_sum_circuit( + &mut builder, + &public_values, + ctl_challenges.challenges[c], + ) + }) + .collect_vec(), + ); // Verify the CTL checks. verify_cross_table_lookups_circuit::( &mut builder, all_cross_table_lookups(), pis.map(|p| p.ctl_zs_first), - Some(&extra_looking_sums), + &extra_looking_sums, stark_config, ); diff --git a/evm_arithmetization/src/keccak_sponge/keccak_sponge_stark.rs b/evm_arithmetization/src/keccak_sponge/keccak_sponge_stark.rs index 131e69560..bb6d466e1 100644 --- a/evm_arithmetization/src/keccak_sponge/keccak_sponge_stark.rs +++ b/evm_arithmetization/src/keccak_sponge/keccak_sponge_stark.rs @@ -13,7 +13,6 @@ use plonky2::iop::ext_target::ExtensionTarget; use plonky2::timed; use plonky2::util::timing::TimingTree; use plonky2::util::transpose; -use plonky2_util::ceil_div_usize; use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; use starky::evaluation_frame::StarkEvaluationFrame; use starky::lookup::{Column, Filter, Lookup}; @@ -137,7 +136,7 @@ pub(crate) fn ctl_looking_memory(i: usize) -> Vec> { /// Returns the number of `KeccakSponge` tables looking into the `LogicStark`. pub(crate) const fn num_logic_ctls() -> usize { const U8S_PER_CTL: usize = 32; - ceil_div_usize(KECCAK_RATE_BYTES, U8S_PER_CTL) + KECCAK_RATE_BYTES.div_ceil(U8S_PER_CTL) } /// Creates the vector of `Columns` required to perform the `i`th logic CTL. diff --git a/evm_arithmetization/src/logic.rs b/evm_arithmetization/src/logic.rs index c5f952465..be389450c 100644 --- a/evm_arithmetization/src/logic.rs +++ b/evm_arithmetization/src/logic.rs @@ -10,7 +10,6 @@ use plonky2::hash::hash_types::RichField; use plonky2::iop::ext_target::ExtensionTarget; use plonky2::timed; use plonky2::util::timing::TimingTree; -use plonky2_util::ceil_div_usize; use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; use starky::evaluation_frame::StarkEvaluationFrame; use starky::lookup::{Column, Filter}; @@ -28,7 +27,7 @@ const VAL_BITS: usize = 256; pub(crate) const PACKED_LIMB_BITS: usize = 32; /// Number of field elements needed to store each input/output at the specified /// packing. -const PACKED_LEN: usize = ceil_div_usize(VAL_BITS, PACKED_LIMB_BITS); +const PACKED_LEN: usize = VAL_BITS.div_ceil(PACKED_LIMB_BITS); /// `LogicStark` columns. pub(crate) mod columns { diff --git a/evm_arithmetization/src/verifier.rs b/evm_arithmetization/src/verifier.rs index 52fa5304f..a8e927b94 100644 --- a/evm_arithmetization/src/verifier.rs +++ b/evm_arithmetization/src/verifier.rs @@ -1,5 +1,6 @@ use anyhow::Result; use ethereum_types::{BigEndianHash, U256}; +use hashbrown::HashMap; use itertools::Itertools; use plonky2::field::extension::Extendable; use plonky2::hash::hash_types::RichField; @@ -10,7 +11,7 @@ use starky::lookup::GrandProductChallenge; use starky::stark::Stark; use starky::verifier::verify_stark_proof_with_challenges; -use crate::all_stark::{AllStark, Table, NUM_TABLES}; +use crate::all_stark::{AllStark, Table, MEMORY_CTL_INDEX, NUM_TABLES}; use crate::cpu::kernel::aggregator::KERNEL; use crate::cpu::kernel::constants::global_metadata::GlobalMetadata; use crate::memory::segments::Segment; @@ -117,12 +118,14 @@ where // Extra sums to add to the looked last value. // Only necessary for the Memory values. - let mut extra_looking_sums = vec![vec![F::ZERO; config.num_challenges]; NUM_TABLES]; + let mut extra_looking_sums = HashMap::new(); - // Memory - extra_looking_sums[Table::Memory as usize] = (0..config.num_challenges) - .map(|i| get_memory_extra_looking_sum(&public_values, ctl_challenges.challenges[i])) - .collect_vec(); + extra_looking_sums.insert( + MEMORY_CTL_INDEX, + (0..config.num_challenges) + .map(|c| get_memory_extra_looking_sum(&public_values, ctl_challenges.challenges[c])) + .collect_vec(), + ); verify_cross_table_lookups::( cross_table_lookups, @@ -130,7 +133,7 @@ where .multi_proof .stark_proofs .map(|p| p.proof.openings.ctl_zs_first.unwrap()), - Some(&extra_looking_sums), + &extra_looking_sums, config, ) } From 4968c01cc9d41c063c76b568dec4cd8f3ada8f64 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Thu, 6 Jun 2024 10:08:15 -0400 Subject: [PATCH 2/5] Update changelog --- CHANGELOG.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca08f80a7..c4e52c2d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.4.0] - TBD + ### Changed +- Some cleanup ([#190](https://github.com/0xPolygonZero/zk_evm/pull/190)) +- Silence jumpdest analysis logs ([#193](https://github.com/0xPolygonZero/zk_evm/pull/193)) +- Charge call value gas prior to call ([#199](https://github.com/0xPolygonZero/zk_evm/pull/199)) +- refactor: fix todos ([#162](https://github.com/0xPolygonZero/zk_evm/pull/162)) +- Remove print call in trace_decoder ([#208](https://github.com/0xPolygonZero/zk_evm/pull/208)) +- Update CODEOWNERS ([#224](https://github.com/0xPolygonZero/zk_evm/pull/224)) +- Fix access lists pointers check ([#217](https://github.com/0xPolygonZero/zk_evm/pull/217)) - Add a few QoL useability functions to the interface ([#169](https://github.com/0xPolygonZero/zk_evm/pull/169)) +- Amortize `sha2` compression loop ([#231](https://github.com/0xPolygonZero/zk_evm/pull/231)) +- ci: add cargo audit job ([#236](https://github.com/0xPolygonZero/zk_evm/pull/236)) +- fix: Revert interpreter stack display ([#238](https://github.com/0xPolygonZero/zk_evm/pull/238)) +- Fix clippy `doc_lazy_continuation` ([#247](https://github.com/0xPolygonZero/zk_evm/pull/247)) +- perf: Improve `blake2` precompile ([#239](https://github.com/0xPolygonZero/zk_evm/pull/239)) +- fix: rustdoc and tests ([#255](https://github.com/0xPolygonZero/zk_evm/pull/255)) +- Native trace processing support ([#246](https://github.com/0xPolygonZero/zk_evm/pull/246)) +- Added `Clone` to a few error types in `mpt_trie` ([#259](https://github.com/0xPolygonZero/zk_evm/pull/259)) +- cleanup: remove outdated segment ([#262](https://github.com/0xPolygonZero/zk_evm/pull/262)) +- fix: add G2 subgroup check for `ECPAIRING` ([#268](https://github.com/0xPolygonZero/zk_evm/pull/268)) ## [0.3.1] - 2024-04-22 From eebbf3bf1417adce62c046032607af20b3dbacc7 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Thu, 6 Jun 2024 10:13:17 -0400 Subject: [PATCH 3/5] Bump versions --- evm_arithmetization/Cargo.toml | 4 ++-- mpt_trie/Cargo.toml | 2 +- proof_gen/Cargo.toml | 2 +- trace_decoder/Cargo.toml | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/evm_arithmetization/Cargo.toml b/evm_arithmetization/Cargo.toml index 43e75b902..f96c69f9e 100644 --- a/evm_arithmetization/Cargo.toml +++ b/evm_arithmetization/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "evm_arithmetization" description = "Implementation of STARKs for the Ethereum Virtual Machine" -version = "0.1.3" +version = "0.2.0" authors = ["Daniel Lubarov ", "William Borgeaud "] readme = "README.md" categories = ["cryptography"] @@ -41,7 +41,7 @@ tiny-keccak = "2.0.2" serde_json = { workspace = true } # Local dependencies -mpt_trie = { version = "0.2.1", path = "../mpt_trie" } +mpt_trie = { version = "0.3.0", path = "../mpt_trie" } [target.'cfg(not(target_env = "msvc"))'.dependencies] jemallocator = "0.5.0" diff --git a/mpt_trie/Cargo.toml b/mpt_trie/Cargo.toml index b4ef7ded2..180cfb3a6 100644 --- a/mpt_trie/Cargo.toml +++ b/mpt_trie/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "mpt_trie" description = "Types and utility functions for building/working with partial Ethereum tries." -version = "0.2.1" +version = "0.3.0" authors = ["Polygon Zero "] readme = "README.md" edition.workspace = true diff --git a/proof_gen/Cargo.toml b/proof_gen/Cargo.toml index b0f6b680e..7b45e6b9c 100644 --- a/proof_gen/Cargo.toml +++ b/proof_gen/Cargo.toml @@ -17,4 +17,4 @@ plonky2 = { workspace = true } serde = { workspace = true } # Local dependencies -evm_arithmetization = { version = "0.1.3", path = "../evm_arithmetization" } +evm_arithmetization = { version = "0.2.0", path = "../evm_arithmetization" } diff --git a/trace_decoder/Cargo.toml b/trace_decoder/Cargo.toml index 08ebdcf48..d155c44ad 100644 --- a/trace_decoder/Cargo.toml +++ b/trace_decoder/Cargo.toml @@ -2,7 +2,7 @@ name = "trace_decoder" description = "Processes trace payloads into Intermediate Representation (IR) format." authors = ["Polygon Zero "] -version = "0.3.1" +version = "0.4.0" edition.workspace = true license.workspace = true repository.workspace = true @@ -27,8 +27,8 @@ serde_with = "3.4.0" thiserror = { workspace = true } # Local dependencies -mpt_trie = { version = "0.2.1", path = "../mpt_trie" } -evm_arithmetization = { version = "0.1.3", path = "../evm_arithmetization" } +mpt_trie = { version = "0.3.0", path = "../mpt_trie" } +evm_arithmetization = { version = "0.2.0", path = "../evm_arithmetization" } [dev-dependencies] pretty_env_logger = "0.5.0" From 9444e4aefc8ac41eccfe1cb7fd17d6a3518d21c8 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Mon, 10 Jun 2024 16:59:11 -0400 Subject: [PATCH 4/5] fix: Add dummy values for all CTLs --- evm_arithmetization/src/all_stark.rs | 5 ++++- evm_arithmetization/src/fixed_recursive_verifier.rs | 8 ++++++-- evm_arithmetization/src/verifier.rs | 5 +++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/evm_arithmetization/src/all_stark.rs b/evm_arithmetization/src/all_stark.rs index 9ad3eea52..12843d678 100644 --- a/evm_arithmetization/src/all_stark.rs +++ b/evm_arithmetization/src/all_stark.rs @@ -123,9 +123,12 @@ pub(crate) fn all_cross_table_lookups() -> Vec> { ] } +/// Total number of CTLs returned by `all_cross_table_lookups()`. +pub(crate) const TOTAL_NUM_CTLS: usize = 7; + /// Position of the Memory CTL within the vector of CTLs returned by /// `all_cross_table_lookups()`. -pub(crate) const MEMORY_CTL_INDEX: usize = Table::Memory as usize; +pub(crate) const MEMORY_CTL_INDEX: usize = TOTAL_NUM_CTLS - 1; /// `CrossTableLookup` for `ArithmeticStark`, to connect it with the `Cpu` /// module. diff --git a/evm_arithmetization/src/fixed_recursive_verifier.rs b/evm_arithmetization/src/fixed_recursive_verifier.rs index 98ee74a35..bd0d9b667 100644 --- a/evm_arithmetization/src/fixed_recursive_verifier.rs +++ b/evm_arithmetization/src/fixed_recursive_verifier.rs @@ -35,7 +35,9 @@ use starky::lookup::{get_grand_product_challenge_set_target, GrandProductChallen use starky::proof::StarkProofWithMetadata; use starky::stark::Stark; -use crate::all_stark::{all_cross_table_lookups, AllStark, Table, MEMORY_CTL_INDEX, NUM_TABLES}; +use crate::all_stark::{ + all_cross_table_lookups, AllStark, Table, MEMORY_CTL_INDEX, NUM_TABLES, TOTAL_NUM_CTLS, +}; use crate::generation::GenerationInputs; use crate::get_challenges::observe_public_values_target; use crate::proof::{ @@ -587,7 +589,9 @@ where // Extra sums to add to the looked last value. // Only necessary for the Memory values. - let mut extra_looking_sums = HashMap::new(); + let mut extra_looking_sums = HashMap::from_iter( + (0..TOTAL_NUM_CTLS).map(|i| (i, vec![builder.zero(); stark_config.num_challenges])), + ); extra_looking_sums.insert( MEMORY_CTL_INDEX, diff --git a/evm_arithmetization/src/verifier.rs b/evm_arithmetization/src/verifier.rs index a8e927b94..2c98b516c 100644 --- a/evm_arithmetization/src/verifier.rs +++ b/evm_arithmetization/src/verifier.rs @@ -11,7 +11,7 @@ use starky::lookup::GrandProductChallenge; use starky::stark::Stark; use starky::verifier::verify_stark_proof_with_challenges; -use crate::all_stark::{AllStark, Table, MEMORY_CTL_INDEX, NUM_TABLES}; +use crate::all_stark::{AllStark, Table, MEMORY_CTL_INDEX, NUM_TABLES, TOTAL_NUM_CTLS}; use crate::cpu::kernel::aggregator::KERNEL; use crate::cpu::kernel::constants::global_metadata::GlobalMetadata; use crate::memory::segments::Segment; @@ -118,7 +118,8 @@ where // Extra sums to add to the looked last value. // Only necessary for the Memory values. - let mut extra_looking_sums = HashMap::new(); + let mut extra_looking_sums = + HashMap::from_iter((0..TOTAL_NUM_CTLS).map(|i| (i, vec![F::ZERO; config.num_challenges]))); extra_looking_sums.insert( MEMORY_CTL_INDEX, From e369a07b5c5047405acb5b14f0c180a0a5d10414 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Wed, 12 Jun 2024 10:59:15 -0400 Subject: [PATCH 5/5] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4e52c2d4..12320e703 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `Clone` to a few error types in `mpt_trie` ([#259](https://github.com/0xPolygonZero/zk_evm/pull/259)) - cleanup: remove outdated segment ([#262](https://github.com/0xPolygonZero/zk_evm/pull/262)) - fix: add G2 subgroup check for `ECPAIRING` ([#268](https://github.com/0xPolygonZero/zk_evm/pull/268)) +- add partial trie builder ([#258](https://github.com/0xPolygonZero/zk_evm/pull/258)) ## [0.3.1] - 2024-04-22