Skip to content

Commit

Permalink
Merge branch 'main' into feat-support-conditional-encode
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Aug 22, 2024
2 parents e49e96d + 3ad820b commit 6d08fd3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 86 deletions.
12 changes: 1 addition & 11 deletions encoding/codecv1/codecv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,7 @@ func (b *DABatch) BlobDataProof() ([]byte, error) {
return nil, fmt.Errorf("failed to create KZG proof at point, err: %w, z: %v", err, hex.EncodeToString(b.z[:]))
}

// Memory layout of ``_blobDataProof``:
// | z | y | kzg_commitment | kzg_proof |
// |---------|---------|----------------|-----------|
// | bytes32 | bytes32 | bytes48 | bytes48 |

values := []interface{}{*b.z, y, commitment, proof}
blobDataProofArgs, err := encoding.GetBlobDataProofArgs()
if err != nil {
return nil, fmt.Errorf("failed to get blob data proof args, err: %w", err)
}
return blobDataProofArgs.Pack(values...)
return encoding.BlobDataProofFromValues(*b.z, y, commitment, proof), nil
}

// Blob returns the blob of the batch.
Expand Down
12 changes: 1 addition & 11 deletions encoding/codecv2/codecv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,7 @@ func (b *DABatch) BlobDataProof() ([]byte, error) {
return nil, fmt.Errorf("failed to create KZG proof at point, err: %w, z: %v", err, hex.EncodeToString(b.z[:]))
}

// Memory layout of ``_blobDataProof``:
// | z | y | kzg_commitment | kzg_proof |
// |---------|---------|----------------|-----------|
// | bytes32 | bytes32 | bytes48 | bytes48 |

values := []interface{}{*b.z, y, commitment, proof}
blobDataProofArgs, err := encoding.GetBlobDataProofArgs()
if err != nil {
return nil, fmt.Errorf("failed to get blob data proof args, err: %w", err)
}
return blobDataProofArgs.Pack(values...)
return encoding.BlobDataProofFromValues(*b.z, y, commitment, proof), nil
}

// Blob returns the blob of the batch.
Expand Down
12 changes: 1 addition & 11 deletions encoding/codecv3/codecv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,7 @@ func (b *DABatch) BlobDataProofForPointEvaluation() ([]byte, error) {
return nil, fmt.Errorf("failed to create KZG proof at point, err: %w, z: %v", err, hex.EncodeToString(b.z[:]))
}

// Memory layout of ``_blobDataProof``:
// | z | y | kzg_commitment | kzg_proof |
// |---------|---------|----------------|-----------|
// | bytes32 | bytes32 | bytes48 | bytes48 |

values := []interface{}{*b.z, y, commitment, proof}
blobDataProofArgs, err := encoding.GetBlobDataProofArgs()
if err != nil {
return nil, fmt.Errorf("failed to get blob data proof args, err: %w", err)
}
return blobDataProofArgs.Pack(values...)
return encoding.BlobDataProofFromValues(*b.z, y, commitment, proof), nil
}

// Blob returns the blob of the batch.
Expand Down
12 changes: 1 addition & 11 deletions encoding/codecv4/codecv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,7 @@ func (b *DABatch) BlobDataProofForPointEvaluation() ([]byte, error) {
return nil, fmt.Errorf("failed to create KZG proof at point, err: %w, z: %v", err, hex.EncodeToString(b.z[:]))
}

// Memory layout of ``_blobDataProof``:
// | z | y | kzg_commitment | kzg_proof |
// |---------|---------|----------------|-----------|
// | bytes32 | bytes32 | bytes48 | bytes48 |

values := []interface{}{*b.z, y, commitment, proof}
blobDataProofArgs, err := encoding.GetBlobDataProofArgs()
if err != nil {
return nil, fmt.Errorf("failed to get blob data proof args, err: %w", err)
}
return blobDataProofArgs.Pack(values...)
return encoding.BlobDataProofFromValues(*b.z, y, commitment, proof), nil
}

// Blob returns the blob of the batch.
Expand Down
59 changes: 17 additions & 42 deletions encoding/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"encoding/binary"
"fmt"
"math/big"
"sync"

"github.com/scroll-tech/go-ethereum/accounts/abi"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/common/hexutil"
"github.com/scroll-tech/go-ethereum/core/types"
Expand Down Expand Up @@ -356,46 +354,6 @@ func MakeBlobCanonical(blobBytes []byte) (*kzg4844.Blob, error) {
return &blob, nil
}

var (
blobDataProofArgs *abi.Arguments
initBlobDataProofArgsOnce sync.Once
)

// GetBlobDataProofArgs gets the blob data proof arguments for batch commitment and returns error if initialization fails.
func GetBlobDataProofArgs() (*abi.Arguments, error) {
var initError error

initBlobDataProofArgsOnce.Do(func() {
// Initialize bytes32 type
bytes32Type, err := abi.NewType("bytes32", "bytes32", nil)
if err != nil {
initError = fmt.Errorf("failed to initialize abi type bytes32: %w", err)
return
}

// Initialize bytes48 type
bytes48Type, err := abi.NewType("bytes48", "bytes48", nil)
if err != nil {
initError = fmt.Errorf("failed to initialize abi type bytes48: %w", err)
return
}

// Successfully create the argument list
blobDataProofArgs = &abi.Arguments{
{Type: bytes32Type, Name: "z"},
{Type: bytes32Type, Name: "y"},
{Type: bytes48Type, Name: "kzg_commitment"},
{Type: bytes48Type, Name: "kzg_proof"},
}
})

if initError != nil {
return nil, initError
}

return blobDataProofArgs, nil
}

// CalculatePaddedBlobSize calculates the required size on blob storage
// where every 32 bytes can store only 31 bytes of actual data, with the first byte being zero.
func CalculatePaddedBlobSize(dataSize uint64) uint64 {
Expand Down Expand Up @@ -446,3 +404,20 @@ func ConstructBatchPayloadInBlob(chunks []*Chunk, MaxNumChunks uint64) ([]byte,
}
return batchBytes, nil
}

// BlobDataProofFromValues creates the blob data proof from the given values.
// Memory layout of ``_blobDataProof``:
// | z | y | kzg_commitment | kzg_proof |
// |---------|---------|----------------|-----------|
// | bytes32 | bytes32 | bytes48 | bytes48 |

func BlobDataProofFromValues(z kzg4844.Point, y kzg4844.Claim, commitment kzg4844.Commitment, proof kzg4844.Proof) []byte {
result := make([]byte, 32+32+48+48)

copy(result[0:32], z[:])
copy(result[32:64], y[:])
copy(result[64:112], commitment[:])
copy(result[112:160], proof[:])

return result
}

0 comments on commit 6d08fd3

Please sign in to comment.