Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 65b7c7c

Browse files
committed
more refactoring
1 parent fc050c4 commit 65b7c7c

File tree

6 files changed

+110
-57
lines changed

6 files changed

+110
-57
lines changed

prover/src/config.rs

Lines changed: 95 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,89 @@
1+
use std::{
2+
collections::HashSet,
3+
fmt,
4+
fs::File,
5+
path::{Path, PathBuf},
6+
sync::LazyLock,
7+
};
8+
19
use crate::utils::read_env_var;
2-
use aggregator::ConfigParams;
3-
use std::{collections::HashSet, fmt, fs::File, path::Path, sync::LazyLock};
410

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

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

11-
pub static LAYER1_CONFIG_PATH: LazyLock<String> =
20+
/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
21+
/// of the [`Layer-1`][LayerId::Layer1] [`Circuit`][halo2_proofs::plonk::Circuit].
22+
pub static LAYER1_CONFIG_PATH: LazyLock<PathBuf> =
1223
LazyLock::new(|| asset_file_path("layer1.config"));
13-
pub static LAYER2_CONFIG_PATH: LazyLock<String> =
24+
25+
/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
26+
/// of the [`Layer-2`][LayerId::Layer2] [`Circuit`][halo2_proofs::plonk::Circuit].
27+
pub static LAYER2_CONFIG_PATH: LazyLock<PathBuf> =
1428
LazyLock::new(|| asset_file_path("layer2.config"));
15-
pub static LAYER3_CONFIG_PATH: LazyLock<String> =
29+
30+
/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
31+
/// of the [`Layer-3`][LayerId::Layer3] [`Circuit`][halo2_proofs::plonk::Circuit].
32+
pub static LAYER3_CONFIG_PATH: LazyLock<PathBuf> =
1633
LazyLock::new(|| asset_file_path("layer3.config"));
17-
pub static LAYER4_CONFIG_PATH: LazyLock<String> =
34+
35+
/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
36+
/// of the [`Layer-4`][LayerId::Layer4] [`Circuit`][halo2_proofs::plonk::Circuit].
37+
pub static LAYER4_CONFIG_PATH: LazyLock<PathBuf> =
1838
LazyLock::new(|| asset_file_path("layer4.config"));
19-
pub static LAYER5_CONFIG_PATH: LazyLock<String> =
39+
40+
/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
41+
/// of the [`Layer-5`][LayerId::Layer5] [`Circuit`][halo2_proofs::plonk::Circuit].
42+
pub static LAYER5_CONFIG_PATH: LazyLock<PathBuf> =
2043
LazyLock::new(|| asset_file_path("layer5.config"));
21-
pub static LAYER6_CONFIG_PATH: LazyLock<String> =
44+
45+
/// The path to the [`Config Parameters`][aggregator::ConfigParams] JSON file that define the shape
46+
/// of the [`Layer-6`][LayerId::Layer6] [`Circuit`][halo2_proofs::plonk::Circuit].
47+
pub static LAYER6_CONFIG_PATH: LazyLock<PathBuf> =
2248
LazyLock::new(|| asset_file_path("layer6.config"));
2349

24-
pub static LAYER1_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER1_CONFIG_PATH));
25-
pub static LAYER2_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER2_CONFIG_PATH));
26-
pub static LAYER3_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER3_CONFIG_PATH));
27-
pub static LAYER4_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER4_CONFIG_PATH));
28-
pub static LAYER5_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER5_CONFIG_PATH));
29-
pub static LAYER6_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&LAYER6_CONFIG_PATH));
50+
/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
51+
/// [`Layer-1`][LayerId::Layer1].
52+
pub static LAYER1_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER1_CONFIG_PATH));
53+
54+
/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
55+
/// [`Layer-2`][LayerId::Layer2].
56+
pub static LAYER2_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER2_CONFIG_PATH));
57+
58+
/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
59+
/// [`Layer-3`][LayerId::Layer3].
60+
pub static LAYER3_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER3_CONFIG_PATH));
3061

