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.

158 lines
4.9 KiB

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