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

Commit 667ace4

Browse files
committed
layer-2 compress only (sp1 chunk proof)
1 parent 1beeb35 commit 667ace4

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

prover/src/proof/chunk.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use snark_verifier::Protocol;
77
use snark_verifier_sdk::Snark;
88

99
/// The innermost SNARK belongs to the following variants.
10-
#[derive(Clone, Debug, Deserialize, Serialize)]
10+
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
1111
pub enum ChunkKind {
1212
/// halo2-based SuperCircuit.
1313
Halo2,
@@ -100,13 +100,13 @@ impl ChunkProof {
100100
dump_as_json(dir, &filename, &self)
101101
}
102102

103-
pub fn to_snark(self) -> Snark {
103+
pub fn to_snark(&self) -> Snark {
104104
let instances = self.proof.instances();
105105
let protocol = serde_json::from_slice::<Protocol<G1Affine>>(&self.protocol).unwrap();
106106

107107
Snark {
108108
protocol,
109-
proof: self.proof.proof,
109+
proof: self.proof.proof.clone(),
110110
instances,
111111
}
112112
}

prover/src/test/chunk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn chunk_prove(desc: &str, chunk: ChunkProvingTask) -> ChunkProof {
3030
let mut prover = CHUNK_PROVER.lock().expect("poisoned chunk-prover");
3131

3232
let proof = prover
33-
.gen_chunk_proof(chunk, None, None, None)
33+
.gen_halo2_chunk_proof(chunk, None, None, None)
3434
.unwrap_or_else(|err| panic!("{desc}: failed to generate chunk snark: {err}"));
3535
log::info!("{desc}: generated chunk proof");
3636

@@ -41,7 +41,7 @@ pub fn chunk_prove(desc: &str, chunk: ChunkProvingTask) -> ChunkProof {
4141
verifier
4242
};
4343

44-
let verified = verifier.verify_chunk_proof(proof.clone());
44+
let verified = verifier.verify_chunk_proof(&proof);
4545
assert!(verified, "{desc}: failed to verify chunk snark");
4646

4747
log::info!("{desc}: chunk-prove END");

prover/src/types.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ pub struct BlockTraceJsonRpcResult {
1111
}
1212
pub use eth_types::base64;
1313

14-
use crate::{BatchProof, ChunkKind, ChunkProof};
14+
use crate::{BatchProof, ChunkProof};
1515

1616
#[derive(Debug, Clone, Deserialize, Serialize)]
1717
pub struct ChunkProvingTask {
1818
/// Prover can check `chunk_info` is consistent with block traces
1919
pub chunk_info: Option<ChunkInfo>,
2020
pub block_traces: Vec<BlockTrace>,
21-
pub chunk_kind: ChunkKind,
2221
}
2322

2423
impl ChunkProvingTask {
25-
pub fn new(block_traces: Vec<BlockTrace>, chunk_kind: ChunkKind) -> Self {
24+
pub fn new(block_traces: Vec<BlockTrace>) -> Self {
2625
Self {
2726
block_traces,
2827
chunk_info: None,
29-
chunk_kind,
3028
}
3129
}
3230
pub fn is_empty(&self) -> bool {

prover/src/zkevm/prover.rs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ use crate::{
1111
circuit::{calculate_row_usage_of_witness_block, chunk_trace_to_witness_block},
1212
ChunkProverError, RowUsage,
1313
},
14-
ChunkProof,
14+
ChunkKind, ChunkProof,
1515
};
1616
use aggregator::ChunkInfo;
1717
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
18+
use snark_verifier_sdk::Snark;
1819

1920
#[derive(Debug)]
2021
pub struct Prover<'params> {
@@ -71,7 +72,7 @@ impl<'params> Prover<'params> {
7172
/// If it is not set, default value(first block number of this chuk) will be used.
7273
/// id:
7374
/// TODO(zzhang). clean this. I think it can only be None or Some(0)...
74-
pub fn gen_chunk_proof(
75+
pub fn gen_halo2_chunk_proof(
7576
&mut self,
7677
chunk: ChunkProvingTask,
7778
chunk_id: Option<&str>,
@@ -114,7 +115,7 @@ impl<'params> Prover<'params> {
114115
snark,
115116
self.prover_impl.pk(LayerId::Layer2.id()),
116117
chunk_info,
117-
chunk.chunk_kind,
118+
ChunkKind::Halo2,
118119
sub_circuit_row_usages,
119120
);
120121

@@ -128,7 +129,72 @@ impl<'params> Prover<'params> {
128129
});
129130

130131
if let Some(verifier) = &self.verifier {
131-
if !verifier.verify_chunk_proof(chunk_proof.clone()) {
132+
if !verifier.verify_chunk_proof(&chunk_proof) {
133+
return Err(String::from("chunk proof verification failed").into());
134+
}
135+
log::info!("chunk proof verified OK");
136+
}
137+
138+
Ok(chunk_proof)
139+
}
140+
141+
/// Generates a chunk proof by compressing the provided SNARK. The generated proof uses the
142+
/// [`CompressionCircuit`][aggregator::CompressionCircuit] to compress the supplied
143+
/// [`SNARK`][snark_verifier_sdk::Snark] only once using thin-compression parameters.
144+
///
145+
/// The [`ChunkProof`] represents the Layer-2 proof in Scroll's proving pipeline and the
146+
/// generated SNARK can then be used as inputs to the [`BatchCircuit`][aggregator::BatchCircuit].
147+
///
148+
/// This method should be used iff the input SNARK was generated from a halo2-backend for Sp1.
149+
/// In order to construct a chunk proof via the halo2-based
150+
/// [`SuperCircuit`][zkevm_circuits::super_circuit::SuperCircuit], please use [`gen_chunk_proof`][Self::gen_chunk_proof].
151+
pub fn gen_sp1_chunk_proof(
152+
&mut self,
153+
inner_snark: Snark,
154+
chunk: ChunkProvingTask,
155+
chunk_id: Option<&str>,
156+
output_dir: Option<&str>,
157+
) -> Result<ChunkProof, ChunkProverError> {
158+
assert!(!chunk.is_empty());
159+
160+
let chunk_id = chunk_id.map_or_else(|| chunk.identifier(), |name| name.to_string());
161+
162+
let snark = self
163+
.prover_impl
164+
.load_or_gen_comp_snark(
165+
&chunk_id,
166+
LayerId::Layer2.id(),
167+
true,
168+
LayerId::Layer2.degree(),
169+
inner_snark,
170+
output_dir,
171+
)
172+
.map_err(|e| ChunkProverError::Custom(e.to_string()))?;
173+
174+
self.check_vk();
175+
176+
let chunk_info = chunk.chunk_info.unwrap_or({
177+
let witness_block = chunk_trace_to_witness_block(chunk.block_traces)?;
178+
ChunkInfo::from_witness_block(&witness_block, false)
179+
});
180+
181+
let chunk_proof = ChunkProof::new(
182+
snark,
183+
self.prover_impl.pk(LayerId::Layer2.id()),
184+
chunk_info,
185+
ChunkKind::Sp1,
186+
vec![], // no row usages for ChunkKind::Sp1
187+
)
188+
.map_err(|e| ChunkProverError::Custom(e.to_string()))?;
189+
190+
if let Some(output_dir) = output_dir {
191+
chunk_proof
192+
.dump(output_dir, &chunk_id)
193+
.map_err(|e| ChunkProverError::Custom(e.to_string()))?;
194+
}
195+
196+
if let Some(verifier) = &self.verifier {
197+
if !verifier.verify_chunk_proof(&chunk_proof) {
132198
return Err(String::from("chunk proof verification failed").into());
133199
}
134200
log::info!("chunk proof verified OK");

prover/src/zkevm/verifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'params> Verifier<'params> {
4141
verifier.into()
4242
}
4343

44-
pub fn verify_chunk_proof(&self, proof: ChunkProof) -> bool {
44+
pub fn verify_chunk_proof(&self, proof: &ChunkProof) -> bool {
4545
self.inner.verify_snark(proof.to_snark())
4646
}
4747
}

0 commit comments

Comments
 (0)