tendermint/signature_test.go

109 lines
2.5 KiB
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
"github.com/tendermint/ed25519"
2018-03-12 12:18:30 +04:00
amino "github.com/tendermint/go-amino"
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()
msg := CRandBytes(128)
sig := privKey.Sign(msg)
// 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()
msg := CRandBytes(128)
sig := privKey.Sign(msg)
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
}
2017-02-22 23:43:26 +01:00
func TestSignatureEncodings(t *testing.T) {
cases := []struct {
privKey PrivKey
sigSize int
2018-03-12 12:18:30 +04:00
sigPrefix amino.PrefixBytes
2017-02-22 23:43:26 +01:00
}{
{
privKey: GenPrivKeyEd25519(),
sigSize: ed25519.SignatureSize,
sigPrefix: [4]byte{0x3d, 0xa1, 0xdb, 0x2a},
2017-02-22 23:43:26 +01:00
},
{
privKey: GenPrivKeySecp256k1(),
sigSize: 0, // unknown
sigPrefix: [4]byte{0x16, 0xe1, 0xfe, 0xea},
2017-02-22 23:43:26 +01:00
},
2015-10-25 13:42:49 -07:00
}
2016-04-19 01:02:31 -07:00
2017-02-22 23:43:26 +01:00
for _, tc := range cases {
// note we embed them from the beginning....
pubKey := tc.privKey.PubKey()
2017-02-22 23:43:26 +01:00
msg := CRandBytes(128)
sig := tc.privKey.Sign(msg)
2017-02-22 23:43:26 +01:00
2018-03-12 12:18:30 +04:00
// store as amino
bin, err := cdc.MarshalBinaryBare(sig)
2017-02-22 23:43:26 +01:00
require.Nil(t, err, "%+v", err)
if tc.sigSize != 0 {
2018-03-12 12:18:30 +04:00
// Q: where is 1 byte coming from?
assert.Equal(t, tc.sigSize+amino.PrefixBytesLen+1, len(bin))
2017-02-22 23:43:26 +01:00
}
2018-03-12 12:18:30 +04:00
assert.Equal(t, tc.sigPrefix[:], bin[0:amino.PrefixBytesLen])
2017-02-22 23:43:26 +01:00
// and back
sig2 := Signature(nil)
2018-03-12 12:18:30 +04:00
err = cdc.UnmarshalBinaryBare(bin, &sig2)
2017-02-22 23:43:26 +01:00
require.Nil(t, err, "%+v", err)
assert.EqualValues(t, sig, sig2)
assert.True(t, pubKey.VerifyBytes(msg, sig2))
/*
// store as json
js, err := data.ToJSON(sig)
require.Nil(t, err, "%+v", err)
assert.True(t, strings.Contains(string(js), tc.sigName))
// and back
sig3 := Signature{}
err = data.FromJSON(js, &sig3)
require.Nil(t, err, "%+v", err)
assert.EqualValues(t, sig, sig3)
assert.True(t, pubKey.VerifyBytes(msg, sig3))
// and make sure we can textify it
text, err := data.ToText(sig)
require.Nil(t, err, "%+v", err)
assert.True(t, strings.HasPrefix(text, tc.sigName))
*/
2017-12-21 19:51:57 -05:00
}
}