You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "time"
  9. "github.com/iden3/go-circom-prover-verifier/parsers"
  10. "github.com/iden3/go-circom-prover-verifier/prover"
  11. "github.com/iden3/go-circom-prover-verifier/verifier"
  12. )
  13. const version = "v0.0.1"
  14. func main() {
  15. fmt.Println("go-circom-prover-verifier")
  16. fmt.Println(" ", version)
  17. prove := flag.Bool("prove", false, "prover mode")
  18. verify := flag.Bool("verify", false, "verifier mode")
  19. convert := flag.Bool("convert", false, "convert mode, to convert between proving_key.json to proving_key.go.bin")
  20. provingKeyPath := flag.String("pk", "proving_key.json", "provingKey path")
  21. witnessPath := flag.String("witness", "witness.json", "witness path")
  22. proofPath := flag.String("proof", "proof.json", "proof path")
  23. verificationKeyPath := flag.String("vk", "verification_key.json", "verificationKey path")
  24. publicPath := flag.String("public", "public.json", "public signals path")
  25. provingKeyBinPath := flag.String("pkbin", "proving_key.go.bin", "provingKey Bin path")
  26. flag.Parse()
  27. if *prove {
  28. err := cmdProve(*provingKeyPath, *witnessPath, *proofPath, *publicPath)
  29. if err != nil {
  30. fmt.Println("Error:", err)
  31. }
  32. os.Exit(0)
  33. } else if *verify {
  34. err := cmdVerify(*proofPath, *verificationKeyPath, *publicPath)
  35. if err != nil {
  36. fmt.Println("Error:", err)
  37. }
  38. os.Exit(0)
  39. } else if *convert {
  40. err := cmdConvert(*provingKeyPath, *provingKeyBinPath)
  41. if err != nil {
  42. fmt.Println("Error:", err)
  43. }
  44. os.Exit(0)
  45. }
  46. flag.PrintDefaults()
  47. }
  48. func cmdProve(provingKeyPath, witnessPath, proofPath, publicPath string) error {
  49. fmt.Println("zkSNARK Groth16 prover")
  50. fmt.Println("Reading proving key file:", provingKeyPath)
  51. provingKeyJson, err := ioutil.ReadFile(provingKeyPath)
  52. if err != nil {
  53. return err
  54. }
  55. pk, err := parsers.ParsePk(provingKeyJson)
  56. if err != nil {
  57. return err
  58. }
  59. fmt.Println("Reading witness file:", witnessPath)
  60. witnessJson, err := ioutil.ReadFile(witnessPath)
  61. if err != nil {
  62. return err
  63. }
  64. w, err := parsers.ParseWitness(witnessJson)
  65. if err != nil {
  66. return err
  67. }
  68. fmt.Println("Generating the proof")
  69. beforeT := time.Now()
  70. proof, pubSignals, err := prover.GenerateProof(pk, w)
  71. if err != nil {
  72. return err
  73. }
  74. fmt.Println("proof generation time elapsed:", time.Since(beforeT))
  75. proofStr, err := parsers.ProofToJson(proof)
  76. if err != nil {
  77. return err
  78. }
  79. // write output
  80. err = ioutil.WriteFile(proofPath, proofStr, 0644)
  81. if err != nil {
  82. return err
  83. }
  84. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  85. if err != nil {
  86. return err
  87. }
  88. err = ioutil.WriteFile(publicPath, publicStr, 0644)
  89. if err != nil {
  90. return err
  91. }
  92. fmt.Println("Proof stored at:", proofPath)
  93. fmt.Println("PublicSignals stored at:", publicPath)
  94. return nil
  95. }
  96. func cmdVerify(proofPath, verificationKeyPath, publicPath string) error {
  97. fmt.Println("zkSNARK Groth16 verifier")
  98. proofJson, err := ioutil.ReadFile(proofPath)
  99. if err != nil {
  100. return err
  101. }
  102. vkJson, err := ioutil.ReadFile(verificationKeyPath)
  103. if err != nil {
  104. return err
  105. }
  106. publicJson, err := ioutil.ReadFile(publicPath)
  107. if err != nil {
  108. return err
  109. }
  110. public, err := parsers.ParsePublicSignals(publicJson)
  111. if err != nil {
  112. return err
  113. }
  114. proof, err := parsers.ParseProof(proofJson)
  115. if err != nil {
  116. return err
  117. }
  118. vk, err := parsers.ParseVk(vkJson)
  119. if err != nil {
  120. return err
  121. }
  122. v := verifier.Verify(vk, proof, public)
  123. fmt.Println("verification:", v)
  124. return nil
  125. }
  126. func cmdConvert(provingKeyPath, provingKeyBinPath string) error {
  127. fmt.Println("Convertion tool")
  128. provingKeyJson, err := ioutil.ReadFile(provingKeyPath)
  129. if err != nil {
  130. return err
  131. }
  132. pk, err := parsers.ParsePk(provingKeyJson)
  133. if err != nil {
  134. return err
  135. }
  136. fmt.Printf("Converting proving key json (%s)\nto go proving key binary (%s)\n", provingKeyPath, provingKeyBinPath)
  137. pkGBin, err := parsers.PkToGoBin(pk)
  138. if err != nil {
  139. return err
  140. }
  141. if err = ioutil.WriteFile(provingKeyBinPath, pkGBin, 0644); err != nil {
  142. return err
  143. }
  144. return nil
  145. }