erigon-pulse/crypto/kzg/kzg.go
Roberto Bayardo b182304fe4
add kzg context to erigon-lib (#984)
Both the core erigon client and the erigon-lib txpool need to access kzg
functionality for EIP-4844, so moving this here where it can be reused
across both.
2023-05-03 06:42:21 +00:00

48 lines
1.2 KiB
Go

package kzg
import (
"crypto/sha256"
"fmt"
"sync"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
)
const (
BlobCommitmentVersionKZG uint8 = 0x01
)
type VersionedHash [32]byte
var gCryptoCtx gokzg4844.Context
var initCryptoCtx sync.Once
// InitializeCryptoCtx initializes the global context object returned via CryptoCtx
func InitializeCryptoCtx() {
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
})
}
// Ctx returns a context object that 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 production services should pre-initialize by calling InitializeCryptoCtx.
func Ctx() gokzg4844.Context {
InitializeCryptoCtx()
return gCryptoCtx
}
// 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)
}