Skip to content

Commit 6c556bd

Browse files
remove legacy kzg verification, put all initialization on top (#44)
1 parent c702b48 commit 6c556bd

File tree

2 files changed

+23
-104
lines changed

2 files changed

+23
-104
lines changed

crypto/kzg/kzg.go

Lines changed: 23 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ var kzgSetupLagrange []bls.G1Point
2222
// KZG CRS for G1 (only used in tests (for proof creation))
2323
var KzgSetupG1 []bls.G1Point
2424

25+
type JSONTrustedSetup struct {
26+
SetupG1 []bls.G1Point
27+
SetupG2 []bls.G2Point
28+
SetupLagrange []bls.G1Point
29+
}
30+
31+
// Initialize KZG subsystem (load the trusted setup data)
32+
func init() {
33+
var parsedSetup = JSONTrustedSetup{}
34+
35+
// TODO: This is dirty. KZG setup should be loaded using an actual config file directive
36+
err := json.Unmarshal([]byte(KZGSetupStr), &parsedSetup)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
kzgSetupG2 = parsedSetup.SetupG2
42+
kzgSetupLagrange = bitReversalPermutation(parsedSetup.SetupLagrange)
43+
KzgSetupG1 = parsedSetup.SetupG1
44+
45+
initDomain()
46+
}
47+
2548
// Convert polynomial in evaluation form to KZG commitment
2649
func BlobToKzg(eval []bls.Fr) *bls.G1Point {
2750
return bls.LinCombG1(kzgSetupLagrange, eval)
@@ -99,73 +122,6 @@ func (batch *BlobsBatch) Verify() error {
99122
return nil
100123
}
101124

102-
// Verify that the list of `commitments` maps to the list of `blobs`
103-
//
104-
// This is an optimization over the naive approach (found in the EIP) of iteratively checking each blob against each
105-
// commitment. The naive approach requires n*l scalar multiplications where `n` is the number of blobs and `l` is
106-
// FIELD_ELEMENTS_PER_BLOB to compute the commitments for all blobs.
107-
//
108-
// A more efficient approach is to build a linear combination of all blobs and commitments and check all of them in a
109-
// single multi-scalar multiplication.
110-
//
111-
// The MSM would look like this (for three blobs with two field elements each):
112-
// r_0(b0_0*L_0 + b0_1*L_1) + r_1(b1_0*L_0 + b1_1*L_1) + r_2(b2_0*L_0 + b2_1*L_1)
113-
// which we would need to check against the linear combination of commitments: r_0*C_0 + r_1*C_1 + r_2*C_2
114-
// In the above, `r` are the random scalars of the linear combination, `b0` is the zero blob, `L` are the elements
115-
// of the KZG_SETUP_LAGRANGE and `C` are the commitments provided.
116-
//
117-
// By regrouping the above equation around the `L` points we can reduce the length of the MSM further
118-
// (down to just `n` scalar multiplications) by making it look like this:
119-
// (r_0*b0_0 + r_1*b1_0 + r_2*b2_0) * L_0 + (r_0*b0_1 + r_1*b1_1 + r_2*b2_1) * L_1
120-
func VerifyBlobsLegacy(commitments []*bls.G1Point, blobs [][]bls.Fr) error {
121-
// Prepare objects to hold our two MSMs
122-
lPoints := make([]bls.G1Point, params.FieldElementsPerBlob)
123-
lScalars := make([]bls.Fr, params.FieldElementsPerBlob)
124-
rPoints := make([]bls.G1Point, len(commitments))
125-
rScalars := make([]bls.Fr, len(commitments))
126-
127-
// Generate list of random scalars for lincomb
128-
rList := make([]bls.Fr, len(blobs))
129-
for i := 0; i < len(blobs); i++ {
130-
bls.CopyFr(&rList[i], bls.RandomFr())
131-
}
132-
133-
// Build left-side MSM:
134-
// (r_0*b0_0 + r_1*b1_0 + r_2*b2_0) * L_0 + (r_0*b0_1 + r_1*b1_1 + r_2*b2_1) * L_1
135-
for c := 0; c < params.FieldElementsPerBlob; c++ {
136-
var sum bls.Fr
137-
for i := 0; i < len(blobs); i++ {
138-
var tmp bls.Fr
139-
140-
r := rList[i]
141-
blob := blobs[i]
142-
143-
bls.MulModFr(&tmp, &r, &blob[c])
144-
bls.AddModFr(&sum, &sum, &tmp)
145-
}
146-
lScalars[c] = sum
147-
lPoints[c] = kzgSetupLagrange[c]
148-
}
149-
150-
// Build right-side MSM: r_0 * C_0 + r_1 * C_1 + r_2 * C_2 + ...
151-
for i, commitment := range commitments {
152-
rScalars[i] = rList[i]
153-
rPoints[i] = *commitment
154-
}
155-
156-
// Compute both MSMs and check equality
157-
lResult := bls.LinCombG1(lPoints, lScalars)
158-
rResult := bls.LinCombG1(rPoints, rScalars)
159-
if !bls.EqualG1(lResult, rResult) {
160-
return errors.New("VerifyBlobs failed")
161-
}
162-
163-
// TODO: Potential improvement is to unify both MSMs into a single MSM, but you would need to batch-invert the `r`s
164-
// of the right-side MSM to effectively pull them to the left side.
165-
166-
return nil
167-
}
168-
169125
// Bit-reversal permutation helper functions
170126

171127
// Check if `value` is a power of two integer.
@@ -239,26 +195,3 @@ func ComputeProof(eval []bls.Fr, z *bls.Fr) (*bls.G1Point, error) {
239195
}
240196
return bls.LinCombG1(kzgSetupLagrange, quotientPoly[:]), nil
241197
}
242-
243-
type JSONTrustedSetup struct {
244-
SetupG1 []bls.G1Point
245-
SetupG2 []bls.G2Point
246-
SetupLagrange []bls.G1Point
247-
}
248-
249-
// Initialize KZG subsystem (load the trusted setup data)
250-
func init() {
251-
var parsedSetup = JSONTrustedSetup{}
252-
253-
// TODO: This is dirty. KZG setup should be loaded using an actual config file directive
254-
err := json.Unmarshal([]byte(KZGSetupStr), &parsedSetup)
255-
if err != nil {
256-
panic(err)
257-
}
258-
259-
kzgSetupG2 = parsedSetup.SetupG2
260-
kzgSetupLagrange = bitReversalPermutation(parsedSetup.SetupLagrange)
261-
KzgSetupG1 = parsedSetup.SetupG1
262-
263-
initDomain()
264-
}

tests/kzg_bench_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ func BenchmarkBlobToKzg(b *testing.B) {
3131
}
3232
}
3333

34-
func BenchmarkVerifyBlobsWithoutKZGProof(b *testing.B) {
35-
var blobs [][]bls.Fr
36-
var commitments []*bls.G1Point
37-
for i := 0; i < 16; i++ {
38-
blob := randomBlob()
39-
blobs = append(blobs, blob)
40-
commitments = append(commitments, kzg.BlobToKzg(blob))
41-
}
42-
b.ResetTimer()
43-
for i := 0; i < b.N; i++ {
44-
kzg.VerifyBlobsLegacy(commitments, blobs)
45-
}
46-
}
47-
4834
func BenchmarkVerifyBlobs(b *testing.B) {
4935
blobs := make([]types.Blob, params.MaxBlobsPerBlock)
5036
var commitments []types.KZGCommitment

0 commit comments

Comments
 (0)