Skip to content

Commit

Permalink
chore(dep): upgrade go kzg 4844 (#718)
Browse files Browse the repository at this point in the history
* crypto/kz4844: pass blobs by ref (ethereum#29050)

This change makes use of the following underlying changes to the kzg-libraries in order to avoid passing large things on the stack:

- c-kzg: ethereum/c-kzg-4844#393 and
- go-kzg: https://github.com/crate-crypto/go-kzg-4844/pull/63

* update version patch

* run go mod tidy

* use blob reference

---------

Co-authored-by: Martin HS <martin@swende.se>
Co-authored-by: Mason Liang <mason@scroll.io>
  • Loading branch information
3 people authored Apr 24, 2024
1 parent 246955a commit 90221d6
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 33 deletions.
8 changes: 4 additions & 4 deletions crypto/kzg4844/kzg4844.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func UseCKZG(use bool) error {
}

// BlobToCommitment creates a small commitment out of a data blob.
func BlobToCommitment(blob Blob) (Commitment, error) {
func BlobToCommitment(blob *Blob) (Commitment, error) {
if useCKZG.Load() {
return ckzgBlobToCommitment(blob)
}
Expand All @@ -75,7 +75,7 @@ func BlobToCommitment(blob Blob) (Commitment, error) {

// ComputeProof computes the KZG proof at the given point for the polynomial
// represented by the blob.
func ComputeProof(blob Blob, point Point) (Proof, Claim, error) {
func ComputeProof(blob *Blob, point Point) (Proof, Claim, error) {
if useCKZG.Load() {
return ckzgComputeProof(blob, point)
}
Expand All @@ -95,15 +95,15 @@ func VerifyProof(commitment Commitment, point Point, claim Claim, proof Proof) e
// the commitment.
//
// This method does not verify that the commitment is correct with respect to blob.
func ComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
func ComputeBlobProof(blob *Blob, commitment Commitment) (Proof, error) {
if useCKZG.Load() {
return ckzgComputeBlobProof(blob, commitment)
}
return gokzgComputeBlobProof(blob, commitment)
}

// VerifyBlobProof verifies that the blob data corresponds to the provided commitment.
func VerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
func VerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error {
if useCKZG.Load() {
return ckzgVerifyBlobProof(blob, commitment, proof)
}
Expand Down
16 changes: 8 additions & 8 deletions crypto/kzg4844/kzg4844_ckzg_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ func ckzgInit() {
}

// ckzgBlobToCommitment creates a small commitment out of a data blob.
func ckzgBlobToCommitment(blob Blob) (Commitment, error) {
func ckzgBlobToCommitment(blob *Blob) (Commitment, error) {
ckzgIniter.Do(ckzgInit)

commitment, err := ckzg4844.BlobToKZGCommitment((ckzg4844.Blob)(blob))
commitment, err := ckzg4844.BlobToKZGCommitment((*ckzg4844.Blob)(blob))
if err != nil {
return Commitment{}, err
}
Expand All @@ -74,10 +74,10 @@ func ckzgBlobToCommitment(blob Blob) (Commitment, error) {

// ckzgComputeProof computes the KZG proof at the given point for the polynomial
// represented by the blob.
func ckzgComputeProof(blob Blob, point Point) (Proof, Claim, error) {
func ckzgComputeProof(blob *Blob, point Point) (Proof, Claim, error) {
ckzgIniter.Do(ckzgInit)

proof, claim, err := ckzg4844.ComputeKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes32)(point))
proof, claim, err := ckzg4844.ComputeKZGProof((*ckzg4844.Blob)(blob), (ckzg4844.Bytes32)(point))
if err != nil {
return Proof{}, Claim{}, err
}
Expand All @@ -103,21 +103,21 @@ func ckzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proo
// the commitment.
//
// This method does not verify that the commitment is correct with respect to blob.
func ckzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
func ckzgComputeBlobProof(blob *Blob, commitment Commitment) (Proof, error) {
ckzgIniter.Do(ckzgInit)

proof, err := ckzg4844.ComputeBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment))
proof, err := ckzg4844.ComputeBlobKZGProof((*ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment))
if err != nil {
return Proof{}, err
}
return (Proof)(proof), nil
}

// ckzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment.
func ckzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
func ckzgVerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error {
ckzgIniter.Do(ckzgInit)

valid, err := ckzg4844.VerifyBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes48)(proof))
valid, err := ckzg4844.VerifyBlobKZGProof((*ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes48)(proof))
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions crypto/kzg4844/kzg4844_ckzg_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func ckzgInit() {
}

// ckzgBlobToCommitment creates a small commitment out of a data blob.
func ckzgBlobToCommitment(blob Blob) (Commitment, error) {
func ckzgBlobToCommitment(blob *Blob) (Commitment, error) {
panic("unsupported platform")
}

// ckzgComputeProof computes the KZG proof at the given point for the polynomial
// represented by the blob.
func ckzgComputeProof(blob Blob, point Point) (Proof, Claim, error) {
func ckzgComputeProof(blob *Blob, point Point) (Proof, Claim, error) {
panic("unsupported platform")
}

Expand All @@ -52,11 +52,11 @@ func ckzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proo
// the commitment.
//
// This method does not verify that the commitment is correct with respect to blob.
func ckzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
func ckzgComputeBlobProof(blob *Blob, commitment Commitment) (Proof, error) {
panic("unsupported platform")
}

// ckzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment.
func ckzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
func ckzgVerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error {
panic("unsupported platform")
}
16 changes: 8 additions & 8 deletions crypto/kzg4844/kzg4844_gokzg.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func gokzgInit() {
}

// gokzgBlobToCommitment creates a small commitment out of a data blob.
func gokzgBlobToCommitment(blob Blob) (Commitment, error) {
func gokzgBlobToCommitment(blob *Blob) (Commitment, error) {
gokzgIniter.Do(gokzgInit)

commitment, err := context.BlobToKZGCommitment((gokzg4844.Blob)(blob), 0)
commitment, err := context.BlobToKZGCommitment((*gokzg4844.Blob)(blob), 0)
if err != nil {
return Commitment{}, err
}
Expand All @@ -58,10 +58,10 @@ func gokzgBlobToCommitment(blob Blob) (Commitment, error) {

// gokzgComputeProof computes the KZG proof at the given point for the polynomial
// represented by the blob.
func gokzgComputeProof(blob Blob, point Point) (Proof, Claim, error) {
func gokzgComputeProof(blob *Blob, point Point) (Proof, Claim, error) {
gokzgIniter.Do(gokzgInit)

proof, claim, err := context.ComputeKZGProof((gokzg4844.Blob)(blob), (gokzg4844.Scalar)(point), 0)
proof, claim, err := context.ComputeKZGProof((*gokzg4844.Blob)(blob), (gokzg4844.Scalar)(point), 0)
if err != nil {
return Proof{}, Claim{}, err
}
Expand All @@ -80,19 +80,19 @@ func gokzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Pro
// the commitment.
//
// This method does not verify that the commitment is correct with respect to blob.
func gokzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
func gokzgComputeBlobProof(blob *Blob, commitment Commitment) (Proof, error) {
gokzgIniter.Do(gokzgInit)

proof, err := context.ComputeBlobKZGProof((gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), 0)
proof, err := context.ComputeBlobKZGProof((*gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), 0)
if err != nil {
return Proof{}, err
}
return (Proof)(proof), nil
}

// gokzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment.
func gokzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
func gokzgVerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error {
gokzgIniter.Do(gokzgInit)

return context.VerifyBlobKZGProof((gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), (gokzg4844.KZGProof)(proof))
return context.VerifyBlobKZGProof((*gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), (gokzg4844.KZGProof)(proof))
}
4 changes: 2 additions & 2 deletions crypto/kzg4844/kzg4844_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func randFieldElement() [32]byte {
return gokzg4844.SerializeScalar(r)
}

func randBlob() Blob {
func randBlob() *Blob {
var blob Blob
for i := 0; i < len(blob); i += gokzg4844.SerializedScalarSize {
fieldElementBytes := randFieldElement()
copy(blob[i:i+gokzg4844.SerializedScalarSize], fieldElementBytes[:])
}
return blob
return &blob
}

func TestCKZGWithPoint(t *testing.T) { testKZGWithPoint(t, true) }
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/cespare/cp v0.1.0
github.com/cloudflare/cloudflare-go v0.14.0
github.com/consensys/gnark-crypto v0.10.0
github.com/crate-crypto/go-kzg-4844 v0.7.0
github.com/crate-crypto/go-kzg-4844 v1.0.0
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH
github.com/consensys/gnark-crypto v0.10.0 h1:zRh22SR7o4K35SoNqouS9J/TKHTyU2QWaj5ldehyXtA=
github.com/consensys/gnark-crypto v0.10.0/go.mod h1:Iq/P3HHl0ElSjsg2E1gsMwhAyxnxoKK5nVyZKd+/KhU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/crate-crypto/go-kzg-4844 v0.7.0 h1:C0vgZRk4q4EZ/JgPfzuSoxdCq3C3mOZMBShovmncxvA=
github.com/crate-crypto/go-kzg-4844 v0.7.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc=
github.com/crate-crypto/go-kzg-4844 v1.0.0 h1:TsSgHwrkTKecKJ4kadtHi4b3xHW5dCFUDFnUp1TsawI=
github.com/crate-crypto/go-kzg-4844 v1.0.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 3 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release
VersionPatch = 1 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down
6 changes: 3 additions & 3 deletions rollup/types/encoding/codecv1/codecv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func constructBlobPayload(chunks []*encoding.Chunk) (*kzg4844.Blob, common.Hash,
}

// compute blob versioned hash
c, err := kzg4844.BlobToCommitment(*blob)
c, err := kzg4844.BlobToCommitment(blob)
if err != nil {
return nil, common.Hash{}, nil, fmt.Errorf("failed to create blob commitment")
}
Expand Down Expand Up @@ -452,12 +452,12 @@ func (b *DABatch) BlobDataProof() ([]byte, error) {
return nil, errors.New("called BlobDataProof with empty z")
}

commitment, err := kzg4844.BlobToCommitment(*b.blob)
commitment, err := kzg4844.BlobToCommitment(b.blob)
if err != nil {
return nil, fmt.Errorf("failed to create blob commitment")
}

proof, y, err := kzg4844.ComputeProof(*b.blob, *b.z)
proof, y, err := kzg4844.ComputeProof(b.blob, *b.z)
if err != nil {
log.Crit("failed to create KZG proof at point", "err", err, "z", hex.EncodeToString(b.z[:]))
}
Expand Down

0 comments on commit 90221d6

Please sign in to comment.