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.

151 lines
4.6 KiB

  1. package prover
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "math/big"
  7. "testing"
  8. "github.com/iden3/go-circom-prover-verifier/parsers"
  9. "github.com/iden3/go-circom-prover-verifier/types"
  10. "github.com/iden3/go-circom-prover-verifier/verifier"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestSmallCircuitGenerateProof(t *testing.T) {
  15. provingKeyJson, err := ioutil.ReadFile("../testdata/small/proving_key.json")
  16. require.Nil(t, err)
  17. pk, err := parsers.ParsePk(provingKeyJson)
  18. require.Nil(t, err)
  19. witnessJson, err := ioutil.ReadFile("../testdata/small/witness.json")
  20. require.Nil(t, err)
  21. w, err := parsers.ParseWitness(witnessJson)
  22. require.Nil(t, err)
  23. assert.Equal(t, types.Witness{big.NewInt(1), big.NewInt(33), big.NewInt(3), big.NewInt(11)}, w)
  24. proof, pubSignals, err := GenerateProof(pk, w)
  25. assert.Nil(t, err)
  26. proofStr, err := parsers.ProofToJson(proof)
  27. assert.Nil(t, err)
  28. err = ioutil.WriteFile("../testdata/small/proof.json", proofStr, 0644)
  29. assert.Nil(t, err)
  30. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  31. assert.Nil(t, err)
  32. err = ioutil.WriteFile("../testdata/small/public.json", publicStr, 0644)
  33. assert.Nil(t, err)
  34. // verify the proof
  35. vkJson, err := ioutil.ReadFile("../testdata/small/verification_key.json")
  36. require.Nil(t, err)
  37. vk, err := parsers.ParseVk(vkJson)
  38. require.Nil(t, err)
  39. v := verifier.Verify(vk, proof, pubSignals)
  40. assert.True(t, v)
  41. // to verify the proof with snarkjs:
  42. // snarkjs verify --vk testdata/small/verification_key.json -p testdata/small/proof.json --pub testdata/small/public.json
  43. }
  44. func TestBigCircuitGenerateProof(t *testing.T) {
  45. provingKeyJson, err := ioutil.ReadFile("../testdata/big/proving_key.json")
  46. require.Nil(t, err)
  47. pk, err := parsers.ParsePk(provingKeyJson)
  48. require.Nil(t, err)
  49. witnessJson, err := ioutil.ReadFile("../testdata/big/witness.json")
  50. require.Nil(t, err)
  51. w, err := parsers.ParseWitness(witnessJson)
  52. require.Nil(t, err)
  53. proof, pubSignals, err := GenerateProof(pk, w)
  54. assert.Nil(t, err)
  55. proofStr, err := parsers.ProofToJson(proof)
  56. assert.Nil(t, err)
  57. err = ioutil.WriteFile("../testdata/big/proof.json", proofStr, 0644)
  58. assert.Nil(t, err)
  59. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  60. assert.Nil(t, err)
  61. err = ioutil.WriteFile("../testdata/big/public.json", publicStr, 0644)
  62. assert.Nil(t, err)
  63. // verify the proof
  64. vkJson, err := ioutil.ReadFile("../testdata/big/verification_key.json")
  65. require.Nil(t, err)
  66. vk, err := parsers.ParseVk(vkJson)
  67. require.Nil(t, err)
  68. v := verifier.Verify(vk, proof, pubSignals)
  69. assert.True(t, v)
  70. // to verify the proof with snarkjs:
  71. // snarkjs verify --vk testdata/big/verification_key.json -p testdata/big/proof.json --pub testdata/big/public.json
  72. }
  73. func TestIdStateCircuitGenerateProof(t *testing.T) {
  74. // this test is to execute the proof generation for a bigger circuit
  75. // (arround 22500 constraints)
  76. //
  77. // to see the time needed to execute this
  78. // test Will need the ../testdata/idstate-circuit compiled &
  79. // trustedsetup files (generated in
  80. // https://github.com/iden3/go-zksnark-full-flow-example)
  81. if false {
  82. fmt.Println("TestIdStateCircuitGenerateProof activated")
  83. provingKeyJson, err := ioutil.ReadFile("../testdata/idstate-circuit/proving_key.json")
  84. require.Nil(t, err)
  85. pk, err := parsers.ParsePk(provingKeyJson)
  86. require.Nil(t, err)
  87. witnessJson, err := ioutil.ReadFile("../testdata/idstate-circuit/witness.json")
  88. require.Nil(t, err)
  89. w, err := parsers.ParseWitness(witnessJson)
  90. require.Nil(t, err)
  91. proof, pubSignals, err := GenerateProof(pk, w)
  92. assert.Nil(t, err)
  93. proofStr, err := parsers.ProofToJson(proof)
  94. assert.Nil(t, err)
  95. err = ioutil.WriteFile("../testdata/idstate-circuit/proof.json", proofStr, 0644)
  96. assert.Nil(t, err)
  97. publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
  98. assert.Nil(t, err)
  99. err = ioutil.WriteFile("../testdata/idstate-circuit/public.json", publicStr, 0644)
  100. assert.Nil(t, err)
  101. // verify the proof
  102. vkJson, err := ioutil.ReadFile("../testdata/idstate-circuit/verification_key.json")
  103. require.Nil(t, err)
  104. vk, err := parsers.ParseVk(vkJson)
  105. require.Nil(t, err)
  106. v := verifier.Verify(vk, proof, pubSignals)
  107. assert.True(t, v)
  108. }
  109. }
  110. func BenchmarkGenerateProof(b *testing.B) {
  111. provingKeyJson, err := ioutil.ReadFile("../testdata/big/proving_key.json")
  112. require.Nil(b, err)
  113. pk, err := parsers.ParsePk(provingKeyJson)
  114. require.Nil(b, err)
  115. witnessJson, err := ioutil.ReadFile("../testdata/big/witness.json")
  116. require.Nil(b, err)
  117. w, err := parsers.ParseWitness(witnessJson)
  118. require.Nil(b, err)
  119. for i := 0; i < b.N; i++ {
  120. GenerateProof(pk, w)
  121. }
  122. }