Skip to content

Commit 947fd3a

Browse files
crypto/kzg4844: add ComputeCells functionality (#31378)
For PeerDAS, we need to compute cell proofs. Both ckzg and gokzg support computing these cell proofs. This PR does the following: - Update the go-kzg library from "github.com/crate-crypto/go-kzg-4844" to "github.com/crate-crypto/go-eth-kzg" which will be the new upstream for go-kzg moving forward - Update ckzg from v1.0.0 to v2.0.1 and switch to /v2 - Updates the trusted setup to contain the g1 points both in lagrange and monomial form - Expose `ComputeCells` to compute the cell proofs
1 parent 0ac4a84 commit 947fd3a

File tree

8 files changed

+4189
-21
lines changed

8 files changed

+4189
-21
lines changed

cmd/utils/flags.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -1854,10 +1854,16 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
18541854
if ctx.String(CryptoKZGFlag.Name) != "gokzg" && ctx.String(CryptoKZGFlag.Name) != "ckzg" {
18551855
Fatalf("--%s flag must be 'gokzg' or 'ckzg'", CryptoKZGFlag.Name)
18561856
}
1857-
log.Info("Initializing the KZG library", "backend", ctx.String(CryptoKZGFlag.Name))
1858-
if err := kzg4844.UseCKZG(ctx.String(CryptoKZGFlag.Name) == "ckzg"); err != nil {
1859-
Fatalf("Failed to set KZG library implementation to %s: %v", ctx.String(CryptoKZGFlag.Name), err)
1860-
}
1857+
// The initialization of the KZG library can take up to 2 seconds
1858+
// We start this here in parallel, so it should be available
1859+
// once we start executing blocks. It's threadsafe.
1860+
go func() {
1861+
log.Info("Initializing the KZG library", "backend", ctx.String(CryptoKZGFlag.Name))
1862+
if err := kzg4844.UseCKZG(ctx.String(CryptoKZGFlag.Name) == "ckzg"); err != nil {
1863+
Fatalf("Failed to set KZG library implementation to %s: %v", ctx.String(CryptoKZGFlag.Name), err)
1864+
}
1865+
}()
1866+
18611867
// VM tracing config.
18621868
if ctx.IsSet(VMTraceFlag.Name) {
18631869
if name := ctx.String(VMTraceFlag.Name); name != "" {

crypto/kzg4844/kzg4844.go

+11
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ func VerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error {
149149
return gokzgVerifyBlobProof(blob, commitment, proof)
150150
}
151151

152+
// ComputeCellProofs returns the KZG cell proofs that are used to verify the blob against
153+
// the commitment.
154+
//
155+
// This method does not verify that the commitment is correct with respect to blob.
156+
func ComputeCellProofs(blob *Blob) ([]Proof, error) {
157+
if useCKZG.Load() {
158+
return ckzgComputeCellProofs(blob)
159+
}
160+
return gokzgComputeCellProofs(blob)
161+
}
162+
152163
// CalcBlobHashV1 calculates the 'versioned blob hash' of a commitment.
153164
// The given hasher must be a sha256 hash instance, otherwise the result will be invalid!
154165
func CalcBlobHashV1(hasher hash.Hash, commit *Commitment) (vh [32]byte) {

crypto/kzg4844/kzg4844_ckzg_cgo.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"errors"
2424
"sync"
2525

26-
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
27-
ckzg4844 "github.com/ethereum/c-kzg-4844/bindings/go"
26+
gokzg4844 "github.com/crate-crypto/go-eth-kzg"
27+
ckzg4844 "github.com/ethereum/c-kzg-4844/v2/bindings/go"
2828
"github.com/ethereum/go-ethereum/common/hexutil"
2929
)
3030

@@ -47,15 +47,21 @@ func ckzgInit() {
4747
if err = gokzg4844.CheckTrustedSetupIsWellFormed(params); err != nil {
4848
panic(err)
4949
}
50-
g1s := make([]byte, len(params.SetupG1Lagrange)*(len(params.SetupG1Lagrange[0])-2)/2)
50+
g1Lag := make([]byte, len(params.SetupG1Lagrange)*(len(params.SetupG1Lagrange[0])-2)/2)
5151
for i, g1 := range params.SetupG1Lagrange {
52+
copy(g1Lag[i*(len(g1)-2)/2:], hexutil.MustDecode(g1))
53+
}
54+
g1s := make([]byte, len(params.SetupG1Monomial)*(len(params.SetupG1Monomial[0])-2)/2)
55+
for i, g1 := range params.SetupG1Monomial {
5256
copy(g1s[i*(len(g1)-2)/2:], hexutil.MustDecode(g1))
5357
}
5458
g2s := make([]byte, len(params.SetupG2)*(len(params.SetupG2[0])-2)/2)
5559
for i, g2 := range params.SetupG2 {
5660
copy(g2s[i*(len(g2)-2)/2:], hexutil.MustDecode(g2))
5761
}
58-
if err = ckzg4844.LoadTrustedSetup(g1s, g2s); err != nil {
62+
// The last parameter determines the multiplication table, see https://notes.ethereum.org/@jtraglia/windowed_multiplications
63+
// I think 6 is an decent compromise between size and speed
64+
if err = ckzg4844.LoadTrustedSetup(g1s, g1Lag, g2s, 6); err != nil {
5965
panic(err)
6066
}
6167
}
@@ -125,3 +131,21 @@ func ckzgVerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error {
125131
}
126132
return nil
127133
}
134+
135+
// ckzgComputeCellProofs returns the KZG cell proofs that are used to verify the blob against
136+
// the commitment.
137+
//
138+
// This method does not verify that the commitment is correct with respect to blob.
139+
func ckzgComputeCellProofs(blob *Blob) ([]Proof, error) {
140+
ckzgIniter.Do(ckzgInit)
141+
142+
_, proofs, err := ckzg4844.ComputeCellsAndKZGProofs((*ckzg4844.Blob)(blob))
143+
if err != nil {
144+
return []Proof{}, err
145+
}
146+
var p []Proof
147+
for _, proof := range proofs {
148+
p = append(p, (Proof)(proof))
149+
}
150+
return p, nil
151+
}

crypto/kzg4844/kzg4844_ckzg_nocgo.go

+8
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,11 @@ func ckzgComputeBlobProof(blob *Blob, commitment Commitment) (Proof, error) {
6060
func ckzgVerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error {
6161
panic("unsupported platform")
6262
}
63+
64+
// ckzgComputeCellProofs returns the KZG cell proofs that are used to verify the blob against
65+
// the commitment.
66+
//
67+
// This method does not verify that the commitment is correct with respect to blob.
68+
func ckzgComputeCellProofs(blob *Blob) ([]Proof, error) {
69+
panic("unsupported platform")
70+
}

crypto/kzg4844/kzg4844_gokzg.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"encoding/json"
2121
"sync"
2222

23-
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
23+
gokzg4844 "github.com/crate-crypto/go-eth-kzg"
2424
)
2525

2626
// context is the crypto primitive pre-seeded with the trusted setup parameters.
@@ -96,3 +96,21 @@ func gokzgVerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error
9696

9797
return context.VerifyBlobKZGProof((*gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), (gokzg4844.KZGProof)(proof))
9898
}
99+
100+
// gokzgComputeCellProofs returns the KZG cell proofs that are used to verify the blob against
101+
// the commitment.
102+
//
103+
// This method does not verify that the commitment is correct with respect to blob.
104+
func gokzgComputeCellProofs(blob *Blob) ([]Proof, error) {
105+
gokzgIniter.Do(gokzgInit)
106+
107+
_, proofs, err := context.ComputeCellsAndKZGProofs((*gokzg4844.Blob)(blob), 0)
108+
if err != nil {
109+
return []Proof{}, err
110+
}
111+
var p []Proof
112+
for _, proof := range proofs {
113+
p = append(p, (Proof)(proof))
114+
}
115+
return p, nil
116+
}

0 commit comments

Comments
 (0)