Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(v0.12.2) Move params out of prover/verifier #1404

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions prover/src/aggregator/prover.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::{env, iter::repeat};
use std::{collections::BTreeMap, env, iter::repeat};

use aggregator::{BatchHash, BatchHeader, ChunkInfo, MAX_AGG_SNARKS};
use anyhow::{bail, Result};
use eth_types::H256;
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use sha2::{Digest, Sha256};
use snark_verifier_sdk::Snark;

use crate::{
common,
config::{LayerId, AGG_DEGREES},
config::LayerId,
consts::{BATCH_KECCAK_ROW, BATCH_VK_FILENAME, BUNDLE_VK_FILENAME, CHUNK_PROTOCOL_FILENAME},
io::{force_to_read, try_to_read},
proof::BundleProof,
Expand All @@ -17,20 +18,23 @@ use crate::{
};

#[derive(Debug)]
pub struct Prover {
pub struct Prover<'params> {
// Make it public for testing with inner functions (unnecessary for FFI).
pub prover_impl: common::Prover,
pub prover_impl: common::Prover<'params>,
pub chunk_protocol: Vec<u8>,
raw_vk_batch: Option<Vec<u8>>,
raw_vk_bundle: Option<Vec<u8>>,
}

impl Prover {
pub fn from_dirs(params_dir: &str, assets_dir: &str) -> Self {
impl<'params> Prover<'params> {
pub fn from_params_and_assets(
params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>,
assets_dir: &str,
) -> Self {
log::debug!("set env KECCAK_ROWS={}", BATCH_KECCAK_ROW.to_string());
env::set_var("KECCAK_ROWS", BATCH_KECCAK_ROW.to_string());

let prover_impl = common::Prover::from_params_dir(params_dir, &AGG_DEGREES);
let prover_impl = common::Prover::from_params_map(params_map);
let chunk_protocol = force_to_read(assets_dir, &CHUNK_PROTOCOL_FILENAME);

let raw_vk_batch = try_to_read(assets_dir, &BATCH_VK_FILENAME);
Expand Down
18 changes: 11 additions & 7 deletions prover/src/aggregator/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ use halo2_proofs::{
};
use snark_verifier_sdk::verify_evm_calldata;
use snark_verifier_sdk::Snark;
use std::env;
use std::{collections::BTreeMap, env};

#[derive(Debug)]
pub struct Verifier {
pub struct Verifier<'params> {
// Make it public for testing with inner functions (unnecessary for FFI).
pub inner: common::Verifier<CompressionCircuit>,
pub inner: common::Verifier<'params, CompressionCircuit>,
deployment_code: Option<Vec<u8>>,
}

impl Verifier {
impl<'params> Verifier<'params> {
pub fn new(
params: ParamsKZG<Bn256>,
params: &'params ParamsKZG<Bn256>,
vk: VerifyingKey<G1Affine>,
deployment_code: Vec<u8>,
) -> Self {
Expand All @@ -36,12 +36,16 @@ impl Verifier {
}
}

pub fn from_dirs(params_dir: &str, assets_dir: &str) -> Self {
pub fn from_params_and_assets(
params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>,
assets_dir: &str,
) -> Self {
let raw_vk = force_to_read(assets_dir, &batch_vk_filename());
let deployment_code = try_to_read(assets_dir, &DEPLOYMENT_CODE_FILENAME);

env::set_var("COMPRESSION_CONFIG", &*LAYER4_CONFIG_PATH);
let inner = common::Verifier::from_params_dir(params_dir, *LAYER4_DEGREE, &raw_vk);
let params = params_map.get(&*LAYER4_DEGREE).expect("should be loaded");
let inner = common::Verifier::from_params(params, &raw_vk);

Self {
inner,
Expand Down
15 changes: 6 additions & 9 deletions prover/src/common/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ mod recursion;
mod utils;

#[derive(Debug)]
pub struct Prover {
pub struct Prover<'params> {
// degree -> params (use BTreeMap to find proper degree for params downsize)
params_map: BTreeMap<u32, ParamsKZG<Bn256>>,
pub params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>,
// Cached id -> pk
pk_map: HashMap<String, ProvingKey<G1Affine>>,
}

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

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

Expand Down Expand Up @@ -63,9 +63,6 @@ impl Prover {
params_map.insert(*d, params);
}

Self {
params_map,
pk_map: HashMap::new(),
}
params_map
}
}
2 changes: 1 addition & 1 deletion prover/src/common/prover/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::Rng;
use snark_verifier_sdk::Snark;
use std::env;

impl Prover {
impl<'params> Prover<'params> {
pub fn gen_agg_snark<const N_SNARKS: usize>(
&mut self,
id: &str,
Expand Down
2 changes: 1 addition & 1 deletion prover/src/common/prover/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{anyhow, Result};
use snark_verifier_sdk::Snark;
use zkevm_circuits::evm_circuit::witness::Block;

impl Prover {
impl<'params> Prover<'params> {
pub fn load_or_gen_final_chunk_snark(
&mut self,
name: &str,
Expand Down
2 changes: 1 addition & 1 deletion prover/src/common/prover/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::Rng;
use snark_verifier_sdk::Snark;
use std::env;

impl Prover {
impl<'params> Prover<'params> {
pub fn gen_comp_snark(
&mut self,
id: &str,
Expand Down
2 changes: 1 addition & 1 deletion prover/src/common/prover/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rand::Rng;
use snark_verifier_sdk::{gen_evm_proof_shplonk, CircuitExt, Snark};
use std::env;

impl Prover {
impl<'params> Prover<'params> {
pub fn load_or_gen_comp_evm_proof(
&mut self,
name: &str,
Expand Down
2 changes: 1 addition & 1 deletion prover/src/common/prover/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::Rng;
use snark_verifier_sdk::{gen_snark_shplonk, Snark};
use zkevm_circuits::evm_circuit::witness::Block;

impl Prover {
impl<'params> Prover<'params> {
pub fn gen_inner_snark<C: TargetCircuit>(
&mut self,
id: &str,
Expand Down
2 changes: 1 addition & 1 deletion prover/src/common/prover/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::LazyLock;

pub static MOCK_PROVE: LazyLock<bool> = LazyLock::new(|| read_env_var("MOCK_PROVE", false));

impl Prover {
impl<'params> Prover<'params> {
pub fn assert_if_mock_prover<C: CircuitExt<Fr>>(id: &str, degree: u32, circuit: &C) {
if !*MOCK_PROVE {
return;
Expand Down
2 changes: 1 addition & 1 deletion prover/src/common/prover/recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{

use super::Prover;

impl Prover {
impl<'params> Prover<'params> {
pub fn gen_recursion_snark(
&mut self,
id: &str,
Expand Down
24 changes: 3 additions & 21 deletions prover/src/common/prover/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use anyhow::Result;
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::{keygen_pk2, Circuit, ProvingKey},
poly::{commitment::Params, kzg::commitment::ParamsKZG},
poly::kzg::commitment::ParamsKZG,
};
use rand::Rng;
use snark_verifier_sdk::{gen_snark_shplonk, CircuitExt, Snark};

impl Prover {
impl<'params> Prover<'params> {
pub fn gen_snark<C: CircuitExt<Fr>>(
&mut self,
id: &str,
Expand All @@ -32,25 +32,7 @@ impl Prover {
Ok(snark)
}

pub fn params(&mut self, degree: u32) -> &ParamsKZG<Bn256> {
if self.params_map.contains_key(&degree) {
return &self.params_map[&degree];
}

log::warn!("Optimization: download params{degree} to params dir");

log::info!("Before generate params of {degree}");
let mut new_params = self
.params_map
.range(degree..)
.next()
.unwrap_or_else(|| panic!("Must have params of degree-{degree}"))
.1
.clone();
new_params.downsize(degree);
log::info!("After generate params of {degree}");

self.params_map.insert(degree, new_params);
pub fn params(&self, degree: u32) -> &ParamsKZG<Bn256> {
roynalnaruto marked this conversation as resolved.
Show resolved Hide resolved
&self.params_map[&degree]
}

Expand Down
19 changes: 6 additions & 13 deletions prover/src/common/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{io::deserialize_vk, utils::load_params};
use crate::io::deserialize_vk;
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::VerifyingKey,
Expand All @@ -11,33 +11,26 @@ mod evm;
mod utils;

#[derive(Debug)]
pub struct Verifier<C: CircuitExt<Fr>> {
params: ParamsKZG<Bn256>,
pub struct Verifier<'params, C: CircuitExt<Fr>> {
params: &'params ParamsKZG<Bn256>,
vk: VerifyingKey<G1Affine>,
phantom: PhantomData<C>,
}

impl<C: CircuitExt<Fr>> Verifier<C> {
pub fn new(params: ParamsKZG<Bn256>, vk: VerifyingKey<G1Affine>) -> Self {
impl<'params, C: CircuitExt<Fr>> Verifier<'params, C> {
pub fn new(params: &'params ParamsKZG<Bn256>, vk: VerifyingKey<G1Affine>) -> Self {
Self {
params,
vk,
phantom: PhantomData,
}
}

pub fn from_params(params: ParamsKZG<Bn256>, raw_vk: &[u8]) -> Self {
pub fn from_params(params: &'params ParamsKZG<Bn256>, raw_vk: &[u8]) -> Self {
let vk = deserialize_vk::<C>(raw_vk);

Self::new(params, vk)
}

pub fn from_params_dir(params_dir: &str, degree: u32, vk: &[u8]) -> Self {
let params = load_params(params_dir, degree, None).unwrap();

Self::from_params(params, vk)
}

pub fn verify_snark(&self, snark: Snark) -> bool {
verify_snark_shplonk::<C>(self.params.verifier_params(), snark, &self.vk)
}
Expand Down
4 changes: 2 additions & 2 deletions prover/src/common/verifier/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::EvmProof;
use halo2_proofs::halo2curves::bn256::Fr;
use snark_verifier_sdk::CircuitExt;

impl<C: CircuitExt<Fr>> Verifier<C> {
impl<'params, C: CircuitExt<Fr>> 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)
crate::evm::gen_evm_verifier::<C>(self.params, &self.vk, evm_proof, output_dir)
}
}
4 changes: 2 additions & 2 deletions prover/src/common/verifier/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use halo2_proofs::{
};
use snark_verifier_sdk::CircuitExt;

impl<C: CircuitExt<Fr>> Verifier<C> {
impl<'params, C: CircuitExt<Fr>> Verifier<'params, C> {
pub fn params(&self) -> &ParamsKZG<Bn256> {
&self.params
self.params
}

pub fn vk(&self) -> &VerifyingKey<G1Affine> {
Expand Down
14 changes: 7 additions & 7 deletions prover/src/inner/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ use std::marker::PhantomData;
mod mock;

#[derive(Debug)]
pub struct Prover<C: TargetCircuit> {
pub struct Prover<'params, C: TargetCircuit> {
// Make it public for testing with inner functions (unnecessary for FFI).
pub prover_impl: common::Prover,
pub prover_impl: common::Prover<'params>,
phantom: PhantomData<C>,
}

impl<C: TargetCircuit> From<common::Prover> for Prover<C> {
fn from(prover_impl: common::Prover) -> Self {
impl<'params, C: TargetCircuit> From<common::Prover<'params>> for Prover<'params, C> {
fn from(prover_impl: common::Prover<'params>) -> Self {
Self {
prover_impl,
phantom: PhantomData,
}
}
}

impl<C: TargetCircuit> Prover<C> {
pub fn from_params_dir(params_dir: &str) -> Self {
common::Prover::from_params_dir(params_dir, &[*INNER_DEGREE]).into()
impl<'params, C: TargetCircuit> Prover<'params, C> {
pub fn degrees() -> Vec<u32> {
vec![*INNER_DEGREE]
}

pub fn gen_inner_snark(&mut self, id: &str, block_traces: Vec<BlockTrace>) -> Result<Snark> {
Expand Down
2 changes: 1 addition & 1 deletion prover/src/inner/prover/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};
use snark_verifier_sdk::CircuitExt;
use zkevm_circuits::witness::Block;

impl<C: TargetCircuit> Prover<C> {
impl<'params, C: TargetCircuit> Prover<'params, C> {
pub fn mock_prove_target_circuit(block_trace: BlockTrace) -> anyhow::Result<()> {
Self::mock_prove_target_circuit_chunk(vec![block_trace])
}
Expand Down
31 changes: 17 additions & 14 deletions prover/src/inner/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
use crate::{
common, config::INNER_DEGREE, io::deserialize_vk, utils::load_params,
zkevm::circuit::TargetCircuit,
};
use halo2_proofs::plonk::keygen_vk;
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;

#[derive(Debug)]
pub struct Verifier<C: TargetCircuit> {
pub struct Verifier<'params, C: TargetCircuit> {
// Make it public for testing with inner functions (unnecessary for FFI).
pub inner: common::Verifier<C::Inner>,
pub inner: common::Verifier<'params, C::Inner>,
}

impl<C: TargetCircuit> From<common::Verifier<C::Inner>> for Verifier<C> {
fn from(inner: common::Verifier<C::Inner>) -> Self {
impl<'params, C: TargetCircuit> From<common::Verifier<'params, C::Inner>> for Verifier<'params, C> {
fn from(inner: common::Verifier<'params, C::Inner>) -> Self {
Self { inner }
}
}

impl<C: TargetCircuit> Verifier<C> {
pub fn from_params_dir(params_dir: &str, raw_vk: Option<&[u8]>) -> Self {
let params = load_params(params_dir, *INNER_DEGREE, None).unwrap();
impl<'params, C: TargetCircuit> Verifier<'params, C> {
pub fn from_params_map(
params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>,
raw_vk: Option<&[u8]>,
) -> Self {
let params = params_map.get(&*INNER_DEGREE).expect("should be loaded");

let vk = raw_vk.map_or_else(
|| {
let dummy_circuit = C::dummy_inner_circuit().expect("gen dummy circuit");
keygen_vk(&params, &dummy_circuit).unwrap()
keygen_vk(params, &dummy_circuit).unwrap()
},
deserialize_vk::<C::Inner>,
);

common::Verifier::new(params, vk).into()
let verifier = common::Verifier::new(params, vk);
verifier.into()
}

pub fn verify_inner_snark(&self, snark: Snark) -> bool {
Expand Down
Loading
Loading