Skip to content

Commit 416dac2

Browse files
committed
update codecv0 & codecv1 encoding/decoding
1 parent 981150c commit 416dac2

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

rollup/types/encoding/codecv1/codecv1.go

+28-20
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ func NewDABatch(batch *encoding.Batch) (*DABatch, error) {
227227
return nil, fmt.Errorf("too many chunks in batch")
228228
}
229229

230+
if len(batch.Chunks) == 0 {
231+
return nil, fmt.Errorf("too few chunks in batch")
232+
}
233+
230234
// batch data hash
231235
dataHash, err := computeBatchDataHash(batch.Chunks, batch.TotalL1MessagePoppedBefore)
232236
if err != nil {
@@ -301,52 +305,51 @@ func constructBlobPayload(chunks []*encoding.Chunk) (*kzg4844.Blob, *kzg4844.Poi
301305
// the raw (un-padded) blob payload
302306
blobBytes := make([]byte, metadataLength)
303307

304-
// the number of chunks that contain at least one L2 transaction
305-
numNonEmptyChunks := 0
306-
307308
// challenge digest preimage
308309
// 1 hash for metadata and 1 for each chunk
309310
challengePreimage := make([]byte, (1+MaxNumChunks)*32)
310311

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)))
313317

314318
// encode blob metadata and L2 transactions,
315319
// and simultaneously also build challenge preimage
316320
for chunkID, chunk := range chunks {
317321
currentChunkStartIndex := len(blobBytes)
318-
hasL2Tx := false
319322

320323
for _, block := range chunk.Blocks {
321324
for _, tx := range block.Transactions {
322325
if tx.Type != types.L1MessageTxType {
323-
hasL2Tx = true
324326
// encode L2 txs into blob payload
325327
rlpTxData, err := encoding.ConvertTxDataToRLPEncoding(tx)
326328
if err != nil {
327329
return nil, nil, err
328330
}
329331
blobBytes = append(blobBytes, rlpTxData...)
330-
continue
331332
}
332333
}
333334
}
334335

335336
// 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))
341339
}
342340

343341
// 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[:])
346344
}
347345

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+
}
350353

351354
// challenge: compute metadata hash
352355
hash := crypto.Keccak256Hash(blobBytes[0:metadataLength])
@@ -359,9 +362,14 @@ func constructBlobPayload(chunks []*encoding.Chunk) (*kzg4844.Blob, *kzg4844.Poi
359362
}
360363

361364
// 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)
365373

366374
return blob, &z, nil
367375
}

0 commit comments

Comments
 (0)