Skip to content

Instantly share code, notes, and snippets.

@moea
Created May 17, 2025 07:56
Show Gist options
  • Save moea/5555670f50dab62f2a97b0545a6a668c to your computer and use it in GitHub Desktop.
Save moea/5555670f50dab62f2a97b0545a6a668c to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
"io"
"os"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
"github.com/consensys/gnark-crypto/ecc/bn254/kzg"
)
func TestSRS(srsFilePath string) error {
srsFile, err := os.Open(srsFilePath)
if err != nil {
return fmt.Errorf("failed to open SRS file '%s': %w", srsFilePath, err)
}
defer srsFile.Close()
var srs kzg.SRS
bytesRead, err := srs.ReadFrom(srsFile)
if err != nil {
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {
return fmt.Errorf("failed to read full SRS from '%s': file may be too short or corrupted. Read %d bytes. Error: %w", srsFilePath, bytesRead, err)
}
return fmt.Errorf("failed to read/deserialize SRS from '%s': %w", srsFilePath, err)
}
numG1Points := uint64(len(srs.Pk.G1))
if numG1Points == 0 {
return fmt.Errorf("SRS Proving Key (Pk.G1) has no G1 points")
}
srsMaxDegree := numG1Points - 1
if len(srs.Vk.G2) < 2 {
return fmt.Errorf("SRS Verifying Key (Vk.G2) has %d points, expected at least 2 ([G2, alpha*G2])", len(srs.Vk.G2))
}
var g1Zero bn254.G1Affine
if srs.Vk.G1.Equal(&g1Zero) {
return fmt.Errorf("SRS Verifying Key G1 generator (Vk.G1) is the zero point")
}
var testDegree := int 3
if numG1Points < uint64(testDegree+1) {
testDegree = int(srsMaxDegree)
}
poly := make([]fr.Element, testDegree+1)
for i := 0; i <= testDegree; i++ {
poly[i].SetUint64(uint64(i + 1))
}
digest, err := kzg.Commit(poly, srs.Pk)
if err != nil {
return fmt.Errorf("failed to commit to polynomial: %w", err)
}
var point fr.Element
point.SetString("12345")
openingProof, err := kzg.Open(poly, point, srs.Pk)
if err != nil {
return fmt.Errorf("failed to generate opening proof: %w", err)
}
err = kzg.Verify(&digest, &openingProof, point, srs.Vk)
if err != nil {
return fmt.Errorf("verification of opening proof failed: %w", err)
}
return nil
}
func main() {
srsFilePath := os.Args[1]
err := TestSRS(srsFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error testing SRS '%s': %v\n", srsFilePath, err)
os.Exit(1)
}
fmt.Printf("SRS file '%s' tested successfully.\n", srsFilePath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment