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.

88 lines
2.6 KiB

  1. package secp256k1_test
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "github.com/btcsuite/btcutil/base58"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/crypto"
  9. "github.com/tendermint/tendermint/crypto/secp256k1"
  10. underlyingSecp256k1 "github.com/btcsuite/btcd/btcec"
  11. )
  12. type keyData struct {
  13. priv string
  14. pub string
  15. addr string
  16. }
  17. var secpDataTable = []keyData{
  18. {
  19. priv: "a96e62ed3955e65be32703f12d87b6b5cf26039ecfa948dc5107a495418e5330",
  20. pub: "02950e1cdfcb133d6024109fd489f734eeb4502418e538c28481f22bce276f248c",
  21. addr: "1CKZ9Nx4zgds8tU7nJHotKSDr4a9bYJCa3",
  22. },
  23. }
  24. func TestPubKeySecp256k1Address(t *testing.T) {
  25. for _, d := range secpDataTable {
  26. privB, _ := hex.DecodeString(d.priv)
  27. pubB, _ := hex.DecodeString(d.pub)
  28. addrBbz, _, _ := base58.CheckDecode(d.addr)
  29. addrB := crypto.Address(addrBbz)
  30. var priv secp256k1.PrivKeySecp256k1
  31. copy(priv[:], privB)
  32. pubKey := priv.PubKey()
  33. pubT, _ := pubKey.(secp256k1.PubKeySecp256k1)
  34. pub := pubT[:]
  35. addr := pubKey.Address()
  36. assert.Equal(t, pub, pubB, "Expected pub keys to match")
  37. assert.Equal(t, addr, addrB, "Expected addresses to match")
  38. }
  39. }
  40. func TestSignAndValidateSecp256k1(t *testing.T) {
  41. privKey := secp256k1.GenPrivKey()
  42. pubKey := privKey.PubKey()
  43. msg := crypto.CRandBytes(128)
  44. sig, err := privKey.Sign(msg)
  45. require.Nil(t, err)
  46. assert.True(t, pubKey.VerifyBytes(msg, sig))
  47. // Mutate the signature, just one bit.
  48. sigEd := sig.(secp256k1.SignatureSecp256k1)
  49. sigEd[3] ^= byte(0x01)
  50. sig = sigEd
  51. assert.False(t, pubKey.VerifyBytes(msg, sig))
  52. }
  53. // This test is intended to justify the removal of calls to the underlying library
  54. // in creating the privkey.
  55. func TestSecp256k1LoadPrivkeyAndSerializeIsIdentity(t *testing.T) {
  56. numberOfTests := 256
  57. for i := 0; i < numberOfTests; i++ {
  58. // Seed the test case with some random bytes
  59. privKeyBytes := [32]byte{}
  60. copy(privKeyBytes[:], crypto.CRandBytes(32))
  61. // This function creates a private and public key in the underlying libraries format.
  62. // The private key is basically calling new(big.Int).SetBytes(pk), which removes leading zero bytes
  63. priv, _ := underlyingSecp256k1.PrivKeyFromBytes(underlyingSecp256k1.S256(), privKeyBytes[:])
  64. // this takes the bytes returned by `(big int).Bytes()`, and if the length is less than 32 bytes,
  65. // pads the bytes from the left with zero bytes. Therefore these two functions composed
  66. // result in the identity function on privKeyBytes, hence the following equality check
  67. // always returning true.
  68. serializedBytes := priv.Serialize()
  69. require.Equal(t, privKeyBytes[:], serializedBytes)
  70. }
  71. }