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.

122 lines
3.8 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 := G.Mul(big.NewInt(3))
  52. b := p.Bytes()
  53. assert.Equal(t, "f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f900", hex.EncodeToString(b)) //nolint:lll
  54. p2, err := NewPointFromBytes(b)
  55. assert.Nil(t, err)
  56. assert.Equal(t, p, p2)
  57. p = G.Mul(big.NewInt(1234))
  58. b = p.Bytes()
  59. assert.Equal(t, "e37648435c60dcd181b3d41d50857ba5b5abebe279429aa76558f6653f1658f200", hex.EncodeToString(b)) //nolint:lll
  60. p2, err = NewPointFromBytes(b)
  61. assert.Nil(t, err)
  62. assert.Equal(t, p, p2)
  63. // PublicKey
  64. pk := PublicKey(*p)
  65. b = pk.Bytes()
  66. assert.Equal(t, "e37648435c60dcd181b3d41d50857ba5b5abebe279429aa76558f6653f1658f200", hex.EncodeToString(b)) //nolint:lll
  67. pk2, err := NewPublicKeyFromBytes(b)
  68. assert.Nil(t, err)
  69. assert.Equal(t, &pk, pk2)
  70. // Signature
  71. sig := Signature{
  72. S: big.NewInt(9876),
  73. F: p,
  74. }
  75. b = sig.Bytes()
  76. assert.Equal(t, "9426000000000000000000000000000000000000000000000000000000000000e37648435c60dcd181b3d41d50857ba5b5abebe279429aa76558f6653f1658f200", hex.EncodeToString(b)) //nolint:lll
  77. sig2, err := NewSignatureFromBytes(b)
  78. assert.Nil(t, err)
  79. assert.Equal(t, &sig, sig2)
  80. // Signature with bigger values
  81. s, ok := new(big.Int).SetString("43744879514016998261043792362491545206150700367692876136431010903034023684055", 10) //nolint:lll
  82. require.True(t, ok)
  83. x, ok := new(big.Int).SetString("56183217574518331862027285308947626162625485037257226169003339923450551228164", 10) //nolint:lll
  84. require.True(t, ok)
  85. y, ok := new(big.Int).SetString("62825693913681695979055350889339417157462875026935818721506450621762231021976", 10) //nolint:lll
  86. require.True(t, ok)
  87. sig = Signature{
  88. S: s,
  89. F: &Point{X: x, Y: y},
  90. }
  91. b = sig.Bytes()
  92. assert.Equal(t, "d7a75050259cc06415f19bde5460a58325e3050806ba949d9ac9728b71b9b6607c369301ed99f1b2ea93f2e3083dd52a2ce8d1d3feca1ad31e78811900ba570400", hex.EncodeToString(b)) //nolint:lll
  93. sig2, err = NewSignatureFromBytes(b)
  94. assert.Nil(t, err)
  95. assert.Equal(t, &sig, sig2)
  96. }
  97. func TestImportECDSApubKey(t *testing.T) {
  98. // Generate an ECDSA key
  99. k, err := crypto.GenerateKey()
  100. assert.Nil(t, err)
  101. // Import the ECDSA Public key bytes into a PublicKey type
  102. pk, err := NewPublicKeyFromECDSA(crypto.FromECDSAPub(&k.PublicKey))
  103. assert.Nil(t, err)
  104. // Set the ECDSA Private key point as a blindsecp256k1 PrivateKey type
  105. bk := PrivateKey(*k.D)
  106. // Compare both public keys
  107. assert.Equal(t, bk.Public().Bytes(), pk.Bytes())
  108. }