Skip to content

Commit 4a8eb78

Browse files
authored
(libzkp): refactor batch header variants (#1755)
1 parent f064832 commit 4a8eb78

File tree

8 files changed

+63
-61
lines changed

8 files changed

+63
-61
lines changed

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ repository = "https://github.com/scroll-tech/scroll"
1717
version = "4.7.1"
1818

1919
[workspace.dependencies]
20-
scroll-zkvm-prover = { git = "https://github.com/scroll-tech/zkvm-prover", branch = "feat/galileo" }
21-
scroll-zkvm-verifier = { git = "https://github.com/scroll-tech/zkvm-prover", branch = "feat/galileo" }
22-
scroll-zkvm-types = { git = "https://github.com/scroll-tech/zkvm-prover", branch = "feat/galileo" }
20+
scroll-zkvm-prover = { git = "https://github.com/scroll-tech/zkvm-prover", tag = "v0.7.0-rc.4" }
21+
scroll-zkvm-verifier = { git = "https://github.com/scroll-tech/zkvm-prover", tag = "v0.7.0-rc.4" }
22+
scroll-zkvm-types = { git = "https://github.com/scroll-tech/zkvm-prover", tag = "v0.7.0-rc.4" }
2323

2424
sbv-primitives = { git = "https://github.com/scroll-tech/stateless-block-verifier", tag = "scroll-v91", features = ["scroll", "rkyv"] }
2525
sbv-utils = { git = "https://github.com/scroll-tech/stateless-block-verifier", tag = "scroll-v91" }

crates/libzkp/src/lib.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use verifier::{TaskType, VerifierConfig};
77
mod utils;
88

99
use sbv_primitives::B256;
10-
use scroll_zkvm_types::utils::vec_as_base64;
10+
use scroll_zkvm_types::{utils::vec_as_base64, version::Version};
1111
use serde::{Deserialize, Serialize};
1212
use serde_json::value::RawValue;
1313
use std::{collections::HashMap, path::Path, sync::OnceLock};
@@ -138,35 +138,56 @@ pub fn gen_universal_task(
138138
let mut task = serde_json::from_str::<ChunkProvingTask>(task_json)?;
139139
// normailze fork name field in task
140140
task.fork_name = task.fork_name.to_lowercase();
141+
let version = Version::from(task.version);
141142
// always respect the fork_name_str (which has been normalized) being passed
142143
// if the fork_name wrapped in task is not match, consider it a malformed task
143144
if fork_name_str != task.fork_name.as_str() {
144145
eyre::bail!("fork name in chunk task not match the calling arg, expected {fork_name_str}, get {}", task.fork_name);
145146
}
147+
if fork_name_str != version.fork.as_str() {
148+
eyre::bail!(
149+
"given task version, expected fork={fork_name_str}, got={version_fork}",
150+
version_fork = version.fork.as_str()
151+
);
152+
}
146153
let (pi_hash, metadata, u_task) =
147-
utils::panic_catch(move || gen_universal_chunk_task(task, fork_name_str.into()))
154+
utils::panic_catch(move || gen_universal_chunk_task(task))
148155
.map_err(|e| eyre::eyre!("caught panic in chunk task{e}"))??;
149156
(pi_hash, AnyMetaData::Chunk(metadata), u_task)
150157
}
151158
x if x == TaskType::Batch as i32 => {
152159
let mut task = serde_json::from_str::<BatchProvingTask>(task_json)?;
153160
task.fork_name = task.fork_name.to_lowercase();
161+
let version = Version::from(task.version);
154162
if fork_name_str != task.fork_name.as_str() {
155163
eyre::bail!("fork name in batch task not match the calling arg, expected {fork_name_str}, get {}", task.fork_name);
156164
}
165+
if fork_name_str != version.fork.as_str() {
166+
eyre::bail!(
167+
"given task version, expected fork={fork_name_str}, got={version_fork}",
168+
version_fork = version.fork.as_str()
169+
);
170+
}
157171
let (pi_hash, metadata, u_task) =
158-
utils::panic_catch(move || gen_universal_batch_task(task, fork_name_str.into()))
172+
utils::panic_catch(move || gen_universal_batch_task(task))
159173
.map_err(|e| eyre::eyre!("caught panic in chunk task{e}"))??;
160174
(pi_hash, AnyMetaData::Batch(metadata), u_task)
161175
}
162176
x if x == TaskType::Bundle as i32 => {
163177
let mut task = serde_json::from_str::<BundleProvingTask>(task_json)?;
164178
task.fork_name = task.fork_name.to_lowercase();
179+
let version = Version::from(task.version);
165180
if fork_name_str != task.fork_name.as_str() {
166181
eyre::bail!("fork name in bundle task not match the calling arg, expected {fork_name_str}, get {}", task.fork_name);
167182
}
183+
if fork_name_str != version.fork.as_str() {
184+
eyre::bail!(
185+
"given task version, expected fork={fork_name_str}, got={version_fork}",
186+
version_fork = version.fork.as_str()
187+
);
188+
}
168189
let (pi_hash, metadata, u_task) =
169-
utils::panic_catch(move || gen_universal_bundle_task(task, fork_name_str.into()))
190+
utils::panic_catch(move || gen_universal_bundle_task(task))
170191
.map_err(|e| eyre::eyre!("caught panic in chunk task{e}"))??;
171192
(pi_hash, AnyMetaData::Bundle(metadata), u_task)
172193
}
@@ -233,9 +254,11 @@ pub fn verifier_init(config: &str) -> eyre::Result<()> {
233254
);
234255
(
235256
config.fork_name.to_lowercase(),
236-
config.features.as_ref()
237-
.map(|features| FeatureOptions::new(features.as_str()))
238-
.unwrap_or_default()
257+
config
258+
.features
259+
.as_ref()
260+
.map(|features| FeatureOptions::new(features.as_str()))
261+
.unwrap_or_default(),
239262
)
240263
})))
241264
.map_err(|c| eyre::eyre!("Fail to init additional features: {c:?}"))?;

