This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
1e70a0a
commit b182304
Showing
3 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters