Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
roynalnaruto committed Oct 25, 2024
1 parent fc050c4 commit 65b7c7c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 57 deletions.
138 changes: 95 additions & 43 deletions prover/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,89 @@
use std::{
collections::HashSet,
fmt,
fs::File,
path::{Path, PathBuf},
sync::LazyLock,
};

use crate::utils::read_env_var;
use aggregator::ConfigParams;
use std::{collections::HashSet, fmt, fs::File, path::Path, sync::LazyLock};

/// Degree (k) used for the inner circuit, i.e.
/// [`SuperCircuit`][zkevm_circuits::super_circuit::SuperCircuit].
pub static INNER_DEGREE: LazyLock<u32> =
LazyLock::new(|| read_env_var("SCROLL_PROVER_INNER_DEGREE", 20));

pub static ASSETS_DIR: LazyLock<String> =
LazyLock::new(|| read_env_var("SCROLL_PROVER_ASSETS_DIR", "configs".to_string()));
/// Name of the directory to find asset files on disk.
pub static ASSETS_DIR: LazyLock<PathBuf> =
LazyLock::new(|| read_env_var("SCROLL_PROVER_ASSETS_DIR", PathBuf::from("configs")));

pub static LAYER1_CONFIG_PATH: LazyLock<String> =
/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
/// of the [`Layer-1`][LayerId::Layer1] [`Circuit`][halo2_proofs::plonk::Circuit].
pub static LAYER1_CONFIG_PATH: LazyLock<PathBuf> =
LazyLock::new(|| asset_file_path("layer1.config"));
pub static LAYER2_CONFIG_PATH: LazyLock<String> =

/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
/// of the [`Layer-2`][LayerId::Layer2] [`Circuit`][halo2_proofs::plonk::Circuit].
pub static LAYER2_CONFIG_PATH: LazyLock<PathBuf> =
LazyLock::new(|| asset_file_path("layer2.config"));
pub static LAYER3_CONFIG_PATH: LazyLock<String> =

/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
/// of the [`Layer-3`][LayerId::Layer3] [`Circuit`][halo2_proofs::plonk::Circuit].
pub static LAYER3_CONFIG_PATH: LazyLock<PathBuf> =
LazyLock::new(|| asset_file_path("layer3.config"));
pub static LAYER4_CONFIG_PATH: LazyLock<String> =

/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
/// of the [`Layer-4`][LayerId::Layer4] [`Circuit`][halo2_proofs::plonk::Circuit].
pub static LAYER4_CONFIG_PATH: LazyLock<PathBuf> =
LazyLock::new(|| asset_file_path("layer4.config"));
pub static LAYER5_CONFIG_PATH: LazyLock<String> =

/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
/// of the [`Layer-5`][LayerId::Layer5] [`Circuit`][halo2_proofs::plonk::Circuit].
pub static LAYER5_CONFIG_PATH: LazyLock<PathBuf> =
LazyLock::new(|| asset_file_path("layer5.config"));
pub static LAYER6_CONFIG_PATH: LazyLock<String> =

/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
/// of the [`Layer-6`][LayerId::Layer6] [`Circuit`][halo2_proofs::plonk::Circuit].
pub static LAYER6_CONFIG_PATH: LazyLock<PathBuf> =
LazyLock::new(|| asset_file_path("layer6.config"));

pub static LAYER1_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER1_CONFIG_PATH));
pub static LAYER2_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER2_CONFIG_PATH));
pub static LAYER3_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER3_CONFIG_PATH));
pub static LAYER4_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER4_CONFIG_PATH));
pub static LAYER5_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER5_CONFIG_PATH));
pub static LAYER6_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER6_CONFIG_PATH));
/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
/// [`Layer-1`][LayerId::Layer1].
pub static LAYER1_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER1_CONFIG_PATH));

/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
/// [`Layer-2`][LayerId::Layer2].
pub static LAYER2_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER2_CONFIG_PATH));

/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
/// [`Layer-3`][LayerId::Layer3].
pub static LAYER3_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER3_CONFIG_PATH));

pub static ZKEVM_DEGREES: LazyLock<Vec<u32>> = LazyLock::new(|| {
/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
/// [`Layer-4`][LayerId::Layer4].
pub static LAYER4_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER4_CONFIG_PATH));

/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
/// [`Layer-5`][LayerId::Layer5].
pub static LAYER5_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER5_CONFIG_PATH));

/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
/// [`Layer-6`][LayerId::Layer6].
pub static LAYER6_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER6_CONFIG_PATH));