crates/libzkp/src/proofs.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ impl ProofMetadata for ChunkProofMetadata {
140140
pub struct BatchProofMetadata {
141141
/// The batch information describing the list of chunks.
142142
pub batch_info: BatchInfo,
143-
/// The [`scroll_zkvm_types::batch::BatchHeader`]'s digest.
144-
pub batch_hash: B256,
145143
}
146144

147145
impl ProofMetadata for BatchProofMetadata {

crates/libzkp/src/tasks.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
utils::panic_catch,
1515
};
1616
use sbv_primitives::B256;
17-
use scroll_zkvm_types::public_inputs::{ForkName, MultiVersionPublicInputs, Version};
17+
use scroll_zkvm_types::public_inputs::{MultiVersionPublicInputs, Version};
1818

1919
fn encode_task_to_witness<T: serde::Serialize>(task: &T) -> eyre::Result<Vec<u8>> {
2020
let config = bincode::config::standard();
@@ -61,7 +61,6 @@ impl ProvintTaskExt {
6161
/// Generate required staff for chunk proving
6262
pub fn gen_universal_chunk_task(
6363
task: ChunkProvingTask,
64-
_: ForkName,
6564
) -> eyre::Result<(B256, ChunkProofMetadata, ProvingTask)> {
6665
let chunk_total_gas = task.stats().total_gas_used;
6766
let (chunk_info, pi_hash) = task.precheck_and_build_metadata()?;
@@ -79,29 +78,22 @@ pub fn gen_universal_chunk_task(
7978
/// Generate required staff for batch proving
8079
pub fn gen_universal_batch_task(
8180
task: BatchProvingTask,
82-
_: ForkName,
8381
) -> eyre::Result<(B256, BatchProofMetadata, ProvingTask)> {
84-
let (batch_info, batch_hash) = task.precheck_and_build_metadata()?;
82+
let (batch_info, batch_pi_hash) = task.precheck_and_build_metadata()?;
8583
let proving_task = task.try_into()?;
86-
8784
Ok((
88-
batch_hash,
89-
BatchProofMetadata {
90-
batch_info,
91-
batch_hash,
92-
},
85+
batch_pi_hash,
86+
BatchProofMetadata { batch_info },
9387
proving_task,
9488
))
9589
}
9690

9791
/// Generate required staff for bundle proving
9892
pub fn gen_universal_bundle_task(
9993
task: BundleProvingTask,
100-
_: ForkName,
10194
) -> eyre::Result<(B256, BundleProofMetadata, ProvingTask)> {
10295
let (bundle_info, bundle_pi_hash) = task.precheck_and_build_metadata()?;
10396
let proving_task = task.try_into()?;
104-
10597
Ok((
10698
bundle_pi_hash,
10799
BundleProofMetadata {

crates/libzkp/src/tasks/batch.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use eyre::Result;
33
use sbv_primitives::{B256, U256};
44
use scroll_zkvm_types::{
55
batch::{
6-
build_point_eval_witness, BatchHeader, BatchHeaderV6, BatchHeaderV7, BatchHeaderV8,
7-
BatchHeaderValidium, BatchInfo, BatchWitness, Envelope, EnvelopeV6, EnvelopeV7, EnvelopeV8,
8-
LegacyBatchWitness, ReferenceHeader, N_BLOB_BYTES,
6+
build_point_eval_witness, BatchHeader, BatchHeaderV6, BatchHeaderV7, BatchHeaderValidium,
7+
BatchInfo, BatchWitness, Envelope, EnvelopeV6, EnvelopeV7, LegacyBatchWitness,
8+
ReferenceHeader, N_BLOB_BYTES,
99
},
1010
chunk::ChunkInfo,
11-
public_inputs::{ForkName, Version, MultiVersionPublicInputs},
11+
public_inputs::{ForkName, MultiVersionPublicInputs, Version},
1212
task::ProvingTask,
1313
utils::{to_rkyv_bytes, RancorError},
1414
version::{Codec, Domain, STFVersion},
@@ -34,6 +34,7 @@ pub struct BatchHeaderValidiumWithHash {
3434
/// defination, i.e. v6- v8 (current), and validium
3535
#[derive(Clone, serde::Deserialize, serde::Serialize)]
3636
#[serde(untagged)]
37+
#[allow(non_camel_case_types)]
3738
pub enum BatchHeaderV {
3839
/// Header for validium mode.
3940
Validium(BatchHeaderValidiumWithHash),
@@ -43,14 +44,14 @@ pub enum BatchHeaderV {
4344
///
4445
/// Since the codec essentially is unchanged for the above STF versions, we do not define new
4546
/// variants, instead re-using the [`BatchHeaderV7`] variant.
46-
V7_8_9(BatchHeaderV7),
47+
V7_V8_V9(BatchHeaderV7),
4748
}
4849

4950
impl core::fmt::Display for BatchHeaderV {
5051
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
5152
match self {
5253
BatchHeaderV::V6(_) => write!(f, "V6"),
53-
BatchHeaderV::V7_8_9(_) => write!(f, "V7_8_9"),
54+
BatchHeaderV::V7_V8_V9(_) => write!(f, "V7_V8_V9"),
5455
BatchHeaderV::Validium(_) => write!(f, "Validium"),
5556
}
5657
}
@@ -60,7 +61,7 @@ impl BatchHeaderV {
6061
pub fn batch_hash(&self) -> B256 {
6162
match self {
6263
BatchHeaderV::V6(h) => h.batch_hash(),
63-
BatchHeaderV::V7_8_9(h) => h.batch_hash(),
64+
BatchHeaderV::V7_V8_V9(h) => h.batch_hash(),
6465
BatchHeaderV::Validium(h) => h.header.batch_hash(),
6566
}
6667
}
@@ -72,17 +73,10 @@ impl BatchHeaderV {
7273
}
7374
}
7475

75-
pub fn must_v7_header(&self) -> &BatchHeaderV7 {
76+
pub fn must_v7_v8_v9_header(&self) -> &BatchHeaderV7 {
7677
match self {
77-
BatchHeaderV::V7_8_9(h) => h,
78-
_ => unreachable!("A header of {} is considered to be v7", self),
79-
}
80-
}
81-
82-
pub fn must_v8_header(&self) -> &BatchHeaderV8 {
83-
match self {
84-
BatchHeaderV::V7_8_9(h) => h,
85-
_ => unreachable!("A header of {} is considered to be v8", self),
78+
BatchHeaderV::V7_V8_V9(h) => h,
79+
_ => unreachable!("A header of {} is considered to be in [v7, v8, v9]", self),
8680
}
8781
}
8882

@@ -162,10 +156,8 @@ impl BatchProvingTask {
162156
version.fork,
163157
ForkName::EuclidV1,
164158
),
165-
BatchHeaderV::V7_8_9(_) => assert!(
166-
version.fork == ForkName::EuclidV2 ||
167-
version.fork == ForkName::Feynman ||
168-
version.fork == ForkName::Galileo,
159+
BatchHeaderV::V7_V8_V9(_) => assert!(
160+
matches!(version.fork, ForkName::EuclidV2 | ForkName::Feynman | ForkName::Galileo),
169161
"hardfork mismatch for da-codec@v7/8/9 header: found={}, expected={:?}",
170162
version.fork,
171163
[ForkName::EuclidV2, ForkName::Feynman, ForkName::Galileo],
@@ -192,8 +184,6 @@ impl BatchProvingTask {
192184
}
193185
Codec::V7 => <EnvelopeV7 as Envelope>::from_slice(padded_blob_bytes.as_slice())
194186
.challenge_digest(versioned_hash),
195-
Codec::V8 => <EnvelopeV8 as Envelope>::from_slice(padded_blob_bytes.as_slice())
196-
.challenge_digest(versioned_hash),
197187
};
198188
let (proof, _) = point_eval::get_kzg_proof(&blob, challenge_digest);
199189

@@ -242,9 +232,6 @@ impl BatchProvingTask {
242232
(Domain::Scroll, STFVersion::V6) => {
243233
ReferenceHeader::V6(*self.batch_header.must_v6_header())
244234
}
245-
(Domain::Scroll, STFVersion::V7) => {
246-
ReferenceHeader::V7(*self.batch_header.must_v7_header())
247-
}
248235
// The da-codec for STF versions v7, v8, v9 is identical. In zkvm-prover we do not
249236
// create additional variants to indicate the identical behaviour of codec. Instead we
250237
// add a separate variant for the STF version.
@@ -255,8 +242,8 @@ impl BatchProvingTask {
255242
// hard-fork (feynman or galileo) and the codec from the version byte.
256243
//
257244
// Refer [`scroll_zkvm_types::public_inputs::Version`].
258-
(Domain::Scroll, STFVersion::V8) | (Domain::Scroll, STFVersion::V9) => {
259-
ReferenceHeader::V8(*self.batch_header.must_v8_header())
245+
(Domain::Scroll, STFVersion::V7 | STFVersion::V8 | STFVersion::V9) => {
246+
ReferenceHeader::V7_V8_V9(*self.batch_header.must_v7_v8_v9_header())
260247
}
261248
(Domain::Validium, STFVersion::V1) => {
262249
ReferenceHeader::Validium(*self.batch_header.must_validium_header())

crates/libzkp/src/tasks/bundle.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use eyre::Result;
22
use sbv_primitives::B256;
33
use scroll_zkvm_types::{
44
bundle::{BundleInfo, BundleWitness, LegacyBundleWitness},
5-
public_inputs::{Version, MultiVersionPublicInputs},
5+
public_inputs::{MultiVersionPublicInputs, Version},
66
task::ProvingTask,
77
utils::{to_rkyv_bytes, RancorError},
88
};
@@ -33,11 +33,13 @@ impl BundleProvingTask {
3333
.first()
3434
.expect(BUNDLE_SANITY_MSG)
3535
.metadata
36+
.batch_info
3637
.batch_hash,
3738
self.batch_proofs
3839
.last()
3940
.expect(BUNDLE_SANITY_MSG)
4041
.metadata
42+
.batch_info
4143
.batch_hash,
4244
);
4345

crates/libzkp/src/tasks/chunk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use sbv_core::BlockWitness;
33
use sbv_primitives::{types::consensus::BlockHeader, B256};
44
use scroll_zkvm_types::{
55
chunk::{execute, ChunkInfo, ChunkWitness, LegacyChunkWitness, ValidiumInputs},
6+
public_inputs::{MultiVersionPublicInputs, Version},
67
task::ProvingTask,
78
utils::{to_rkyv_bytes, RancorError},
8-
public_inputs::{Version, MultiVersionPublicInputs},
99
};
1010

1111
use super::chunk_interpreter::*;

0 commit comments

Comments
 (0)