Skip to content

Commit

Permalink
opt(libzkp): allow params be shared between multi versions (#1499)
Browse files Browse the repository at this point in the history
Co-authored-by: georgehao <[email protected]>
Co-authored-by: colin <[email protected]>
Co-authored-by: Rohit Narurkar <[email protected]>
  • Loading branch information
4 people authored Aug 27, 2024
1 parent 31a6141 commit 4470c81
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 59 deletions.
40 changes: 20 additions & 20 deletions common/libzkp/impl/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions common/libzkp/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.
snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", branch = "develop", default-features = false, features = ["loader_halo2", "loader_evm", "halo2-pse"] }

# darwin
prover_v4 = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.12.0", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] }
prover_v4 = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.12.2", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] }
# darwin_v2
prover_v5 = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.13.0", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] }
prover_v5 = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.13.1", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] }

base64 = "0.13.0"
env_logger = "0.9.0"
Expand Down
57 changes: 42 additions & 15 deletions common/libzkp/impl/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ mod darwin;
mod darwin_v2;

use anyhow::{bail, Result};
// use darwin::DarwinVerifier;
use darwin::DarwinVerifier;
use darwin_v2::DarwinV2Verifier;
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use prover_v4::utils::load_params;
use serde::{Deserialize, Serialize};
use std::{cell::OnceCell, rc::Rc};
use std::{cell::OnceCell, collections::BTreeMap, rc::Rc};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TaskType {
Expand Down Expand Up @@ -36,22 +38,47 @@ type HardForkName = String;
struct VerifierPair(HardForkName, Rc<Box<dyn ProofVerifier>>);

static mut VERIFIER_HIGH: OnceCell<VerifierPair> = OnceCell::new();
// static mut VERIFIER_LOW: OnceCell<VerifierPair> = OnceCell::new();
static mut VERIFIER_LOW: OnceCell<VerifierPair> = OnceCell::new();
static mut PARAMS_MAP: OnceCell<BTreeMap<u32, ParamsKZG<Bn256>>> = OnceCell::new();

pub fn init(config: VerifierConfig) {
// let low_conf = config.low_version_circuit;
// let verifier = DarwinVerifier::new(&low_conf.params_path, &low_conf.assets_path);
let low_conf = config.low_version_circuit;

// unsafe {
// VERIFIER_LOW
// .set(VerifierPair(
// low_conf.fork_name,
// Rc::new(Box::new(verifier)),
// ))
// .unwrap_unchecked();
// }
// params should be shared between low and high
let mut params_map = BTreeMap::new();
for degree in [
*prover_v4::config::LAYER2_DEGREE,
*prover_v4::config::LAYER4_DEGREE,
] {
if let std::collections::btree_map::Entry::Vacant(e) = params_map.entry(degree) {
match load_params(&low_conf.params_path, degree, None) {
Ok(params) => {
e.insert(params);
}
Err(e) => panic!(
"failed to load params, degree {}, dir {}, err {}",
degree, low_conf.params_path, e
),
}
}
}
unsafe {
PARAMS_MAP.set(params_map).unwrap_unchecked();
}

let verifier = DarwinVerifier::new(unsafe { PARAMS_MAP.get().unwrap() }, &low_conf.assets_path);

unsafe {
VERIFIER_LOW
.set(VerifierPair(
low_conf.fork_name,
Rc::new(Box::new(verifier)),
))
.unwrap_unchecked();
}
let high_conf = config.high_version_circuit;
let verifier = DarwinV2Verifier::new(&high_conf.params_path, &high_conf.assets_path);
let verifier =
DarwinV2Verifier::new(unsafe { PARAMS_MAP.get().unwrap() }, &high_conf.assets_path);
unsafe {
VERIFIER_HIGH
.set(VerifierPair(
Expand All @@ -64,7 +91,7 @@ pub fn init(config: VerifierConfig) {

pub fn get_verifier(fork_name: &str) -> Result<Rc<Box<dyn ProofVerifier>>> {
unsafe {
if let Some(verifier) = VERIFIER_HIGH.get() {
if let Some(verifier) = VERIFIER_LOW.get() {
if verifier.0 == fork_name {
return Ok(verifier.1.clone());
}
Expand Down
21 changes: 10 additions & 11 deletions common/libzkp/impl/src/verifier/darwin.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
use super::{ProofVerifier, TaskType};

use anyhow::Result;
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};

use crate::utils::panic_catch;
use prover_v4::{
aggregator::Verifier as AggVerifier, zkevm::Verifier, BatchProof, BundleProof, ChunkProof,
};
use std::env;
use std::{collections::BTreeMap, env};

pub struct DarwinVerifier {
verifier: Verifier,
agg_verifier: AggVerifier,
pub struct DarwinVerifier<'params> {
verifier: Verifier<'params>,
agg_verifier: AggVerifier<'params>,
}

impl DarwinVerifier {
#[allow(dead_code)]
pub fn new(params_dir: &str, assets_dir: &str) -> Self {
impl<'params> DarwinVerifier<'params> {
pub fn new(params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>, assets_dir: &str) -> Self {
env::set_var("SCROLL_PROVER_ASSETS_DIR", assets_dir);
let verifier = Verifier::from_dirs(params_dir, assets_dir);

let agg_verifier = AggVerifier::from_dirs(params_dir, assets_dir);
let verifier = Verifier::from_params_and_assets(params_map, assets_dir);
let agg_verifier = AggVerifier::from_params_and_assets(params_map, assets_dir);

Self {
verifier,
Expand All @@ -28,7 +27,7 @@ impl DarwinVerifier {
}
}

impl ProofVerifier for DarwinVerifier {
impl<'params> ProofVerifier for DarwinVerifier<'params> {
fn verify(&self, task_type: super::TaskType, proof: Vec<u8>) -> Result<bool> {
let result = panic_catch(|| match task_type {
TaskType::Chunk => {
Expand Down
20 changes: 10 additions & 10 deletions common/libzkp/impl/src/verifier/darwin_v2.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use super::{ProofVerifier, TaskType};

use anyhow::Result;
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};

use crate::utils::panic_catch;
use prover_v5::{
aggregator::Verifier as AggVerifier, zkevm::Verifier, BatchProof, BundleProof, ChunkProof,
};
use std::env;
use std::{collections::BTreeMap, env};

pub struct DarwinV2Verifier {
verifier: Verifier,
agg_verifier: AggVerifier,
pub struct DarwinV2Verifier<'params> {
verifier: Verifier<'params>,
agg_verifier: AggVerifier<'params>,
}

impl DarwinV2Verifier {
pub fn new(params_dir: &str, assets_dir: &str) -> Self {
impl<'params> DarwinV2Verifier<'params> {
pub fn new(params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>, assets_dir: &str) -> Self {
env::set_var("SCROLL_PROVER_ASSETS_DIR", assets_dir);
let verifier = Verifier::from_dirs(params_dir, assets_dir);

let agg_verifier = AggVerifier::from_dirs(params_dir, assets_dir);
let verifier = Verifier::from_params_and_assets(params_map, assets_dir);
let agg_verifier = AggVerifier::from_params_and_assets(params_map, assets_dir);

Self {
verifier,
Expand All @@ -27,7 +27,7 @@ impl DarwinV2Verifier {
}
}

impl ProofVerifier for DarwinV2Verifier {
impl<'params> ProofVerifier for DarwinV2Verifier<'params> {
fn verify(&self, task_type: super::TaskType, proof: Vec<u8>) -> Result<bool> {
let result = panic_catch(|| match task_type {
TaskType::Chunk => {
Expand Down
2 changes: 1 addition & 1 deletion common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime/debug"
)

var tag = "v4.4.49"
var tag = "v4.4.50"

var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
Expand Down

0 comments on commit 4470c81

Please sign in to comment.