update kzg dependencies (#7432)

Update to latest erigon-lib which now has the kzg context and update
crate-crypto to latest.

---------

Co-authored-by: alex.sharov <AskAlexSharov@gmail.com>
This commit is contained in:
Roberto Bayardo 2023-05-03 20:36:26 -07:00 committed by GitHub
parent 0e0b12d68f
commit d0a6d20ca2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 79 deletions

View File

@ -9,16 +9,16 @@ import (
"math/big"
"time"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
"github.com/holiman/uint256"
"github.com/protolambda/ztyp/codec"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
libkzg "github.com/ledgerwatch/erigon-lib/crypto/kzg"
types2 "github.com/ledgerwatch/erigon-lib/types"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
"github.com/protolambda/ztyp/codec"
"github.com/ledgerwatch/erigon/crypto/kzg"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rlp"
)
@ -59,7 +59,7 @@ func (p *KZGCommitment) UnmarshalText(text []byte) error {
}
func (c KZGCommitment) ComputeVersionedHash() libcommon.Hash {
return libcommon.Hash(kzg.KZGToVersionedHash(gokzg4844.KZGCommitment(c)))
return libcommon.Hash(libkzg.KZGToVersionedHash(gokzg4844.KZGCommitment(c)))
}
// Compressed BLS12-381 G1 element
@ -272,20 +272,20 @@ func (blobs Blobs) ComputeCommitmentsAndProofs() (commitments []KZGCommitment, v
proofs = make([]KZGProof, len(blobs))
versionedHashes = make([]libcommon.Hash, len(blobs))
cryptoCtx := kzg.CrpytoCtx()
kzgCtx := libkzg.Ctx()
for i, blob := range blobs {
commitment, err := cryptoCtx.BlobToKZGCommitment(gokzg4844.Blob(blob))
commitment, err := kzgCtx.BlobToKZGCommitment(gokzg4844.Blob(blob), 1 /*numGoRoutines*/)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not convert blob to commitment: %v", err)
}
proof, err := cryptoCtx.ComputeBlobKZGProof(gokzg4844.Blob(blob), commitment)
proof, err := kzgCtx.ComputeBlobKZGProof(gokzg4844.Blob(blob), commitment, 1 /*numGoRoutnes*/)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not compute proof for blob: %v", err)
}
commitments[i] = KZGCommitment(commitment)
proofs[i] = KZGProof(proof)
versionedHashes[i] = libcommon.Hash(kzg.KZGToVersionedHash(commitment))
versionedHashes[i] = libcommon.Hash(libkzg.KZGToVersionedHash(commitment))
}
return commitments, versionedHashes, proofs, nil
@ -348,7 +348,7 @@ func (txw *BlobTxWrapper) ValidateBlobTransactionWrapper() error {
l2 := len(txw.Commitments)
l3 := len(txw.Blobs)
l4 := len(txw.Proofs)
if l1 != l2 || l2 != l3 || l1 != l4 {
if l1 != l2 || l1 != l3 || l1 != l4 {
return fmt.Errorf("lengths don't match %v %v %v %v", l1, l2, l3, l4)
}
// the following check isn't strictly necessary as it would be caught by data gas processing
@ -357,8 +357,8 @@ func (txw *BlobTxWrapper) ValidateBlobTransactionWrapper() error {
if l1 > params.MaxBlobsPerBlock {
return fmt.Errorf("number of blobs exceeds max: %v", l1)
}
cryptoCtx := kzg.CrpytoCtx()
err := cryptoCtx.VerifyBlobKZGProofBatch(toBlobs(txw.Blobs), toComms(txw.Commitments), toProofs(txw.Proofs))
kzgCtx := libkzg.Ctx()
err := kzgCtx.VerifyBlobKZGProofBatch(toBlobs(txw.Blobs), toComms(txw.Commitments), toProofs(txw.Proofs))
if err != nil {
return fmt.Errorf("error during proof verification: %v", err)
}

View File

@ -1,35 +1,18 @@
package kzg
import (
"crypto/sha256"
"errors"
"fmt"
"math/big"
"sync"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
libkzg "github.com/ledgerwatch/erigon-lib/crypto/kzg"
)
const (
BlobCommitmentVersionKZG uint8 = 0x01
FieldElementsPerBlob int = 4096
)
type VersionedHash [32]byte
type Root [32]byte
type Slot uint64
type BlobsSidecar struct {
BeaconBlockRoot Root
BeaconBlockSlot Slot
Blobs []gokzg4844.Blob
Proofs []gokzg4844.KZGProof
}
const (
BlobTxType = 5
PrecompileInputLength = 192
BlobVersionedHashesOffset = 258 // position of blob_versioned_hashes offset in a serialized blob tx, see TxPeekBlobVersionedHashes
PrecompileInputLength int = 192
)
var (
@ -39,32 +22,10 @@ var (
// The value that gets returned when the `verify_kzg_proof“ precompile is called
var precompileReturnValue [64]byte
var gCryptoCtx gokzg4844.Context
var initCryptoCtx sync.Once
// InitializeCrypytoCtx initializes the global context object returned via CryptoCtx
func InitializeCrypytoCtx() {
initCryptoCtx.Do(func() {
// Initialize context to match the configurations that the
// specs are using.
ctx, err := gokzg4844.NewContext4096Insecure1337()
if err != nil {
panic(fmt.Sprintf("could not create context, err : %v", err))
}
gCryptoCtx = *ctx
// Initialize the precompile return value
new(big.Int).SetUint64(gokzg4844.ScalarsPerBlob).FillBytes(precompileReturnValue[:32])
copy(precompileReturnValue[32:], gokzg4844.BlsModulus[:])
})
}
// CryptoCtx returns a context object stores all of the necessary configurations
// to allow one to create and verify blob proofs.
// This function is expensive to run if the crypto context isn't initialized, so it is recommended to
// pre-initialize by calling InitializeCryptoCtx
func CrpytoCtx() gokzg4844.Context {
InitializeCrypytoCtx()
return gCryptoCtx
func init() {
new(big.Int).SetUint64(gokzg4844.ScalarsPerBlob).FillBytes(precompileReturnValue[:32])
copy(precompileReturnValue[32:], gokzg4844.BlsModulus[:])
}
// PointEvaluationPrecompile implements point_evaluation_precompile from EIP-4844
@ -85,7 +46,7 @@ func PointEvaluationPrecompile(input []byte) ([]byte, error) {
// input kzg point: next 48 bytes
var dataKZG [48]byte
copy(dataKZG[:], input[96:144])
if KZGToVersionedHash(gokzg4844.KZGCommitment(dataKZG)) != VersionedHash(versionedHash) {
if libkzg.KZGToVersionedHash(dataKZG) != versionedHash {
return nil, errors.New("mismatched versioned hash")
}
@ -93,7 +54,7 @@ func PointEvaluationPrecompile(input []byte) ([]byte, error) {
var quotientKZG [48]byte
copy(quotientKZG[:], input[144:PrecompileInputLength])
cryptoCtx := CrpytoCtx()
cryptoCtx := libkzg.Ctx()
err := cryptoCtx.VerifyKZGProof(dataKZG, x, y, quotientKZG)
if err != nil {
return nil, fmt.Errorf("verify_kzg_proof error: %v", err)
@ -103,11 +64,3 @@ func PointEvaluationPrecompile(input []byte) ([]byte, error) {
return result[:], nil
}
// KZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844
func KZGToVersionedHash(kzg gokzg4844.KZGCommitment) VersionedHash {
h := sha256.Sum256(kzg[:])
h[0] = BlobCommitmentVersionKZG
return VersionedHash(h)
}

8
go.mod
View File

@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon
go 1.19
require (
github.com/ledgerwatch/erigon-lib v0.0.0-20230429180756-1b57bef163a0
github.com/ledgerwatch/erigon-lib v0.0.0-20230503064221-b182304fe445
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230404044759-5dec854ce336
github.com/ledgerwatch/log/v3 v3.7.0
github.com/ledgerwatch/secp256k1 v1.0.0
@ -23,9 +23,9 @@ require (
github.com/benesch/cgosymbolizer v0.0.0-20190515212042-bec6fe6e597b
github.com/btcsuite/btcd/btcec/v2 v2.1.3
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b
github.com/consensys/gnark-crypto v0.9.0
github.com/consensys/gnark-crypto v0.10.0
github.com/crate-crypto/go-ipa v0.0.0-20221111143132-9aa5d42120bc
github.com/crate-crypto/go-kzg-4844 v0.0.0-20230405223534-4364e2f9d209
github.com/crate-crypto/go-kzg-4844 v0.2.0
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v1.8.0
github.com/deckarep/golang-set/v2 v2.3.0
@ -124,7 +124,7 @@ require (
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/benbjohnson/immutable v0.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.2.2 // indirect
github.com/bits-and-blooms/bitset v1.5.0 // indirect
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect

16
go.sum
View File

@ -122,8 +122,8 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bits-and-blooms/bitset v1.2.2 h1:J5gbX05GpMdBjCvQ9MteIg2KKDExr7DrgK+Yc15FvIk=
github.com/bits-and-blooms/bitset v1.2.2/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bits-and-blooms/bitset v1.5.0 h1:NpE8frKRLGHIcEzkR+gZhiioW1+WbYV6fKwD6ZIpQT8=
github.com/bits-and-blooms/bitset v1.5.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/bradfitz/iter v0.0.0-20140124041915-454541ec3da2/go.mod h1:PyRFw1Lt2wKX4ZVSQ2mk+PeDa1rxyObEDlApuIsUKuo=
github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c/go.mod h1:PyRFw1Lt2wKX4ZVSQ2mk+PeDa1rxyObEDlApuIsUKuo=
@ -145,8 +145,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ=
github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
github.com/consensys/gnark-crypto v0.9.0 h1:xspjHTygkgHmX4Behn00VJUTfEGvs+e6lFlfERfA28E=
github.com/consensys/gnark-crypto v0.9.0/go.mod h1:CkbdF9hbRidRJYMRzmfX8TMOr95I2pYXRHF18MzRrvA=
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/containerd/cgroups v0.0.0-20201119153540-4cbc285b3327/go.mod h1:ZJeTFisyysqgcCdecO57Dj79RfL0LNeGiFUqLYQRYLE=
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
@ -160,8 +160,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHH
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/crate-crypto/go-ipa v0.0.0-20221111143132-9aa5d42120bc h1:mtR7MuscVeP/s0/ERWA2uSr5QOrRYy1pdvZqG1USfXI=
github.com/crate-crypto/go-ipa v0.0.0-20221111143132-9aa5d42120bc/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-kzg-4844 v0.0.0-20230405223534-4364e2f9d209 h1:OnTdosxWDRxchZa7uOT8zz1sm3TZQdCiqtj69wYGnH8=
github.com/crate-crypto/go-kzg-4844 v0.0.0-20230405223534-4364e2f9d209/go.mod h1:bsF9NlLDLBdRmnU0hiImPGjwoDSrjLRXKAP9vVT6IsI=
github.com/crate-crypto/go-kzg-4844 v0.2.0 h1:UVuHOE+5tIWrim4zf/Xaa43+MIsDCPyW76QhUpiMGj4=
github.com/crate-crypto/go-kzg-4844 v0.2.0/go.mod h1:SBP7ikXEgDnUPONgm33HtuDZEDtWa3L4QtN1ocJSEQ4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@ -440,8 +440,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/ledgerwatch/erigon-lib v0.0.0-20230429180756-1b57bef163a0 h1:OScZjxP4sf0UU2PRcFa6pnGPFmTYiIsEKlw4qfuGtqU=
github.com/ledgerwatch/erigon-lib v0.0.0-20230429180756-1b57bef163a0/go.mod h1:NMvXxA0hP92i39cdY4f79JYLfi7nJjWppX9Ati2KPbs=
github.com/ledgerwatch/erigon-lib v0.0.0-20230503064221-b182304fe445 h1:wDTp0HuP58ksl7/7XGdULYXSr/UtXULne+QAiQWgEpk=
github.com/ledgerwatch/erigon-lib v0.0.0-20230503064221-b182304fe445/go.mod h1:FI75QtzEIarMlAJaeWZp6JlrOyehEtS7XOYamdFmdcg=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230404044759-5dec854ce336 h1:Yxmt4Wyd0RCLr7UJJAl0ApCP/f5qkWfvHfgPbnI8ghM=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230404044759-5dec854ce336/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
github.com/ledgerwatch/log/v3 v3.7.0 h1:aFPEZdwZx4jzA3+/Pf8wNDN5tCI0cIolq/kfvgcM+og=