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.

114 lines
2.6 KiB

9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package crypto
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/ed25519"
  7. amino "github.com/tendermint/go-amino"
  8. )
  9. func TestSignAndValidateEd25519(t *testing.T) {
  10. privKey := GenPrivKeyEd25519()
  11. pubKey, err := privKey.PubKey()
  12. require.Nil(t, err)
  13. msg := CRandBytes(128)
  14. sig, err := privKey.Sign(msg)
  15. require.Nil(t, err)
  16. // Test the signature
  17. assert.True(t, pubKey.VerifyBytes(msg, sig))
  18. // Mutate the signature, just one bit.
  19. sigEd := sig.(SignatureEd25519)
  20. sigEd[7] ^= byte(0x01)
  21. sig = sigEd
  22. assert.False(t, pubKey.VerifyBytes(msg, sig))
  23. }
  24. func TestSignAndValidateSecp256k1(t *testing.T) {
  25. privKey := GenPrivKeySecp256k1()
  26. pubKey, err := privKey.PubKey()
  27. require.Nil(t, err)
  28. msg := CRandBytes(128)
  29. sig, err := privKey.Sign(msg)
  30. require.Nil(t, err)
  31. assert.True(t, pubKey.VerifyBytes(msg, sig))
  32. // Mutate the signature, just one bit.
  33. sigEd := sig.(SignatureSecp256k1)
  34. sigEd[3] ^= byte(0x01)
  35. sig = sigEd
  36. assert.False(t, pubKey.VerifyBytes(msg, sig))
  37. }
  38. func TestSignatureEncodings(t *testing.T) {
  39. cases := []struct {
  40. privKey PrivKey
  41. sigSize int
  42. sigPrefix amino.PrefixBytes
  43. }{
  44. {
  45. privKey: GenPrivKeyEd25519(),
  46. sigSize: ed25519.SignatureSize,
  47. sigPrefix: [4]byte{0x3d, 0xa1, 0xdb, 0x2a},
  48. },
  49. {
  50. privKey: GenPrivKeySecp256k1(),
  51. sigSize: 0, // unknown
  52. sigPrefix: [4]byte{0x16, 0xe1, 0xfe, 0xea},
  53. },
  54. }
  55. for _, tc := range cases {
  56. // note we embed them from the beginning....
  57. pubKey, err := tc.privKey.PubKey()
  58. require.Nil(t, err)
  59. msg := CRandBytes(128)
  60. sig, err := tc.privKey.Sign(msg)
  61. require.Nil(t, err)
  62. // store as amino
  63. bin, err := cdc.MarshalBinaryBare(sig)
  64. require.Nil(t, err, "%+v", err)
  65. if tc.sigSize != 0 {
  66. // Q: where is 1 byte coming from?
  67. assert.Equal(t, tc.sigSize+amino.PrefixBytesLen+1, len(bin))
  68. }
  69. assert.Equal(t, tc.sigPrefix[:], bin[0:amino.PrefixBytesLen])
  70. // and back
  71. sig2 := Signature(nil)
  72. err = cdc.UnmarshalBinaryBare(bin, &sig2)
  73. require.Nil(t, err, "%+v", err)
  74. assert.EqualValues(t, sig, sig2)
  75. assert.True(t, pubKey.VerifyBytes(msg, sig2))
  76. /*
  77. // store as json
  78. js, err := data.ToJSON(sig)
  79. require.Nil(t, err, "%+v", err)
  80. assert.True(t, strings.Contains(string(js), tc.sigName))
  81. // and back
  82. sig3 := Signature{}
  83. err = data.FromJSON(js, &sig3)
  84. require.Nil(t, err, "%+v", err)
  85. assert.EqualValues(t, sig, sig3)
  86. assert.True(t, pubKey.VerifyBytes(msg, sig3))
  87. // and make sure we can textify it
  88. text, err := data.ToText(sig)
  89. require.Nil(t, err, "%+v", err)
  90. assert.True(t, strings.HasPrefix(text, tc.sigName))
  91. */
  92. }
  93. }