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.

118 lines
4.6 KiB

  1. package kzgceremony
  2. import (
  3. "encoding/hex"
  4. "strings"
  5. "testing"
  6. qt "github.com/frankban/quicktest"
  7. bls12381 "github.com/kilic/bls12-381"
  8. )
  9. func TestParseSRS(t *testing.T) {
  10. c := qt.New(t)
  11. srs_0 := newEmptySRS(2, 2)
  12. contr_1, err := Contribute(srs_0, []byte("1111111111111111111111111111111111111111111111111111111111111111"))
  13. c.Assert(err, qt.IsNil)
  14. g1s, g2s, err := contr_1.SRS.ToEthJSON()
  15. c.Assert(err, qt.IsNil)
  16. parsedSRS, err := ParseEthJSON(g1s, g2s)
  17. c.Assert(err, qt.IsNil)
  18. g1 := bls12381.NewG1()
  19. g2 := bls12381.NewG2()
  20. for i := 0; i < len(contr_1.SRS.G1s); i++ {
  21. c.Assert(g1.Equal(parsedSRS.G1s[i], contr_1.SRS.G1s[i]), qt.IsTrue)
  22. }
  23. for i := 0; i < len(contr_1.SRS.G2s); i++ {
  24. c.Assert(g2.Equal(parsedSRS.G2s[i], contr_1.SRS.G2s[i]), qt.IsTrue)
  25. }
  26. }
  27. func TestParseCompressedG1Point(t *testing.T) {
  28. // this test is just to check that github.com/kilic/bls12-381 is
  29. // compatible with the compressed points from zkcrypto/bls12-381
  30. c := qt.New(t)
  31. g1 := bls12381.NewG1()
  32. p1Str := "0x97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb"
  33. g1Bytes, err := hex.DecodeString(strings.TrimPrefix(p1Str, "0x"))
  34. g1Point, err := g1.FromCompressed(g1Bytes)
  35. c.Assert(err, qt.IsNil)
  36. recompressed := g1.ToCompressed(g1Point)
  37. c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p1Str)
  38. p1Str = "0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  39. g1Bytes, err = hex.DecodeString(strings.TrimPrefix(p1Str, "0x"))
  40. g1Point, err = g1.FromCompressed(g1Bytes)
  41. c.Assert(err, qt.IsNil)
  42. recompressed = g1.ToCompressed(g1Point)
  43. c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p1Str)
  44. // additionally check that g1Point is zero
  45. c.Assert(g1.Equal(g1Point, g1.Zero()), qt.IsTrue)
  46. }
  47. func TestParseCompressedG2Point(t *testing.T) {
  48. // this test is just to check that github.com/kilic/bls12-381 is
  49. // compatible with the compressed points from zkcrypto/bls12-381
  50. c := qt.New(t)
  51. g2 := bls12381.NewG2()
  52. p2Str := "0x93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8"
  53. g2Bytes, err := hex.DecodeString(strings.TrimPrefix(p2Str, "0x"))
  54. g2Point, err := g2.FromCompressed(g2Bytes)
  55. c.Assert(err, qt.IsNil)
  56. recompressed := g2.ToCompressed(g2Point)
  57. c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p2Str)
  58. p2Str = "0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  59. g2Bytes, err = hex.DecodeString(strings.TrimPrefix(p2Str, "0x"))
  60. g2Point, err = g2.FromCompressed(g2Bytes)
  61. c.Assert(err, qt.IsNil)
  62. recompressed = g2.ToCompressed(g2Point)
  63. c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p2Str)
  64. // additionally check that g1Point is zero
  65. c.Assert(g2.Equal(g2Point, g2.Zero()), qt.IsTrue)
  66. }
  67. func TestVectorFromRustImpl(t *testing.T) {
  68. c := qt.New(t)
  69. testVectorG1s := []string{
  70. "0x97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb",
  71. "0xae4a5e0fe4947fd5551664fc49453d805ce6af2dd6f053e147d95d9ede825190b06028f228f47c0d3118b71313235aa7",
  72. "0x888a1073366b3e974c318be5862f607c18fb5241cfefc3c8b82c31c4db10f866c546804db6e44bf61b61d8c31d006099",
  73. "0x84afca7ce5cc42aa998725610d14cd47078e523a9cb96b66856f563d62d44ff6489f0b1827b853e94d9474b75a138ec6",
  74. "0x8ae778a5b534c6eadcce4c4e7dd1dab3674d20bfe686f70506fba1695bbdae2455169b035505e5246d726a9e0c982335",
  75. }
  76. testVectorG2s := []string{
  77. "0x93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8",
  78. "0xa5d4fc7bfa882ef674fbbcdda73c66c2627a5a723ddf6fc0f242b3f71301a6e7181708a8c63756787a2c3e47c2b347ca18c64b103461036830880cd26bacad80cdbfe00c69e9214b7c90bf30ab0804f520bed3ce608aefeb6f85011f0d745575",
  79. "0x87b93440866545adf37f5119d640036b2856a790bd09c2b7f69a1a1bdc7846c5f48f622fcbf5c46a51dede46a4682b7b118542548df17ff7c7e6f63049e1d1c56ca1a637f8a55136d0ed7ab466e78550f635dc06d78d87a3003dab133b8e275a",
  80. }
  81. parsedSRS, err := ParseEthJSON(testVectorG1s, testVectorG2s)
  82. c.Assert(err, qt.IsNil)
  83. g1s, g2s, err := parsedSRS.ToEthJSON()
  84. c.Assert(err, qt.IsNil)
  85. c.Assert(len(testVectorG1s), qt.Equals, len(g1s))
  86. c.Assert(len(testVectorG2s), qt.Equals, len(g2s))
  87. for i := 0; i < len(g1s); i++ {
  88. c.Assert(g1s[i], qt.Equals, testVectorG1s[i])
  89. }
  90. for i := 0; i < len(g2s); i++ {
  91. c.Assert(g2s[i], qt.Equals, testVectorG2s[i])
  92. }
  93. }