This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 856
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add exp circuit bench * fmt * fmt * bugfix Co-authored-by: Chih Cheng Liang <chihchengliang@gmail.com>
- Loading branch information
1 parent
cc1e0d6
commit 9400c62
Showing
4 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters