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

Commit b621bce

Browse files
committed
clean up + refactor
1 parent dc52147 commit b621bce

File tree

7 files changed

+110
-82
lines changed

7 files changed

+110
-82
lines changed

aggregator/src/aggregation.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,43 @@ pub(crate) use rlc::RlcConfig;
2525

2626
pub use circuit::BatchCircuit;
2727
pub use config::BatchCircuitConfig;
28+
use halo2_base::halo2_proofs::halo2curves::bn256::{Fr, G1Affine};
29+
use snark_verifier::Protocol;
30+
31+
/// Alias for a list of G1 points.
32+
pub type PreprocessedPolyCommits = Vec<G1Affine>;
33+
/// Alias for the transcript's initial state.
34+
pub type TranscriptInitState = Fr;
35+
36+
/// Alias for the fixed part of the protocol which consists of the commitments to the preprocessed
37+
/// polynomials and the initial state of the transcript.
38+
#[derive(Clone)]
39+
pub struct FixedProtocol {
40+
/// The commitments to the preprocessed polynomials.
41+
pub preprocessed: PreprocessedPolyCommits,
42+
/// The initial state of the transcript.
43+
pub init_state: TranscriptInitState,
44+
}
45+
46+
impl From<Protocol<G1Affine>> for FixedProtocol {
47+
fn from(protocol: Protocol<G1Affine>) -> Self {
48+
Self {
49+
preprocessed: protocol.preprocessed,
50+
init_state: protocol
51+
.transcript_initial_state
52+
.expect("protocol transcript init state None"),
53+
}
54+
}
55+
}
56+
57+
impl From<&Protocol<G1Affine>> for FixedProtocol {
58+
fn from(protocol: &Protocol<G1Affine>) -> Self {
59+
Self {
60+
preprocessed: protocol.preprocessed.clone(),
61+
init_state: protocol
62+
.transcript_initial_state
63+
.clone()
64+
.expect("protocol transcript init state None"),
65+
}
66+
}
67+
}

