Skip to content

Commit

Permalink
move more modules into prover::utils
Browse files Browse the repository at this point in the history
  • Loading branch information
roynalnaruto committed Oct 25, 2024
1 parent 65b7c7c commit f8fdd32
Show file tree
Hide file tree
Showing 22 changed files with 95 additions and 83 deletions.
2 changes: 1 addition & 1 deletion prover/src/aggregator/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use crate::{
BATCH_KECCAK_ROW, BATCH_VK_FILENAME, BUNDLE_VK_FILENAME, FD_HALO2_CHUNK_PROTOCOL,
FD_SP1_CHUNK_PROTOCOL,
},
io::{force_to_read, try_to_read},
proof::BundleProof,
types::BundleProvingTask,
utils::{force_to_read, try_to_read},
BatchProof, BatchProvingTask, ChunkKind, ChunkProof, ParamsMap,
};

Expand Down
3 changes: 1 addition & 2 deletions prover/src/aggregator/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use crate::{
common,
config::{LAYER4_CONFIG_PATH, LAYER4_DEGREE},
consts::{batch_vk_filename, DEPLOYMENT_CODE_FILENAME},
evm::deploy_and_call,
io::{force_to_read, try_to_read},
proof::BundleProof,
utils::{deploy_and_call, force_to_read, try_to_read},
ParamsMap,
};

Expand Down
5 changes: 4 additions & 1 deletion prover/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
mod prover;
pub use prover::Prover;

mod verifier;
pub use verifier::Verifier;

pub use self::{prover::Prover, verifier::Verifier};
// Re-export from the aggregator crate.
pub use aggregator::{ChunkInfo, CompressionCircuit};
17 changes: 11 additions & 6 deletions prover/src/common/prover.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use crate::utils::{load_params, param_path_for_degree};
use std::collections::{BTreeMap, BTreeSet, HashMap};

use halo2_proofs::{
halo2curves::bn256::{Bn256, G1Affine},
halo2curves::bn256::G1Affine,
plonk::ProvingKey,
poly::{commitment::Params, kzg::commitment::ParamsKZG},
};
use std::collections::{BTreeMap, BTreeSet, HashMap};

use crate::{
utils::{load_params, param_path_for_degree},
ParamsMap,
};

mod aggregation;
mod chunk;
Expand All @@ -18,20 +23,20 @@ mod utils;
#[derive(Debug)]
pub struct Prover<'params> {
// degree -> params (use BTreeMap to find proper degree for params downsize)
pub params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>,
pub params_map: &'params ParamsMap,
// Cached id -> pk
pk_map: HashMap<String, ProvingKey<G1Affine>>,
}

