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.

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