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.

107 lines
2.4 KiB

9 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
  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. "github.com/tendermint/go-wire"
  8. )
  9. func TestSignAndValidateEd25519(t *testing.T) {
  10. privKey := GenPrivKeyEd25519()
  11. pubKey := privKey.PubKey()
  12. msg := CRandBytes(128)
  13. sig := privKey.Sign(msg)
  14. // Test the signature
  15. assert.True(t, pubKey.VerifyBytes(msg, sig))
  16. // Mutate the signature, just one bit.
  17. sigEd := sig.(SignatureEd25519)
  18. sigEd[7] ^= byte(0x01)
  19. sig = sigEd
  20. assert.False(t, pubKey.VerifyBytes(msg, sig))
  21. }
  22. func TestSignAndValidateSecp256k1(t *testing.T) {
  23. privKey := GenPrivKeySecp256k1()
  24. pubKey := privKey.PubKey()
  25. msg := CRandBytes(128)
  26. sig := privKey.Sign(msg)
  27. assert.True(t, pubKey.VerifyBytes(msg, sig))
  28. // Mutate the signature, just one bit.
  29. sigEd := sig.(SignatureSecp256k1)
  30. sigEd[3] ^= byte(0x01)
  31. sig = sigEd
  32. assert.False(t, pubKey.VerifyBytes(msg, sig))
  33. }
  34. func TestSignatureEncodings(t *testing.T) {
  35. cases := []struct {
  36. privKey PrivKey
  37. sigSize int
  38. sigPrefix wire.PrefixBytes
  39. }{
  40. {
  41. privKey: GenPrivKeyEd25519(),
  42. sigSize: ed25519.SignatureSize,
  43. sigPrefix: [4]byte{0xe4, 0x51, 0x7b, 0xa3},
  44. },
  45. {
  46. privKey: GenPrivKeySecp256k1(),
  47. sigSize: 0, // unknown
  48. sigPrefix: [4]byte{0x37, 0xb9, 0x21, 0x3e},
  49. },
  50. }
  51. for _, tc := range cases {
  52. // note we embed them from the beginning....
  53. pubKey := tc.privKey.PubKey()
  54. msg := CRandBytes(128)
  55. sig := tc.privKey.Sign(msg)
  56. // store as wire
  57. bin, err := cdc.MarshalBinary(sig)
  58. require.Nil(t, err, "%+v", err)
  59. if tc.sigSize != 0 {
  60. assert.Equal(t, tc.sigSize+4, len(bin))
  61. }
  62. assert.Equal(t, tc.sigPrefix[:], bin[0:4])
  63. // and back
  64. sig2 := Signature(nil)
  65. err = cdc.UnmarshalBinary(bin, &sig2)
  66. require.Nil(t, err, "%+v", err)
  67. assert.EqualValues(t, sig, sig2)
  68. assert.True(t, pubKey.VerifyBytes(msg, sig2))
  69. /*
  70. // store as json
  71. js, err := data.ToJSON(sig)
  72. require.Nil(t, err, "%+v", err)
  73. assert.True(t, strings.Contains(string(js), tc.sigName))
  74. // and back
  75. sig3 := Signature{}
  76. err = data.FromJSON(js, &sig3)
  77. require.Nil(t, err, "%+v", err)
  78. assert.EqualValues(t, sig, sig3)
  79. assert.True(t, pubKey.VerifyBytes(msg, sig3))
  80. // and make sure we can textify it
  81. text, err := data.ToText(sig)
  82. require.Nil(t, err, "%+v", err)
  83. assert.True(t, strings.HasPrefix(text, tc.sigName))
  84. */
  85. }
  86. }