Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/circuits/batch-circuit/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use scroll_zkvm_types_circuit::{
io::read_witnesses,
public_inputs::{
Version,
batch::{BatchInfo, VersionedBatchInfo},
chunk::VersionedChunkInfo,
scroll::{
batch::{BatchInfo, VersionedBatchInfo},
chunk::VersionedChunkInfo,
},
},
};

Expand Down
6 changes: 4 additions & 2 deletions crates/circuits/bundle-circuit/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use scroll_zkvm_types_circuit::{
io::read_witnesses,
public_inputs::{
Version,
batch::VersionedBatchInfo,
bundle::{BundleInfo, VersionedBundleInfo},
scroll::{
batch::VersionedBatchInfo,
bundle::{BundleInfo, VersionedBundleInfo},
},
},
};

Expand Down
2 changes: 1 addition & 1 deletion crates/circuits/chunk-circuit/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use scroll_zkvm_types_circuit::{
io::read_witnesses,
public_inputs::{
Version,
chunk::{ChunkInfo, VersionedChunkInfo},
scroll::chunk::{ChunkInfo, VersionedChunkInfo},
},
};

Expand Down
5 changes: 3 additions & 2 deletions crates/integration/tests/bundle_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use scroll_zkvm_integration::{
utils::metadata_from_bundle_witnesses,
};
use scroll_zkvm_prover::{Prover, ProverConfig};
use scroll_zkvm_types::version::Version;
use scroll_zkvm_types::{
proof::OpenVmEvmProof,
public_inputs::{ForkName, MultiVersionPublicInputs},
public_inputs::{ForkName, MultiVersionPublicInputs, PublicInputs},
};
use std::str::FromStr;

Expand Down Expand Up @@ -114,7 +115,7 @@ fn verify_bundle_info_pi() {
};

assert_eq!(
info.pi_hash_euclidv1(),
(info, Version::euclid_v1()).pi_hash(),
B256::from_str("0x5e49fc59ce02b42a2f693c738c582b36bd08e9cfe3acb8cee299216743869bd4")
.unwrap()
);
Expand Down
23 changes: 16 additions & 7 deletions crates/types/base/src/public_inputs.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
use crate::utils::keccak256;
use alloy_primitives::B256;

pub mod batch;
pub mod bundle;
pub mod chunk;
pub mod scroll;

pub use crate::{fork_name::ForkName, version::Version};

/// Defines behaviour to be implemented by types representing the public-input values of a circuit.
pub trait PublicInputs {
/// Public inputs encoded.
fn pi(&self) -> Vec<u8>;

/// Keccak-256 digest of the public inputs. The public-input hash are revealed as public values
/// via [`openvm::io::reveal`].
fn pi_hash(&self) -> B256;
fn pi_hash(&self) -> B256 {
keccak256(self.pi())
}

/// Validation logic between public inputs of two contiguous instances.
fn validate(&self, prev_pi: &Self);
}

/// helper trait to extend PublicInputs
pub trait MultiVersionPublicInputs {
fn pi_hash_by_version(&self, version: Version) -> B256;
/// Public inputs encoded for a specific version.
fn pi_by_version(&self, version: Version) -> Vec<u8>;
/// Keccak-256 digest of the public inputs for a specific version.
fn pi_hash_by_version(&self, version: Version) -> B256 {
keccak256(self.pi_by_version(version))
}
fn validate(&self, prev_pi: &Self, version: Version);
}