impl<'params> Prover<'params> {
pub fn from_params_map(params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>) -> Self {
pub fn from_params_map(params_map: &'params ParamsMap) -> Self {
Self {
params_map,
pk_map: HashMap::new(),
}
}

pub fn load_params_map(params_dir: &str, degrees: &[u32]) -> BTreeMap<u32, ParamsKZG<Bn256>> {
pub fn load_params_map(params_dir: &str, degrees: &[u32]) -> ParamsMap {
let degrees = BTreeSet::from_iter(degrees);
let max_degree = **degrees.last().unwrap();

Expand Down
17 changes: 9 additions & 8 deletions prover/src/common/prover/aggregation.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use super::Prover;
use crate::{
config::layer_config_path,
io::{load_snark, write_snark},
utils::gen_rng,
};
use std::env;

use aggregator::{BatchCircuit, BatchHash};
use anyhow::{anyhow, Result};
use halo2_proofs::halo2curves::bn256::G1Affine;
use rand::Rng;
use snark_verifier_sdk::Snark;
use std::env;

impl<'params> Prover<'params> {
use crate::{
config::layer_config_path,
utils::gen_rng,
utils::{load_snark, write_snark},
};

impl<'params> super::Prover<'params> {
pub fn gen_agg_snark<const N_SNARKS: usize>(
&mut self,
id: &str,
Expand Down
17 changes: 9 additions & 8 deletions prover/src/common/prover/compression.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use super::Prover;
use crate::{
config::layer_config_path,
io::{load_snark, write_snark},
utils::gen_rng,
};
use std::env;

use aggregator::CompressionCircuit;
use anyhow::{anyhow, Result};
use rand::Rng;
use snark_verifier_sdk::Snark;
use std::env;

impl<'params> Prover<'params> {
use crate::{
config::layer_config_path,
utils::gen_rng,
utils::{load_snark, write_snark},
};

impl<'params> super::Prover<'params> {
pub fn gen_comp_snark(
&mut self,
id: &str,
Expand Down
16 changes: 8 additions & 8 deletions prover/src/common/prover/evm.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use super::Prover;
use crate::{
config::layer_config_path,
utils::{gen_rng, read_env_var},
EvmProof,
};
use aggregator::CompressionCircuit;
use anyhow::{anyhow, Result};
use halo2_proofs::halo2curves::bn256::Fr;
use rand::Rng;
use snark_verifier_sdk::{gen_evm_proof_shplonk, CircuitExt, Snark};
use std::env;

impl<'params> Prover<'params> {
use crate::{
config::layer_config_path,
utils::{gen_evm_verifier, gen_rng, read_env_var},
EvmProof,
};

impl<'params> super::Prover<'params> {
pub fn load_or_gen_comp_evm_proof(
&mut self,
name: &str,
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<'params> Prover<'params> {
let evm_proof = EvmProof::new(proof, &instances, num_instance, Some(pk))?;

if read_env_var("SCROLL_PROVER_DUMP_YUL", false) {
crate::evm::gen_evm_verifier::<C>(params, pk.get_vk(), &evm_proof, output_dir);
gen_evm_verifier::<C>(params, pk.get_vk(), &evm_proof, output_dir);
}

Ok(evm_proof)
Expand Down
14 changes: 7 additions & 7 deletions prover/src/common/prover/inner.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::Prover;
use anyhow::Result;
use rand::Rng;
use snark_verifier_sdk::{gen_snark_shplonk, Snark};
use zkevm_circuits::evm_circuit::witness::Block;

use crate::{
config::INNER_DEGREE,
io::{load_snark, write_snark},
utils::{gen_rng, metric_of_witness_block},
utils::{load_snark, write_snark},
zkevm::circuit::{SuperCircuit, TargetCircuit},
};
use anyhow::Result;
use rand::Rng;
use snark_verifier_sdk::{gen_snark_shplonk, Snark};
use zkevm_circuits::evm_circuit::witness::Block;

impl<'params> Prover<'params> {
impl<'params> super::Prover<'params> {
pub fn gen_inner_snark<C: TargetCircuit>(
&mut self,
id: &str,
Expand Down
6 changes: 2 additions & 4 deletions prover/src/common/prover/recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ use snark_verifier_sdk::{gen_snark_shplonk, Snark};
use crate::{
aggregator::RecursionTask,
config::layer_config_path,
io::{load_snark, write_snark},
utils::gen_rng,
utils::{load_snark, write_snark},
};

use super::Prover;

impl<'params> Prover<'params> {
impl<'params> super::Prover<'params> {
pub fn gen_recursion_snark(
&mut self,
id: &str,
Expand Down
6 changes: 3 additions & 3 deletions prover/src/common/prover/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use super::Prover;
use crate::io::serialize_vk;
use anyhow::Result;
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
Expand All @@ -9,7 +7,9 @@ use halo2_proofs::{
use rand::Rng;
use snark_verifier_sdk::{gen_snark_shplonk, CircuitExt, Snark};

impl<'params> Prover<'params> {
use crate::utils::serialize_vk;

impl<'params> super::Prover<'params> {
pub fn gen_snark<C: CircuitExt<Fr>>(
&mut self,
id: &str,
Expand Down
3 changes: 2 additions & 1 deletion prover/src/common/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::io::deserialize_vk;
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::VerifyingKey,
Expand All @@ -7,6 +6,8 @@ use halo2_proofs::{
use snark_verifier_sdk::{verify_snark_shplonk, CircuitExt, Snark};
use std::marker::PhantomData;

use crate::utils::deserialize_vk;

mod evm;
mod utils;

Expand Down
8 changes: 4 additions & 4 deletions prover/src/common/verifier/evm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::Verifier;
use crate::EvmProof;
use halo2_proofs::halo2curves::bn256::Fr;
use snark_verifier_sdk::CircuitExt;

impl<'params, C: CircuitExt<Fr>> Verifier<'params, C> {
use crate::{utils::gen_evm_verifier, EvmProof};

impl<'params, C: CircuitExt<Fr>> super::Verifier<'params, C> {
pub fn gen_evm_verifier(&self, evm_proof: &EvmProof, output_dir: Option<&str>) {
crate::evm::gen_evm_verifier::<C>(self.params, &self.vk, evm_proof, output_dir)
gen_evm_verifier::<C>(self.params, &self.vk, evm_proof, output_dir)
}
}
2 changes: 1 addition & 1 deletion prover/src/inner/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::marker::PhantomData;
use crate::{
common,
config::INNER_DEGREE,
io::serialize_vk,
utils::gen_rng,
utils::serialize_vk,
zkevm::circuit::{chunk_trace_to_witness_block, TargetCircuit},
Proof,
};
Expand Down
3 changes: 2 additions & 1 deletion prover/src/inner/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::BTreeMap;

use crate::{common, config::INNER_DEGREE, io::deserialize_vk, zkevm::circuit::TargetCircuit};
use halo2_proofs::{halo2curves::bn256::Bn256, plonk::keygen_vk, poly::kzg::commitment::ParamsKZG};
use snark_verifier_sdk::Snark;

use crate::{common, config::INNER_DEGREE, utils::deserialize_vk, zkevm::circuit::TargetCircuit};

#[derive(Debug)]
pub struct Verifier<'params, C: TargetCircuit> {
// Make it public for testing with inner functions (unnecessary for FFI).
Expand Down
6 changes: 0 additions & 6 deletions prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,9 @@ pub use config::*;
mod consts;
pub use consts::*;

mod evm;
pub use evm::deploy_and_call;

mod inner;
pub use inner::*;

mod io;
pub use io::*;

mod proof;
pub use proof::{BatchProof, BundleProof, ChunkKind, ChunkProof, EvmProof, Proof};

Expand Down
16 changes: 9 additions & 7 deletions prover/src/proof/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
io::{deserialize_fr, deserialize_vk, serialize_fr, serialize_vk, write_file},
utils::short_git_version,
};
use std::{fs::File, path::PathBuf};

use anyhow::Result;
use eth_types::base64;
use halo2_proofs::{
Expand All @@ -10,7 +8,11 @@ use halo2_proofs::{
};
use serde_derive::{Deserialize, Serialize};
use snark_verifier_sdk::Snark;
use std::{fs::File, path::PathBuf};

use crate::utils::{
deploy_and_call, deserialize_fr, deserialize_vk, serialize_fr, serialize_vk, short_git_version,
write_file,
};

mod batch;
mod bundle;
Expand Down Expand Up @@ -74,7 +76,7 @@ impl Proof {
let instances = self.instances();
let proof = self.proof().to_vec();
let calldata = snark_verifier::loader::evm::encode_calldata(&instances, &proof);
crate::evm::deploy_and_call(deployment_code, calldata).is_ok()
deploy_and_call(deployment_code, calldata).is_ok()
}

pub fn instances(&self) -> Vec<Vec<Fr>> {
Expand Down Expand Up @@ -118,7 +120,7 @@ pub fn dump_vk(dir: &str, filename: &str, raw_vk: &[u8]) {

pub fn from_json_file<'de, P: serde::Deserialize<'de>>(dir: &str, filename: &str) -> Result<P> {
let file_path = dump_proof_path(dir, filename);
crate::io::from_json_file(&file_path)
crate::utils::from_json_file(&file_path)
}

fn dump_proof_path(dir: &str, filename: &str) -> String {
Expand Down
3 changes: 1 addition & 2 deletions prover/src/test/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use crate::{
aggregator::{Prover, Verifier},
config::{LayerId, BATCH_PROVER_DEGREES},
consts::DEPLOYMENT_CODE_FILENAME,
io::force_to_read,
types::BundleProvingTask,
utils::read_env_var,
utils::{force_to_read, read_env_var},
BatchProvingTask, ParamsMap,
};

Expand Down
13 changes: 7 additions & 6 deletions prover/src/evm.rs → prover/src/utils/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ use revm::{
use snark_verifier::pcs::kzg::{Bdfg21, Kzg};
use snark_verifier_sdk::CircuitExt;

use crate::{io::write_file, EvmProof};
use crate::{utils::write_file, EvmProof};

/// Dump YUL and binary bytecode(use `solc` in PATH) to output_dir.
/// Panic if error encountered.
///
/// Panics if the verifier contract cannot successfully verify the [`EvmProof`].
pub fn gen_evm_verifier<C: CircuitExt<Fr>>(
params: &ParamsKZG<Bn256>,
vk: &VerifyingKey<G1Affine>,
Expand All @@ -36,18 +37,18 @@ pub fn gen_evm_verifier<C: CircuitExt<Fr>>(
yul_file_path.as_deref(),
);

// Write the contract binary if an output directory was specified.
if let Some(dir) = output_dir {
// Dump bytecode.
let mut dir = PathBuf::from_str(dir).unwrap();
write_file(&mut dir, "evm_verifier.bin", &deployment_code);
}

let success = evm_proof.proof.evm_verify(deployment_code);
assert!(success);
assert!(evm_proof.proof.evm_verify(deployment_code));
}

/// Deploy contract and then call with calldata.
/// Returns gas_used of call to deployed contract if both transactions are successful.
///
/// Returns the gas used to verify proof.
pub fn deploy_and_call(deployment_code: Vec<u8>, calldata: Vec<u8>) -> Result<u64, String> {
let mut evm = EVM {
env: Default::default(),
Expand Down
11 changes: 6 additions & 5 deletions prover/src/io.rs → prover/src/utils/io.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::{
fs::File,
io::{Cursor, Read, Write},
path::{Path, PathBuf},
};

use anyhow;
use halo2_proofs::{
halo2curves::bn256::{Fr, G1Affine},
Expand All @@ -6,11 +12,6 @@ use halo2_proofs::{
};
use snark_verifier::util::arithmetic::PrimeField;
use snark_verifier_sdk::Snark;
use std::{
fs::File,
io::{Cursor, Read, Write},
path::{Path, PathBuf},
};

pub fn from_json_file<'de, P: serde::Deserialize<'de>>(file_path: &str) -> anyhow::Result<P> {
if !Path::new(&file_path).exists() {
Expand Down
Loading

0 comments on commit f8fdd32

Please sign in to comment.