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.

94 lines
2.8 KiB

  1. package blindsecp256k1
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "math/big"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestMarshalers(t *testing.T) {
  11. // Point
  12. p := G.Mul(big.NewInt(1234))
  13. b, err := json.Marshal(p)
  14. require.Nil(t, err)
  15. assert.Equal(t,
  16. `{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}`, //nolint:lll
  17. string(b))
  18. var p2 *Point
  19. err = json.Unmarshal(b, &p2)
  20. require.Nil(t, err)
  21. assert.Equal(t, p, p2)
  22. // PublicKey
  23. pk := PublicKey(*p)
  24. b, err = json.Marshal(pk)
  25. require.Nil(t, err)
  26. assert.Equal(t,
  27. `{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}`, //nolint:lll
  28. string(b))
  29. var pk2 PublicKey
  30. err = json.Unmarshal(b, &pk2)
  31. require.Nil(t, err)
  32. assert.Equal(t, pk, pk2)
  33. // Signature
  34. sig := Signature{
  35. S: big.NewInt(9876),
  36. F: p,
  37. }
  38. b, err = json.Marshal(sig)
  39. require.Nil(t, err)
  40. assert.Equal(t,
  41. `{"s":"9876","f":{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}}`, //nolint:lll
  42. string(b))
  43. var sig2 Signature
  44. err = json.Unmarshal(b, &sig2)
  45. require.Nil(t, err)
  46. assert.Equal(t, sig, sig2)
  47. }
  48. func TestBytes(t *testing.T) {
  49. // Point
  50. p := &Point{
  51. X: big.NewInt(3),
  52. Y: big.NewInt(3),
  53. }
  54. b := p.Bytes()
  55. assert.Equal(t, "03000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(b)) //nolint:lll
  56. p2, err := NewPointFromBytes(b)
  57. assert.Nil(t, err)
  58. assert.Equal(t, p, p2)
  59. p = G.Mul(big.NewInt(1234))
  60. b = p.Bytes()
  61. assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
  62. p2, err = NewPointFromBytes(b)
  63. assert.Nil(t, err)
  64. assert.Equal(t, p, p2)
  65. // PublicKey
  66. pk := PublicKey(*p)
  67. b = pk.Bytes()
  68. assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
  69. pk2, err := NewPublicKeyFromBytes(b)
  70. assert.Nil(t, err)
  71. assert.Equal(t, &pk, pk2)
  72. // Signature
  73. sig := Signature{
  74. S: big.NewInt(9876),
  75. F: p,
  76. }
  77. b = sig.Bytes()
  78. assert.Equal(t, "9426000000000000000000000000000000000000000000000000000000000000f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
  79. sig2, err := NewSignatureFromBytes(b)
  80. assert.Nil(t, err)
  81. assert.Equal(t, &sig, sig2)
  82. }