2023-05-03 06:42:21 +00:00
|
|
|
package kzg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BlobCommitmentVersionKZG uint8 = 0x01
|
|
|
|
)
|
|
|
|
|
|
|
|
type VersionedHash [32]byte
|
|
|
|
|
2023-06-07 15:29:33 +00:00
|
|
|
var gCryptoCtx *gokzg4844.Context
|
2023-05-03 06:42:21 +00:00
|
|
|
var initCryptoCtx sync.Once
|
|
|
|
|
|
|
|
// InitializeCryptoCtx initializes the global context object returned via CryptoCtx
|
|
|
|
func InitializeCryptoCtx() {
|
|
|
|
initCryptoCtx.Do(func() {
|
2023-06-07 15:29:33 +00:00
|
|
|
var err error
|
2023-05-03 06:42:21 +00:00
|
|
|
// Initialize context to match the configurations that the
|
|
|
|
// specs are using.
|
2023-06-07 15:29:33 +00:00
|
|
|
gCryptoCtx, err = gokzg4844.NewContext4096Insecure1337()
|
2023-05-03 06:42:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("could not create context, err : %v", err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2023-06-07 15:29:33 +00:00
|
|
|
func Ctx() *gokzg4844.Context {
|
2023-05-03 06:42:21 +00:00
|
|
|
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)
|
|
|
|
}
|