Skip to content

Commit

Permalink
Move inner-prove and chunk-prove to common prover test feature. (
Browse files Browse the repository at this point in the history
  • Loading branch information
silathdiir authored Sep 19, 2023
1 parent 25f8bde commit f7b4a19
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 135 deletions.
1 change: 1 addition & 0 deletions prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ default = []
parallel_syn = ["halo2_proofs/parallel_syn", "zkevm-circuits/parallel_syn"]
scroll = ["bus-mapping/scroll", "eth-types/scroll", "zkevm-circuits/scroll"]
shanghai = ["bus-mapping/shanghai", "eth-types/shanghai", "zkevm-circuits/shanghai"]
test = []
2 changes: 2 additions & 0 deletions prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub mod consts;
pub mod inner;
pub mod io;
pub mod proof;
#[cfg(feature = "test")]
pub mod test;
pub mod types;
pub mod utils;
pub mod zkevm;
Expand Down
5 changes: 5 additions & 0 deletions prover/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod chunk;
mod inner;

pub use chunk::chunk_prove;
pub use inner::inner_prove;
67 changes: 67 additions & 0 deletions prover/src/test/chunk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::{
common::{Prover, Verifier},
config::{LayerId, ZKEVM_DEGREES},
utils::read_env_var,
CompressionCircuit, WitnessBlock,
};
use once_cell::sync::Lazy;
use std::env;

static mut CHUNK_PROVER: Lazy<Prover> = Lazy::new(|| {
let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string());
let prover = Prover::from_params_dir(&params_dir, &ZKEVM_DEGREES);
log::info!("Constructed chunk-prover");

prover
});

static mut CHUNK_VERIFIER: Lazy<Verifier<CompressionCircuit>> = Lazy::new(|| {
env::set_var("COMPRESSION_CONFIG", LayerId::Layer2.config_path());

let prover = unsafe { &mut CHUNK_PROVER };
let params = prover.params(LayerId::Layer2.degree()).clone();

let pk = prover
.pk(LayerId::Layer2.id())
.expect("Failed to get chunk-prove PK");
let vk = pk.get_vk().clone();

let verifier = Verifier::new(params, vk);
log::info!("Constructed chunk-verifier");

verifier
});

pub fn chunk_prove(test: &str, witness_block: &WitnessBlock) {
log::info!("{test}: chunk-prove BEGIN");

let prover = unsafe { &mut CHUNK_PROVER };
let inner_id = read_env_var("INNER_LAYER_ID", LayerId::Inner.id().to_string());
let inner_id_changed = prover.pk(&inner_id).is_none();

// Clear previous PKs if inner-layer ID changed.
if inner_id_changed {
prover.clear_pks();
}

let snark = prover
.load_or_gen_final_chunk_snark(test, witness_block, Some(&inner_id), None)
.unwrap_or_else(|err| panic!("{test}: failed to generate chunk snark: {err}"));
log::info!("{test}: generated chunk snark");

let verifier = unsafe { &mut CHUNK_VERIFIER };

// Reset VK if inner-layer ID changed.
if inner_id_changed {
let pk = prover
.pk(LayerId::Layer2.id())
.unwrap_or_else(|| panic!("{test}: failed to get inner-prove PK"));
let vk = pk.get_vk().clone();
verifier.set_vk(vk);
}

let verified = verifier.verify_snark(snark);
assert!(verified, "{test}: failed to verify chunk snark");

log::info!("{test}: chunk-prove END");
}
66 changes: 66 additions & 0 deletions prover/src/test/inner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::{
common::{Prover, Verifier},
config::{LayerId, INNER_DEGREE},
utils::{gen_rng, read_env_var},
zkevm::circuit::{SuperCircuit, TargetCircuit},
WitnessBlock,
};
use once_cell::sync::Lazy;

static mut INNER_PROVER: Lazy<Prover> = Lazy::new(|| {
let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string());
let prover = Prover::from_params_dir(&params_dir, &[*INNER_DEGREE]);
log::info!("Constructed inner-prover");

prover
});

static mut INNER_VERIFIER: Lazy<Verifier<<SuperCircuit as TargetCircuit>::Inner>> =
Lazy::new(|| {
let prover = unsafe { &mut INNER_PROVER };
let params = prover.params(*INNER_DEGREE).clone();

let inner_id = read_env_var("INNER_LAYER_ID", LayerId::Inner.id().to_string());
let pk = prover.pk(&inner_id).expect("Failed to get inner-prove PK");
let vk = pk.get_vk().clone();

let verifier = Verifier::new(params, vk);
log::info!("Constructed inner-verifier");

verifier
});

