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.

86 lines
2.5 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/tendermint/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. sig[3] ^= byte(0x01)
  49. assert.False(t, pubKey.VerifyBytes(msg, sig))
  50. }
  51. // This test is intended to justify the removal of calls to the underlying library
  52. // in creating the privkey.
  53. func TestSecp256k1LoadPrivkeyAndSerializeIsIdentity(t *testing.T) {
  54. numberOfTests := 256
  55. for i := 0; i < numberOfTests; i++ {
  56. // Seed the test case with some random bytes
  57. privKeyBytes := [32]byte{}
  58. copy(privKeyBytes[:], crypto.CRandBytes(32))
  59. // This function creates a private and public key in the underlying libraries format.
  60. // The private key is basically calling new(big.Int).SetBytes(pk), which removes leading zero bytes
  61. priv, _ := underlyingSecp256k1.PrivKeyFromBytes(underlyingSecp256k1.S256(), privKeyBytes[:])
  62. // this takes the bytes returned by `(big int).Bytes()`, and if the length is less than 32 bytes,
  63. // pads the bytes from the left with zero bytes. Therefore these two functions composed
  64. // result in the identity function on privKeyBytes, hence the following equality check
  65. // always returning true.
  66. serializedBytes := priv.Serialize()
  67. require.Equal(t, privKeyBytes[:], serializedBytes)
  68. }
  69. }