From ee302ec78c363f0384011d35c76907a9deebe93a Mon Sep 17 00:00:00 2001 From: Gianbelinche <39842759+gianbelinche@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:47:03 -0300 Subject: [PATCH] Send uncoded commitment --- core/lib/l1_contract_interface/Cargo.toml | 5 +- .../src/i_executor/structures/blob_info.rs | 140 ++++++++++++++++++ .../structures/commit_batch_info.rs | 32 +++- .../src/i_executor/structures/mod.rs | 1 + 4 files changed, 169 insertions(+), 9 deletions(-) create mode 100644 core/lib/l1_contract_interface/src/i_executor/structures/blob_info.rs diff --git a/core/lib/l1_contract_interface/Cargo.toml b/core/lib/l1_contract_interface/Cargo.toml index 8b68df854e71..e7bcf19d4ca3 100644 --- a/core/lib/l1_contract_interface/Cargo.toml +++ b/core/lib/l1_contract_interface/Cargo.toml @@ -23,8 +23,11 @@ sha2.workspace = true sha3.workspace = true hex.workspace = true once_cell.workspace = true +rlp.workspace = true +serde.workspace = true +bincode.workspace = true [dev-dependencies] -serde.workspace = true serde_json.workspace = true serde_with = { workspace = true, features = ["base64", "hex"] } + diff --git a/core/lib/l1_contract_interface/src/i_executor/structures/blob_info.rs b/core/lib/l1_contract_interface/src/i_executor/structures/blob_info.rs new file mode 100644 index 000000000000..18a433fdc7aa --- /dev/null +++ b/core/lib/l1_contract_interface/src/i_executor/structures/blob_info.rs @@ -0,0 +1,140 @@ +use rlp::Decodable; +use rlp::DecoderError; +use rlp::Rlp; +use serde::{Serialize, Deserialize}; + +#[derive(Debug,Serialize, Deserialize)] +struct G1Commitment { + pub x: Vec, + pub y: Vec, +} + +impl Decodable for G1Commitment { + fn decode(rlp: &Rlp) -> Result { + let x: Vec = rlp.val_at(0)?; // Decode first element as Vec + let y: Vec = rlp.val_at(1)?; // Decode second element as Vec + + Ok(G1Commitment { x, y }) + } +} + +#[derive(Debug,Serialize, Deserialize)] +struct BlobQuorumParam { + pub quorum_number: u32, + pub adversary_threshold_percentage: u32, + pub confirmation_threshold_percentage: u32, + pub chunk_length: u32 +} + +impl Decodable for BlobQuorumParam { + fn decode(rlp: &Rlp) -> Result { + Ok(BlobQuorumParam { + quorum_number: rlp.val_at(0)?, + adversary_threshold_percentage: rlp.val_at(1)?, + confirmation_threshold_percentage: rlp.val_at(2)?, + chunk_length: rlp.val_at(3)?, + }) + } +} + +#[derive(Debug,Serialize, Deserialize)] +struct BlobHeader { + pub commitment: G1Commitment, + pub data_length: u32, + pub blob_quorum_params: Vec +} + +impl Decodable for BlobHeader { + fn decode(rlp: &Rlp) -> Result { + let commitment: G1Commitment = rlp.val_at(0)?; + let data_length: u32 = rlp.val_at(1)?; + let blob_quorum_params: Vec = rlp.list_at(2)?; + + Ok(BlobHeader { + commitment, + data_length, + blob_quorum_params, + }) + } +} + +#[derive(Debug,Serialize, Deserialize)] +struct BatchHeader { + pub batch_root: Vec, + pub quorum_numbers: Vec, + pub quorum_signed_percentages: Vec, + pub reference_block_number: u32 +} + +impl Decodable for BatchHeader { + fn decode(rlp: &Rlp) -> Result { + Ok(BatchHeader { + batch_root: rlp.val_at(0)?, + quorum_numbers: rlp.val_at(1)?, + quorum_signed_percentages: rlp.val_at(2)?, + reference_block_number: rlp.val_at(3)?, + }) + } +} + +#[derive(Debug,Serialize, Deserialize)] +struct BatchMetadata { + pub batch_header: BatchHeader, + pub signatory_record_hash: Vec, + pub fee: Vec, + pub confirmation_block_number: u32, + pub batch_header_hash: Vec +} + +impl Decodable for BatchMetadata { + fn decode(rlp: &Rlp) -> Result { + let batch_header: BatchHeader = rlp.val_at(0)?; + + Ok(BatchMetadata { + batch_header, + signatory_record_hash: rlp.val_at(1)?, + fee: rlp.val_at(2)?, + confirmation_block_number: rlp.val_at(3)?, + batch_header_hash: rlp.val_at(4)?, + }) + } +} + +#[derive(Debug,Serialize, Deserialize)] +struct BlobVerificationProof { + pub batch_id: u32, + pub blob_index: u32, + pub batch_medatada: BatchMetadata, + pub inclusion_proof: Vec, + pub quorum_indexes: Vec +} + +impl Decodable for BlobVerificationProof { + fn decode(rlp: &Rlp) -> Result { + Ok(BlobVerificationProof { + batch_id: rlp.val_at(0)?, + blob_index: rlp.val_at(1)?, + batch_medatada: rlp.val_at(2)?, + inclusion_proof: rlp.val_at(3)?, + quorum_indexes: rlp.val_at(4)?, + }) + } +} + +#[derive(Debug,Serialize, Deserialize)] +pub struct BlobInfo { + pub blob_header: BlobHeader, + pub blob_verification_proof: BlobVerificationProof +} + +impl Decodable for BlobInfo { + fn decode(rlp: &Rlp) -> Result { + let blob_header: BlobHeader = rlp.val_at(0)?; + let blob_verification_proof: BlobVerificationProof = rlp.val_at(1)?; + + Ok(BlobInfo { + blob_header, + blob_verification_proof, + }) + } +} diff --git a/core/lib/l1_contract_interface/src/i_executor/structures/commit_batch_info.rs b/core/lib/l1_contract_interface/src/i_executor/structures/commit_batch_info.rs index 0d1ff91163fa..42bcc3526a13 100644 --- a/core/lib/l1_contract_interface/src/i_executor/structures/commit_batch_info.rs +++ b/core/lib/l1_contract_interface/src/i_executor/structures/commit_batch_info.rs @@ -1,3 +1,4 @@ +use rlp::decode; use zksync_types::{ commitment::{ pre_boojum_serialize_commitments, serialize_commitments, L1BatchCommitmentMode, @@ -13,6 +14,8 @@ use crate::{ i_executor::commit::kzg::{KzgInfo, ZK_SYNC_BYTES_PER_BLOB}, Tokenizable, }; +use bincode; +use super::blob_info::BlobInfo; /// These are used by the L1 Contracts to indicate what DA layer is used for pubdata const PUBDATA_SOURCE_CALLDATA: u8 = 0; @@ -217,14 +220,27 @@ impl Tokenizable for CommitBatchInfo<'_> { } (L1BatchCommitmentMode::Validium, PubdataDA::Custom) => { let mut operator_da_input = vec![PUBDATA_SOURCE_CUSTOM]; - operator_da_input.extend( - &self - .l1_batch_with_metadata - .metadata - .da_blob_id - .clone() - .unwrap_or_default() - ); + + let commitment = &self + .l1_batch_with_metadata + .metadata + .da_blob_id + .clone() + .unwrap_or_default(); + + let data = &hex::decode(commitment).unwrap()[3..]; + + let blob_info: BlobInfo = match decode(&data) { + Ok(blob_info) => blob_info, + Err(e) => panic!("Error decoding commitment: {}", e) + }; + + let bytes = match bincode::serialize(&blob_info) { + Ok(bytes) => bytes, + Err(e) => panic!("Error serializing commitment: {}", e) + }; + + operator_da_input.extend(bytes); operator_da_input } diff --git a/core/lib/l1_contract_interface/src/i_executor/structures/mod.rs b/core/lib/l1_contract_interface/src/i_executor/structures/mod.rs index d1ed57e41f2e..90c16d37c573 100644 --- a/core/lib/l1_contract_interface/src/i_executor/structures/mod.rs +++ b/core/lib/l1_contract_interface/src/i_executor/structures/mod.rs @@ -2,5 +2,6 @@ mod commit_batch_info; mod stored_batch_info; +mod blob_info; pub use self::{commit_batch_info::CommitBatchInfo, stored_batch_info::StoredBatchInfo};