github.com/consensys/gnark@v0.11.0/internal/generator/backend/main.go (about) 1 package main 2 3 import ( 4 "os" 5 "os/exec" 6 "path/filepath" 7 "strings" 8 "sync" 9 10 "github.com/consensys/bavard" 11 "github.com/consensys/gnark-crypto/field/generator" 12 "github.com/consensys/gnark-crypto/field/generator/config" 13 ) 14 15 const copyrightHolder = "ConsenSys Software Inc." 16 17 var bgen = bavard.NewBatchGenerator(copyrightHolder, 2020, "gnark") 18 19 //go:generate go run main.go 20 func main() { 21 22 bls12_377 := templateData{ 23 RootPath: "../../../backend/{?}/bls12-377/", 24 CSPath: "../../../constraint/bls12-377/", 25 Curve: "BLS12-377", 26 CurveID: "BLS12_377", 27 } 28 bls12_381 := templateData{ 29 RootPath: "../../../backend/{?}/bls12-381/", 30 CSPath: "../../../constraint/bls12-381/", 31 Curve: "BLS12-381", 32 CurveID: "BLS12_381", 33 } 34 bn254 := templateData{ 35 RootPath: "../../../backend/{?}/bn254/", 36 CSPath: "../../../constraint/bn254/", 37 Curve: "BN254", 38 CurveID: "BN254", 39 } 40 bw6_761 := templateData{ 41 RootPath: "../../../backend/{?}/bw6-761/", 42 CSPath: "../../../constraint/bw6-761/", 43 Curve: "BW6-761", 44 CurveID: "BW6_761", 45 } 46 bls24_315 := templateData{ 47 RootPath: "../../../backend/{?}/bls24-315/", 48 CSPath: "../../../constraint/bls24-315/", 49 Curve: "BLS24-315", 50 CurveID: "BLS24_315", 51 } 52 bls24_317 := templateData{ 53 RootPath: "../../../backend/{?}/bls24-317/", 54 CSPath: "../../../constraint/bls24-317/", 55 Curve: "BLS24-317", 56 CurveID: "BLS24_317", 57 } 58 bw6_633 := templateData{ 59 RootPath: "../../../backend/{?}/bw6-633/", 60 CSPath: "../../../constraint/bw6-633/", 61 Curve: "BW6-633", 62 CurveID: "BW6_633", 63 } 64 tiny_field := templateData{ 65 RootPath: "../../../internal/tinyfield/", 66 CSPath: "../../../constraint/tinyfield", 67 Curve: "tinyfield", 68 CurveID: "UNKNOWN", 69 noBackend: true, 70 NoGKR: true, 71 } 72 73 // autogenerate tinyfield 74 tinyfieldConf, err := config.NewFieldConfig("tinyfield", "Element", "0x2f", false) 75 if err != nil { 76 panic(err) 77 } 78 if err := generator.GenerateFF(tinyfieldConf, tiny_field.RootPath); err != nil { 79 panic(err) 80 } 81 82 datas := []templateData{ 83 bls12_377, 84 bls12_381, 85 bn254, 86 bw6_761, 87 bls24_315, 88 bls24_317, 89 bw6_633, 90 tiny_field, 91 } 92 93 const importCurve = "../imports.go.tmpl" 94 95 var wg sync.WaitGroup 96 97 for _, d := range datas { 98 99 wg.Add(1) 100 101 go func(d templateData) { 102 defer wg.Done() 103 104 var ( 105 groth16Dir = strings.Replace(d.RootPath, "{?}", "groth16", 1) 106 groth16MpcSetupDir = filepath.Join(groth16Dir, "mpcsetup") 107 plonkDir = strings.Replace(d.RootPath, "{?}", "plonk", 1) 108 ) 109 110 if err := os.MkdirAll(groth16Dir, 0700); err != nil { 111 panic(err) 112 } 113 if err := os.MkdirAll(plonkDir, 0700); err != nil { 114 panic(err) 115 } 116 117 csDir := d.CSPath 118 119 // constraint systems 120 entries := []bavard.Entry{ 121 {File: filepath.Join(csDir, "system.go"), Templates: []string{"system.go.tmpl", importCurve}}, 122 {File: filepath.Join(csDir, "marshal.go"), Templates: []string{"marshal.go.tmpl", importCurve}}, 123 {File: filepath.Join(csDir, "coeff.go"), Templates: []string{"coeff.go.tmpl", importCurve}}, 124 {File: filepath.Join(csDir, "solver.go"), Templates: []string{"solver.go.tmpl", importCurve}}, 125 } 126 if err := bgen.Generate(d, "cs", "./template/representations/", entries...); err != nil { 127 panic(err) 128 } 129 130 // gkr backend 131 if d.Curve != "tinyfield" { 132 entries = []bavard.Entry{{File: filepath.Join(csDir, "gkr.go"), Templates: []string{"gkr.go.tmpl", importCurve}}} 133 if err := bgen.Generate(d, "cs", "./template/representations/", entries...); err != nil { 134 panic(err) 135 } 136 } 137 138 entries = []bavard.Entry{ 139 {File: filepath.Join(csDir, "r1cs_test.go"), Templates: []string{"tests/r1cs.go.tmpl", importCurve}}, 140 } 141 if err := bgen.Generate(d, "cs_test", "./template/representations/", entries...); err != nil { 142 panic(err) 143 } 144 145 // groth16 & plonk 146 if d.noBackend { 147 // no backend with just the field defined 148 return 149 } 150 151 if err := os.MkdirAll(groth16Dir, 0700); err != nil { 152 panic(err) 153 } 154 if err := os.MkdirAll(plonkDir, 0700); err != nil { 155 panic(err) 156 } 157 158 entries = []bavard.Entry{ 159 {File: filepath.Join(groth16Dir, "verify.go"), Templates: []string{"groth16/groth16.verify.go.tmpl", importCurve}}, 160 {File: filepath.Join(groth16Dir, "prove.go"), Templates: []string{"groth16/groth16.prove.go.tmpl", importCurve}}, 161 {File: filepath.Join(groth16Dir, "setup.go"), Templates: []string{"groth16/groth16.setup.go.tmpl", importCurve}}, 162 {File: filepath.Join(groth16Dir, "marshal.go"), Templates: []string{"groth16/groth16.marshal.go.tmpl", importCurve}}, 163 {File: filepath.Join(groth16Dir, "marshal_test.go"), Templates: []string{"groth16/tests/groth16.marshal.go.tmpl", importCurve}}, 164 } 165 if err := bgen.Generate(d, "groth16", "./template/zkpschemes/", entries...); err != nil { 166 panic(err) // TODO handle 167 } 168 169 entries = []bavard.Entry{ 170 {File: filepath.Join(groth16Dir, "commitment_test.go"), Templates: []string{"groth16/tests/groth16.commitment.go.tmpl", importCurve}}, 171 } 172 if err := bgen.Generate(d, "groth16_test", "./template/zkpschemes/", entries...); err != nil { 173 panic(err) // TODO handle 174 } 175 176 // groth16 mpcsetup 177 entries = []bavard.Entry{ 178 {File: filepath.Join(groth16MpcSetupDir, "lagrange.go"), Templates: []string{"groth16/mpcsetup/lagrange.go.tmpl", importCurve}}, 179 {File: filepath.Join(groth16MpcSetupDir, "marshal.go"), Templates: []string{"groth16/mpcsetup/marshal.go.tmpl", importCurve}}, 180 {File: filepath.Join(groth16MpcSetupDir, "marshal_test.go"), Templates: []string{"groth16/mpcsetup/marshal_test.go.tmpl", importCurve}}, 181 {File: filepath.Join(groth16MpcSetupDir, "phase1.go"), Templates: []string{"groth16/mpcsetup/phase1.go.tmpl", importCurve}}, 182 {File: filepath.Join(groth16MpcSetupDir, "phase2.go"), Templates: []string{"groth16/mpcsetup/phase2.go.tmpl", importCurve}}, 183 {File: filepath.Join(groth16MpcSetupDir, "setup.go"), Templates: []string{"groth16/mpcsetup/setup.go.tmpl", importCurve}}, 184 {File: filepath.Join(groth16MpcSetupDir, "setup_test.go"), Templates: []string{"groth16/mpcsetup/setup_test.go.tmpl", importCurve}}, 185 {File: filepath.Join(groth16MpcSetupDir, "utils.go"), Templates: []string{"groth16/mpcsetup/utils.go.tmpl", importCurve}}, 186 } 187 188 if err := bgen.Generate(d, "mpcsetup", "./template/zkpschemes/", entries...); err != nil { 189 panic(err) // TODO handle 190 } 191 192 // plonk 193 entries = []bavard.Entry{ 194 {File: filepath.Join(plonkDir, "verify.go"), Templates: []string{"plonk/plonk.verify.go.tmpl", importCurve}}, 195 {File: filepath.Join(plonkDir, "prove.go"), Templates: []string{"plonk/plonk.prove.go.tmpl", importCurve}}, 196 {File: filepath.Join(plonkDir, "setup.go"), Templates: []string{"plonk/plonk.setup.go.tmpl", importCurve}}, 197 {File: filepath.Join(plonkDir, "marshal.go"), Templates: []string{"plonk/plonk.marshal.go.tmpl", importCurve}}, 198 {File: filepath.Join(plonkDir, "marshal_test.go"), Templates: []string{"plonk/tests/marshal.go.tmpl", importCurve}}, 199 } 200 if err := bgen.Generate(d, "plonk", "./template/zkpschemes/", entries...); err != nil { 201 panic(err) 202 } 203 204 }(d) 205 206 } 207 208 wg.Wait() 209 210 // run go fmt on whole directory 211 cmd := exec.Command("gofmt", "-s", "-w", "../../../") 212 cmd.Stdout = os.Stdout 213 cmd.Stderr = os.Stderr 214 if err := cmd.Run(); err != nil { 215 panic(err) 216 } 217 218 } 219 220 type templateData struct { 221 RootPath string 222 CSPath string 223 Curve string 224 CurveID string 225 noBackend bool 226 NoGKR bool 227 }