Skip to content

Commit

Permalink
Replaces go-kzg with gnark-kzg (ethereum#95)
Browse files Browse the repository at this point in the history
* change go-kzg for gnark-kzg

* go mod

* Fix blob_tx MarshalText

---------

Co-authored-by: inphi <mlaw2501@gmail.com>
  • Loading branch information
kevaundray and Inphi authored Feb 1, 2023
1 parent 5e5214d commit 39ef7d7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 89 deletions.
104 changes: 43 additions & 61 deletions core/types/data_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"fmt"
"io"

api "github.com/crate-crypto/go-proto-danksharding-crypto"
"github.com/crate-crypto/go-proto-danksharding-crypto/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/params"
"github.com/protolambda/go-kzg/eth"

"github.com/protolambda/ztyp/codec"
)

Expand Down Expand Up @@ -49,7 +51,7 @@ func (p *KZGCommitment) UnmarshalText(text []byte) error {
}

func (c KZGCommitment) ComputeVersionedHash() common.Hash {
return common.Hash(eth.KZGToVersionedHash(eth.KZGCommitment(c)))
return common.Hash(eth.KZGToVersionedHash(api.KZGCommitment(c)))
}

// Compressed BLS12-381 G1 element
Expand Down Expand Up @@ -103,38 +105,26 @@ func (p *BLSFieldElement) UnmarshalText(text []byte) error {
}

// Blob data
type Blob [params.FieldElementsPerBlob]BLSFieldElement

// eth.Blob interface
func (blob Blob) Len() int {
return len(blob)
}

// eth.Blob interface
func (blob Blob) At(i int) [32]byte {
return [32]byte(blob[i])
}
type Blob [params.FieldElementsPerBlob * 32]byte

