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.

77 lines
2.4 KiB

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