keccak plonky2 implementation from polygon zk_evm

This commit is contained in:
Manish Kumar 2024-07-22 15:02:46 +05:30
parent 658faf232e
commit 86daf7b08d
5 changed files with 92 additions and 70 deletions

3
.gitmodules vendored
View File

@ -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

View File

@ -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"

View File

@ -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 = <C as GenericConfig<D>>::F;
type S = KeccakStark<F, D>;
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 = <C as GenericConfig<D>>::F;
type S = KeccakStark<F, D>;
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::<F, C, D>::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::<F, C, D>::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"));
}

View File

@ -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<String> = 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);

1
hash/plonky2/external/zk_evm vendored Submodule

@ -0,0 +1 @@
Subproject commit 29f8620338f2b3bb8be62def5666a2d9d7ba5331