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.

120 lines
4.8 KiB

  1. package bellmanverifier
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestParsePoints(t *testing.T) {
  9. aHex := []string{"0x2cdfab288afda1ba399d60951423e76445754b7d2e7827634732988373c8e0ff", "0x1bb2e3543dfdd373610db3ea82703bc70e4d6f01b5c0e10709e27545670824f4"}
  10. a, err := hexToG1(aHex)
  11. assert.Nil(t, err)
  12. assert.Equal(t, "bn256.G1(2cdfab288afda1ba399d60951423e76445754b7d2e7827634732988373c8e0ff, 1bb2e3543dfdd373610db3ea82703bc70e4d6f01b5c0e10709e27545670824f4)", a.String())
  13. bHex := [][]string{
  14. {
  15. "0x0f9cd75cae408d3b6d2731035dbb6aa6f66cf900c1d64b46510cbbcd7cf94f64",
  16. "0x165997362d7c2bf5672d16206fbee620ad8eb54f609b98c49c7c6287c9979077",
  17. },
  18. {
  19. "0x0392982b7cd7bdbbee79d5f808c67ead8ef3e2347810b546b419da5f738aab92",
  20. "0x0f94d6781b9de113b86abd1930accd2260d0c979d520bc1a0e79dec3c8ce76a3",
  21. },
  22. }
  23. b, err := hexToG2(bHex)
  24. assert.Nil(t, err)
  25. assert.Equal(t, "bn256.G2((165997362d7c2bf5672d16206fbee620ad8eb54f609b98c49c7c6287c9979077, 0f9cd75cae408d3b6d2731035dbb6aa6f66cf900c1d64b46510cbbcd7cf94f64), (0f94d6781b9de113b86abd1930accd2260d0c979d520bc1a0e79dec3c8ce76a3, 0392982b7cd7bdbbee79d5f808c67ead8ef3e2347810b546b419da5f738aab92))", b.String())
  26. gHex := [][]string{
  27. {
  28. "0x2d0c4fa1239184802aeda1f206e49104940aa3eccc1b3e0141c25b2dba8e7caf",
  29. "0x15c9b1123841897787badbe858eb00943fc8a99454666f21acf4e79e13547471",
  30. },
  31. {
  32. "0x256ad09ecb0abc15fd48f20c37d28ffcf0f8eb3b23cb10cdeee7365b598963ac",
  33. "0x2bc9bc381cf68badd992338c637b36b54936b69cb8560eaf5a8cbe2c20ff8522",
  34. },
  35. }
  36. g, err := hexToG2(gHex)
  37. assert.Nil(t, err)
  38. assert.Equal(t, "bn256.G2((15c9b1123841897787badbe858eb00943fc8a99454666f21acf4e79e13547471, 2d0c4fa1239184802aeda1f206e49104940aa3eccc1b3e0141c25b2dba8e7caf), (2bc9bc381cf68badd992338c637b36b54936b69cb8560eaf5a8cbe2c20ff8522, 256ad09ecb0abc15fd48f20c37d28ffcf0f8eb3b23cb10cdeee7365b598963ac))", g.String())
  39. }
  40. func TestParseProof(t *testing.T) {
  41. proofJson, err := ioutil.ReadFile("testdata/proof0.json")
  42. require.Nil(t, err)
  43. vkJson, err := ioutil.ReadFile("testdata/vk0.json")
  44. require.Nil(t, err)
  45. proof, err := ParseProofRaw(proofJson)
  46. require.Nil(t, err)
  47. vk, err := ParseVkRaw(vkJson)
  48. require.Nil(t, err)
  49. assert.Equal(t, "bn256.G1(2cdfab288afda1ba399d60951423e76445754b7d2e7827634732988373c8e0ff, 1bb2e3543dfdd373610db3ea82703bc70e4d6f01b5c0e10709e27545670824f4)", proof.A.String())
  50. assert.Equal(t, "bn256.G1(0b710c2f32925ad4576f925e8c5954c7dace91437fd6bc3ded1d15b70990a885, 2cd6ea79e38ad566aceb09113e67142d278d12a76b49031ce1bde1dedca696b4)", proof.C.String())
  51. assert.Equal(t, "bn256.G1(0c0e14d07f2281c592952c72f86a7f5df7189ab6d00b84609ad777fbf062f38d, 2f60c0e4913ec4691bdf2dd9f2b5fed9b80a3267eec107b9f1d69418a19a30a8)", vk.Alpha.String())
  52. assert.Equal(t, "bn256.G2((2275d97dce5445433ec7bc6d01c35f0afad9afcf6f3350cd15eeef1023242c01, 0b5f21c2d981916cd5e1037b446b170b6c60dd184fdbb3381b7d0880fb48300d), (2c2a08a60032f536afbcb21c079b563cdce68b7ef906e973c52f574121a95df0, 1690100372c53776b60c0ee56926debb4d0acee90f7952ecc63861e0269a098a))", vk.Beta.String())
  53. assert.Equal(t, "bn256.G2((15c9b1123841897787badbe858eb00943fc8a99454666f21acf4e79e13547471, 2d0c4fa1239184802aeda1f206e49104940aa3eccc1b3e0141c25b2dba8e7caf), (2bc9bc381cf68badd992338c637b36b54936b69cb8560eaf5a8cbe2c20ff8522, 256ad09ecb0abc15fd48f20c37d28ffcf0f8eb3b23cb10cdeee7365b598963ac))", vk.Gamma.String())
  54. }
  55. func TestVerify0(t *testing.T) {
  56. proofJson, err := ioutil.ReadFile("testdata/proof0.json")
  57. require.Nil(t, err)
  58. vkJson, err := ioutil.ReadFile("testdata/vk0.json")
  59. require.Nil(t, err)
  60. publicJson, err := ioutil.ReadFile("testdata/public0.json")
  61. require.Nil(t, err)
  62. public, err := ParsePublicRaw(publicJson)
  63. require.Nil(t, err)
  64. proof, err := ParseProofRaw(proofJson)
  65. require.Nil(t, err)
  66. vk, err := ParseVkRaw(vkJson)
  67. require.Nil(t, err)
  68. v := Verify(vk, proof, public)
  69. assert.True(t, v)
  70. }
  71. func TestVerify1(t *testing.T) {
  72. proofJson, err := ioutil.ReadFile("testdata/proof1.json")
  73. require.Nil(t, err)
  74. vkJson, err := ioutil.ReadFile("testdata/vk1.json")
  75. require.Nil(t, err)
  76. publicJson, err := ioutil.ReadFile("testdata/public1.json")
  77. require.Nil(t, err)
  78. public, err := ParsePublicRaw(publicJson)
  79. require.Nil(t, err)
  80. proof, err := ParseProofRaw(proofJson)
  81. require.Nil(t, err)
  82. vk, err := ParseVkRaw(vkJson)
  83. require.Nil(t, err)
  84. v := Verify(vk, proof, public)
  85. assert.True(t, v)
  86. }
  87. func BenchmarkVerify(b *testing.B) {
  88. proofJson, err := ioutil.ReadFile("testdata/proof0.json")
  89. require.Nil(b, err)
  90. vkJson, err := ioutil.ReadFile("testdata/vk0.json")
  91. require.Nil(b, err)
  92. publicJson, err := ioutil.ReadFile("testdata/public0.json")
  93. require.Nil(b, err)
  94. public, err := ParsePublicRaw(publicJson)
  95. require.Nil(b, err)
  96. proof, err := ParseProofRaw(proofJson)
  97. require.Nil(b, err)
  98. vk, err := ParseVkRaw(vkJson)
  99. require.Nil(b, err)
  100. for i := 0; i < b.N; i++ {
  101. Verify(vk, proof, public)
  102. }
  103. }