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.

91 lines
2.8 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. witnessBinFile, err := os.Open("../testdata/" + circuit + "/witness.bin")
  37. require.Nil(t, err)
  38. defer witnessBinFile.Close()
  39. w, err := parsers.ParseWitnessBin(witnessBinFile)
  40. require.Nil(t, err)
  41. beforeT := time.Now()
  42. proof, pubSignals, err := GenerateProof(pk, w)
  43. assert.Nil(t, err)
  44. fmt.Println("proof generation time for "+circuit+" elapsed:", time.Since(beforeT))
  45. proofStr, err := parsers.ProofToJson(proof)
  46. assert.Nil(t, err)
  47. err = ioutil.WriteFile("../testdata/"+circuit+"/proof.json", proofStr, 0644)
  48. assert.Nil(t, err)
  49. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  50. assert.Nil(t, err)
  51. err = ioutil.WriteFile("../testdata/"+circuit+"/public.json", publicStr, 0644)
  52. assert.Nil(t, err)
  53. // verify the proof
  54. vkJson, err := ioutil.ReadFile("../testdata/" + circuit + "/verification_key.json")
  55. require.Nil(t, err)
  56. vk, err := parsers.ParseVk(vkJson)
  57. require.Nil(t, err)
  58. v := verifier.Verify(vk, proof, pubSignals)
  59. assert.True(t, v)
  60. // to verify the proof with snarkjs:
  61. // snarkjs verify --vk testdata/circuitX/verification_key.json -p testdata/circuitX/proof.json --pub testdata/circuitX/public.json
  62. }
  63. func BenchmarkGenerateProof(b *testing.B) {
  64. // benchmark with a circuit of 10000 constraints
  65. provingKeyJson, err := ioutil.ReadFile("../testdata/circuit5k/proving_key.json")
  66. require.Nil(b, err)
  67. pk, err := parsers.ParsePk(provingKeyJson)
  68. require.Nil(b, err)
  69. witnessJson, err := ioutil.ReadFile("../testdata/circuit5k/witness.json")
  70. require.Nil(b, err)
  71. w, err := parsers.ParseWitness(witnessJson)
  72. require.Nil(b, err)
  73. for i := 0; i < b.N; i++ {
  74. GenerateProof(pk, w)
  75. }
  76. }