Skip to content

Commit 247ebd6

Browse files
committed
crypto/kzg4844: do lazy init in all ckzg funcs (ethereum#27679)
* crypto/kzg4844: remove unnecessary init call & fix typo * Fix kzg4844 tests/benchmarks * Make init lazy & revert changes to tests
1 parent 541ddee commit 247ebd6

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

crypto/kzg4844/kzg4844.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func UseCKZG(use bool) error {
5454
useCKZG.Store(use)
5555

5656
// Initializing the library can take 2-4 seconds - and can potentially crash
57-
// on CKZG and non-ADX CPUs - so might as well so it now and don't wait until
57+
// on CKZG and non-ADX CPUs - so might as well do it now and don't wait until
5858
// a crypto operation is actually needed live.
5959
if use {
6060
ckzgIniter.Do(ckzgInit)

crypto/kzg4844/kzg4844_ckzg_cgo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func ckzgBlobToCommitment(blob Blob) (Commitment, error) {
7474
// ckzgComputeProof computes the KZG proof at the given point for the polynomial
7575
// represented by the blob.
7676
func ckzgComputeProof(blob Blob, point Point) (Proof, Claim, error) {
77+
ckzgIniter.Do(ckzgInit)
78+
7779
proof, claim, err := ckzg4844.ComputeKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes32)(point))
7880
if err != nil {
7981
return Proof{}, Claim{}, err
@@ -84,6 +86,8 @@ func ckzgComputeProof(blob Blob, point Point) (Proof, Claim, error) {
8486
// ckzgVerifyProof verifies the KZG proof that the polynomial represented by the blob
8587
// evaluated at the given point is the claimed value.
8688
func ckzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proof) error {
89+
ckzgIniter.Do(ckzgInit)
90+
8791
valid, err := ckzg4844.VerifyKZGProof((ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes32)(point), (ckzg4844.Bytes32)(claim), (ckzg4844.Bytes48)(proof))
8892
if err != nil {
8993
return err
@@ -99,6 +103,8 @@ func ckzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proo
99103
//
100104
// This method does not verify that the commitment is correct with respect to blob.
101105
func ckzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
106+
ckzgIniter.Do(ckzgInit)
107+
102108
proof, err := ckzg4844.ComputeBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment))
103109
if err != nil {
104110
return Proof{}, err
@@ -108,6 +114,8 @@ func ckzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
108114

109115
// ckzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment.
110116
func ckzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
117+
ckzgIniter.Do(ckzgInit)
118+
111119
valid, err := ckzg4844.VerifyBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes48)(proof))
112120
if err != nil {
113121
return err

crypto/kzg4844/kzg4844_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func randBlob() Blob {
4747

4848
func TestCKZGWithPoint(t *testing.T) { testKZGWithPoint(t, true) }
4949
func TestGoKZGWithPoint(t *testing.T) { testKZGWithPoint(t, false) }
50-
5150
func testKZGWithPoint(t *testing.T, ckzg bool) {
5251
if ckzg && !ckzgAvailable {
5352
t.Skip("CKZG unavailable in this test build")
@@ -73,7 +72,6 @@ func testKZGWithPoint(t *testing.T, ckzg bool) {
7372

7473
func TestCKZGWithBlob(t *testing.T) { testKZGWithBlob(t, true) }
7574
func TestGoKZGWithBlob(t *testing.T) { testKZGWithBlob(t, false) }
76-
7775
func testKZGWithBlob(t *testing.T, ckzg bool) {
7876
if ckzg && !ckzgAvailable {
7977
t.Skip("CKZG unavailable in this test build")
@@ -106,6 +104,8 @@ func benchmarkBlobToCommitment(b *testing.B, ckzg bool) {
106104
useCKZG.Store(ckzg)
107105

108106
blob := randBlob()
107+
108+
b.ResetTimer()
109109
for i := 0; i < b.N; i++ {
110110
BlobToCommitment(blob)
111111
}
@@ -124,6 +124,8 @@ func benchmarkComputeProof(b *testing.B, ckzg bool) {
124124
blob = randBlob()
125125
point = randFieldElement()
126126
)
127+
128+
b.ResetTimer()
127129
for i := 0; i < b.N; i++ {
128130
ComputeProof(blob, point)
129131
}
@@ -144,6 +146,8 @@ func benchmarkVerifyProof(b *testing.B, ckzg bool) {
144146
commitment, _ = BlobToCommitment(blob)
145147
proof, claim, _ = ComputeProof(blob, point)
146148
)
149+
150+
b.ResetTimer()
147151
for i := 0; i < b.N; i++ {
148152
VerifyProof(commitment, point, claim, proof)
149153
}
@@ -162,6 +166,8 @@ func benchmarkComputeBlobProof(b *testing.B, ckzg bool) {
162166
blob = randBlob()
163167
commitment, _ = BlobToCommitment(blob)
164168
)
169+
170+
b.ResetTimer()
165171
for i := 0; i < b.N; i++ {
166172
ComputeBlobProof(blob, commitment)
167173
}
@@ -181,6 +187,8 @@ func benchmarkVerifyBlobProof(b *testing.B, ckzg bool) {
181187
commitment, _ = BlobToCommitment(blob)
182188
proof, _ = ComputeBlobProof(blob, commitment)
183189
)
190+
191+
b.ResetTimer()
184192
for i := 0; i < b.N; i++ {
185193
VerifyBlobProof(blob, commitment, proof)
186194
}

0 commit comments

Comments
 (0)