Skip to content

Commit b78632f

Browse files
committed
kzg: Document kzg code further
1 parent 0080bf4 commit b78632f

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

core/types/data_blob.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,11 @@ func (b *BlobTxWrapData) checkWrapping(inner TxData) error {
324324
}
325325
}
326326

327-
// Extract cryptographic material out of our types and pass them to the crypto layer
327+
// Time to verify that the KZG commitments match the included blobs:
328+
// first extract crypto material out of our types and pass them to the crypto layer
328329
commitments, err := b.BlobKzgs.Commitments()
329330
if err != nil {
330-
return fmt.Errorf("internal commitment error")
331+
return fmt.Errorf("internal commitments error")
331332
}
332333
blobs, err := b.Blobs.Blobs()
333334
if err != nil {

crypto/kzg/kzg.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,19 @@ func VerifyKzgProof(commitment *bls.G1Point, x *bls.Fr, y *bls.Fr, proof *bls.G1
3636
var commitmentMinusY bls.G1Point
3737
bls.SubG1(&commitmentMinusY, commitment, &yG1)
3838

39-
// This trick may be applied in the BLS-lib specific code:
40-
//
41-
// e([commitment - y], [1]) = e([proof], [s - x])
42-
// equivalent to
43-
// e([commitment - y]^(-1), [1]) * e([proof], [s - x]) = 1_T
44-
//
4539
return bls.PairingsVerify(&commitmentMinusY, &bls.GenG2, proof, &sMinuxX)
4640
}
4741

42+
// Return versioned hash that corresponds to KZG commitment
4843
func KzgToVersionedHash(commitment *bls.G1Point) [32]byte {
4944
h := crypto.Keccak256Hash(bls.ToCompressedG1(commitment))
5045
h[0] = byte(params.BlobCommitmentVersionKZG)
5146
return h
5247
}
5348

54-
// Verify that the list of `blobs` map to the list of `commitments`
49+
// Verify that the list of `commitments` maps to the list of `blobs`
5550
//
56-
// This is an optimization over the naive approach (written in the EIP) of iteratively checking each blob against each
51+
// This is an optimization over the naive approach (found in the EIP) of iteratively checking each blob against each
5752
// commitment. The naive approach requires n*l scalar multiplications where `n` is the number of blobs and `l` is
5853
// FIELD_ELEMENTS_PER_BLOB to compute the commitments for all blobs.
5954
//
@@ -66,8 +61,8 @@ func KzgToVersionedHash(commitment *bls.G1Point) [32]byte {
6661
// In the above, `r` are the random scalars of the linear combination, `b0` is the zero blob, `L` are the elements
6762
// of the KZG_SETUP_LAGRANGE and `C` are the commitments provided.
6863
//
69-
// By re-grouping the above equation around the `L` points we can reduce the amount of scalar multiplications further
70-
// (down to just `n` scalar multiplications) by making the MSM look like this:
64+
// By regrouping the above equation around the `L` points we can reduce the length of the MSM further
65+
// (down to just `n` scalar multiplications) by making it look like this:
7166
// (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
7267
func VerifyBlobs(commitments []*bls.G1Point, blobs [][]bls.Fr) error {
7368
// Prepare objects to hold our two MSMs

tests/sharding_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ func ComputeProof(poly []bls.Fr, x uint64, crsG1 []bls.G1Point) *bls.G1Point {
7676
return bls.LinCombG1(crsG1[:len(quotientPolynomial)], quotientPolynomial)
7777
}
7878

79+
// Test the go-kzg library for correctness
80+
// Do the trusted setup, generate a polynomial, commit to it, make proof, verify proof.
7981
func TestGoKzg(t *testing.T) {
80-
/// Test the go-kzg library for correctness
81-
/// Do the trusted setup, generate a polynomial, commit to it, make proof, verify proof.
82-
8382
// Generate roots of unity
8483
fs := gokzg.NewFFTSettings(uint8(math.Log2(params.FieldElementsPerBlob)))
8584

@@ -131,9 +130,8 @@ func TestGoKzg(t *testing.T) {
131130
}
132131
}
133132

133+
// Test the geth KZG module (use our trusted setup instead of creating a new one)
134134
func TestKzg(t *testing.T) {
135-
/// Test the geth KZG module (use our trusted setup instead of creating a new one)
136-
137135
// First let's do some go-kzg preparations to be able to convert polynomial between coefficient and evaluation form
138136
fs := gokzg.NewFFTSettings(uint8(math.Log2(params.FieldElementsPerBlob)))
139137

@@ -175,6 +173,7 @@ type JSONTestdataBlobs struct {
175173
KzgBlob2 string
176174
}
177175

176+
// Test the optimized VerifyBlobs function
178177
func TestVerifyBlobs(t *testing.T) {
179178
data, err := ioutil.ReadFile("kzg_testdata/kzg_blobs.json")
180179
if err != nil {
@@ -242,6 +241,7 @@ func TestVerifyBlobs(t *testing.T) {
242241
}
243242
}
244243

244+
// Helper: Create test vector for the BlobVerification precompile
245245
func TestBlobVerificationTestVector(t *testing.T) {
246246
data := []byte(strings.Repeat("HELPMELOVEME ", 10083))[:params.FieldElementsPerBlob*32]
247247

@@ -261,6 +261,7 @@ func TestBlobVerificationTestVector(t *testing.T) {
261261
fmt.Printf("%d\n", len(testVector))
262262
}
263263

264+
// Helper: Create test vector for the PointEvaluation precompile
264265
func TestPointEvaluationTestVector(t *testing.T) {
265266
fs := gokzg.NewFFTSettings(uint8(math.Log2(params.FieldElementsPerBlob)))
266267

0 commit comments

Comments
 (0)