From 86daf7b08d1290d60aa9aee044cd9e6e80919ae4 Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Mon, 22 Jul 2024 15:02:46 +0530 Subject: [PATCH] keccak plonky2 implementation from polygon zk_evm --- .gitmodules | 3 + hash/plonky2/bench/Cargo.toml | 6 +- .../src/bench/keccak256/keccak_polygon.rs | 140 +++++++++--------- hash/plonky2/bench/src/main.rs | 12 +- hash/plonky2/external/zk_evm | 1 + 5 files changed, 92 insertions(+), 70 deletions(-) create mode 160000 hash/plonky2/external/zk_evm diff --git a/.gitmodules b/.gitmodules index e579aae..b4df4dd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "hash/risc0/external/risc0"] path = hash/risc0/external/risc0 url = https://github.com/risc0/risc0.git +[submodule "hash/plonky2/external/zk_evm"] + path = hash/plonky2/external/zk_evm + url = https://github.com/hashcloak/zk_evm.git diff --git a/hash/plonky2/bench/Cargo.toml b/hash/plonky2/bench/Cargo.toml index 9441eb3..ccadada 100644 --- a/hash/plonky2/bench/Cargo.toml +++ b/hash/plonky2/bench/Cargo.toml @@ -8,14 +8,14 @@ edition = "2021" [dependencies] # TODO: This can be later changed to original github -plonky2_u32 ={ git = "https://github.com/man2706kum/plonky2-u32.git"} +plonky2_u32 ={ git = "https://github.com/hashcloak/plonky2-u32.git"} plonky2 = "0.2.2" rand = "0.8.5" anyhow = "1.0.86" sha2 = "0.10" tiny-keccak={version="2.0.2", features=["keccak"]} hex="0.4.3" -evm_arithmetization ={ git = "https://github.com/0xPolygonZero/zk_evm.git", rev = "a5b92b2b2bcc7bbf8725190aaa0fba7007e51a6a"} -starky ={ git = "https://github.com/0xPolygonZero/plonky2.git", rev = "0e363e16a37a2eacd3349946bd071a460485ad26"} +evm_arithmetization ={ path = "../external/zk_evm/evm_arithmetization" } +starky = "0.4.0" env_logger = "0.11.3" log = "0.4.21" diff --git a/hash/plonky2/bench/src/bench/keccak256/keccak_polygon.rs b/hash/plonky2/bench/src/bench/keccak256/keccak_polygon.rs index a2c6f3b..445d0d8 100644 --- a/hash/plonky2/bench/src/bench/keccak256/keccak_polygon.rs +++ b/hash/plonky2/bench/src/bench/keccak256/keccak_polygon.rs @@ -1,93 +1,101 @@ use evm_arithmetization::keccak::keccak_stark::KeccakStark; use anyhow::Result; -use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV}; -use plonky2::field::types::PrimeField64; use plonky2::fri::oracle::PolynomialBatch; use plonky2::iop::challenger::Challenger; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; -use starky::config::StarkConfig; use starky::cross_table_lookup::{CtlData, CtlZData}; use starky::lookup::{GrandProductChallenge, GrandProductChallengeSet}; -use starky::stark_testing::{test_stark_circuit_constraints, test_stark_low_degree}; -use tiny_keccak::keccakf; use plonky2::field::polynomial::PolynomialValues; use plonky2::field::types::Field; use plonky2::timed; -use evm_arithmetization::testing_utils::init_logger; +// use evm_arithmetization::testing_utils::init_logger; use plonky2::util::timing::TimingTree; use evm_arithmetization::prover::prove_single_table; use starky::lookup::Filter; use starky::lookup::Column; -use rand::random; -const NUM_INPUTS: usize = 84; +use evm_arithmetization::StarkConfig; +// use starky::verifier::verify_stark_proof; +// use starky::prover::prove; +use env_logger::DEFAULT_FILTER_ENV; +use env_logger::Env; +use env_logger::try_init_from_env; -pub fn keccak_polygon_bench() -> Result<()> { +// use evm_arithmetization::prover::prove; +// use evm_arithmetization::generation::generate_traces; +// use evm_arithmetization::AllStark; - const NUM_PERMS: usize = 85; - const D: usize = 2; - type C = PoseidonGoldilocksConfig; - type F = >::F; - type S = KeccakStark; - let stark = S::default(); - let config = StarkConfig::standard_fast_config(); +const NUM_INPUTS: usize = 25; - init_logger(); +pub fn keccak_polygon_bench(num_perms: usize) -> Result<()> { - let input: Vec<([u64; NUM_INPUTS], usize)> = - (0..NUM_PERMS).map(|_| (rand::random(), 0)).collect(); + const D: usize = 2; + type C = PoseidonGoldilocksConfig; + type F = >::F; + type S = KeccakStark; + let stark = S::default(); + let config = StarkConfig::standard_fast_config(); - let mut timing = TimingTree::new("prove", log::Level::Debug); - let trace_poly_values = timed!( - timing, - "generate trace", - stark.generate_trace(input, 8, &mut timing) - ); + init_logger(); - let cloned_trace_poly_values = timed!(timing, "clone", trace_poly_values.clone()); + let input: Vec<([u64; NUM_INPUTS], usize)> = + (0..num_perms).map(|_| (rand::random(), 0)).collect(); - let trace_commitments = timed!( - timing, - "compute trace commitment", - PolynomialBatch::::from_values( - cloned_trace_poly_values, - config.fri_config.rate_bits, - false, - config.fri_config.cap_height, - &mut timing, - None, - ) - ); - let degree = 1 << trace_commitments.degree_log; + let mut timing = TimingTree::new("prove", log::Level::Debug); + let trace_poly_values = timed!( + timing, + "generate trace", + stark.generate_trace(input, 8, &mut timing) + ); - // Fake CTL data. - let ctl_z_data = CtlZData::new( - vec![PolynomialValues::zero(degree)], - PolynomialValues::zero(degree), - GrandProductChallenge { - beta: F::ZERO, - gamma: F::ZERO, - }, - vec![], - vec![Filter::new_simple(Column::constant(F::ZERO))], - ); - let ctl_data = CtlData { - zs_columns: vec![ctl_z_data.clone(); config.num_challenges], - }; + let cloned_trace_poly_values = timed!(timing, "clone", trace_poly_values.clone()); - prove_single_table( - &stark, - &config, - &trace_poly_values, - &trace_commitments, - &ctl_data, - &GrandProductChallengeSet { - challenges: vec![ctl_z_data.challenge; config.num_challenges], - }, - &mut Challenger::new(), + let trace_commitments = timed!( + timing, + "compute trace commitment", + PolynomialBatch::::from_values( + cloned_trace_poly_values, + config.fri_config.rate_bits, + false, + config.fri_config.cap_height, &mut timing, None, - )?; + ) + ); + let degree = 1 << trace_commitments.degree_log; - timing.print(); - Ok(()) + // Fake CTL data. + let ctl_z_data = CtlZData::new( + vec![PolynomialValues::zero(degree)], + PolynomialValues::zero(degree), + GrandProductChallenge { + beta: F::ZERO, + gamma: F::ZERO, + }, + vec![], + vec![Filter::new_simple(Column::constant(F::ZERO))], + ); + let ctl_data = CtlData { + zs_columns: vec![ctl_z_data.clone(); config.num_challenges], + }; + + prove_single_table( + &stark, + &config, + &trace_poly_values, + &trace_commitments, + &ctl_data, + &GrandProductChallengeSet { + challenges: vec![ctl_z_data.challenge; config.num_challenges], + }, + &mut Challenger::new(), + &mut timing, + None, + )?; + + timing.print(); + Ok(()) +} + +fn init_logger() { + let _ = try_init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "debug")); } \ No newline at end of file diff --git a/hash/plonky2/bench/src/main.rs b/hash/plonky2/bench/src/main.rs index ee9a612..a5853f8 100644 --- a/hash/plonky2/bench/src/main.rs +++ b/hash/plonky2/bench/src/main.rs @@ -1,3 +1,4 @@ + use std::process; mod bench{ pub mod poseidon; @@ -13,7 +14,7 @@ mod bench{ pub mod keccak256{ pub mod keccak; - // pub mod keccak_polygon; + pub mod keccak_polygon; } } @@ -26,6 +27,8 @@ mod arithmetic { use bench::poseidon::poseidon_bench; use bench::keccak256::keccak::keccak_bench; use bench::sha256::sha::sha256_bench; +use bench::keccak256::keccak_polygon::keccak_polygon_bench; + fn main() { let args: Vec = std::env::args().collect(); @@ -53,6 +56,13 @@ fn main() { let _ = keccak_bench(size); } + "keccak-polygon" => { + println!("Running keccak of plolygon zk_evm: "); + eprintln!("number of permutation: {:?}", size); + let _ = keccak_polygon_bench(size); + } + + "sha256" => { println!("Running sha256: "); let _ = sha256_bench(size); diff --git a/hash/plonky2/external/zk_evm b/hash/plonky2/external/zk_evm new file mode 160000 index 0000000..29f8620 --- /dev/null +++ b/hash/plonky2/external/zk_evm @@ -0,0 +1 @@ +Subproject commit 29f8620338f2b3bb8be62def5666a2d9d7ba5331