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.

106 lines
3.1 KiB

  1. package prover
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "math/big"
  6. "testing"
  7. "github.com/iden3/go-circom-prover-verifier/parsers"
  8. "github.com/iden3/go-circom-prover-verifier/types"
  9. "github.com/iden3/go-circom-prover-verifier/verifier"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestSmallCircuitGenerateProf(t *testing.T) {
  14. provingKeyJson, err := ioutil.ReadFile("../testdata/small/proving_key.json")
  15. require.Nil(t, err)
  16. pk, err := parsers.ParsePk(provingKeyJson)
  17. require.Nil(t, err)
  18. witnessJson, err := ioutil.ReadFile("../testdata/small/witness.json")
  19. require.Nil(t, err)
  20. w, err := parsers.ParseWitness(witnessJson)
  21. require.Nil(t, err)
  22. assert.Equal(t, types.Witness{big.NewInt(1), big.NewInt(33), big.NewInt(3), big.NewInt(11)}, w)
  23. proof, pubSignals, err := GenerateProof(pk, w)
  24. assert.Nil(t, err)
  25. proofStr, err := parsers.ProofToJson(proof)
  26. assert.Nil(t, err)
  27. err = ioutil.WriteFile("../testdata/small/proof.json", proofStr, 0644)
  28. assert.Nil(t, err)
  29. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  30. assert.Nil(t, err)
  31. err = ioutil.WriteFile("../testdata/small/public.json", publicStr, 0644)
  32. assert.Nil(t, err)
  33. // verify the proof
  34. vkJson, err := ioutil.ReadFile("../testdata/small/verification_key.json")
  35. require.Nil(t, err)
  36. vk, err := parsers.ParseVk(vkJson)
  37. require.Nil(t, err)
  38. v := verifier.Verify(vk, proof, pubSignals)
  39. assert.True(t, v)
  40. // to verify the proof with snarkjs:
  41. // snarkjs verify --vk testdata/small/verification_key.json -p testdata/small/proof.json --pub testdata/small/public.json
  42. }
  43. func TestBigCircuitGenerateProf(t *testing.T) {
  44. provingKeyJson, err := ioutil.ReadFile("../testdata/big/proving_key.json")
  45. require.Nil(t, err)
  46. pk, err := parsers.ParsePk(provingKeyJson)
  47. require.Nil(t, err)
  48. witnessJson, err := ioutil.ReadFile("../testdata/big/witness.json")
  49. require.Nil(t, err)
  50. w, err := parsers.ParseWitness(witnessJson)
  51. require.Nil(t, err)
  52. proof, pubSignals, err := GenerateProof(pk, w)
  53. assert.Nil(t, err)
  54. proofStr, err := parsers.ProofToJson(proof)
  55. assert.Nil(t, err)
  56. err = ioutil.WriteFile("../testdata/big/proof.json", proofStr, 0644)
  57. assert.Nil(t, err)
  58. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  59. assert.Nil(t, err)
  60. err = ioutil.WriteFile("../testdata/big/public.json", publicStr, 0644)
  61. assert.Nil(t, err)
  62. // verify the proof
  63. vkJson, err := ioutil.ReadFile("../testdata/big/verification_key.json")
  64. require.Nil(t, err)
  65. vk, err := parsers.ParseVk(vkJson)
  66. require.Nil(t, err)
  67. v := verifier.Verify(vk, proof, pubSignals)
  68. assert.True(t, v)
  69. // to verify the proof with snarkjs:
  70. // snarkjs verify --vk testdata/big/verification_key.json -p testdata/big/proof.json --pub testdata/big/public.json
  71. }
  72. func BenchmarkGenerateProof(b *testing.B) {
  73. provingKeyJson, err := ioutil.ReadFile("../testdata/big/proving_key.json")
  74. require.Nil(b, err)
  75. pk, err := parsers.ParsePk(provingKeyJson)
  76. require.Nil(b, err)
  77. witnessJson, err := ioutil.ReadFile("../testdata/big/witness.json")
  78. require.Nil(b, err)
  79. w, err := parsers.ParseWitness(witnessJson)
  80. require.Nil(b, err)
  81. for i := 0; i < b.N; i++ {
  82. GenerateProof(pk, w)
  83. }
  84. }