pub fn inner_prove(test: &str, witness_block: &WitnessBlock) {
log::info!("{test}: inner-prove BEGIN");

let prover = unsafe { &mut INNER_PROVER };
let inner_id = read_env_var("INNER_LAYER_ID", LayerId::Inner.id().to_string());
let inner_id_changed = prover.pk(&inner_id).is_none();

// Clear previous PKs if inner-layer ID changed.
if inner_id_changed {
prover.clear_pks();
}

let rng = gen_rng();
let snark = prover
.gen_inner_snark::<SuperCircuit>(&inner_id, rng, witness_block)
.unwrap_or_else(|err| panic!("{test}: failed to generate inner snark: {err}"));
log::info!("{test}: generated inner snark");

let verifier = unsafe { &mut INNER_VERIFIER };

// Reset VK if inner-layer ID changed.
if inner_id_changed {
let pk = prover
.pk(&inner_id)
.unwrap_or_else(|| panic!("{test}: failed to get inner-prove PK"));
let vk = pk.get_vk().clone();
verifier.set_vk(vk);
}

let verified = verifier.verify_snark(snark);
assert!(verified, "{test}: failed to verify inner snark");

log::info!("{test}: inner-prove END");
}
4 changes: 2 additions & 2 deletions testool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ skip-self-destruct = []
shanghai = ["bus-mapping/shanghai", "eth-types/shanghai", "mock/shanghai", "zkevm-circuits/shanghai", "prover?/shanghai"]
scroll = ["bus-mapping/scroll", "eth-types/scroll", "external-tracer/scroll", "mock/scroll", "zkevm-circuits/scroll", "prover?/scroll"]
parallel_syn = ["halo2_proofs/parallel_syn", "zkevm-circuits/parallel_syn", "prover?/parallel_syn"]
inner-prove = ["prover", "parallel_syn", "scroll", "shanghai"]
chunk-prove = ["prover", "parallel_syn", "scroll", "shanghai"]
inner-prove = ["prover/test", "parallel_syn", "scroll", "shanghai"]
chunk-prove = ["prover/test", "parallel_syn", "scroll", "shanghai"]
144 changes: 11 additions & 133 deletions testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ use external_tracer::{LoggerConfig, TraceConfig};
use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr, plonk::Circuit};
use itertools::Itertools;
use once_cell::sync::Lazy;
#[cfg(any(feature = "inner-prove", feature = "chunk-prove"))]
use prover::{
common::{Prover, Verifier},
config::LayerId,
config::{INNER_DEGREE, ZKEVM_DEGREES},
};
#[cfg(feature = "inner-prove")]
use prover::{utils::gen_rng, zkevm::circuit};
use std::{collections::HashMap, env, str::FromStr};
use thiserror::Error;
use zkevm_circuits::{
Expand All @@ -43,58 +35,6 @@ pub fn read_env_var<T: Clone + FromStr>(var_name: &'static str, default: T) -> T
/// Which circuit to test. Default is evm + state.
pub static CIRCUIT: Lazy<String> = Lazy::new(|| read_env_var("CIRCUIT", "".to_string()));

#[cfg(any(feature = "inner-prove", feature = "chunk-prove"))]
static mut REAL_PROVER: Lazy<Prover> = Lazy::new(|| {
let params_dir = "./test_params";

let degrees: Vec<u32> = if cfg!(feature = "inner-prove") {
vec![*INNER_DEGREE]
} else {
// for chunk-prove
(*ZKEVM_DEGREES).clone()
};

let prover = Prover::from_params_dir(params_dir, &degrees);
log::info!("Constructed real-prover");

prover
});

#[cfg(feature = "inner-prove")]
static mut INNER_VERIFIER: Lazy<
Verifier<<circuit::SuperCircuit as circuit::TargetCircuit>::Inner>,
> = Lazy::new(|| {
let prover = unsafe { &mut REAL_PROVER };
let params = prover.params(*INNER_DEGREE).clone();

let id = read_env_var("COINBASE", LayerId::Inner.id().to_string());
let pk = prover.pk(&id).expect("Failed to get inner-prove PK");
let vk = pk.get_vk().clone();

let verifier = Verifier::new(params, vk);
log::info!("Constructed inner-verifier");

verifier
});

#[cfg(feature = "chunk-prove")]
static mut CHUNK_VERIFIER: Lazy<Verifier<prover::CompressionCircuit>> = Lazy::new(|| {
env::set_var("COMPRESSION_CONFIG", LayerId::Layer2.config_path());

let prover = unsafe { &mut REAL_PROVER };
let params = prover.params(LayerId::Layer2.degree()).clone();

let pk = prover
.pk(LayerId::Layer2.id())
.expect("Failed to get chunk-prove PK");
let vk = pk.get_vk().clone();

let verifier = Verifier::new(params, vk);
log::info!("Constructed chunk-verifier");

verifier
});

#[derive(PartialEq, Eq, Error, Debug)]
pub enum StateTestError {
#[cfg(not(feature = "scroll"))]
Expand Down Expand Up @@ -721,9 +661,15 @@ pub fn run_test(
check_ccc();
} else {
#[cfg(feature = "inner-prove")]
inner_prove(&test_id, &st.env.current_coinbase, &witness_block);
{
set_env_coinbase(&st.env.current_coinbase);
prover::test::inner_prove(&test_id, &witness_block);
}
#[cfg(feature = "chunk-prove")]
chunk_prove(&test_id, &st.env.current_coinbase, &witness_block);
{
set_env_coinbase(&st.env.current_coinbase);
prover::test::chunk_prove(&test_id, &witness_block);
}
#[cfg(not(any(feature = "inner-prove", feature = "chunk-prove")))]
mock_prove(&test_id, &witness_block);
}
Expand Down Expand Up @@ -769,6 +715,9 @@ fn set_env_coinbase(coinbase: &Address) -> String {
let coinbase = format!("0x{}", hex::encode(coinbase));
env::set_var("COINBASE", &coinbase);

// Used for inner-prove and chunk-prove.
env::set_var("INNER_LAYER_ID", &coinbase);

coinbase
}

Expand All @@ -785,74 +734,3 @@ fn mock_prove(test_id: &str, witness_block: &Block<Fr>) {

log::info!("{test_id}: mock-prove END");
}

#[cfg(feature = "inner-prove")]
fn inner_prove(test_id: &str, coinbase: &Address, witness_block: &Block<Fr>) {
let coinbase = set_env_coinbase(coinbase);
log::info!("{test_id}: inner-prove BEGIN, coinbase = {coinbase}");

let prover = unsafe { &mut REAL_PROVER };
let coinbase_changed = prover.pk(&coinbase).is_none();

// Clear previous PKs if coinbase address changed.
if coinbase_changed {
prover.clear_pks();
}

let rng = gen_rng();
let snark = prover
.gen_inner_snark::<circuit::SuperCircuit>(&coinbase, rng, witness_block)
.unwrap_or_else(|err| panic!("{test_id}: failed to generate inner snark: {err}"));
log::info!("{test_id}: generated inner snark");

let verifier = unsafe { &mut INNER_VERIFIER };

// Reset VK if coinbase address changed.
if coinbase_changed {
let pk = prover.pk(&coinbase).unwrap_or_else(|| {
panic!("{test_id}: failed to get inner-prove PK, coinbase = {coinbase}")
});
let vk = pk.get_vk().clone();
verifier.set_vk(vk);
}

let verified = verifier.verify_snark(snark);
assert!(verified, "{test_id}: failed to verify inner snark");

log::info!("{test_id}: inner-prove END, coinbase = {coinbase}");
}

#[cfg(feature = "chunk-prove")]
fn chunk_prove(test_id: &str, coinbase: &Address, witness_block: &Block<Fr>) {
let coinbase = set_env_coinbase(coinbase);
log::info!("{test_id}: chunk-prove BEGIN, coinbase = {coinbase}");

let prover = unsafe { &mut REAL_PROVER };
let coinbase_changed = prover.pk(&coinbase).is_none();

// Clear previous PKs if coinbase address changed.
if coinbase_changed {
prover.clear_pks();
}

let snark = prover
.load_or_gen_final_chunk_snark(test_id, witness_block, Some(&coinbase), None)
.unwrap_or_else(|err| panic!("{test_id}: failed to generate chunk snark: {err}"));
log::info!("{test_id}: generated chunk snark");

let verifier = unsafe { &mut CHUNK_VERIFIER };

// Reset VK if coinbase address changed.
if coinbase_changed {
let pk = prover.pk(LayerId::Layer2.id()).unwrap_or_else(|| {
panic!("{test_id}: failed to get inner-prove PK, coinbase = {coinbase}")
});
let vk = pk.get_vk().clone();
verifier.set_vk(vk);
}

let verified = verifier.verify_snark(snark);
assert!(verified, "{test_id}: failed to verify chunk snark");

log::info!("{test_id}: chunk-prove END, coinbase = {coinbase}");
}

0 comments on commit f7b4a19

Please sign in to comment.