Skip to content

Commit

Permalink
tweak structures
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Jul 17, 2024
1 parent 371be46 commit f1a8c1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 66 deletions.
86 changes: 22 additions & 64 deletions encoding/codecv3/codecv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package codecv3
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"

Expand All @@ -28,66 +27,21 @@ type DAChunk = codecv2.DAChunk
// DABatch contains metadata about a batch of DAChunks.
type DABatch struct {
// header
Version uint8 `json:"version"`
BatchIndex uint64 `json:"batch_index"`
L1MessagePopped uint64 `json:"l1_message_popped"`
TotalL1MessagePopped uint64 `json:"total_l1_message_popped"`
DataHash common.Hash `json:"data_hash"`
BlobVersionedHash common.Hash `json:"blob_versioned_hash"`
ParentBatchHash common.Hash `json:"parent_batch_hash"`
LastBlockTimestamp uint64 `json:"last_block_timestamp"`
BlobDataProof [64]byte `json:"-"`
Version uint8 `json:"version"`
BatchIndex uint64 `json:"batch_index"`
L1MessagePopped uint64 `json:"l1_message_popped"`
TotalL1MessagePopped uint64 `json:"total_l1_message_popped"`
DataHash common.Hash `json:"data_hash"`
BlobVersionedHash common.Hash `json:"blob_versioned_hash"`
ParentBatchHash common.Hash `json:"parent_batch_hash"`
LastBlockTimestamp uint64 `json:"last_block_timestamp"`
BlobDataProof [2]common.Hash `json:"blob_data_proof"`

// blob payload
blob *kzg4844.Blob
z *kzg4844.Point
}

