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.

108 lines
2.5 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
7 years ago
7 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 := 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 amino.PrefixBytes
  39. }{
  40. {
  41. privKey: GenPrivKeyEd25519(),
  42. sigSize: ed25519.SignatureSize,
  43. sigPrefix: [4]byte{0xc8, 0x5d, 0xf4, 0xba},
  44. },
  45. {
  46. privKey: GenPrivKeySecp256k1(),
  47. sigSize: 0, // unknown
  48. sigPrefix: [4]byte{0xc6, 0xa0, 0xa, 0x42},
  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 amino
  57. bin, err := cdc.MarshalBinaryBare(sig)
  58. require.Nil(t, err, "%+v", err)
  59. if tc.sigSize != 0 {
  60. // Q: where is 1 byte coming from?
  61. assert.Equal(t, tc.sigSize+amino.PrefixBytesLen+1, len(bin))
  62. }
  63. assert.Equal(t, tc.sigPrefix[:], bin[0:amino.PrefixBytesLen])
  64. // and back
  65. sig2 := Signature(nil)
  66. err = cdc.UnmarshalBinaryBare(bin, &sig2)
  67. require.Nil(t, err, "%+v", err)
  68. assert.EqualValues(t, sig, sig2)
  69. assert.True(t, pubKey.VerifyBytes(msg, sig2))
  70. /*
  71. // store as json
  72. js, err := data.ToJSON(sig)
  73. require.Nil(t, err, "%+v", err)
  74. assert.True(t, strings.Contains(string(js), tc.sigName))
  75. // and back
  76. sig3 := Signature{}
  77. err = data.FromJSON(js, &sig3)
  78. require.Nil(t, err, "%+v", err)
  79. assert.EqualValues(t, sig, sig3)
  80. assert.True(t, pubKey.VerifyBytes(msg, sig3))
  81. // and make sure we can textify it
  82. text, err := data.ToText(sig)
  83. require.Nil(t, err, "%+v", err)
  84. assert.True(t, strings.HasPrefix(text, tc.sigName))
  85. */
  86. }
  87. }