impl<T: MultiVersionPublicInputs> PublicInputs for (T, Version) {
fn pi_hash(&self) -> B256 {
self.0.pi_hash_by_version(self.1)
fn pi(&self) -> Vec<u8> {
self.0.pi_by_version(self.1)
}

fn validate(&self, prev_pi: &Self) {
Expand Down
3 changes: 3 additions & 0 deletions crates/types/base/src/public_inputs/scroll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod batch;
pub mod bundle;
pub mod chunk;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloy_primitives::B256;

use crate::{
public_inputs::{ForkName, MultiVersionPublicInputs},
utils::keccak256,
version::{Domain, STFVersion, Version},
};

Expand Down Expand Up @@ -48,33 +47,31 @@ pub struct BatchInfo {
}

impl BatchInfo {
/// Public input hash for a batch (euclidv1 or da-codec@v6) is defined as
/// Public inputs encoded for a batch (euclidv1 or da-codec@v6) is defined as
///
/// keccak(
/// concat(
/// parent state root ||
/// parent batch hash ||
/// state root ||
/// batch hash ||
/// chain id ||
/// withdraw root ||
/// )
fn pi_hash_euclidv1(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
fn pi_euclidv1(&self) -> Vec<u8> {
std::iter::empty()
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.copied()
.collect()
}

/// Public input hash for a batch (euclidv2 or da-codec@v7) is defined as
/// Public inputs encoded for a batch (euclidv2 or da-codec@v7) is defined as
///
/// keccak(
/// concat(
/// parent state root ||
/// parent batch hash ||
/// state root ||
Expand All @@ -84,32 +81,30 @@ impl BatchInfo {
/// prev msg queue hash ||
/// post msg queue hash
/// )
fn pi_hash_euclidv2(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.prev_msg_queue_hash.as_slice())
.chain(self.post_msg_queue_hash.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
fn pi_euclidv2(&self) -> Vec<u8> {
std::iter::empty()
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.prev_msg_queue_hash.as_slice())
.chain(self.post_msg_queue_hash.as_slice())
.copied()
.collect()
}

/// Public input hash for a batch (feynman or da-codec@v8).
/// Public inputs encoded for a batch (feynman or da-codec@v8).
///
/// Unchanged from euclid-v2.
fn pi_hash_feynman(&self) -> B256 {
self.pi_hash_euclidv2()
fn pi_feynman(&self) -> Vec<u8> {
self.pi_euclidv2()
}

/// Public input hash for a batch (galileo or da-codec@v9) is defined as
/// Public inputs encoded for a batch (galileo or da-codec@v9) is defined as
///
/// keccak(
/// concat(
/// version ||
/// parent state root ||
/// parent batch hash ||
Expand All @@ -120,33 +115,31 @@ impl BatchInfo {
/// prev msg queue hash ||
/// post msg queue hash
/// )
fn pi_hash_galileo(&self, version: Version) -> B256 {
keccak256(
std::iter::empty()
.chain(&[version.as_version_byte()])
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.prev_msg_queue_hash.as_slice())
.chain(self.post_msg_queue_hash.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
fn pi_galileo(&self, version: Version) -> Vec<u8> {
std::iter::empty()
.chain(&[version.as_version_byte()])
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.prev_msg_queue_hash.as_slice())
.chain(self.post_msg_queue_hash.as_slice())
.copied()
.collect()
}

/// Public input hash for a batch (galileo or da-codec@v9) is defined as
/// Public inputs encoded for a batch (galileo or da-codec@v9) is defined as
///
/// Same as galileo.
pub fn pi_hash_galileo_v2(&self, version: Version) -> B256 {
self.pi_hash_galileo(version)
pub fn pi_galileo_v2(&self, version: Version) -> Vec<u8> {
self.pi_galileo(version)
}

/// Public input hash for a L3 validium @ v1.
/// Public inputs encoded for a L3 validium @ v1.
///
/// keccak(
/// concat(
/// version ||
/// parent state root ||
/// parent batch hash ||
Expand All @@ -158,36 +151,34 @@ impl BatchInfo {
/// post msg queue hash
/// encryption key
/// )
fn pi_hash_validium(&self, version: Version) -> B256 {
keccak256(
std::iter::empty()
.chain(&[version.as_version_byte()])
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.prev_msg_queue_hash.as_slice())
.chain(self.post_msg_queue_hash.as_slice())
.chain(self.encryption_key.as_ref().expect("domain=Validium"))
.cloned()
.collect::<Vec<u8>>(),
)
fn pi_validium(&self, version: Version) -> Vec<u8> {
std::iter::empty()
.chain(&[version.as_version_byte()])
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.prev_msg_queue_hash.as_slice())
.chain(self.post_msg_queue_hash.as_slice())
.chain(self.encryption_key.as_ref().expect("domain=Validium"))
.copied()
.collect()
}
}

pub type VersionedBatchInfo = (BatchInfo, Version);

impl MultiVersionPublicInputs for BatchInfo {
fn pi_hash_by_version(&self, version: Version) -> B256 {
fn pi_by_version(&self, version: Version) -> Vec<u8> {
match (version.domain, version.stf_version) {
(Domain::Scroll, STFVersion::V6) => self.pi_hash_euclidv1(),
(Domain::Scroll, STFVersion::V7) => self.pi_hash_euclidv2(),
(Domain::Scroll, STFVersion::V8) => self.pi_hash_feynman(),
(Domain::Scroll, STFVersion::V9) => self.pi_hash_galileo(version),
(Domain::Scroll, STFVersion::V10) => self.pi_hash_galileo_v2(version),
(Domain::Validium, STFVersion::V1) => self.pi_hash_validium(version),
(Domain::Scroll, STFVersion::V6) => self.pi_euclidv1(),
(Domain::Scroll, STFVersion::V7) => self.pi_euclidv2(),
(Domain::Scroll, STFVersion::V8) => self.pi_feynman(),
(Domain::Scroll, STFVersion::V9) => self.pi_galileo(version),
(Domain::Scroll, STFVersion::V10) => self.pi_galileo_v2(version),
(Domain::Validium, STFVersion::V1) => self.pi_validium(version),
(domain, stf_version) => {
unreachable!("unsupported version=({domain:?}, {stf_version:?})")
}
Expand Down
Loading
Loading