func (blob *Blob) Deserialize(dr *codec.DecodingReader) error {
if blob == nil {
return errors.New("cannot decode ssz into nil Blob")
}
for i := uint64(0); i < params.FieldElementsPerBlob; i++ {
// TODO: do we want to check if each field element is within range?
if _, err := dr.Read(blob[i][:]); err != nil {
return err
}

// We treat the blob as an opaque sequence of bytes
// and therefore we do not do any validation related to field
// elements
if _, err := dr.Read(blob[:]); err != nil {
return err
}

return nil
}

func (blob *Blob) Serialize(w *codec.EncodingWriter) error {
for i := range blob {
if err := w.Write(blob[i][:]); err != nil {
return err
}
}
return nil
return w.Write(blob[:])

}

func (blob *Blob) ByteLength() (out uint64) {
Expand All @@ -148,11 +138,8 @@ func (blob *Blob) FixedLength() uint64 {
func (blob *Blob) MarshalText() ([]byte, error) {
out := make([]byte, 2+params.FieldElementsPerBlob*32*2)
copy(out[:2], "0x")
j := 2
for _, elem := range blob {
hex.Encode(out[j:j+64], elem[:])
j += 64
}
hex.Encode(out[2:], blob[:])

return out, nil
}

Expand All @@ -175,27 +162,15 @@ func (blob *Blob) UnmarshalText(text []byte) error {
if !(text[0] == '0' && text[1] == 'x') {
return fmt.Errorf("expected '0x' prefix in Blob string")
}
j := 0
for i := 2; i < l; i += 64 {
if _, err := hex.Decode(blob[j][:], text[i:i+64]); err != nil {
return fmt.Errorf("blob item %d is not formatted correctly: %v", j, err)
}
j += 1
if _, err := hex.Decode(blob[2:], text); err != nil {
return fmt.Errorf("blob is not formatted correctly: %v", err)
}

return nil
}

type BlobKzgs []KZGCommitment

// eth.KZGCommitmentSequence interface
func (bk BlobKzgs) Len() int {
return len(bk)
}

func (bk BlobKzgs) At(i int) eth.KZGCommitment {
return eth.KZGCommitment(bk[i])
}

func (li *BlobKzgs) Deserialize(dr *codec.DecodingReader) error {
return dr.List(func() codec.Deserializable {
i := len(*li)
Expand Down Expand Up @@ -226,16 +201,6 @@ func (li BlobKzgs) copy() BlobKzgs {

type Blobs []Blob

// eth.BlobSequence interface
func (blobs Blobs) Len() int {
return len(blobs)
}

// eth.BlobSequence interface
func (blobs Blobs) At(i int) eth.Blob {
return blobs[i]
}

func (a *Blobs) Deserialize(dr *codec.DecodingReader) error {
return dr.List(func() codec.Deserializable {
i := len(*a)
Expand Down Expand Up @@ -268,16 +233,18 @@ func (blobs Blobs) copy() Blobs {
func (blobs Blobs) ComputeCommitmentsAndAggregatedProof() (commitments []KZGCommitment, versionedHashes []common.Hash, aggregatedProof KZGProof, err error) {
commitments = make([]KZGCommitment, len(blobs))
versionedHashes = make([]common.Hash, len(blobs))

for i, blob := range blobs {
c, ok := eth.BlobToKZGCommitment(blob)
if !ok {
return nil, nil, KZGProof{}, errors.New("could not convert blob to commitment")
commitment, err := eth.CryptoCtx.BlobToCommitment(blob)
if err != nil {

return nil, nil, KZGProof{}, fmt.Errorf("could not convert blob to commitment: %v", err)
}
commitments[i] = KZGCommitment(c)
versionedHashes[i] = common.Hash(eth.KZGToVersionedHash(c))
commitments[i] = KZGCommitment(commitment)
versionedHashes[i] = common.Hash(eth.KZGToVersionedHash(commitment))
}

proof, err := eth.ComputeAggregateKZGProof(blobs)
proof, _, err := eth.CryptoCtx.ComputeAggregateKZGProof(toBlobs(blobs))
if err != nil {
return nil, nil, KZGProof{}, err
}
Expand All @@ -286,6 +253,21 @@ func (blobs Blobs) ComputeCommitmentsAndAggregatedProof() (commitments []KZGComm
return commitments, versionedHashes, kzgProof, nil
}

func toBlobs(_blobs Blobs) []api.Blob {
blobs := make([]api.Blob, len(_blobs))
for i, _blob := range _blobs {
blobs[i] = api.Blob(_blob)
}
return blobs
}
func toComms(_comms BlobKzgs) []api.KZGCommitment {
comms := make([]api.KZGCommitment, len(_comms))
for i, _comm := range _comms {
comms[i] = api.KZGCommitment(_comm)
}
return comms
}

type BlobTxWrapper struct {
Tx SignedBlobTx
BlobKzgs BlobKzgs
Expand Down Expand Up @@ -338,7 +320,7 @@ func (b *BlobTxWrapData) validateBlobTransactionWrapper(inner TxData) error {
if l1 > params.MaxBlobsPerBlock {
return fmt.Errorf("number of blobs exceeds max: %v", l1)
}
ok, err := eth.VerifyAggregateKZGProof(b.Blobs, b.BlobKzgs, eth.KZGProof(b.KzgAggregatedProof))
err := eth.CryptoCtx.VerifyAggregateKZGProof(toBlobs(b.Blobs), api.KZGProof(b.KzgAggregatedProof), toComms(b.BlobKzgs))
if err != nil {
return fmt.Errorf("error during proof verification: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"time"

"github.com/holiman/uint256"
"github.com/protolambda/go-kzg/bls"
"github.com/protolambda/ztyp/view"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -493,8 +492,8 @@ func TestTransactionCoding(t *testing.T) {
BlobVersionedHashes: VersionedHashesView{common.HexToHash("0x010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c444014")},
},
}
var kzgProof KZGProof
copy(kzgProof[:], bls.ToCompressedG1((*bls.G1Point)(&bls.ZeroG1)))
// This is the identity point serialised
var kzgProof KZGProof = [48]byte{192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
wrapData = &BlobTxWrapData{
BlobKzgs: BlobKzgs{KZGCommitment{0: 0xc0}},
Blobs: Blobs{Blob{}},
Expand Down
2 changes: 1 addition & 1 deletion core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"math/big"

"github.com/crate-crypto/go-proto-danksharding-crypto/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -30,7 +31,6 @@ import (
"github.com/ethereum/go-ethereum/crypto/bn256"
"github.com/ethereum/go-ethereum/params"
big2 "github.com/holiman/big"
"github.com/protolambda/go-kzg/eth"
"golang.org/x/crypto/ripemd160"
)

Expand Down
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.2.0
github.com/cespare/cp v0.1.0
github.com/cloudflare/cloudflare-go v0.14.0
github.com/consensys/gnark-crypto v0.4.1-0.20210426202927-39ac3d4b3f1f
github.com/consensys/gnark-crypto v0.8.0
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set/v2 v2.1.0
github.com/docker/docker v1.6.2
Expand Down Expand Up @@ -55,22 +55,24 @@ require (
github.com/rs/cors v1.7.0
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
github.com/status-im/keycard-go v0.2.0
github.com/stretchr/testify v1.7.2
github.com/stretchr/testify v1.8.0
github.com/supranational/blst v0.3.8-0.20220526154634-513d2456b344
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tyler-smith/go-bip39 v1.1.0
github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa
golang.org/x/crypto v0.1.0
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/sys v0.2.0
golang.org/x/sys v0.4.0
golang.org/x/term v0.1.0
golang.org/x/text v0.4.0
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
golang.org/x/tools v0.1.12
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce
)

require github.com/crate-crypto/go-proto-danksharding-crypto v0.0.0-20230127142520-613d3342e817

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v0.8.3 // indirect
Expand All @@ -81,6 +83,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.1.1 // indirect
github.com/aws/smithy-go v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20220523130400-f11357ae11c7 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
Expand All @@ -95,6 +98,7 @@ require (
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/pointerstructure v1.2.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -109,4 +113,5 @@ require (
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit 39ef7d7

Please sign in to comment.