diff --git a/account/priv_account.go b/account/priv_account.go index 5ef64ec9a..48e1b75c7 100644 --- a/account/priv_account.go +++ b/account/priv_account.go @@ -1,6 +1,7 @@ package account import ( + "github.com/tendermint/ed25519" . "github.com/tendermint/tendermint/common" ) @@ -12,10 +13,14 @@ type PrivAccount struct { // Generates a new account with private key. func GenPrivAccount() *PrivAccount { - privKey := PrivKeyEd25519(CRandBytes(32)) + privKeyBytes := new([64]byte) + copy(privKeyBytes[:32], CRandBytes(32)) + pubKeyBytes := ed25519.MakePublicKey(privKeyBytes) + pubKey := PubKeyEd25519(pubKeyBytes[:]) + privKey := PrivKeyEd25519(privKeyBytes[:]) return &PrivAccount{ - Address: privKey.PubKey().Address(), - PubKey: privKey.PubKey(), + Address: pubKey.Address(), + PubKey: pubKey, PrivKey: privKey, } } diff --git a/account/privkey.go b/account/privkey.go index ad233d9a9..bf088b5ea 100644 --- a/account/privkey.go +++ b/account/privkey.go @@ -1,15 +1,14 @@ package account import ( - "errors" - - "github.com/tendermint/go-ed25519" + "github.com/tendermint/ed25519" "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) // PrivKey is part of PrivAccount and state.PrivValidator. type PrivKey interface { + TypeByte() byte Sign(msg []byte) Signature PubKey() PubKey } @@ -30,22 +29,21 @@ var _ = binary.RegisterInterface( // Implements PrivKey type PrivKeyEd25519 []byte -func (key PrivKeyEd25519) TypeByte() byte { return PrivKeyTypeEd25519 } - -func (key PrivKeyEd25519) ValidateBasic() error { - if len(key) != ed25519.PrivateKeySize { - return errors.New("Invalid PrivKeyEd25519 privkey size") - } - return nil -} +func (privKey PrivKeyEd25519) TypeByte() byte { return PrivKeyTypeEd25519 } -func (key PrivKeyEd25519) Sign(msg []byte) Signature { - signature := ed25519.SignMessage(msg, key, ed25519.MakePubKey(key)) - return SignatureEd25519(signature) +func (privKey PrivKeyEd25519) Sign(msg []byte) Signature { + pubKey := privKey.PubKey().(PubKeyEd25519) + privKeyBytes := new([64]byte) + copy(privKeyBytes[:32], privKey[:]) + copy(privKeyBytes[32:], pubKey[:]) + signatureBytes := ed25519.Sign(privKeyBytes, msg) + return SignatureEd25519(signatureBytes[:]) } func (key PrivKeyEd25519) PubKey() PubKey { - return PubKeyEd25519(ed25519.MakePubKey(key)) + keyBytes := new([64]byte) + copy(keyBytes[:], key[:]) + return PubKeyEd25519(ed25519.MakePublicKey(keyBytes)[:]) } func (key PrivKeyEd25519) String() string { diff --git a/account/pubkey.go b/account/pubkey.go index 1a9f1c92a..48a58c80f 100644 --- a/account/pubkey.go +++ b/account/pubkey.go @@ -3,16 +3,17 @@ package account import ( "errors" - "github.com/tendermint/go-ed25519" + "github.com/tendermint/ed25519" "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) // PubKey is part of Account and Validator. type PubKey interface { + TypeByte() byte + IsNil() bool Address() []byte VerifyBytes(msg []byte, sig Signature) bool - TypeByte() byte } // Types of PubKey implementations @@ -35,6 +36,8 @@ type PubKeyNil struct{} func (key PubKeyNil) TypeByte() byte { return PubKeyTypeNil } +func (key PubKeyNil) IsNil() bool { return true } + func (key PubKeyNil) Address() []byte { panic("PubKeyNil has no address") } @@ -43,6 +46,13 @@ func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool { panic("PubKeyNil cannot verify messages") } +func (key PubKeyEd25519) ValidateBasic() error { + if len(key) != ed25519.PublicKeySize { + return errors.New("Invalid PubKeyEd25519 key size") + } + return nil +} + func (key PubKeyNil) String() string { return "PubKeyNil{}" } @@ -52,31 +62,25 @@ func (key PubKeyNil) String() string { // Implements PubKey type PubKeyEd25519 []byte -func (key PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 } +func (pubKey PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 } -// TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.) -func (key PubKeyEd25519) Address() []byte { return binary.BinaryRipemd160(key) } +func (pubKey PubKeyEd25519) IsNil() bool { return false } -func (key PubKeyEd25519) ValidateBasic() error { - if len(key) != ed25519.PublicKeySize { - return errors.New("Invalid PubKeyEd25519 key size") - } - return nil -} +// TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.) +func (pubKey PubKeyEd25519) Address() []byte { return binary.BinaryRipemd160(pubKey) } -func (key PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool { +func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool { sig, ok := sig_.(SignatureEd25519) if !ok { panic("PubKeyEd25519 expects an SignatureEd25519 signature") } - v1 := &ed25519.Verify{ - Message: msg, - PubKey: key, - Signature: sig, - } - return ed25519.VerifyBatch([]*ed25519.Verify{v1}) + pubKeyBytes := new([32]byte) + copy(pubKeyBytes[:], pubKey) + sigBytes := new([64]byte) + copy(sigBytes[:], sig) + return ed25519.Verify(pubKeyBytes, msg, sigBytes) } -func (key PubKeyEd25519) String() string { - return Fmt("PubKeyEd25519{%X}", []byte(key)) +func (pubKey PubKeyEd25519) String() string { + return Fmt("PubKeyEd25519{%X}", pubKey) } diff --git a/account/signature.go b/account/signature.go index 428889863..53678f723 100644 --- a/account/signature.go +++ b/account/signature.go @@ -1,47 +1,50 @@ package account import ( - "errors" "fmt" - "github.com/tendermint/go-ed25519" "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) // Signature is a part of Txs and consensus Votes. type Signature interface { + TypeByte() byte } // Types of Signature implementations const ( + SignatureTypeNil = byte(0x00) SignatureTypeEd25519 = byte(0x01) ) // for binary.readReflect var _ = binary.RegisterInterface( struct{ Signature }{}, + binary.ConcreteType{SignatureNil{}}, binary.ConcreteType{SignatureEd25519{}}, ) //------------------------------------- +// Implements Signature +type SignatureNil struct{} + +func (sig SignatureNil) TypeByte() byte { return SignatureTypeNil } + +func (sig SignatureNil) IsNil() bool { return true } + +func (sig SignatureNil) String() string { return "SignatureNil{}" } + +//------------------------------------- + // Implements Signature type SignatureEd25519 []byte func (sig SignatureEd25519) TypeByte() byte { return SignatureTypeEd25519 } -func (sig SignatureEd25519) ValidateBasic() error { - if len(sig) != ed25519.SignatureSize { - return errors.New("Invalid SignatureEd25519 signature size") - } - return nil -} +func (sig SignatureEd25519) IsNil() bool { return false } -func (sig SignatureEd25519) IsZero() bool { - return len(sig) == 0 -} +func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 } -func (sig SignatureEd25519) String() string { - return fmt.Sprintf("%X", Fingerprint(sig)) -} +func (sig SignatureEd25519) String() string { return fmt.Sprintf("%X", Fingerprint(sig)) } diff --git a/account/signature_test.go b/account/signature_test.go index 5c71d5826..8168c5c6c 100644 --- a/account/signature_test.go +++ b/account/signature_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - "github.com/tendermint/go-ed25519" + "github.com/tendermint/ed25519" "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) diff --git a/state/priv_validator.go b/state/priv_validator.go index 326f92ce1..c7afe63bd 100644 --- a/state/priv_validator.go +++ b/state/priv_validator.go @@ -16,7 +16,7 @@ import ( "github.com/tendermint/tendermint/config" . "github.com/tendermint/tendermint/consensus/types" - "github.com/tendermint/go-ed25519" + "github.com/tendermint/ed25519" ) const ( @@ -56,10 +56,11 @@ type PrivValidator struct { // Generates a new validator with private key. func GenPrivValidator() *PrivValidator { - privKeyBytes := CRandBytes(32) - pubKeyBytes := ed25519.MakePubKey(privKeyBytes) - pubKey := account.PubKeyEd25519(pubKeyBytes) - privKey := account.PrivKeyEd25519(privKeyBytes) + privKeyBytes := new([64]byte) + copy(privKeyBytes[:32], CRandBytes(32)) + pubKeyBytes := ed25519.MakePublicKey(privKeyBytes) + pubKey := account.PubKeyEd25519(pubKeyBytes[:]) + privKey := account.PrivKeyEd25519(privKeyBytes[:]) return &PrivValidator{ Address: pubKey.Address(), PubKey: pubKey,