Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit e52b511

Browse files
Standalone BatchHeader Constructor (#1362)
* Create standalone constructor for batch header * Recover decoder config subcomponent * corresponding changes to prover --------- Co-authored-by: Rohit Narurkar <[email protected]>
1 parent d87fe2c commit e52b511

File tree

8 files changed

+194
-116
lines changed

8 files changed

+194
-116
lines changed

aggregator/src/aggregation/circuit.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -506,21 +506,20 @@ impl<const N_SNARKS: usize> Circuit<Fr> for BatchCircuit<N_SNARKS> {
506506
"original and recovered bytes mismatch"
507507
);
508508

509-
// batch_circuit_debug
510-
// let decoder_exports = config.decoder_config.assign(
511-
// &mut layouter,
512-
// &batch_bytes,
513-
// &encoded_batch_bytes,
514-
// witness_rows,
515-
// decoded_literals,
516-
// fse_aux_tables,
517-
// block_info_arr,
518-
// sequence_info_arr,
519-
// address_table_arr,
520-
// sequence_exec_info_arr,
521-
// &challenges,
522-
// LOG_DEGREE, // TODO: configure k for batch circuit instead of hard-coded here.
523-
// )?;
509+
let decoder_exports = config.decoder_config.assign(
510+
&mut layouter,
511+
&batch_bytes,
512+
&encoded_batch_bytes,
513+
witness_rows,
514+
decoded_literals,
515+
fse_aux_tables,
516+
block_info_arr,
517+
sequence_info_arr,
518+
address_table_arr,
519+
sequence_exec_info_arr,
520+
&challenges,
521+
LOG_DEGREE, // TODO: configure k for batch circuit instead of hard-coded here.
522+
)?;
524523

525524
layouter.assign_region(
526525
|| "consistency checks",
@@ -569,27 +568,26 @@ impl<const N_SNARKS: usize> Circuit<Fr> for BatchCircuit<N_SNARKS> {
569568
region.constrain_equal(c.cell(), ec.cell())?;
570569
}
571570

572-
// batch_circuit_debug
573-
// // equate rlc (from blob data) with decoder's encoded_rlc
574-
// region.constrain_equal(
575-
// blob_data_exports.bytes_rlc.cell(),
576-
// decoder_exports.encoded_rlc.cell(),
577-
// )?;
578-
// // equate len(blob_bytes) with decoder's encoded_len
579-
// region.constrain_equal(
580-
// blob_data_exports.bytes_len.cell(),
581-
// decoder_exports.encoded_len.cell(),
582-
// )?;
583-
// // equate rlc (from batch data) with decoder's decoded_rlc
584-
// region.constrain_equal(
585-
// batch_data_exports.bytes_rlc.cell(),
586-
// decoder_exports.decoded_rlc.cell(),
587-
// )?;
588-
// // equate len(batch_data) with decoder's decoded_len
589-
// region.constrain_equal(
590-
// batch_data_exports.batch_data_len.cell(),
591-
// decoder_exports.decoded_len.cell(),
592-
// )?;
571+
// equate rlc (from blob data) with decoder's encoded_rlc
572+
region.constrain_equal(
573+
blob_data_exports.bytes_rlc.cell(),
574+
decoder_exports.encoded_rlc.cell(),
575+
)?;
576+
// equate len(blob_bytes) with decoder's encoded_len
577+
region.constrain_equal(
578+
blob_data_exports.bytes_len.cell(),
579+
decoder_exports.encoded_len.cell(),
580+
)?;
581+
// equate rlc (from batch data) with decoder's decoded_rlc
582+
region.constrain_equal(
583+
batch_data_exports.bytes_rlc.cell(),
584+
decoder_exports.decoded_rlc.cell(),
585+
)?;
586+
// equate len(batch_data) with decoder's decoded_len
587+
region.constrain_equal(
588+
batch_data_exports.batch_data_len.cell(),
589+
decoder_exports.decoded_len.cell(),
590+
)?;
593591

594592
Ok(())
595593
},

aggregator/src/aggregation/config.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ pub struct BatchCircuitConfig<const N_SNARKS: usize> {
3838
pub blob_data_config: BlobDataConfig<N_SNARKS>,
3939
/// The batch data's config.
4040
pub batch_data_config: BatchDataConfig<N_SNARKS>,
41-
// batch_circuit_debug
42-
// /// The zstd decoder's config.
43-
// pub decoder_config: DecoderConfig<1024, 512>,
41+
/// The zstd decoder's config.
42+
pub decoder_config: DecoderConfig<1024, 512>,
4443
/// Config to do the barycentric evaluation on blob polynomial.
4544
pub barycentric: BarycentricEvaluationConfig,
4645
/// Instance for public input; stores
@@ -131,30 +130,29 @@ impl<const N_SNARKS: usize> BatchCircuitConfig<N_SNARKS> {
131130
);
132131

133132
// Zstd decoder.
134-
// batch_circuit_debug
135-
// let pow_rand_table = PowOfRandTable::construct(meta, &challenges_expr);
136-
137-
// let pow2_table = Pow2Table::construct(meta);
138-
// let range8 = RangeTable::construct(meta);
139-
// let range16 = RangeTable::construct(meta);
140-
// let range512 = RangeTable::construct(meta);
141-
// let range_block_len = RangeTable::construct(meta);
142-
// let bitwise_op_table = BitwiseOpTable::construct(meta);
143-
144-
// let decoder_config = DecoderConfig::configure(
145-
// meta,
146-
// &challenges_expr,
147-
// DecoderConfigArgs {
148-
// pow_rand_table,
149-
// pow2_table,
150-
// u8_table,
151-
// range8,
152-
// range16,
153-
// range512,
154-
// range_block_len,
155-
// bitwise_op_table,
156-
// },
157-
// );
133+
let pow_rand_table = PowOfRandTable::construct(meta, &challenges_expr);
134+
135+
let pow2_table = Pow2Table::construct(meta);
136+
let range8 = RangeTable::construct(meta);
137+
let range16 = RangeTable::construct(meta);
138+
let range512 = RangeTable::construct(meta);
139+
let range_block_len = RangeTable::construct(meta);
140+
let bitwise_op_table = BitwiseOpTable::construct(meta);
141+
142+
let decoder_config = DecoderConfig::configure(
143+
meta,
144+
&challenges_expr,
145+
DecoderConfigArgs {
146+
pow_rand_table,
147+
pow2_table,
148+
u8_table,
149+
range8,
150+
range16,
151+
range512,
152+
range_block_len,
153+
bitwise_op_table,
154+
},
155+
);
158156

159157
// Instance column stores public input column
160158
// the public instance for this circuit consists of
@@ -179,8 +177,7 @@ impl<const N_SNARKS: usize> BatchCircuitConfig<N_SNARKS> {
179177
instance,
180178
barycentric,
181179
batch_data_config,
182-
// batch_circuit_debug
183-
// decoder_config,
180+
decoder_config,
184181
}
185182
}
186183

aggregator/src/batch.rs

Lines changed: 91 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
/// Batch header provides additional fields from the context (within recursion)
1515
/// for constructing the preimage of the batch hash.
1616
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)]
17-
pub struct BatchHeader {
17+
pub struct BatchHeader<const N_SNARKS: usize> {
1818
/// the batch version
1919
pub version: u8,
2020
/// the index of the batch
@@ -35,7 +35,65 @@ pub struct BatchHeader {
3535
pub blob_data_proof: [H256; 2],
3636
}
3737

38-
impl BatchHeader {
38+
impl<const N_SNARKS: usize> BatchHeader<N_SNARKS> {
39+
/// Constructs the correct batch header from chunks data and context variables
40+
pub fn construct_from_chunks(
41+
version: u8,
42+
batch_index: u64,
43+
l1_message_popped: u64,
44+
total_l1_message_popped: u64,
45+
parent_batch_hash: H256,
46+
last_block_timestamp: u64,
47+
chunks: &[ChunkInfo],
48+
) -> Self {
49+
assert_ne!(chunks.len(), 0);
50+
assert!(chunks.len() <= N_SNARKS);
51+
52+
let mut chunks_with_padding = chunks.to_vec();
53+
if chunks.len() < N_SNARKS {
54+
let last_chunk = chunks.last().unwrap();
55+
let mut padding_chunk = last_chunk.clone();
56+
padding_chunk.is_padding = true;
57+
chunks_with_padding
58+
.extend(std::iter::repeat(padding_chunk).take(N_SNARKS - chunks.len()));
59+
}
60+
61+
let number_of_valid_chunks = match chunks_with_padding
62+
.iter()
63+
.enumerate()
64+
.find(|(_index, chunk)| chunk.is_padding)
65+
{
66+
Some((index, _)) => index,
67+
None => N_SNARKS,
68+
};
69+
70+
let batch_data_hash_preimage = chunks_with_padding
71+
.iter()
72+
.take(number_of_valid_chunks)
73+
.flat_map(|chunk_info| chunk_info.data_hash.0.iter())
74+
.cloned()
75+
.collect::<Vec<_>>();
76+
let batch_data_hash = keccak256(batch_data_hash_preimage);
77+
78+
let batch_data = BatchData::<N_SNARKS>::new(number_of_valid_chunks, &chunks_with_padding);
79+
let point_evaluation_assignments = PointEvaluationAssignments::from(&batch_data);
80+
81+
Self {
82+
version,
83+
batch_index,
84+
l1_message_popped,
85+
total_l1_message_popped,
86+
parent_batch_hash,
87+
last_block_timestamp,
88+
data_hash: batch_data_hash.into(),
89+
blob_versioned_hash: batch_data.get_versioned_hash(),
90+
blob_data_proof: [
91+
H256::from_slice(&point_evaluation_assignments.challenge.to_be_bytes()),
92+
H256::from_slice(&point_evaluation_assignments.evaluation.to_be_bytes()),
93+
],
94+
}
95+
}
96+
3997
/// Returns the batch hash as per BatchHeaderV3.
4098
pub fn batch_hash(&self) -> H256 {
4199
// the current batch hash is build as
@@ -107,12 +165,15 @@ pub struct BatchHash<const N_SNARKS: usize> {
107165
/// The 4844 versioned hash for the blob.
108166
pub(crate) versioned_hash: H256,
109167
/// The context batch header
110-
pub(crate) batch_header: BatchHeader,
168+
pub(crate) batch_header: BatchHeader<N_SNARKS>,
111169
}
112170

113171
impl<const N_SNARKS: usize> BatchHash<N_SNARKS> {
114172
/// Build Batch hash from an ordered list of chunks. Will pad if needed
115-
pub fn construct_with_unpadded(chunks: &[ChunkInfo], batch_header: BatchHeader) -> Self {
173+
pub fn construct_with_unpadded(
174+
chunks: &[ChunkInfo],
175+
batch_header: BatchHeader<N_SNARKS>,
176+
) -> Self {
116177
assert_ne!(chunks.len(), 0);
117178
assert!(chunks.len() <= N_SNARKS);
118179
let mut chunks_with_padding = chunks.to_vec();
@@ -132,15 +193,16 @@ impl<const N_SNARKS: usize> BatchHash<N_SNARKS> {
132193
}
133194

134195
/// Build Batch hash from an ordered list of #N_SNARKS of chunks.
135-
pub fn construct(chunks_with_padding: &[ChunkInfo], batch_header: BatchHeader) -> Self {
196+
pub fn construct(
197+
chunks_with_padding: &[ChunkInfo],
198+
batch_header: BatchHeader<N_SNARKS>,
199+
) -> Self {
136200
assert_eq!(
137201
chunks_with_padding.len(),
138202
N_SNARKS,
139203
"input chunk slice does not match N_SNARKS"
140204
);
141205

142-
let mut export_batch_header = batch_header;
143-
144206
let number_of_valid_chunks = match chunks_with_padding
145207
.iter()
146208
.enumerate()
@@ -209,24 +271,34 @@ impl<const N_SNARKS: usize> BatchHash<N_SNARKS> {
209271
.collect::<Vec<_>>();
210272
let batch_data_hash = keccak256(preimage);
211273

212-
// Update export value
213-
export_batch_header.data_hash = batch_data_hash.into();
274+
assert_eq!(
275+
batch_header.data_hash,
276+
H256::from_slice(&batch_data_hash),
277+
"Expect provided BatchHeader's data_hash field to be correct"
278+
);
214279

215280
let batch_data = BatchData::<N_SNARKS>::new(number_of_valid_chunks, chunks_with_padding);
216281
let point_evaluation_assignments = PointEvaluationAssignments::from(&batch_data);
217282

218-
// Update export value
219-
export_batch_header.blob_data_proof[0] =
220-
H256::from_slice(&point_evaluation_assignments.challenge.to_be_bytes());
221-
export_batch_header.blob_data_proof[1] =
222-
H256::from_slice(&point_evaluation_assignments.evaluation.to_be_bytes());
283+
assert_eq!(
284+
batch_header.blob_data_proof[0],
285+
H256::from_slice(&point_evaluation_assignments.challenge.to_be_bytes()),
286+
"Expect provided BatchHeader's blob_data_proof field 0 to be correct"
287+
);
288+
assert_eq!(
289+
batch_header.blob_data_proof[1],
290+
H256::from_slice(&point_evaluation_assignments.evaluation.to_be_bytes()),
291+
"Expect provided BatchHeader's blob_data_proof field 1 to be correct"
292+
);
223293

224294
let versioned_hash = batch_data.get_versioned_hash();
225295

226-
// Update export value
227-
export_batch_header.blob_versioned_hash = versioned_hash;
296+
assert_eq!(
297+
batch_header.blob_versioned_hash, versioned_hash,
298+
"Expect provided BatchHeader's blob_versioned_hash field to be correct"
299+
);
228300

229-
let current_batch_hash = export_batch_header.batch_hash();
301+
let current_batch_hash = batch_header.batch_hash();
230302

231303
log::info!(
232304
"batch hash {:?}, datahash {}, z {}, y {}, versioned hash {:x}",
@@ -248,7 +320,7 @@ impl<const N_SNARKS: usize> BatchHash<N_SNARKS> {
248320
number_of_valid_chunks,
249321
point_evaluation_assignments,
250322
versioned_hash,
251-
batch_header: export_batch_header,
323+
batch_header,
252324
}
253325
}
254326

@@ -378,7 +450,7 @@ impl<const N_SNARKS: usize> BatchHash<N_SNARKS> {
378450
}
379451

380452
/// ...
381-
pub fn batch_header(&self) -> BatchHeader {
453+
pub fn batch_header(&self) -> BatchHeader<N_SNARKS> {
382454
self.batch_header
383455
}
384456
}

0 commit comments

Comments
 (0)