arnaucube
arnaucube/gnark-plonky2-verifierPublic Self-hosted repository
gnark-plonky2-verifier/trusted_setup/trusted_setup.go
trusted_setup/trusted_setup.goRaw
  1package trusted_setup
  2
  3import (
  4	"log"
  5	"os"
  6
  7	"github.com/consensys/gnark-crypto/ecc/bn254"
  8	"github.com/consensys/gnark-crypto/ecc/bn254/fr"
  9	kzg_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/kzg"
 10	"github.com/consensys/gnark-ignition-verifier/ignition"
 11)
 12
 13func sanityCheck(srs *kzg_bn254.SRS) {
 14	// we can now use the SRS to verify a proof
 15	// create a polynomial
 16	f := randomPolynomial(60)
 17
 18	// commit the polynomial
 19	digest, err := kzg_bn254.Commit(f, srs.Pk)
 20	if err != nil {
 21		log.Fatal(err)
 22	}
 23
 24	// compute opening proof at a random point
 25	var point fr.Element
 26	point.SetString("4321")
 27	proof, err := kzg_bn254.Open(f, point, srs.Pk)
 28	if err != nil {
 29		log.Fatal(err)
 30	}
 31
 32	// verify the claimed valued
 33	expected := eval(f, point)
 34	if !proof.ClaimedValue.Equal(&expected) {
 35		log.Fatal("inconsistent claimed value")
 36	}
 37
 38	// verify correct proof
 39	err = kzg_bn254.Verify(&digest, &proof, point, srs.Vk)
 40	if err != nil {
 41		log.Fatal(err)
 42	}
 43}
 44
 45func randomPolynomial(size int) []fr.Element {
 46	f := make([]fr.Element, size)
 47	for i := 0; i < size; i++ {
 48		f[i].SetRandom()
 49	}
 50	return f
 51}
 52
 53// eval returns p(point) where p is interpreted as a polynomial
 54// ∑_{i<len(p)}p[i]Xⁱ
 55func eval(p []fr.Element, point fr.Element) fr.Element {
 56	var res fr.Element
 57	n := len(p)
 58	res.Set(&p[n-1])
 59	for i := n - 2; i >= 0; i-- {
 60		res.Mul(&res, &point).Add(&res, &p[i])
 61	}
 62	return res
 63}
 64
 65func DownloadAndSaveAztecIgnitionSrs(startIdx int, fileName string) {
 66	config := ignition.Config{
 67		BaseURL:  "https://aztec-ignition.s3.amazonaws.com/",
 68		Ceremony: "MAIN IGNITION", // "TINY_TEST_5"
 69		CacheDir: "./data",
 70	}
 71
 72	if config.CacheDir != "" {
 73		err := os.MkdirAll(config.CacheDir, os.ModePerm)
 74
 75		if err != nil {
 76			log.Fatal("when creating cache dir: ", err)
 77			panic(err)
 78		}
 79	}
 80
 81	log.Println("fetch manifest")
 82
 83	manifest, err := ignition.NewManifest(config)
 84
 85	if err != nil {
 86		log.Fatal("when fetching manifest: ", err)
 87	}
 88
 89	current, next := ignition.NewContribution(manifest.NumG1Points), ignition.NewContribution(manifest.NumG1Points)
 90
 91	if err := current.Get(manifest.Participants[startIdx], config); err != nil {
 92		log.Fatal("when fetching contribution: ", err)
 93	}
 94	if err := next.Get(manifest.Participants[startIdx+1], config); err != nil {
 95		log.Fatal("when fetching contribution: ", err)
 96	}
 97	if !next.Follows(&current) {
 98		log.Fatalf("contribution %d does not follow contribution %d", startIdx+1, startIdx)
 99	}
100
101	for i := startIdx + 2; i < len(manifest.Participants); i++ {
102		log.Println("processing contribution ", i+1)
103		current, next = next, current
104		if err := next.Get(manifest.Participants[i], config); err != nil {
105			log.Fatal("when fetching contribution ", i+1, ": ", err)
106		}
107		if !next.Follows(&current) {
108			log.Fatal("contribution ", i+1, " does not follow contribution ", i, ": ", err)
109		}
110	}
111
112	log.Println("success ✅: all contributions are valid")
113
114	_, _, _, g2gen := bn254.Generators()
115	// we use the last contribution to build a kzg SRS for bn254
116	srs := kzg_bn254.SRS{
117		Pk: kzg_bn254.ProvingKey{
118			G1: next.G1,
119		},
120		Vk: kzg_bn254.VerifyingKey{
121			G1: next.G1[0],
122			G2: [2]bn254.G2Affine{
123				g2gen,
124				next.G2[0],
125			},
126		},
127	}
128
129	// sanity check
130	sanityCheck(&srs)
131	log.Println("success ✅: kzg sanity check with SRS")
132
133	fSRS, err := os.Create(fileName)
134	if err != nil {
135		log.Fatal("error creating srs file: ", err)
136		panic(err)
137	}
138	defer fSRS.Close()
139
140	_, err = srs.WriteTo(fSRS)
141	if err != nil {
142		log.Fatal("error writing srs file: ", err)
143		panic(err)
144	}
145}