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.

134 lines
3.1 KiB

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. "time"
  8. "github.com/iden3/go-circom-prover-verifier/parsers"
  9. "github.com/iden3/go-circom-prover-verifier/prover"
  10. "github.com/iden3/go-circom-prover-verifier/verifier"
  11. )
  12. const version = "v0.0.1"
  13. func main() {
  14. fmt.Println("go-circom-prover-verifier")
  15. fmt.Println(" ", version)
  16. prove := flag.Bool("prove", false, "prover mode")
  17. verify := flag.Bool("verify", false, "verifier mode")
  18. provingKeyPath := flag.String("provingkey", "proving_key.json", "provingKey path")
  19. witnessPath := flag.String("witness", "witness.json", "witness path")
  20. proofPath := flag.String("proof", "proof.json", "proof path")
  21. verificationKeyPath := flag.String("verificationkey", "verification_key.json", "verificationKey path")
  22. publicPath := flag.String("public", "public.json", "public signals path")
  23. flag.Parse()
  24. if *prove {
  25. err := cmdProve(*provingKeyPath, *witnessPath, *proofPath, *publicPath)
  26. if err != nil {
  27. fmt.Println("Error:", err)
  28. }
  29. return
  30. } else if *verify {
  31. err := cmdVerify(*proofPath, *verificationKeyPath, *publicPath)
  32. if err != nil {
  33. fmt.Println("Error:", err)
  34. }
  35. return
  36. }
  37. fmt.Println("use -help for the list of commands")
  38. }
  39. func cmdProve(provingKeyPath, witnessPath, proofPath, publicPath string) error {
  40. fmt.Println("zkSNARK Groth16 prover")
  41. fmt.Println("Reading proving key file:", provingKeyPath)
  42. provingKeyJson, err := ioutil.ReadFile(provingKeyPath)
  43. if err != nil {
  44. return err
  45. }
  46. pk, err := parsers.ParsePk(provingKeyJson)
  47. if err != nil {
  48. return err
  49. }
  50. fmt.Println("Reading witness file:", witnessPath)
  51. witnessJson, err := ioutil.ReadFile(witnessPath)
  52. if err != nil {
  53. return err
  54. }
  55. w, err := parsers.ParseWitness(witnessJson)
  56. if err != nil {
  57. return err
  58. }
  59. fmt.Println("Generating the proof")
  60. beforeT := time.Now()
  61. proof, pubSignals, err := prover.GenerateProof(pk, w)
  62. if err != nil {
  63. return err
  64. }
  65. fmt.Println("proof generation time elapsed:", time.Since(beforeT))
  66. proofStr, err := parsers.ProofToJson(proof)
  67. if err != nil {
  68. return err
  69. }
  70. // write output
  71. err = ioutil.WriteFile(proofPath, proofStr, 0644)
  72. if err != nil {
  73. return err
  74. }
  75. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  76. if err != nil {
  77. return err
  78. }
  79. err = ioutil.WriteFile(publicPath, publicStr, 0644)
  80. if err != nil {
  81. return err
  82. }
  83. fmt.Println("Proof stored at:", proofPath)
  84. fmt.Println("PublicSignals stored at:", publicPath)
  85. return nil
  86. }
  87. func cmdVerify(proofPath, verificationKeyPath, publicPath string) error {
  88. fmt.Println("zkSNARK Groth16 verifier")
  89. proofJson, err := ioutil.ReadFile(proofPath)
  90. if err != nil {
  91. return err
  92. }
  93. vkJson, err := ioutil.ReadFile(verificationKeyPath)
  94. if err != nil {
  95. return err
  96. }
  97. publicJson, err := ioutil.ReadFile(publicPath)
  98. if err != nil {
  99. return err
  100. }
  101. public, err := parsers.ParsePublicSignals(publicJson)
  102. if err != nil {
  103. return err
  104. }
  105. proof, err := parsers.ParseProof(proofJson)
  106. if err != nil {
  107. return err
  108. }
  109. vk, err := parsers.ParseVk(vkJson)
  110. if err != nil {
  111. return err
  112. }
  113. v := verifier.Verify(vk, proof, public)
  114. fmt.Println("verification:", v)
  115. return nil
  116. }