Browse Source

removed cgo dependency, go-ed25519 -> agl/ed25519

pull/39/head
Jae Kwon 10 years ago
parent
commit
892a51014e
6 changed files with 69 additions and 58 deletions
  1. +8
    -3
      account/priv_account.go
  2. +13
    -15
      account/privkey.go
  3. +24
    -20
      account/pubkey.go
  4. +17
    -14
      account/signature.go
  5. +1
    -1
      account/signature_test.go
  6. +6
    -5
      state/priv_validator.go

+ 8
- 3
account/priv_account.go View File

@ -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,
}
}


+ 13
- 15
account/privkey.go View File

@ -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 {


+ 24
- 20
account/pubkey.go View File

@ -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)
}

+ 17
- 14
account/signature.go View File

@ -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)) }

+ 1
- 1
account/signature_test.go View File

@ -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"
)


+ 6
- 5
state/priv_validator.go View File

@ -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,


Loading…
Cancel
Save