mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-09 19:41:21 +00:00
First tests of go-data encoding
This commit is contained in:
parent
e6d35ee641
commit
1bc1947e3f
17
pub_key.go
17
pub_key.go
@ -83,6 +83,11 @@ func (pubKey PubKeyEd25519) Bytes() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
|
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
|
||||||
|
// unwrap if needed
|
||||||
|
if wrap, ok := sig_.(SignatureS); ok {
|
||||||
|
sig_ = wrap.Signature
|
||||||
|
}
|
||||||
|
// make sure we use the same algorithm to sign
|
||||||
sig, ok := sig_.(SignatureEd25519)
|
sig, ok := sig_.(SignatureEd25519)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
@ -155,14 +160,20 @@ func (pubKey PubKeySecp256k1) Bytes() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
|
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
|
||||||
pub__, err := secp256k1.ParsePubKey(append([]byte{0x04}, pubKey[:]...), secp256k1.S256())
|
// unwrap if needed
|
||||||
if err != nil {
|
if wrap, ok := sig_.(SignatureS); ok {
|
||||||
return false
|
sig_ = wrap.Signature
|
||||||
}
|
}
|
||||||
|
// and assert same algorithm to sign and verify
|
||||||
sig, ok := sig_.(SignatureSecp256k1)
|
sig, ok := sig_.(SignatureSecp256k1)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub__, err := secp256k1.ParsePubKey(append([]byte{0x04}, pubKey[:]...), secp256k1.S256())
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
|
sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
|
@ -91,7 +91,7 @@ func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error {
|
|||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
|
|
||||||
// Implements Signature
|
// Implements Signature
|
||||||
type SignatureSecp256k1 data.Bytes
|
type SignatureSecp256k1 []byte
|
||||||
|
|
||||||
func (sig SignatureSecp256k1) Bytes() []byte {
|
func (sig SignatureSecp256k1) Bytes() []byte {
|
||||||
return wire.BinaryBytes(struct{ Signature }{sig})
|
return wire.BinaryBytes(struct{ Signature }{sig})
|
||||||
@ -108,3 +108,10 @@ func (sig SignatureSecp256k1) Equals(other Signature) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (p SignatureSecp256k1) MarshalJSON() ([]byte, error) {
|
||||||
|
return data.Encoder.Marshal(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
|
||||||
|
return data.Encoder.Unmarshal((*[]byte)(p), enc)
|
||||||
|
}
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/tendermint/ed25519"
|
"github.com/tendermint/ed25519"
|
||||||
"github.com/tendermint/go-wire"
|
data "github.com/tendermint/go-data"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSignAndValidateEd25519(t *testing.T) {
|
func TestSignAndValidateEd25519(t *testing.T) {
|
||||||
@ -56,68 +59,60 @@ func TestSignAndValidateSecp256k1(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBinaryDecodeEd25519(t *testing.T) {
|
func TestSignatureEncodings(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
privKey := GenPrivKeyEd25519()
|
privKey PrivKeyS
|
||||||
pubKey := privKey.PubKey()
|
sigSize int
|
||||||
|
sigType byte
|
||||||
msg := CRandBytes(128)
|
sigName string
|
||||||
sig := privKey.Sign(msg)
|
}{
|
||||||
t.Logf("msg: %X, sig: %X", msg, sig)
|
{
|
||||||
|
privKey: PrivKeyS{GenPrivKeyEd25519()},
|
||||||
buf, n, err := new(bytes.Buffer), new(int), new(error)
|
sigSize: ed25519.SignatureSize,
|
||||||
wire.WriteBinary(struct{ Signature }{sig}, buf, n, err)
|
sigType: SignatureTypeEd25519,
|
||||||
if *err != nil {
|
sigName: SignatureNameEd25519,
|
||||||
t.Fatalf("Failed to write Signature: %v", err)
|
},
|
||||||
|
{
|
||||||
|
privKey: PrivKeyS{GenPrivKeySecp256k1()},
|
||||||
|
sigSize: 0, // unknown
|
||||||
|
sigType: SignatureTypeSecp256k1,
|
||||||
|
sigName: SignatureNameSecp256k1,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(buf.Bytes()) != ed25519.SignatureSize+1 {
|
for _, tc := range cases {
|
||||||
// 1 byte TypeByte, 64 bytes signature bytes
|
// note we embed them from the beginning....
|
||||||
t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
|
pubKey := PubKeyS{tc.privKey.PubKey()}
|
||||||
}
|
|
||||||
if buf.Bytes()[0] != SignatureTypeEd25519 {
|
|
||||||
t.Fatalf("Unexpected signature type byte")
|
|
||||||
}
|
|
||||||
|
|
||||||
sigStruct := struct{ Signature }{}
|
msg := CRandBytes(128)
|
||||||
sig2 := wire.ReadBinary(sigStruct, buf, 0, n, err)
|
sig := SignatureS{tc.privKey.Sign(msg)}
|
||||||
if *err != nil {
|
|
||||||
t.Fatalf("Failed to read Signature: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test the signature
|
// store as wire
|
||||||
if !pubKey.VerifyBytes(msg, sig2.(struct{ Signature }).Signature.(SignatureEd25519)) {
|
bin, err := data.ToWire(sig)
|
||||||
t.Errorf("Account message signature verification failed")
|
require.Nil(t, err, "%+v", err)
|
||||||
}
|
if tc.sigSize != 0 {
|
||||||
}
|
assert.Equal(t, tc.sigSize+1, len(bin))
|
||||||
|
}
|
||||||
func TestBinaryDecodeSecp256k1(t *testing.T) {
|
assert.Equal(t, tc.sigType, bin[0])
|
||||||
|
|
||||||
privKey := GenPrivKeySecp256k1()
|
// and back
|
||||||
pubKey := privKey.PubKey()
|
sig2 := SignatureS{}
|
||||||
|
err = data.FromWire(bin, &sig2)
|
||||||
msg := CRandBytes(128)
|
require.Nil(t, err, "%+v", err)
|
||||||
sig := privKey.Sign(msg)
|
assert.EqualValues(t, sig, sig2)
|
||||||
t.Logf("msg: %X, sig: %X", msg, sig)
|
assert.True(t, pubKey.VerifyBytes(msg, sig2))
|
||||||
|
|
||||||
buf, n, err := new(bytes.Buffer), new(int), new(error)
|
// store as json
|
||||||
wire.WriteBinary(struct{ Signature }{sig}, buf, n, err)
|
js, err := data.ToJSON(sig)
|
||||||
if *err != nil {
|
require.Nil(t, err, "%+v", err)
|
||||||
t.Fatalf("Failed to write Signature: %v", err)
|
assert.True(t, strings.Contains(string(js), tc.sigName))
|
||||||
}
|
fmt.Println(string(js))
|
||||||
|
|
||||||
if buf.Bytes()[0] != SignatureTypeSecp256k1 {
|
// and back
|
||||||
t.Fatalf("Unexpected signature type byte")
|
sig3 := SignatureS{}
|
||||||
}
|
err = data.FromJSON(js, &sig3)
|
||||||
|
require.Nil(t, err, "%+v", err)
|
||||||
sigStruct := struct{ Signature }{}
|
assert.EqualValues(t, sig, sig3)
|
||||||
sig2 := wire.ReadBinary(sigStruct, buf, 0, n, err)
|
assert.True(t, pubKey.VerifyBytes(msg, sig3))
|
||||||
if *err != nil {
|
|
||||||
t.Fatalf("Failed to read Signature: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test the signature
|
|
||||||
if !pubKey.VerifyBytes(msg, sig2.(struct{ Signature }).Signature.(SignatureSecp256k1)) {
|
|
||||||
t.Errorf("Account message signature verification failed")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user