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.

79 lines
2.7 KiB

  1. package kzgceremony
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "io/ioutil"
  6. "strings"
  7. "testing"
  8. qt "github.com/frankban/quicktest"
  9. bls12381 "github.com/kilic/bls12-381"
  10. )
  11. func TestStateMarshalers(t *testing.T) {
  12. c := qt.New(t)
  13. j, err := ioutil.ReadFile("current_state_10.json")
  14. c.Assert(err, qt.IsNil)
  15. state := &State{}
  16. err = json.Unmarshal(j, state)
  17. c.Assert(err, qt.IsNil)
  18. b, err := json.Marshal(state)
  19. c.Assert(err, qt.IsNil)
  20. err = ioutil.WriteFile("parsed_state.json", b, 0600)
  21. c.Assert(err, qt.IsNil)
  22. }
  23. func TestParseCompressedG1Point(t *testing.T) {
  24. // this test is just to check that github.com/kilic/bls12-381 is
  25. // compatible with the compressed points from zkcrypto/bls12-381
  26. c := qt.New(t)
  27. g1 := bls12381.NewG1()
  28. p1Str := "0x97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb"
  29. g1Bytes, err := hex.DecodeString(strings.TrimPrefix(p1Str, "0x"))
  30. g1Point, err := g1.FromCompressed(g1Bytes)
  31. c.Assert(err, qt.IsNil)
  32. recompressed := g1.ToCompressed(g1Point)
  33. c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p1Str)
  34. p1Str = "0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  35. g1Bytes, err = hex.DecodeString(strings.TrimPrefix(p1Str, "0x"))
  36. g1Point, err = g1.FromCompressed(g1Bytes)
  37. c.Assert(err, qt.IsNil)
  38. recompressed = g1.ToCompressed(g1Point)
  39. c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p1Str)
  40. // additionally check that g1Point is zero
  41. c.Assert(g1.Equal(g1Point, g1.Zero()), qt.IsTrue)
  42. }
  43. func TestParseCompressedG2Point(t *testing.T) {
  44. // this test is just to check that github.com/kilic/bls12-381 is
  45. // compatible with the compressed points from zkcrypto/bls12-381
  46. c := qt.New(t)
  47. g2 := bls12381.NewG2()
  48. p2Str := "0x93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8"
  49. g2Bytes, err := hex.DecodeString(strings.TrimPrefix(p2Str, "0x"))
  50. g2Point, err := g2.FromCompressed(g2Bytes)
  51. c.Assert(err, qt.IsNil)
  52. recompressed := g2.ToCompressed(g2Point)
  53. c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p2Str)
  54. p2Str = "0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  55. g2Bytes, err = hex.DecodeString(strings.TrimPrefix(p2Str, "0x"))
  56. g2Point, err = g2.FromCompressed(g2Bytes)
  57. c.Assert(err, qt.IsNil)
  58. recompressed = g2.ToCompressed(g2Point)
  59. c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p2Str)
  60. // additionally check that g1Point is zero
  61. c.Assert(g2.Equal(g2Point, g2.Zero()), qt.IsTrue)
  62. }