@@ -227,6 +227,10 @@ func NewDABatch(batch *encoding.Batch) (*DABatch, error) {
227
227
return nil , fmt .Errorf ("too many chunks in batch" )
228
228
}
229
229
230
+ if len (batch .Chunks ) == 0 {
231
+ return nil , fmt .Errorf ("too few chunks in batch" )
232
+ }
233
+
230
234
// batch data hash
231
235
dataHash , err := computeBatchDataHash (batch .Chunks , batch .TotalL1MessagePoppedBefore )
232
236
if err != nil {
@@ -301,52 +305,51 @@ func constructBlobPayload(chunks []*encoding.Chunk) (*kzg4844.Blob, *kzg4844.Poi
301
305
// the raw (un-padded) blob payload
302
306
blobBytes := make ([]byte , metadataLength )
303
307
304
- // the number of chunks that contain at least one L2 transaction
305
- numNonEmptyChunks := 0
306
-
307
308
// challenge digest preimage
308
309
// 1 hash for metadata and 1 for each chunk
309
310
challengePreimage := make ([]byte , (1 + MaxNumChunks )* 32 )
310
311
311
- // the challenge point z
312
- var z kzg4844.Point
312
+ // the chunk data hash used for calculating the challenge preimage
313
+ var chunkDataHash common.Hash
314
+
315
+ // blob metadata: num_chunks
316
+ binary .BigEndian .PutUint16 (blobBytes [0 :], uint16 (len (chunks )))
313
317
314
318
// encode blob metadata and L2 transactions,
315
319
// and simultaneously also build challenge preimage
316
320
for chunkID , chunk := range chunks {
317
321
currentChunkStartIndex := len (blobBytes )
318
- hasL2Tx := false
319
322
320
323
for _ , block := range chunk .Blocks {
321
324
for _ , tx := range block .Transactions {
322
325
if tx .Type != types .L1MessageTxType {
323
- hasL2Tx = true
324
326
// encode L2 txs into blob payload
325
327
rlpTxData , err := encoding .ConvertTxDataToRLPEncoding (tx )
326
328
if err != nil {
327
329
return nil , nil , err
328
330
}
329
331
blobBytes = append (blobBytes , rlpTxData ... )
330
- continue
331
332
}
332
333
}
333
334
}
334
335
335
336
// blob metadata: chunki_size
336
- chunkSize := len (blobBytes ) - currentChunkStartIndex
337
- binary .BigEndian .PutUint32 (blobBytes [2 + 4 * chunkID :], uint32 (chunkSize ))
338
-
339
- if hasL2Tx {
340
- numNonEmptyChunks ++
337
+ if chunkSize := len (blobBytes ) - currentChunkStartIndex ; chunkSize != 0 {
338
+ binary .BigEndian .PutUint32 (blobBytes [2 + 4 * chunkID :], uint32 (chunkSize ))
341
339
}
342
340
343
341
// challenge: compute chunk data hash
344
- hash : = crypto .Keccak256Hash (blobBytes [currentChunkStartIndex :])
345
- copy (challengePreimage [32 + chunkID * 32 :], hash [:])
342
+ chunkDataHash = crypto .Keccak256Hash (blobBytes [currentChunkStartIndex :])
343
+ copy (challengePreimage [32 + chunkID * 32 :], chunkDataHash [:])
346
344
}
347
345
348
- // blob metadata: num_chunks
349
- binary .BigEndian .PutUint16 (blobBytes [0 :], uint16 (numNonEmptyChunks ))
346
+ // if we have fewer than MaxNumChunks chunks, the rest
347
+ // of the blob metadata is correctly initialized to 0,
348
+ // but we need to add padding to the challenge preimage
349
+ for chunkID := len (chunks ); chunkID < MaxNumChunks ; chunkID ++ {
350
+ // use the last chunk's data hash as padding
351
+ copy (challengePreimage [32 + chunkID * 32 :], chunkDataHash [:])
352
+ }
350
353
351
354
// challenge: compute metadata hash
352
355
hash := crypto .Keccak256Hash (blobBytes [0 :metadataLength ])
@@ -359,9 +362,14 @@ func constructBlobPayload(chunks []*encoding.Chunk) (*kzg4844.Blob, *kzg4844.Poi
359
362
}
360
363
361
364
// compute z = challenge_digest % BLS_MODULUS
362
- challengeDigest := crypto .Keccak256Hash (challengePreimage [:])
363
- point := new (big.Int ).Mod (new (big.Int ).SetBytes (challengeDigest [:]), BLSModulus )
364
- copy (z [:], point .Bytes ()[0 :32 ])
365
+ challengeDigest := crypto .Keccak256Hash (challengePreimage )
366
+ pointBigInt := new (big.Int ).Mod (new (big.Int ).SetBytes (challengeDigest [:]), BLSModulus )
367
+ pointBytes := pointBigInt .Bytes ()
368
+
369
+ // the challenge point z
370
+ var z kzg4844.Point
371
+ start := 32 - len (pointBytes )
372
+ copy (z [start :], pointBytes )
365
373
366
374
return blob , & z , nil
367
375
}
0 commit comments