/// The list of degrees for Inner, Layer-1 and Layer-2, i.e. the proof generation [`layers`][LayerId]
/// covered by the [`ChunkProver`][crate::ChunkProver].
pub static CHUNK_PROVER_DEGREES: LazyLock<Vec<u32>> = LazyLock::new(|| {
Vec::from_iter(HashSet::from([
*INNER_DEGREE,
*LAYER1_DEGREE,
*LAYER2_DEGREE,
]))
});

pub static AGG_DEGREES: LazyLock<Vec<u32>> = LazyLock::new(|| {
/// The list of degrees for Layer-3, Layer-4, Layer-5 and Layer-6, i.e. the proof generation [`layers`][LayerId]
/// covered by the [`BatchProver`][crate::BatchProver].
pub static BATCH_PROVER_DEGREES: LazyLock<Vec<u32>> = LazyLock::new(|| {
Vec::from_iter(HashSet::from([
*LAYER3_DEGREE,
*LAYER4_DEGREE,
Expand All @@ -45,6 +92,7 @@ pub static AGG_DEGREES: LazyLock<Vec<u32>> = LazyLock::new(|| {
]))
});

/// The various proof layers in the proof generation pipeline.
#[derive(Clone, Copy, Debug)]
pub enum LayerId {
/// Super (inner) circuit layer
Expand All @@ -70,6 +118,7 @@ impl fmt::Display for LayerId {
}

impl LayerId {
/// Returns the identifier by layer.
pub fn id(&self) -> &str {
match self {
Self::Inner => "inner",
Expand All @@ -82,6 +131,7 @@ impl LayerId {
}
}

/// The degree (k) for the [`Circuit`][halo2_proofs::plonk::Circuit] by layer.
pub fn degree(&self) -> u32 {
match self {
Self::Inner => *INNER_DEGREE,
Expand All @@ -94,43 +144,45 @@ impl LayerId {
}
}

pub fn config_path(&self) -> &str {
/// The path to the [`Config Parameters`][aggregator::ConfigParams] used to configure the shape
/// of the [`Circuit`][halo2_proofs::plonk::Circuit].
pub fn config_path(&self) -> PathBuf {
match self {
Self::Layer1 => &LAYER1_CONFIG_PATH,
Self::Layer2 => &LAYER2_CONFIG_PATH,
Self::Layer3 => &LAYER3_CONFIG_PATH,
Self::Layer4 => &LAYER4_CONFIG_PATH,
Self::Layer5 => &LAYER5_CONFIG_PATH,
Self::Layer6 => &LAYER6_CONFIG_PATH,
Self::Layer1 => LAYER1_CONFIG_PATH.to_path_buf(),
Self::Layer2 => LAYER2_CONFIG_PATH.to_path_buf(),
Self::Layer3 => LAYER3_CONFIG_PATH.to_path_buf(),
Self::Layer4 => LAYER4_CONFIG_PATH.to_path_buf(),
Self::Layer5 => LAYER5_CONFIG_PATH.to_path_buf(),
Self::Layer6 => LAYER6_CONFIG_PATH.to_path_buf(),
Self::Inner => unreachable!("No config file for super (inner) circuit"),
}
}
}

pub fn asset_file_path(filename: &str) -> String {
Path::new(&*ASSETS_DIR)
.join(filename)
.to_string_lossy()
.into_owned()
}

pub fn layer_config_path(id: &str) -> &str {
/// Returns the path to the [`Config Parameters`][aggregator::ConfigParams] that configure the
/// shape of the [`Circuit`][halo2_proofs::plonk::Circuit] given the [`id`][LayerId::id] of the
/// layer.
pub fn layer_config_path(id: &str) -> PathBuf {
match id {
"layer1" => &LAYER1_CONFIG_PATH,
"layer2" => &LAYER2_CONFIG_PATH,
"layer3" => &LAYER3_CONFIG_PATH,
"layer4" => &LAYER4_CONFIG_PATH,
"layer5" => &LAYER5_CONFIG_PATH,
"layer6" => &LAYER6_CONFIG_PATH,
"layer1" => LAYER1_CONFIG_PATH.to_path_buf(),
"layer2" => LAYER2_CONFIG_PATH.to_path_buf(),
"layer3" => LAYER3_CONFIG_PATH.to_path_buf(),
"layer4" => LAYER4_CONFIG_PATH.to_path_buf(),
"layer5" => LAYER5_CONFIG_PATH.to_path_buf(),
"layer6" => LAYER6_CONFIG_PATH.to_path_buf(),
_ => panic!("Wrong id-{id} to get layer config path"),
}
}

fn layer_degree(config_file: &str) -> u32 {
let f = File::open(config_file).unwrap_or_else(|_| panic!("Failed to open {config_file}"));
fn asset_file_path(filename: &str) -> PathBuf {
ASSETS_DIR.join(filename)
}

fn layer_degree<P: AsRef<Path> + fmt::Debug>(path: P) -> u32 {
let f = File::open(&path).unwrap_or_else(|_| panic!("Failed to open {path:?}"));

let params: ConfigParams =
serde_json::from_reader(f).unwrap_or_else(|_| panic!("Failed to parse {config_file}"));
let params = serde_json::from_reader::<_, aggregator::ConfigParams>(f)
.unwrap_or_else(|_| panic!("Failed to parse {path:?}"));

params.degree
}
3 changes: 2 additions & 1 deletion prover/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::read_env_var;
use std::sync::LazyLock;

use crate::utils::read_env_var;

// TODO: is it a good design to use LazyLock? Why not read env var each time?

pub fn bundle_vk_filename() -> String {
Expand Down
15 changes: 8 additions & 7 deletions prover/src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use crate::{io::write_file, EvmProof};
use std::{path::PathBuf, str::FromStr};

use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::VerifyingKey,
poly::kzg::commitment::ParamsKZG,
};
use revm::{
primitives::{CreateScheme, ExecutionResult, Output, TransactTo, TxEnv},
InMemoryDB, EVM,
};
use snark_verifier::pcs::kzg::{Bdfg21, Kzg};
use snark_verifier_sdk::CircuitExt;
use std::{path::PathBuf, str::FromStr};

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

/// Dump YUL and binary bytecode(use `solc` in PATH) to output_dir.
/// Panic if error encountered.
Expand Down Expand Up @@ -40,11 +46,6 @@ pub fn gen_evm_verifier<C: CircuitExt<Fr>>(
assert!(success);
}

use revm::{
primitives::{CreateScheme, ExecutionResult, Output, TransactTo, TxEnv},
InMemoryDB, EVM,
};

/// Deploy contract and then call with calldata.
/// Returns gas_used of call to deployed contract if both transactions are successful.
pub fn deploy_and_call(deployment_code: Vec<u8>, calldata: Vec<u8>) -> Result<u64, String> {
Expand Down
3 changes: 1 addition & 2 deletions prover/src/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ impl Proof {
let instances = self.instances();
let proof = self.proof().to_vec();
let calldata = snark_verifier::loader::evm::encode_calldata(&instances, &proof);
let result = crate::evm::deploy_and_call(deployment_code, calldata);
result.is_ok()
crate::evm::deploy_and_call(deployment_code, calldata).is_ok()
}

pub fn instances(&self) -> Vec<Vec<Fr>> {
Expand Down
4 changes: 2 additions & 2 deletions prover/src/test/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::{LazyLock, Mutex};

use crate::{
aggregator::{Prover, Verifier},
config::{LayerId, AGG_DEGREES},
config::{LayerId, BATCH_PROVER_DEGREES},
consts::DEPLOYMENT_CODE_FILENAME,
io::force_to_read,
types::BundleProvingTask,
Expand All @@ -12,7 +12,7 @@ use crate::{

static PARAMS_MAP: LazyLock<ParamsMap> = LazyLock::new(|| {
let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string());
crate::common::Prover::load_params_map(&params_dir, &AGG_DEGREES)
crate::common::Prover::load_params_map(&params_dir, &BATCH_PROVER_DEGREES)
});

static BATCH_PROVER: LazyLock<Mutex<Prover>> = LazyLock::new(|| {
Expand Down
4 changes: 2 additions & 2 deletions prover/src/test/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::sync::{LazyLock, Mutex};

use crate::{
config::ZKEVM_DEGREES,
config::CHUNK_PROVER_DEGREES,
utils::read_env_var,
zkevm::{Prover, Verifier},
ChunkProof, ChunkProvingTask, ParamsMap,
};

static PARAMS_MAP: LazyLock<ParamsMap> = LazyLock::new(|| {
let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string());
crate::common::Prover::load_params_map(&params_dir, &ZKEVM_DEGREES)
crate::common::Prover::load_params_map(&params_dir, &CHUNK_PROVER_DEGREES)
});

static CHUNK_PROVER: LazyLock<Mutex<Prover>> = LazyLock::new(|| {
Expand Down

0 comments on commit 65b7c7c

Please sign in to comment.