Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 17 additions & 3 deletions crates/types/base/src/public_inputs/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl BatchInfo {
/// withdraw root ||
/// prev msg queue hash ||
/// post msg queue hash
/// )
/// )
fn pi_hash_euclidv2(&self) -> B256 {
keccak256(
std::iter::empty()
Expand All @@ -108,9 +108,23 @@ impl BatchInfo {
}

/// Public input hash for a L3 validium @ v1.
fn pi_hash_validium_v1(&self) -> B256 {
///
/// keccak(
/// version ||
/// parent state root ||
/// parent batch hash ||
/// state root ||
/// batch hash ||
/// chain id ||
/// withdraw root ||
/// prev msg queue hash ||
/// 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())
Expand Down Expand Up @@ -142,7 +156,7 @@ impl MultiVersionPublicInputs for BatchInfo {
(Domain::Scroll, STFVersion::V6) => self.pi_hash_by_fork(ForkName::EuclidV1),
(Domain::Scroll, STFVersion::V7) => self.pi_hash_by_fork(ForkName::EuclidV2),
(Domain::Scroll, STFVersion::V8) => self.pi_hash_by_fork(ForkName::Feynman),
(Domain::Validium, STFVersion::V1) => self.pi_hash_validium_v1(),
(Domain::Validium, STFVersion::V1) => self.pi_hash_validium(version),
(domain, stf_version) => {
unreachable!("unsupported version=({domain:?}, {stf_version:?})")
}
Expand Down
6 changes: 4 additions & 2 deletions crates/types/base/src/public_inputs/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ impl ChunkInfo {
/// Public input hash for a given chunk for L3 validium @ v1:
///
/// keccak(
/// version ||
/// chain id ||
/// prev state root ||
/// post state root ||
Expand All @@ -288,9 +289,10 @@ impl ChunkInfo {
/// post blockhash ||
/// encryption key
/// )
pub fn pi_hash_validium_v1(&self) -> B256 {
pub fn pi_hash_validium_v1(&self, version: Version) -> B256 {
keccak256(
std::iter::empty()
.chain(&[version.as_version_byte()])
.chain(&self.chain_id.to_be_bytes())
.chain(self.prev_state_root.as_slice())
.chain(self.post_state_root.as_slice())
Expand Down Expand Up @@ -339,7 +341,7 @@ impl MultiVersionPublicInputs for ChunkInfo {
(Domain::Scroll, STFVersion::V6) => self.pi_hash_by_fork(ForkName::EuclidV1),
(Domain::Scroll, STFVersion::V7) => self.pi_hash_by_fork(ForkName::EuclidV2),
(Domain::Scroll, STFVersion::V8) => self.pi_hash_by_fork(ForkName::Feynman),
(Domain::Validium, STFVersion::V1) => self.pi_hash_validium_v1(),
(Domain::Validium, STFVersion::V1) => self.pi_hash_validium_v1(version),
(domain, stf_version) => {
unreachable!("unsupported version=({domain:?}, {stf_version:?})")
}
Expand Down
28 changes: 18 additions & 10 deletions crates/types/batch/src/builder/validium.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use types_base::public_inputs::{batch::BatchInfo, chunk::ChunkInfo};
use types_base::{
public_inputs::{batch::BatchInfo, chunk::ChunkInfo},
version::Version,
};

use crate::header::{BatchHeader, ValidiumBatchHeader, validium::BatchHeaderValidium};

pub struct ValidiumBuilderArgs {
pub version: u8,
pub header: BatchHeaderValidium,
pub chunk_infos: Vec<ChunkInfo>,
#[allow(dead_code)]
pub batch_bytes: Vec<u8>,
}

impl ValidiumBuilderArgs {
pub fn new(
header: BatchHeaderValidium,
chunk_infos: Vec<ChunkInfo>,
batch_bytes: Vec<u8>,
) -> Self {
pub fn new(version: u8, header: BatchHeaderValidium, chunk_infos: Vec<ChunkInfo>) -> Self {
Self {
version,
header,
chunk_infos,
batch_bytes,
}
}
}
Expand All @@ -27,6 +25,10 @@ pub struct ValidiumBatchInfoBuilder;

impl ValidiumBatchInfoBuilder {
pub fn build(args: ValidiumBuilderArgs) -> BatchInfo {
// Check that the batch's STF-version is correct.
let version = Version::from(args.version);
assert_eq!(version.stf_version as u8, args.header.version());

match &args.header {
BatchHeaderValidium::V1(_) => {
// nothing to do for v1 header since blob data is not included in validium
Expand All @@ -42,9 +44,15 @@ impl ValidiumBatchInfoBuilder {
.expect("at least one chunk in batch"),
);

// Additionally check that the batch's commitment field is set correctly.
// Check that the batch's commitment field is set correctly.
assert_eq!(last_chunk.post_blockhash.to_vec(), args.header.commitment());

// Check that the batch's state root is correct.
assert_eq!(last_chunk.post_state_root, args.header.post_state_root());

// Check that the batch's withdraw root is correct.
assert_eq!(last_chunk.withdraw_root, args.header.withdraw_root());

BatchInfo {
parent_state_root: first_chunk.prev_state_root,
parent_batch_hash: args.header.parent_batch_hash(),
Expand Down
6 changes: 6 additions & 0 deletions crates/types/batch/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ pub trait BatchHeader {
pub trait ValidiumBatchHeader: BatchHeader {
/// The commitment attached to the batch header.
fn commitment(&self) -> Vec<u8>;

/// The state root after applying batch.
fn post_state_root(&self) -> B256;

/// The withdraw root from the last block in the batch.
fn withdraw_root(&self) -> B256;
}

/// Reference header indicate the version of batch header base on which batch hash
Expand Down
16 changes: 16 additions & 0 deletions crates/types/batch/src/header/validium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,28 @@ impl ValidiumBatchHeader for BatchHeaderValidium {
Self::V1(header) => header.commitment(),
}
}
fn post_state_root(&self) -> B256 {
match self {
Self::V1(header) => header.post_state_root(),
}
}
fn withdraw_root(&self) -> B256 {
match self {
Self::V1(header) => header.withdraw_root(),
}
}
}

impl ValidiumBatchHeader for BatchHeaderValidiumV1 {
fn commitment(&self) -> Vec<u8> {
self.commitment.to_vec()
}
fn post_state_root(&self) -> B256 {
self.post_state_root
}
fn withdraw_root(&self) -> B256 {
self.withdraw_root
}
}

impl BatchHeader for BatchHeaderValidium {
Expand Down
2 changes: 1 addition & 1 deletion crates/types/batch/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl From<&BatchWitness> for BatchInfo {
BatchInfoBuilderV8::build(args)
}
ReferenceHeader::Validium(header) => ValidiumBatchInfoBuilder::build(
ValidiumBuilderArgs::new(*header, chunk_infos, witness.blob_bytes.to_vec()),
ValidiumBuilderArgs::new(witness.version, *header, chunk_infos),
),
}
}
Expand Down