31-
pub static ZKEVM_DEGREES: LazyLock<Vec<u32>> = LazyLock::new(|| {
62+
/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
63+
/// [`Layer-4`][LayerId::Layer4].
64+
pub static LAYER4_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER4_CONFIG_PATH));
65+
66+
/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
67+
/// [`Layer-5`][LayerId::Layer5].
68+
pub static LAYER5_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER5_CONFIG_PATH));
69+
70+
/// The degree (k) for the halo2 [`Circuit`][halo2_proofs::plonk::Circuit] at
71+
/// [`Layer-6`][LayerId::Layer6].
72+
pub static LAYER6_DEGREE: LazyLock<u32> = LazyLock::new(|| layer_degree(&*LAYER6_CONFIG_PATH));
73+
74+
/// The list of degrees for Inner, Layer-1 and Layer-2, i.e. the proof generation [`layers`][LayerId]
75+
/// covered by the [`ChunkProver`][crate::ChunkProver].
76+
pub static CHUNK_PROVER_DEGREES: LazyLock<Vec<u32>> = LazyLock::new(|| {
3277
Vec::from_iter(HashSet::from([
3378
*INNER_DEGREE,
3479
*LAYER1_DEGREE,
3580
*LAYER2_DEGREE,
3681
]))
3782
});
3883

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

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

