Skip to content

Commit

Permalink
[Refactor] Verifier for compression circuit (#331)
Browse files Browse the repository at this point in the history
Refactor verifier
  • Loading branch information
noel2004 authored Jul 22, 2024
1 parent 6366e8a commit 6c90372
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
1 change: 1 addition & 0 deletions integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod capacity_checker;
pub mod evm;
pub mod prove;
pub mod test_util;
mod verifier;
21 changes: 10 additions & 11 deletions integration/src/prove.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::test_util::PARAMS_DIR;
use crate::{test_util::PARAMS_DIR, verifier::*};
use prover::{
aggregator::{Prover as BatchProver, Verifier as BatchVerifier},
zkevm::{Prover as ChunkProver, Verifier as ChunkVerifier},
BatchProof, BatchProvingTask, BundleProvingTask, ChunkProvingTask,
aggregator::Prover as BatchProver, zkevm::Prover as ChunkProver, BatchProof, BatchProvingTask,
BundleProvingTask, ChunkProvingTask,
};
use std::{env, time::Instant};

Expand Down Expand Up @@ -36,8 +35,8 @@ pub fn prove_and_verify_chunk(

// output_dir is used to load chunk vk
env::set_var("CHUNK_VK_FILENAME", "vk_chunk_0.vkey");
let verifier = ChunkVerifier::from_dirs(params_path, output_dir);
assert!(verifier.verify_chunk_proof(chunk_proof));
let verifier = new_chunk_verifier(params_path, output_dir);
assert!(verifier.verify_snark(chunk_proof.to_snark()));
log::info!("Verified chunk proof");
}

Expand All @@ -54,10 +53,10 @@ pub fn prove_and_verify_batch(
.unwrap();

env::set_var("BATCH_VK_FILENAME", "vk_batch_agg.vkey");
let verifier = BatchVerifier::from_dirs(PARAMS_DIR, output_dir);
let verifier = new_batch_verifier(PARAMS_DIR, output_dir);
log::info!("Constructed aggregator verifier");

assert!(verifier.verify_batch_proof(&batch_proof));
assert!(verifier.verify_snark((&batch_proof).into()));
log::info!("Verified batch proof");

log::info!("Prove batch END: chunk_num = {chunk_num}");
Expand All @@ -76,11 +75,11 @@ pub fn prove_and_verify_bundle(
.gen_bundle_proof(bundle, None, Some(output_dir))
.unwrap();

env::set_var("BATCH_VK_FILENAME", "vk_batch_agg.vkey");
let verifier = BatchVerifier::from_dirs(PARAMS_DIR, output_dir);
env::set_var("BATCH_VK_FILENAME", "vk_bundle_recursion.vkey");
let verifier = EVMVerifier::from_dirs(output_dir);
log::info!("Constructed bundle verifier");

assert!(verifier.verify_bundle_proof(bundle_proof));
assert!(verifier.verify_evm_proof(bundle_proof.calldata()));
log::info!("Verifier bundle proof");

log::info!("Prove bundle END");
Expand Down
34 changes: 34 additions & 0 deletions integration/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use prover::{common::Verifier, config, consts, io::force_to_read, CompressionCircuit};
use snark_verifier_sdk::verify_evm_calldata;
use std::env;

type SnarkVerifier = Verifier<CompressionCircuit>;

pub fn new_chunk_verifier(params_dir: &str, assets_dir: &str) -> SnarkVerifier {
let raw_vk = force_to_read(assets_dir, &consts::chunk_vk_filename());
env::set_var("COMPRESSION_CONFIG", &*config::LAYER2_CONFIG_PATH);
SnarkVerifier::from_params_dir(params_dir, *config::LAYER2_DEGREE, &raw_vk)
}

pub fn new_batch_verifier(params_dir: &str, assets_dir: &str) -> SnarkVerifier {
let raw_vk = force_to_read(assets_dir, &consts::batch_vk_filename());
env::set_var("COMPRESSION_CONFIG", &*config::LAYER4_CONFIG_PATH);
SnarkVerifier::from_params_dir(params_dir, *config::LAYER4_DEGREE, &raw_vk)
}

#[derive(Debug)]
pub struct EVMVerifier(Vec<u8>);

impl EVMVerifier {
pub fn new(deployment_code: Vec<u8>) -> Self {
Self(deployment_code)
}

pub fn from_dirs(assets_dir: &str) -> Self {
Self::new(force_to_read(assets_dir, &consts::DEPLOYMENT_CODE_FILENAME))
}

pub fn verify_evm_proof(&self, call_data: Vec<u8>) -> bool {
verify_evm_calldata(self.0.clone(), call_data)
}
}

0 comments on commit 6c90372

Please sign in to comment.