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.

127 lines
2.9 KiB

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