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.

125 lines
4.2 KiB

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