// MarshalJSON implements the json.Marshaler interface.
func (b *DABatch) MarshalJSON() ([]byte, error) {
type Alias DABatch
proofHex := hex.EncodeToString(b.BlobDataProof[:])
proofArray := []string{
"0x" + proofHex[:64],
"0x" + proofHex[64:],
}
return json.Marshal(&struct {
*Alias
BlobDataProof []string `json:"blob_data_proof"`
}{
Alias: (*Alias)(b),
BlobDataProof: proofArray,
})
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func (b *DABatch) UnmarshalJSON(data []byte) error {
type Alias DABatch
aux := &struct {
*Alias
BlobDataProof []string `json:"blob_data_proof"`
}{
Alias: (*Alias)(b),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
if len(aux.BlobDataProof) == 2 {
var proofHex string
for _, s := range aux.BlobDataProof {
if len(s) > 2 {
proofHex += s[2:]
}
}
proofBytes, err := hex.DecodeString(proofHex)
if err != nil {
return err
}
copy(b.BlobDataProof[:], proofBytes)
}
return nil
}

// NewDABlock creates a new DABlock from the given encoding.Block and the total number of L1 messages popped before.
func NewDABlock(block *encoding.Block, totalL1MessagePoppedBefore uint64) (*DABlock, error) {
return codecv2.NewDABlock(block, totalL1MessagePoppedBefore)
Expand Down Expand Up @@ -184,7 +138,10 @@ func NewDABatchFromBytes(data []byte) (*DABatch, error) {
BlobVersionedHash: common.BytesToHash(data[57:89]),
ParentBatchHash: common.BytesToHash(data[89:121]),
LastBlockTimestamp: binary.BigEndian.Uint64(data[121:129]),
BlobDataProof: [64]byte(data[129:193]),
BlobDataProof: [2]common.Hash{
common.BytesToHash(data[129:161]),
common.BytesToHash(data[161:193]),
},
}

return b, nil
Expand All @@ -201,7 +158,8 @@ func (b *DABatch) Encode() []byte {
copy(batchBytes[57:89], b.BlobVersionedHash[:])
copy(batchBytes[89:121], b.ParentBatchHash[:])
binary.BigEndian.PutUint64(batchBytes[121:129], b.LastBlockTimestamp)
copy(batchBytes[129:193], b.BlobDataProof[:])
copy(batchBytes[129:161], b.BlobDataProof[0].Bytes())
copy(batchBytes[161:193], b.BlobDataProof[1].Bytes())
return batchBytes
}

Expand All @@ -212,26 +170,26 @@ func (b *DABatch) Hash() common.Hash {
}

// blobDataProofForPICircuit computes the abi-encoded blob verification data.
func (b *DABatch) blobDataProofForPICircuit() ([64]byte, error) {
func (b *DABatch) blobDataProofForPICircuit() ([2]common.Hash, error) {
if b.blob == nil {
return [64]byte{}, errors.New("called blobDataProofForPICircuit with empty blob")
return [2]common.Hash{}, errors.New("called blobDataProofForPICircuit with empty blob")
}
if b.z == nil {
return [64]byte{}, errors.New("called blobDataProofForPICircuit with empty z")
return [2]common.Hash{}, errors.New("called blobDataProofForPICircuit with empty z")
}

_, y, err := kzg4844.ComputeProof(b.blob, *b.z)
if err != nil {
return [64]byte{}, fmt.Errorf("failed to create KZG proof at point, err: %w, z: %v", err, hex.EncodeToString(b.z[:]))
return [2]common.Hash{}, fmt.Errorf("failed to create KZG proof at point, err: %w, z: %v", err, hex.EncodeToString(b.z[:]))
}

// Memory layout of result:
// | z | y |
// |---------|---------|
// | bytes32 | bytes32 |
var result [64]byte
copy(result[0:32], b.z[:])
copy(result[32:64], y[:])
var result [2]common.Hash
result[0] = common.BytesToHash(b.z[:])
result[1] = common.BytesToHash(y[:])

return result, nil
}
Expand Down
6 changes: 4 additions & 2 deletions encoding/codecv3/codecv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,8 @@ func TestCodecV3DABatchJSONMarshalUnmarshal(t *testing.T) {
assert.Equal(t, common.HexToHash("0x0120096572a3007f75c2a3ff82fa652976eae1c9428ec87ec258a8dcc84f488e"), batch.BlobVersionedHash)
assert.Equal(t, common.HexToHash("0xc37d3f6881f0ca6b02b1dc071483e02d0fe88cf2ff3663bb1ba9aa0dc034faee"), batch.ParentBatchHash)
assert.Equal(t, uint64(1721130505), batch.LastBlockTimestamp)
assert.Equal(t, common.Hex2Bytes("496b144866cffedfd71423639984bf0d9ad4309ff7e35693f1baef3cdaf1471e5eba7d42db109bfa124d1bc4dbcb421944b8aae6eae13a9d55eb460ce402785b"), batch.BlobDataProof[:])
assert.Equal(t, common.HexToHash("0x496b144866cffedfd71423639984bf0d9ad4309ff7e35693f1baef3cdaf1471e"), batch.BlobDataProof[0])
assert.Equal(t, common.HexToHash("0x5eba7d42db109bfa124d1bc4dbcb421944b8aae6eae13a9d55eb460ce402785b"), batch.BlobDataProof[1])

batchHash := batch.Hash()

Expand Down Expand Up @@ -944,7 +945,8 @@ func TestCodecV3DABatchJSONMarshalUnmarshal(t *testing.T) {
assert.Equal(t, uint64(1720174236), batch.LastBlockTimestamp)
assert.Equal(t, common.HexToHash("0xa1a518fa8e636dcb736629c296ed10341536c4cf850a3bc0a808d8d66d7f1ee6"), batch.DataHash)
assert.Equal(t, common.HexToHash("0x01c61b784ba4cd0fd398717fdc3470729d1a28d70632d520174c9e47614c80e1"), batch.BlobVersionedHash)
assert.Equal(t, common.Hex2Bytes("1ee03153fd007529c214a68934b2cfd51e8586bd142e157564328946a0fc8899118e196a9432c84c53db5a5a7bfbe13ef1ff8ffdba12fbccaf6360110eb71a10"), batch.BlobDataProof[:])
assert.Equal(t, common.HexToHash("0x1ee03153fd007529c214a68934b2cfd51e8586bd142e157564328946a0fc8899"), batch.BlobDataProof[0])
assert.Equal(t, common.HexToHash("0x118e196a9432c84c53db5a5a7bfbe13ef1ff8ffdba12fbccaf6360110eb71a10"), batch.BlobDataProof[1])

batchHash := batch.Hash()

Expand Down

0 comments on commit f1a8c1b

Please sign in to comment.