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.

98 lines
3.1 KiB

  1. package prover
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/iden3/go-circom-prover-verifier/parsers"
  10. "github.com/iden3/go-circom-prover-verifier/verifier"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestCircuitsGenerateProof(t *testing.T) {
  15. testCircuitGenerateProof(t, "circuit1k") // 1000 constraints
  16. testCircuitGenerateProof(t, "circuit5k") // 5000 constraints
  17. // testCircuitGenerateProof(t, "circuit10k") // 10000 constraints
  18. // testCircuitGenerateProof(t, "circuit20k") // 20000 constraints
  19. }
  20. func testCircuitGenerateProof(t *testing.T, circuit string) {
  21. // Using json provingKey file:
  22. // provingKeyJson, err := ioutil.ReadFile("../testdata/" + circuit + "/proving_key.json")
  23. // require.Nil(t, err)
  24. // pk, err := parsers.ParsePk(provingKeyJson)
  25. // require.Nil(t, err)
  26. // witnessJson, err := ioutil.ReadFile("../testdata/" + circuit + "/witness.json")
  27. // require.Nil(t, err)
  28. // w, err := parsers.ParseWitness(witnessJson)
  29. // require.Nil(t, err)
  30. // Using bin provingKey file:
  31. // pkBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.bin")
  32. // require.Nil(t, err)
  33. // defer pkBinFile.Close()
  34. // pk, err := parsers.ParsePkBin(pkBinFile)
  35. // require.Nil(t, err)
  36. // Using go bin provingKey file:
  37. pkGoBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.go.bin")
  38. require.Nil(t, err)
  39. defer pkGoBinFile.Close()
  40. pk, err := parsers.ParsePkGoBin(pkGoBinFile)
  41. require.Nil(t, err)
  42. witnessBinFile, err := os.Open("../testdata/" + circuit + "/witness.bin")
  43. require.Nil(t, err)
  44. defer witnessBinFile.Close()
  45. w, err := parsers.ParseWitnessBin(witnessBinFile)
  46. require.Nil(t, err)
  47. beforeT := time.Now()
  48. proof, pubSignals, err := GenerateProof(pk, w)
  49. assert.Nil(t, err)
  50. fmt.Println("proof generation time for "+circuit+" elapsed:", time.Since(beforeT))
  51. proofStr, err := parsers.ProofToJson(proof)
  52. assert.Nil(t, err)
  53. err = ioutil.WriteFile("../testdata/"+circuit+"/proof.json", proofStr, 0644)
  54. assert.Nil(t, err)
  55. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  56. assert.Nil(t, err)
  57. err = ioutil.WriteFile("../testdata/"+circuit+"/public.json", publicStr, 0644)
  58. assert.Nil(t, err)
  59. // verify the proof
  60. vkJson, err := ioutil.ReadFile("../testdata/" + circuit + "/verification_key.json")
  61. require.Nil(t, err)
  62. vk, err := parsers.ParseVk(vkJson)
  63. require.Nil(t, err)
  64. v := verifier.Verify(vk, proof, pubSignals)
  65. assert.True(t, v)
  66. // to verify the proof with snarkjs:
  67. // snarkjs verify --vk testdata/circuitX/verification_key.json -p testdata/circuitX/proof.json --pub testdata/circuitX/public.json
  68. }
  69. func BenchmarkGenerateProof(b *testing.B) {
  70. // benchmark with a circuit of 10000 constraints
  71. provingKeyJson, err := ioutil.ReadFile("../testdata/circuit5k/proving_key.json")
  72. require.Nil(b, err)
  73. pk, err := parsers.ParsePk(provingKeyJson)
  74. require.Nil(b, err)
  75. witnessJson, err := ioutil.ReadFile("../testdata/circuit5k/witness.json")
  76. require.Nil(b, err)
  77. w, err := parsers.ParseWitness(witnessJson)
  78. require.Nil(b, err)
  79. for i := 0; i < b.N; i++ {
  80. GenerateProof(pk, w)
  81. }
  82. }