72120
impl LayerId {
121+
/// Returns the identifier by layer.
73122
pub fn id(&self) -> &str {
74123
match self {
75124
Self::Inner => "inner",
@@ -82,6 +131,7 @@ impl LayerId {
82131
}
83132
}
84133

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

97-
pub fn config_path(&self) -> &str {
147+
/// The path to the [`Config Parameters`][aggregator::ConfigParams] used to configure the shape
148+
/// of the [`Circuit`][halo2_proofs::plonk::Circuit].
149+
pub fn config_path(&self) -> PathBuf {
98150
match self {
99-
Self::Layer1 => &LAYER1_CONFIG_PATH,
100-
Self::Layer2 => &LAYER2_CONFIG_PATH,
101-
Self::Layer3 => &LAYER3_CONFIG_PATH,
102-
Self::Layer4 => &LAYER4_CONFIG_PATH,
103-
Self::Layer5 => &LAYER5_CONFIG_PATH,
104-
Self::Layer6 => &LAYER6_CONFIG_PATH,
151+
Self::Layer1 => LAYER1_CONFIG_PATH.to_path_buf(),
152+
Self::Layer2 => LAYER2_CONFIG_PATH.to_path_buf(),
153+
Self::Layer3 => LAYER3_CONFIG_PATH.to_path_buf(),
154+
Self::Layer4 => LAYER4_CONFIG_PATH.to_path_buf(),
155+
Self::Layer5 => LAYER5_CONFIG_PATH.to_path_buf(),
156+
Self::Layer6 => LAYER6_CONFIG_PATH.to_path_buf(),
105157
Self::Inner => unreachable!("No config file for super (inner) circuit"),
106158
}
107159
}
108160
}
109161

110-
pub fn asset_file_path(filename: &str) -> String {
111-
Path::new(&*ASSETS_DIR)
112-
.join(filename)
113-
.to_string_lossy()
114-
.into_owned()
115-
}
116-
117-
pub fn layer_config_path(id: &str) -> &str {
162+
/// Returns the path to the [`Config Parameters`][aggregator::ConfigParams] that configure the
163+
/// shape of the [`Circuit`][halo2_proofs::plonk::Circuit] given the [`id`][LayerId::id] of the
164+
/// layer.
165+
pub fn layer_config_path(id: &str) -> PathBuf {
118166
match id {
119-
"layer1" => &LAYER1_CONFIG_PATH,
120-
"layer2" => &LAYER2_CONFIG_PATH,
121-
"layer3" => &LAYER3_CONFIG_PATH,
122-
"layer4" => &LAYER4_CONFIG_PATH,
123-
"layer5" => &LAYER5_CONFIG_PATH,
124-
"layer6" => &LAYER6_CONFIG_PATH,
167+
"layer1" => LAYER1_CONFIG_PATH.to_path_buf(),
168+
"layer2" => LAYER2_CONFIG_PATH.to_path_buf(),
169+
"layer3" => LAYER3_CONFIG_PATH.to_path_buf(),
170+
"layer4" => LAYER4_CONFIG_PATH.to_path_buf(),
171+
"layer5" => LAYER5_CONFIG_PATH.to_path_buf(),
172+
"layer6" => LAYER6_CONFIG_PATH.to_path_buf(),
125173
_ => panic!("Wrong id-{id} to get layer config path"),
126174
}
127175
}
128176

129-
fn layer_degree(config_file: &str) -> u32 {
130-
let f = File::open(config_file).unwrap_or_else(|_| panic!("Failed to open {config_file}"));
177+
fn asset_file_path(filename: &str) -> PathBuf {
178+
ASSETS_DIR.join(filename)
179+
}
180+
181+
fn layer_degree<P: AsRef<Path> + fmt::Debug>(path: P) -> u32 {
182+
let f = File::open(&path).unwrap_or_else(|_| panic!("Failed to open {path:?}"));
131183

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

135187
params.degree
136188
}

prover/src/consts.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::utils::read_env_var;
21
use std::sync::LazyLock;
32

3+
use crate::utils::read_env_var;
4+
45
// TODO: is it a good design to use LazyLock? Why not read env var each time?
56

67
pub fn bundle_vk_filename() -> String {

prover/src/evm.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
use crate::{io::write_file, EvmProof};
1+
use std::{path::PathBuf, str::FromStr};
2+
23
use halo2_proofs::{
34
halo2curves::bn256::{Bn256, Fr, G1Affine},
45
plonk::VerifyingKey,
56
poly::kzg::commitment::ParamsKZG,
67
};
8+
use revm::{
9+
primitives::{CreateScheme, ExecutionResult, Output, TransactTo, TxEnv},
10+
InMemoryDB, EVM,
11+
};
712
use snark_verifier::pcs::kzg::{Bdfg21, Kzg};
813
use snark_verifier_sdk::CircuitExt;
9-
use std::{path::PathBuf, str::FromStr};
14+
15+
use crate::{io::write_file, EvmProof};
1016

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

43-
use revm::{
44-
primitives::{CreateScheme, ExecutionResult, Output, TransactTo, TxEnv},
45-
InMemoryDB, EVM,
46-
};
47-
4849
/// Deploy contract and then call with calldata.
4950
/// Returns gas_used of call to deployed contract if both transactions are successful.
5051
pub fn deploy_and_call(deployment_code: Vec<u8>, calldata: Vec<u8>) -> Result<u64, String> {

prover/src/proof/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ impl Proof {
7474
let instances = self.instances();
7575
let proof = self.proof().to_vec();
7676
let calldata = snark_verifier::loader::evm::encode_calldata(&instances, &proof);
77-
let result = crate::evm::deploy_and_call(deployment_code, calldata);
78-
result.is_ok()
77+
crate::evm::deploy_and_call(deployment_code, calldata).is_ok()
7978
}
8079

8180
pub fn instances(&self) -> Vec<Vec<Fr>> {

prover/src/test/batch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::{LazyLock, Mutex};
22

33
use crate::{
44
aggregator::{Prover, Verifier},
5-
config::{LayerId, AGG_DEGREES},
5+
config::{LayerId, BATCH_PROVER_DEGREES},
66
consts::DEPLOYMENT_CODE_FILENAME,
77
io::force_to_read,
88
types::BundleProvingTask,
@@ -12,7 +12,7 @@ use crate::{
1212

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

1818
static BATCH_PROVER: LazyLock<Mutex<Prover>> = LazyLock::new(|| {

prover/src/test/chunk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::sync::{LazyLock, Mutex};
22

33
use crate::{
4-
config::ZKEVM_DEGREES,
4+
config::CHUNK_PROVER_DEGREES,
55
utils::read_env_var,
66
zkevm::{Prover, Verifier},
77
ChunkProof, ChunkProvingTask, ParamsMap,
88
};
99

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

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

0 commit comments

Comments
 (0)