Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Add exp circuit bench (#980)
Browse files Browse the repository at this point in the history
* Add exp circuit bench

* fmt

* fmt

* bugfix

Co-authored-by: Chih Cheng Liang <chihchengliang@gmail.com>
  • Loading branch information
SuccinctPaul and ChihChengLiang authored Dec 15, 2022
1 parent cc1e0d6 commit 9400c62
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ pi_bench: ## Run Public Input Circuit benchmarks
copy_bench: ## Run Copy Circuit benchmarks
@cargo test --profile bench bench_copy_circuit_prover -p circuit-benchmarks --features benches -- --nocapture

tx_bench: ## Run Tx Circuit benchmarks
@cargo test --profile bench bench_tx_circuit_prover -p circuit-benchmarks --features benches -- --nocapture

exp_bench: ## Run Exp Circuit benchmarks
@cargo test --profile bench bench_exp_circuit_prover -p circuit-benchmarks --features benches -- --nocapture

circuit_benches: evm_bench state_bench ## Run All Circuit benchmarks


Expand Down
135 changes: 135 additions & 0 deletions circuit-benchmarks/src/exp_circuit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//! Exp circuit benchmarks

#[cfg(test)]
mod tests {
use ark_std::{end_timer, start_timer};
use bus_mapping::circuit_input_builder::CircuitsParams;
use bus_mapping::mock::BlockData;
use env_logger::Env;
use eth_types::geth_types::GethData;
use eth_types::{bytecode, Word};
use halo2_proofs::plonk::{create_proof, keygen_pk, keygen_vk, verify_proof};
use halo2_proofs::poly::kzg::commitment::{KZGCommitmentScheme, ParamsKZG, ParamsVerifierKZG};
use halo2_proofs::poly::kzg::multiopen::{ProverSHPLONK, VerifierSHPLONK};
use halo2_proofs::poly::kzg::strategy::SingleStrategy;
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
poly::commitment::ParamsProver,
transcript::{
Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer,
},
};
use mock::test_ctx::helpers::*;
use mock::test_ctx::TestContext;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use zkevm_circuits::evm_circuit::witness::{block_convert, Block};
use zkevm_circuits::exp_circuit::ExpCircuit;

use crate::bench_params::DEGREE;

#[cfg_attr(not(feature = "benches"), ignore)]
#[test]
fn bench_exp_circuit_prover() {
env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();

// Initialize the circuit

let base = Word::from(132);
let exponent = Word::from(27);
let block = generate_full_events_block(DEGREE, base, exponent);
let circuit = ExpCircuit::<Fr>::new(block);

// Initialize the polynomial commitment parameters
let mut rng = XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
0xbc, 0xe5,
]);

// Bench setup generation
let setup_message = format!("Setup generation with degree = {}", DEGREE);
let start1 = start_timer!(|| setup_message);
let general_params = ParamsKZG::<Bn256>::setup(DEGREE as u32, &mut rng);
let verifier_params: ParamsVerifierKZG<Bn256> = general_params.verifier_params().clone();
end_timer!(start1);

// Initialize the proving key
let vk = keygen_vk(&general_params, &circuit).expect("keygen_vk should not fail");
let pk = keygen_pk(&general_params, vk, &circuit).expect("keygen_pk should not fail");
// Create a proof
let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);

// Bench proof generation time
let proof_message = format!("Exp Circuit Proof generation with degree = {}", DEGREE);
let start2 = start_timer!(|| proof_message);
create_proof::<
KZGCommitmentScheme<Bn256>,
ProverSHPLONK<'_, Bn256>,
Challenge255<G1Affine>,
XorShiftRng,
Blake2bWrite<Vec<u8>, G1Affine, Challenge255<G1Affine>>,
ExpCircuit<Fr>,
>(
&general_params,
&pk,
&[circuit],
&[&[]],
rng,
&mut transcript,
)
.expect("proof generation should not fail");
let proof = transcript.finalize();
end_timer!(start2);

// Bench verification time
let start3 = start_timer!(|| "Exp Circuit Proof verification");
let mut verifier_transcript = Blake2bRead::<_, G1Affine, Challenge255<_>>::init(&proof[..]);
let strategy = SingleStrategy::new(&general_params);

verify_proof::<
KZGCommitmentScheme<Bn256>,
VerifierSHPLONK<'_, Bn256>,
Challenge255<G1Affine>,
Blake2bRead<&[u8], G1Affine, Challenge255<G1Affine>>,
SingleStrategy<'_, Bn256>,
>(
&verifier_params,
pk.get_vk(),
strategy,
&[&[]],
&mut verifier_transcript,
)
.expect("failed to verify bench circuit");
end_timer!(start3);
}

fn generate_full_events_block(degree: usize, base: Word, exponent: Word) -> Block<Fr> {
let code = bytecode! {
PUSH32(exponent)
PUSH32(base)
EXP
STOP
};

let test_ctx = TestContext::<2, 1>::new(
None,
account_0_code_account_1_no_code(code),
tx_from_1_to_0,
|block, _txs| block.number(0xcafeu64),
)
.unwrap();
let block: GethData = test_ctx.into();
let mut builder = BlockData::new_from_geth_data_with_params(
block.clone(),
CircuitsParams {
max_rws: 1 << (degree - 1),
..Default::default()
},
)
.new_circuit_input_builder();
builder
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();
block_convert(&builder.block, &builder.code_db).unwrap()
}
}
4 changes: 4 additions & 0 deletions circuit-benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ pub mod pi_circuit;
#[cfg(test)]
#[cfg(feature = "benches")]
pub mod copy_circuit;

#[cfg(test)]
#[cfg(feature = "benches")]
pub mod exp_circuit;
3 changes: 2 additions & 1 deletion zkevm-circuits/src/exp_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ pub struct ExpCircuit<F> {
}

impl<F: Field> ExpCircuit<F> {
pub(crate) fn new(block: Block<F>) -> Self {
/// Return a new ExpCircuit
pub fn new(block: Block<F>) -> Self {
Self { block: Some(block) }
}
}
Expand Down

0 comments on commit 9400c62

Please sign in to comment.