From 65b7c7c91af559ddfd7edfded0ef31ef448a5598 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Fri, 25 Oct 2024 03:04:12 +0100 Subject: [PATCH] more refactoring --- prover/src/config.rs | 138 +++++++++++++++++++++++++++------------ prover/src/consts.rs | 3 +- prover/src/evm.rs | 15 +++-- prover/src/proof/mod.rs | 3 +- prover/src/test/batch.rs | 4 +- prover/src/test/chunk.rs | 4 +- 6 files changed, 110 insertions(+), 57 deletions(-) diff --git a/prover/src/config.rs b/prover/src/config.rs index 8db379416f..cbd4dc5fc7 100644 --- a/prover/src/config.rs +++ b/prover/src/config.rs @@ -1,34 +1,79 @@ +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 = LazyLock::new(|| read_env_var("SCROLL_PROVER_INNER_DEGREE", 20)); -pub static ASSETS_DIR: LazyLock = - 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 = + LazyLock::new(|| read_env_var("SCROLL_PROVER_ASSETS_DIR", PathBuf::from("configs"))); -pub static LAYER1_CONFIG_PATH: LazyLock = +/// 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 = LazyLock::new(|| asset_file_path("layer1.config")); -pub static LAYER2_CONFIG_PATH: LazyLock = + +/// 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 = LazyLock::new(|| asset_file_path("layer2.config")); -pub static LAYER3_CONFIG_PATH: LazyLock = + +/// 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 = LazyLock::new(|| asset_file_path("layer3.config")); -pub static LAYER4_CONFIG_PATH: LazyLock = + +/// 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 = LazyLock::new(|| asset_file_path("layer4.config")); -pub static LAYER5_CONFIG_PATH: LazyLock = + +/// 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 = LazyLock::new(|| asset_file_path("layer5.config")); -pub static LAYER6_CONFIG_PATH: LazyLock = + +/// 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 = LazyLock::new(|| asset_file_path("layer6.config")); -pub static LAYER1_DEGREE: LazyLock = LazyLock::new(|| layer_degree(&LAYER1_CONFIG_PATH)); -pub static LAYER2_DEGREE: LazyLock = LazyLock::new(|| layer_degree(&LAYER2_CONFIG_PATH)); -pub static LAYER3_DEGREE: LazyLock = LazyLock::new(|| layer_degree(&LAYER3_CONFIG_PATH)); -pub static LAYER4_DEGREE: LazyLock = LazyLock::new(|| layer_degree(&LAYER4_CONFIG_PATH)); -pub static LAYER5_DEGREE: LazyLock = LazyLock::new(|| layer_degree(&LAYER5_CONFIG_PATH)); -pub static LAYER6_DEGREE: LazyLock = 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 = 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 = 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 = LazyLock::new(|| layer_degree(&*LAYER3_CONFIG_PATH)); -pub static ZKEVM_DEGREES: LazyLock> = LazyLock::new(|| { +/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at +/// [`Layer-4`][LayerId::Layer4]. +pub static LAYER4_DEGREE: LazyLock = 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 = 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 = 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> = LazyLock::new(|| { Vec::from_iter(HashSet::from([ *INNER_DEGREE, *LAYER1_DEGREE, @@ -36,7 +81,9 @@ pub static ZKEVM_DEGREES: LazyLock> = LazyLock::new(|| { ])) }); -pub static AGG_DEGREES: LazyLock> = 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> = LazyLock::new(|| { Vec::from_iter(HashSet::from([ *LAYER3_DEGREE, *LAYER4_DEGREE, @@ -45,6 +92,7 @@ pub static AGG_DEGREES: LazyLock> = LazyLock::new(|| { ])) }); +/// The various proof layers in the proof generation pipeline. #[derive(Clone, Copy, Debug)] pub enum LayerId { /// Super (inner) circuit layer @@ -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", @@ -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, @@ -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 + 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 } diff --git a/prover/src/consts.rs b/prover/src/consts.rs index 19b1800ddb..e9c7af048a 100644 --- a/prover/src/consts.rs +++ b/prover/src/consts.rs @@ -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 { diff --git a/prover/src/evm.rs b/prover/src/evm.rs index 905e8723e9..a7e066f546 100644 --- a/prover/src/evm.rs +++ b/prover/src/evm.rs @@ -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. @@ -40,11 +46,6 @@ pub fn gen_evm_verifier>( 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, calldata: Vec) -> Result { diff --git a/prover/src/proof/mod.rs b/prover/src/proof/mod.rs index cb1672d574..73d5915198 100644 --- a/prover/src/proof/mod.rs +++ b/prover/src/proof/mod.rs @@ -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> { diff --git a/prover/src/test/batch.rs b/prover/src/test/batch.rs index 0e0a8165fd..a4cedb56d1 100644 --- a/prover/src/test/batch.rs +++ b/prover/src/test/batch.rs @@ -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, @@ -12,7 +12,7 @@ use crate::{ static PARAMS_MAP: LazyLock = LazyLock::new(|| { let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string()); - crate::common::Prover::load_params_map(¶ms_dir, &AGG_DEGREES) + crate::common::Prover::load_params_map(¶ms_dir, &BATCH_PROVER_DEGREES) }); static BATCH_PROVER: LazyLock> = LazyLock::new(|| { diff --git a/prover/src/test/chunk.rs b/prover/src/test/chunk.rs index d3a66f0089..8977edb46d 100644 --- a/prover/src/test/chunk.rs +++ b/prover/src/test/chunk.rs @@ -1,7 +1,7 @@ use std::sync::{LazyLock, Mutex}; use crate::{ - config::ZKEVM_DEGREES, + config::CHUNK_PROVER_DEGREES, utils::read_env_var, zkevm::{Prover, Verifier}, ChunkProof, ChunkProvingTask, ParamsMap, @@ -9,7 +9,7 @@ use crate::{ static PARAMS_MAP: LazyLock = LazyLock::new(|| { let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string()); - crate::common::Prover::load_params_map(¶ms_dir, &ZKEVM_DEGREES) + crate::common::Prover::load_params_map(¶ms_dir, &CHUNK_PROVER_DEGREES) }); static CHUNK_PROVER: LazyLock> = LazyLock::new(|| {