Skip to content

Commit

Permalink
add sanity check (decoded blob == batch bytes)
Browse files Browse the repository at this point in the history
  • Loading branch information
roynalnaruto committed Aug 26, 2024
1 parent 1668cb9 commit 339fe40
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
33 changes: 33 additions & 0 deletions aggregator/src/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,36 @@ pub fn get_blob_bytes(batch_bytes: &[u8]) -> Vec<u8> {

blob_bytes
}

/// Given the blob's bytes, take into account the first byte, i.e. enable_encoding? and either spit
/// out the raw bytes or zstd decode them.
pub fn decode_blob(blob_bytes: &[u8]) -> std::io::Result<Vec<u8>> {
let enable_encoding = blob_bytes[0].eq(&1);

// If not encoded, spit out the rest of the bytes, as it is.
if !enable_encoding {
return Ok(blob_bytes[1..].to_vec());
}

// The bytes following the first byte represent the zstd-encoded bytes.
let mut encoded_bytes = blob_bytes[1..].to_vec();
let mut encoded_len = encoded_bytes.len();
let mut decoded_bytes = Vec::with_capacity(5 * 4096 * 32);
loop {
let mut decoder = zstd_encoder::zstd::stream::read::Decoder::new(encoded_bytes.as_slice())?;
decoder.include_magicbytes(false)?;
decoder.window_log_max(30)?;

decoded_bytes.clear();

if std::io::copy(&mut decoder, &mut decoded_bytes).is_ok() {
break;
}

// The error above means we need to truncate the suffix 0-byte.
encoded_len -= 1;
encoded_bytes.truncate(encoded_len);
}

Ok(decoded_bytes)
}
14 changes: 13 additions & 1 deletion prover/src/aggregator/prover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{collections::BTreeMap, env, iter::repeat};

use aggregator::{BatchHash, BatchHeader, ChunkInfo, MAX_AGG_SNARKS};
use aggregator::{
eip4844::decode_blob, BatchData, BatchHash, BatchHeader, ChunkInfo, MAX_AGG_SNARKS,
};
use anyhow::{bail, Result};
use eth_types::H256;
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
Expand Down Expand Up @@ -212,6 +214,16 @@ impl<'params> Prover<'params> {
let batch_hash = batch_header.batch_hash();
let batch_info: BatchHash<N_SNARKS> =
BatchHash::construct(&chunk_hashes, batch_header, &batch.blob_bytes);
let batch_data: BatchData<N_SNARKS> = BatchData::from(&batch_info);

// sanity check:
// - conditionally decoded blob should match batch data.
let batch_bytes = batch_data.get_batch_data_bytes();
let decoded_blob_bytes = decode_blob(&batch.blob_bytes)?;
assert_eq!(
batch_bytes, decoded_blob_bytes,
"BatchProvingTask(sanity) mismatch batch bytes and decoded blob bytes",
);

let layer3_snark = self.prover_impl.load_or_gen_agg_snark(
name,
Expand Down

0 comments on commit 339fe40

Please sign in to comment.