github.com/consensys/gnark-crypto@v0.14.0/internal/generator/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "sync" 9 10 "github.com/consensys/bavard" 11 "github.com/consensys/gnark-crypto/field/generator" 12 field "github.com/consensys/gnark-crypto/field/generator/config" 13 "github.com/consensys/gnark-crypto/internal/generator/config" 14 "github.com/consensys/gnark-crypto/internal/generator/crypto/hash/mimc" 15 "github.com/consensys/gnark-crypto/internal/generator/ecc" 16 "github.com/consensys/gnark-crypto/internal/generator/ecdsa" 17 "github.com/consensys/gnark-crypto/internal/generator/edwards" 18 "github.com/consensys/gnark-crypto/internal/generator/edwards/eddsa" 19 "github.com/consensys/gnark-crypto/internal/generator/fft" 20 fri "github.com/consensys/gnark-crypto/internal/generator/fri/template" 21 "github.com/consensys/gnark-crypto/internal/generator/gkr" 22 "github.com/consensys/gnark-crypto/internal/generator/hash_to_field" 23 "github.com/consensys/gnark-crypto/internal/generator/iop" 24 "github.com/consensys/gnark-crypto/internal/generator/kzg" 25 "github.com/consensys/gnark-crypto/internal/generator/pairing" 26 "github.com/consensys/gnark-crypto/internal/generator/pedersen" 27 "github.com/consensys/gnark-crypto/internal/generator/permutation" 28 "github.com/consensys/gnark-crypto/internal/generator/plookup" 29 "github.com/consensys/gnark-crypto/internal/generator/polynomial" 30 "github.com/consensys/gnark-crypto/internal/generator/sis" 31 "github.com/consensys/gnark-crypto/internal/generator/sumcheck" 32 "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils" 33 "github.com/consensys/gnark-crypto/internal/generator/tower" 34 ) 35 36 const ( 37 copyrightHolder = "Consensys Software Inc." 38 copyrightYear = 2020 39 baseDir = "../../" 40 ) 41 42 var bgen = bavard.NewBatchGenerator(copyrightHolder, copyrightYear, "consensys/gnark-crypto") 43 44 //go:generate go run main.go 45 func main() { 46 var wg sync.WaitGroup 47 48 for _, conf := range config.Curves { 49 wg.Add(1) 50 // for each curve, generate the needed files 51 go func(conf config.Curve) { 52 defer wg.Done() 53 var err error 54 55 curveDir := filepath.Join(baseDir, "ecc", conf.Name) 56 57 // generate base field 58 conf.Fp, err = field.NewFieldConfig("fp", "Element", conf.FpModulus, true) 59 assertNoError(err) 60 61 conf.Fr, err = field.NewFieldConfig("fr", "Element", conf.FrModulus, !conf.Equal(config.STARK_CURVE)) 62 assertNoError(err) 63 64 conf.FpUnusedBits = 64 - (conf.Fp.NbBits % 64) 65 66 assertNoError(generator.GenerateFF(conf.Fr, filepath.Join(curveDir, "fr"))) 67 assertNoError(generator.GenerateFF(conf.Fp, filepath.Join(curveDir, "fp"))) 68 69 // generate ecdsa 70 assertNoError(ecdsa.Generate(conf, curveDir, bgen)) 71 72 if conf.Equal(config.STARK_CURVE) { 73 return // TODO @yelhousni 74 } 75 76 // generate G1, G2, multiExp, ... 77 assertNoError(ecc.Generate(conf, curveDir, bgen)) 78 79 if conf.Equal(config.SECP256K1) { 80 return 81 } 82 83 // generate tower of extension 84 assertNoError(tower.Generate(conf, filepath.Join(curveDir, "internal", "fptower"), bgen)) 85 86 // generate pairing tests 87 assertNoError(pairing.Generate(conf, curveDir, bgen)) 88 89 // generate fri on fr 90 assertNoError(fri.Generate(conf, filepath.Join(curveDir, "fr", "fri"), bgen)) 91 92 // generate fft on fr 93 assertNoError(fft.Generate(conf, filepath.Join(curveDir, "fr", "fft"), bgen)) 94 95 if conf.Equal(config.BN254) || conf.Equal(config.BLS12_377) { 96 assertNoError(sis.Generate(conf, filepath.Join(curveDir, "fr", "sis"), bgen)) 97 } 98 99 // generate kzg on fr 100 assertNoError(kzg.Generate(conf, filepath.Join(curveDir, "kzg"), bgen)) 101 102 // generate pedersen on fr 103 assertNoError(pedersen.Generate(conf, filepath.Join(curveDir, "fr", "pedersen"), bgen)) 104 105 // generate plookup on fr 106 assertNoError(plookup.Generate(conf, filepath.Join(curveDir, "fr", "plookup"), bgen)) 107 108 // generate permutation on fr 109 assertNoError(permutation.Generate(conf, filepath.Join(curveDir, "fr", "permutation"), bgen)) 110 111 // generate mimc on fr 112 assertNoError(mimc.Generate(conf, filepath.Join(curveDir, "fr", "mimc"), bgen)) 113 114 frInfo := config.FieldDependency{ 115 FieldPackagePath: "github.com/consensys/gnark-crypto/ecc/" + conf.Name + "/fr", 116 FieldPackageName: "fr", 117 ElementType: "fr.Element", 118 } 119 120 // generate polynomial on fr 121 assertNoError(polynomial.Generate(frInfo, filepath.Join(curveDir, "fr", "polynomial"), true, bgen)) 122 123 // generate eddsa on companion curves 124 assertNoError(fri.Generate(conf, filepath.Join(curveDir, "fr", "fri"), bgen)) 125 126 // generate sumcheck on fr 127 assertNoError(sumcheck.Generate(frInfo, filepath.Join(curveDir, "fr", "sumcheck"), bgen)) 128 129 // generate gkr on fr 130 assertNoError(gkr.Generate(gkr.Config{ 131 FieldDependency: frInfo, 132 GenerateTests: true, 133 TestVectorsRelativePath: "../../../../internal/generator/gkr/test_vectors", 134 }, filepath.Join(curveDir, "fr", "gkr"), bgen)) 135 136 // generate test vector utils on fr 137 assertNoError(test_vector_utils.Generate(test_vector_utils.Config{ 138 FieldDependency: frInfo, 139 RandomizeMissingHashEntries: false, 140 }, filepath.Join(curveDir, "fr", "test_vector_utils"), bgen)) 141 142 // generate iop functions 143 assertNoError(iop.Generate(conf, filepath.Join(curveDir, "fr", "iop"), bgen)) 144 145 fpInfo := config.FieldDependency{ 146 FieldPackagePath: "github.com/consensys/gnark-crypto/ecc/" + conf.Name + "/fp", 147 FieldPackageName: "fp", 148 ElementType: "fp.Element", 149 } 150 151 // generate wrapped hash-to-field for both fr and fp 152 assertNoError(hash_to_field.Generate(frInfo, filepath.Join(curveDir, "fr", "hash_to_field"), bgen)) 153 assertNoError(hash_to_field.Generate(fpInfo, filepath.Join(curveDir, "fp", "hash_to_field"), bgen)) 154 155 }(conf) 156 157 } 158 159 for _, conf := range config.TwistedEdwardsCurves { 160 wg.Add(1) 161 162 go func(conf config.TwistedEdwardsCurve) { 163 defer wg.Done() 164 165 curveDir := filepath.Join(baseDir, "ecc", conf.Name, conf.Package) 166 // generate twisted edwards companion curves 167 assertNoError(edwards.Generate(conf, curveDir, bgen)) 168 169 // generate eddsa on companion curves 170 assertNoError(eddsa.Generate(conf, curveDir, bgen)) 171 }(conf) 172 173 } 174 175 wg.Add(1) 176 go func() { 177 defer wg.Done() 178 assertNoError(test_vector_utils.GenerateRationals(bgen)) 179 }() 180 wg.Wait() 181 182 // format the whole directory 183 184 cmd := exec.Command("gofmt", "-s", "-w", baseDir) 185 cmd.Stdout = os.Stdout 186 cmd.Stderr = os.Stderr 187 assertNoError(cmd.Run()) 188 189 cmd = exec.Command("asmfmt", "-w", baseDir) 190 cmd.Stdout = os.Stdout 191 cmd.Stderr = os.Stderr 192 assertNoError(cmd.Run()) 193 194 //mathfmt doesn't accept directories. TODO: PR pending 195 /*cmd = exec.Command("mathfmt", "-w", baseDir) 196 cmd.Stdout = os.Stdout 197 cmd.Stderr = os.Stderr 198 assertNoError(cmd.Run())*/ 199 200 wg.Add(2) 201 202 go func() { 203 // generate test vectors for sumcheck 204 cmd := exec.Command("go", "run", "./sumcheck/test_vectors") 205 cmd.Stdout = os.Stdout 206 cmd.Stderr = os.Stderr 207 assertNoError(cmd.Run()) 208 wg.Done() 209 }() 210 211 go func() { 212 // generate test vectors for gkr 213 cmd := exec.Command("go", "run", "./gkr/test_vectors") 214 cmd.Stdout = os.Stdout 215 cmd.Stderr = os.Stderr 216 assertNoError(cmd.Run()) 217 wg.Done() 218 }() 219 220 wg.Wait() 221 } 222 223 func assertNoError(err error) { 224 if err != nil { 225 fmt.Printf("\n%s\n", err.Error()) 226 os.Exit(-1) 227 } 228 }