aggregator/src/aggregation/circuit.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
aggregation::decoder::WORKED_EXAMPLE,
33
blob::BatchData,
44
witgen::{zstd_encode, MultiBlockProcessResult},
5-
LOG_DEGREE, PI_CHAIN_ID, PI_CURRENT_BATCH_HASH, PI_CURRENT_STATE_ROOT,
5+
FixedProtocol, LOG_DEGREE, PI_CHAIN_ID, PI_CURRENT_BATCH_HASH, PI_CURRENT_STATE_ROOT,
66
PI_CURRENT_WITHDRAW_ROOT, PI_PARENT_BATCH_HASH, PI_PARENT_STATE_ROOT,
77
};
88
use ark_std::{end_timer, start_timer};
@@ -47,7 +47,7 @@ use zkevm_circuits::util::Challenges;
4747
use crate::{
4848
aggregation::witgen::process,
4949
batch::BatchHash,
50-
constants::{ACC_LEN, DIGEST_LEN, FIXED_PROTOCOL_HALO2, FIXED_PROTOCOL_SP1},
50+
constants::{ACC_LEN, DIGEST_LEN},
5151
core::{assign_batch_hashes, extract_proof_and_instances_with_pairing_check},
5252
util::parse_hash_digest_cells,
5353
AssignedBarycentricEvaluationConfig, ConfigParams,
@@ -76,14 +76,21 @@ pub struct BatchCircuit<const N_SNARKS: usize> {
7676
// batch hash circuit for which the snarks are generated
7777
// the chunks in this batch are also padded already
7878
pub batch_hash: BatchHash<N_SNARKS>,
79+
80+
/// The SNARK protocol from the halo2-based inner circuit route.
81+
pub halo2_protocol: FixedProtocol,
82+
/// The SNARK protocol from the sp1-based inner circuit route.
83+
pub sp1_protocol: FixedProtocol,
7984
}
8085

8186
impl<const N_SNARKS: usize> BatchCircuit<N_SNARKS> {
82-
pub fn new(
87+
pub fn new<P: Into<FixedProtocol>>(
8388
params: &ParamsKZG<Bn256>,
8489
snarks_with_padding: &[Snark],
8590
rng: impl Rng + Send,
8691
batch_hash: BatchHash<N_SNARKS>,
92+
halo2_protocol: P,
93+
sp1_protocol: P,
8794
) -> Result<Self, snark_verifier::Error> {
8895
let timer = start_timer!(|| "generate aggregation circuit");
8996

@@ -141,6 +148,8 @@ impl<const N_SNARKS: usize> BatchCircuit<N_SNARKS> {
141148
flattened_instances,
142149
as_proof: Value::known(as_proof),
143150
batch_hash,
151+
halo2_protocol: halo2_protocol.into(),
152+
sp1_protocol: sp1_protocol.into(),
144153
})
145154
}
146155

@@ -304,9 +313,7 @@ impl<const N_SNARKS: usize> Circuit<Fr> for BatchCircuit<N_SNARKS> {
304313
log::trace!("{}-th instance: {:?}", i, e.value)
305314
}
306315

307-
loader
308-
.ctx_mut()
309-
.print_stats(&["snark aggregation"]);
316+
loader.ctx_mut().print_stats(&["snark aggregation"]);
310317

311318
let mut ctx = Rc::into_inner(loader).unwrap().into_ctx();
312319

@@ -318,11 +325,8 @@ impl<const N_SNARKS: usize> Circuit<Fr> for BatchCircuit<N_SNARKS> {
318325
log::info!("populating constants");
319326
let mut preprocessed_polys_halo2 = Vec::with_capacity(7);
320327
let mut preprocessed_polys_sp1 = Vec::with_capacity(7);
321-
let (fixed_preprocessed_polys_halo2, fixed_transcript_init_state_halo2) =
322-
FIXED_PROTOCOL_HALO2.clone();
323-
let (fixed_preprocessed_polys_sp1, fixed_transcript_init_state_sp1) =
324-
FIXED_PROTOCOL_SP1.clone();
325-
for (i, &preprocessed_poly) in fixed_preprocessed_polys_halo2.iter().enumerate()
328+
for (i, &preprocessed_poly) in
329+
self.halo2_protocol.preprocessed.iter().enumerate()
326330
{
327331
log::debug!("load const {i}");
328332
preprocessed_polys_halo2.push(
@@ -332,7 +336,8 @@ impl<const N_SNARKS: usize> Circuit<Fr> for BatchCircuit<N_SNARKS> {
332336
);
333337
log::debug!("load const {i} OK");
334338
}
335-
for (i, &preprocessed_poly) in fixed_preprocessed_polys_sp1.iter().enumerate() {
339+
for (i, &preprocessed_poly) in self.sp1_protocol.preprocessed.iter().enumerate()
340+
{
336341
log::debug!("load const (sp1) {i}");
337342
preprocessed_polys_sp1.push(
338343
config
@@ -346,15 +351,15 @@ impl<const N_SNARKS: usize> Circuit<Fr> for BatchCircuit<N_SNARKS> {
346351
.field_chip()
347352
.range()
348353
.gate()
349-
.assign_constant(&mut ctx, fixed_transcript_init_state_halo2)
354+
.assign_constant(&mut ctx, self.halo2_protocol.init_state)
350355
.expect("IntegerInstructions::assign_constant infallible");
351356
log::debug!("load transcript OK");
352357
let transcript_init_state_sp1 = config
353358
.ecc_chip()
354359
.field_chip()
355360
.range()
356361
.gate()
357-
.assign_constant(&mut ctx, fixed_transcript_init_state_sp1)
362+
.assign_constant(&mut ctx, self.sp1_protocol.init_state)
358363
.expect("IntegerInstructions::assign_constant infallible");
359364
log::info!("populating constants OK");
360365

aggregator/src/constants.rs

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use halo2_proofs::halo2curves::bn256::{Fr, G1Affine};
2-
use std::sync::LazyLock;
3-
41
// A chain_id is u64 and uses 8 bytes
52
pub(crate) const CHAIN_ID_LEN: usize = 8;
63

@@ -88,61 +85,3 @@ pub(crate) const BITS: usize = 88;
8885
/// If the input size is less than this, dummy snarks
8986
/// will be padded.
9087
pub const MAX_AGG_SNARKS: usize = 45;
91-
92-
/// Alias for a list of G1 points.
93-
type PreprocessedPolyCommits = Vec<G1Affine>;
94-
/// Alias for the transcript's initial state.
95-
type TranscriptInitState = Fr;
96-
/// Alias for the fixed part of the protocol which consists of the commitments to the preprocessed
97-
/// polynomials and the initial state of the transcript.
98-
type FixedProtocol = (PreprocessedPolyCommits, TranscriptInitState);
99-
100-
/// The [`Batch Circuit`] supports aggregation of up to [`MAX_AGG_SNARKS`] SNARKs, where either
101-
/// SNARK is of 2 kinds, namely:
102-
///
103-
/// 1. halo2-based [`SuperCircuit`] -> [`CompressionCircuit`] (wide) -> `CompressionCircuit` (thin)
104-
/// 2. sp1-based STARK -> halo2-based backend -> `CompressionCircuit` (thin)
105-
///
106-
/// For each SNARK witness provided for aggregation, we require that the commitments to the
107-
/// preprocessed polynomials and the transcript's initial state belong to a fixed set, one
108-
/// belonging to each of the above SNARK kinds.
109-
///
110-
/// Represents the fixed commitments to the preprocessed polynomials and the initial state of the
111-
/// transcript for [`ChunkKind::Halo2`].
112-
pub static FIXED_PROTOCOL_HALO2: LazyLock<FixedProtocol> = LazyLock::new(|| {
113-
let name =
114-
std::env::var("HALO2_CHUNK_PROTOCOL").unwrap_or("chunk_chunk_halo2.protocol".to_string());
115-
let dir =
116-
std::env::var("SCROLL_PROVER_ASSETS_DIR").unwrap_or("./tests/test_assets".to_string());
117-
let path = std::path::Path::new(&dir).join(name);
118-
let file = std::fs::File::open(&path).expect("could not open file");
119-
let reader = std::io::BufReader::new(file);
120-
let protocol: snark_verifier::Protocol<G1Affine> =
121-
serde_json::from_reader(reader).expect("could not deserialise protocol");
122-
(
123-
protocol.preprocessed,
124-
protocol
125-
.transcript_initial_state
126-
.expect("transcript initial state is None"),
127-
)
128-
});
129-
130-
/// Represents the fixed commitments to the preprocessed polynomials and the initial state of the
131-
/// transcript for [`ChunkKind::Sp1`].
132-
pub static FIXED_PROTOCOL_SP1: LazyLock<FixedProtocol> = LazyLock::new(|| {
133-
let name =
134-
std::env::var("SP1_CHUNK_PROTOCOL").unwrap_or("chunk_chunk_sp1.protocol".to_string());
135-
let dir =
136-
std::env::var("SCROLL_PROVER_ASSETS_DIR").unwrap_or("./tests/test_assets".to_string());
137-
let path = std::path::Path::new(&dir).join(name);
138-
let file = std::fs::File::open(&path).expect("could not open file");
139-
let reader = std::io::BufReader::new(file);
140-
let protocol: snark_verifier::Protocol<G1Affine> =
141-
serde_json::from_reader(reader).expect("could not deserialise protocol");
142-
(
143-
protocol.preprocessed,
144-
protocol
145-
.transcript_initial_state
146-
.expect("transcript initial state is None"),
147-
)
148-
});

aggregator/src/tests/aggregation.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ fn build_new_batch_circuit<const N_SNARKS: usize>(
181181
})
182182
.collect_vec()
183183
};
184+
let snark_protocol = real_snarks[0].protocol.clone();
184185

185186
// ==========================
186187
// padded chunks
@@ -198,6 +199,8 @@ fn build_new_batch_circuit<const N_SNARKS: usize>(
198199
[real_snarks, padded_snarks].concat().as_ref(),
199200
rng,
200201
batch_hash,
202+
&snark_protocol,
203+
&snark_protocol,
201204
)
202205
.unwrap()
203206
}
@@ -266,6 +269,8 @@ fn build_batch_circuit_skip_encoding<const N_SNARKS: usize>() -> BatchCircuit<N_
266269
})
267270
.collect_vec()
268271
};
272+
let snark_protocol = real_snarks[0].protocol.clone();
273+
269274
// ==========================
270275
// padded chunks
271276
// ==========================
@@ -275,6 +280,8 @@ fn build_batch_circuit_skip_encoding<const N_SNARKS: usize>() -> BatchCircuit<N_
275280
[real_snarks, padded_snarks].concat().as_ref(),
276281
rng,
277282
batch_hash,
283+
&snark_protocol,
284+
&snark_protocol,
278285
)
279286
.unwrap()
280287
}

