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.

140 lines
4.4 KiB

  1. package blindsecp256k1
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "math/big"
  6. "testing"
  7. "github.com/btcsuite/btcd/btcec"
  8. "github.com/ethereum/go-ethereum/crypto"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestMarshalers(t *testing.T) {
  13. curv := btcec.S256()
  14. G := &Point{
  15. X: curv.Params().Gx,
  16. Y: curv.Params().Gy,
  17. }
  18. // Point
  19. p := Curve{curv}.Mul(G, big.NewInt(1234))
  20. b, err := json.Marshal(p)
  21. require.Nil(t, err)
  22. assert.Equal(t,
  23. `{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}`, //nolint:lll
  24. string(b))
  25. var p2 *Point
  26. err = json.Unmarshal(b, &p2)
  27. require.Nil(t, err)
  28. assert.Equal(t, p, p2)
  29. // PublicKey
  30. pk := PublicKey(*p)
  31. b, err = json.Marshal(pk)
  32. require.Nil(t, err)
  33. assert.Equal(t,
  34. `{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}`, //nolint:lll
  35. string(b))
  36. var pk2 PublicKey
  37. err = json.Unmarshal(b, &pk2)
  38. require.Nil(t, err)
  39. assert.Equal(t, pk, pk2)
  40. // Signature
  41. sig := Signature{
  42. S: big.NewInt(9876),
  43. F: p,
  44. }
  45. b, err = json.Marshal(sig)
  46. require.Nil(t, err)
  47. assert.Equal(t,
  48. `{"s":"9876","f":{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}}`, //nolint:lll
  49. string(b))
  50. var sig2 Signature
  51. err = json.Unmarshal(b, &sig2)
  52. require.Nil(t, err)
  53. assert.Equal(t, sig, sig2)
  54. }
  55. func TestBytes(t *testing.T) {
  56. curv := btcec.S256()
  57. G := &Point{
  58. X: curv.Params().Gx,
  59. Y: curv.Params().Gy,
  60. }
  61. // Point
  62. p := &Point{
  63. X: big.NewInt(3),
  64. Y: big.NewInt(3),
  65. }
  66. b := p.Bytes()
  67. assert.Equal(t, "03000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(b)) //nolint:lll
  68. p2, err := NewPointFromBytes(b)
  69. assert.Nil(t, err)
  70. assert.Equal(t, p, p2)
  71. p = Curve{curv}.Mul(G, big.NewInt(1234))
  72. b = p.Bytes()
  73. assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
  74. p2, err = NewPointFromBytes(b)
  75. assert.Nil(t, err)
  76. assert.Equal(t, p, p2)
  77. // PublicKey
  78. pk := PublicKey(*p)
  79. b = pk.Bytes()
  80. assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
  81. pk2, err := NewPublicKeyFromBytes(b)
  82. assert.Nil(t, err)
  83. assert.Equal(t, &pk, pk2)
  84. // Signature
  85. sig := Signature{
  86. S: big.NewInt(9876),
  87. F: p,
  88. }
  89. b = sig.Bytes()
  90. assert.Equal(t, "9426000000000000000000000000000000000000000000000000000000000000f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
  91. sig2, err := NewSignatureFromBytes(b)
  92. assert.Nil(t, err)
  93. assert.Equal(t, &sig, sig2)
  94. // Signature with bigger values
  95. s, ok := new(big.Int).SetString("43744879514016998261043792362491545206150700367692876136431010903034023684055", 10) //nolint:lll
  96. require.True(t, ok)
  97. x, ok := new(big.Int).SetString("56183217574518331862027285308947626162625485037257226169003339923450551228164", 10) //nolint:lll
  98. require.True(t, ok)
  99. y, ok := new(big.Int).SetString("62825693913681695979055350889339417157462875026935818721506450621762231021976", 10) //nolint:lll
  100. require.True(t, ok)
  101. sig = Signature{
  102. S: s,
  103. F: &Point{X: x, Y: y},
  104. }
  105. b = sig.Bytes()
  106. assert.Equal(t, "d7a75050259cc06415f19bde5460a58325e3050806ba949d9ac9728b71b9b6600457ba001981781ed31acafed3d1e82c2ad53d08e3f293eab2f199ed0193367c98311f1894598c91f10fe415ba4a6d04e1351d07430631c7decdbbdb2615e68a", hex.EncodeToString(b)) //nolint:lll
  107. sig2, err = NewSignatureFromBytes(b)
  108. assert.Nil(t, err)
  109. assert.Equal(t, &sig, sig2)
  110. }
  111. func TestImportECDSApubKey(t *testing.T) {
  112. curv := btcec.S256()
  113. // Generate an ECDSA key
  114. k, err := crypto.GenerateKey()
  115. assert.Nil(t, err)
  116. // Import the ECDSA Public key bytes into a PublicKey type
  117. pk, err := NewPublicKeyFromECDSA(crypto.FromECDSAPub(&k.PublicKey))
  118. assert.Nil(t, err)
  119. // Set the ECDSA Private key point as a blindsecp256k1 PrivateKey type
  120. bk := PrivateKey(*k.D)
  121. // Compare both public keys
  122. assert.Equal(t, bk.Public(curv).Bytes(), pk.Bytes())
  123. }