tendermint/signature_test.go

47 lines
934 B
Go
Raw Normal View History

2015-10-25 13:45:13 -07:00
package crypto
2015-10-25 13:42:49 -07:00
import (
"testing"
2017-02-22 23:43:26 +01:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2015-10-25 13:42:49 -07:00
)
2016-04-19 01:02:31 -07:00
func TestSignAndValidateEd25519(t *testing.T) {
2015-10-25 13:42:49 -07:00
privKey := GenPrivKeyEd25519()
pubKey := privKey.PubKey()
2015-10-25 13:42:49 -07:00
msg := CRandBytes(128)
sig, err := privKey.Sign(msg)
require.Nil(t, err)
2015-10-25 13:42:49 -07:00
// Test the signature
2017-02-23 13:20:30 +01:00
assert.True(t, pubKey.VerifyBytes(msg, sig))
2015-10-25 13:42:49 -07:00
// Mutate the signature, just one bit.
sigEd := sig.(SignatureEd25519)
sigEd[7] ^= byte(0x01)
sig = sigEd
2015-10-25 13:42:49 -07:00
2017-02-23 13:20:30 +01:00
assert.False(t, pubKey.VerifyBytes(msg, sig))
2015-10-25 13:42:49 -07:00
}
2016-04-19 01:02:31 -07:00
func TestSignAndValidateSecp256k1(t *testing.T) {
privKey := GenPrivKeySecp256k1()
pubKey := privKey.PubKey()
2016-04-19 01:02:31 -07:00
msg := CRandBytes(128)
sig, err := privKey.Sign(msg)
require.Nil(t, err)
2016-04-19 01:02:31 -07:00
2017-02-23 13:20:30 +01:00
assert.True(t, pubKey.VerifyBytes(msg, sig))
2016-04-19 01:02:31 -07:00
// Mutate the signature, just one bit.
sigEd := sig.(SignatureSecp256k1)
sigEd[3] ^= byte(0x01)
sig = sigEd
2016-04-19 01:02:31 -07:00
2017-02-23 13:20:30 +01:00
assert.False(t, pubKey.VerifyBytes(msg, sig))
2016-04-19 01:02:31 -07:00
}