prover/src/aggregator/prover.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ impl<'params> Prover<'params> {
240240
LayerId::Layer3.id(),
241241
LayerId::Layer3.degree(),
242242
batch_info,
243+
&self.halo2_protocol,
244+
&self.sp1_protocol,
243245
&layer2_snarks,
244246
output_dir,
245247
)?;

prover/src/common/prover/aggregation.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
};
77
use aggregator::{BatchCircuit, BatchHash};
88
use anyhow::{anyhow, Result};
9+
use halo2_proofs::halo2curves::bn256::G1Affine;
910
use rand::Rng;
1011
use snark_verifier_sdk::Snark;
1112
use std::env;
@@ -17,13 +18,26 @@ impl<'params> Prover<'params> {
1718
degree: u32,
1819
mut rng: impl Rng + Send,
1920
batch_info: BatchHash<N_SNARKS>,
21+
halo2_protocol: &[u8],
22+
sp1_protocol: &[u8],
2023
previous_snarks: &[Snark],
2124
) -> Result<Snark> {
2225
env::set_var("AGGREGATION_CONFIG", layer_config_path(id));
2326

24-
let circuit: BatchCircuit<N_SNARKS> =
25-
BatchCircuit::new(self.params(degree), previous_snarks, &mut rng, batch_info)
26-
.map_err(|err| anyhow!("Failed to construct aggregation circuit: {err:?}"))?;
27+
let halo2_protocol =
28+
serde_json::from_slice::<snark_verifier::Protocol<G1Affine>>(halo2_protocol)?;
29+
let sp1_protocol =
30+
serde_json::from_slice::<snark_verifier::Protocol<G1Affine>>(sp1_protocol)?;
31+
32+
let circuit: BatchCircuit<N_SNARKS> = BatchCircuit::new(
33+
self.params(degree),
34+
previous_snarks,
35+
&mut rng,
36+
batch_info,
37+
halo2_protocol,
38+
sp1_protocol,
39+
)
40+
.map_err(|err| anyhow!("Failed to construct aggregation circuit: {err:?}"))?;
2741

2842
self.gen_snark(id, degree, &mut rng, circuit, "gen_agg_snark")
2943
}
@@ -34,6 +48,8 @@ impl<'params> Prover<'params> {
3448
id: &str,
3549
degree: u32,
3650
batch_info: BatchHash<N_SNARKS>,
51+
halo2_protocol: &[u8],
52+
sp1_protocol: &[u8],
3753
previous_snarks: &[Snark],
3854
output_dir: Option<&str>,
3955
) -> Result<Snark> {
@@ -48,7 +64,15 @@ impl<'params> Prover<'params> {
4864
Some(snark) => Ok(snark),
4965
None => {
5066
let rng = gen_rng();
51-
let result = self.gen_agg_snark(id, degree, rng, batch_info, previous_snarks);
67+
let result = self.gen_agg_snark(
68+
id,
69+
degree,
70+
rng,
71+
batch_info,
72+
halo2_protocol,
73+
sp1_protocol,
74+
previous_snarks,
75+
);
5276
if let (Some(_), Ok(snark)) = (output_dir, &result) {
5377
write_snark(&file_path, snark);
5478
}

prover/src/consts.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@ pub fn chunk_vk_filename() -> String {
1313
read_env_var("CHUNK_VK_FILENAME", "vk_chunk.vkey".to_string())
1414
}
1515

16-
pub static CHUNK_PROTOCOL_FILENAME: LazyLock<String> =
17-
LazyLock::new(|| read_env_var("CHUNK_PROTOCOL_FILENAME", "chunk.protocol".to_string()));
18-
16+
/// The file descriptor for the JSON serialised SNARK [`protocol`][protocol] that
17+
/// defines the [`CompressionCircuit`][compr_circuit] SNARK that uses halo2-based
18+
/// [`SuperCircuit`][super_circuit].
19+
///
20+
/// [protocol]: snark_verifier::Protocol
21+
/// [compr_circuit]: aggregator::CompressionCircuit
22+
/// [super_circuit]: zkevm_circuits::super_circuit::SuperCircuit
1923
pub static FD_HALO2_CHUNK_PROTOCOL: LazyLock<String> =
2024
LazyLock::new(|| read_env_var("HALO2_CHUNK_PROTOCOL", "chunk_halo2.protocol".to_string()));
25+
26+
/// The file descriptor for the JSON serialised SNARK [`protocol`][protocol] that
27+
/// defines the [`CompressionCircuit`][compr_circuit] SNARK that uses sp1-based
28+
/// STARK that is SNARKified using a halo2-backend.
29+
///
30+
/// [protocol]: snark_verifier::Protocol
31+
/// [compr_circuit]: aggregator::CompressionCircuit
2132
pub static FD_SP1_CHUNK_PROTOCOL: LazyLock<String> =
2233
LazyLock::new(|| read_env_var("SP1_CHUNK_PROTOCOL", "chunk_sp1.protocol".to_string()));
2334

0 commit comments

Comments
 (0)