Skip to content

Commit

Permalink
crypto/ed25519/chainkd: remove ed25519 convenience constructor
Browse files Browse the repository at this point in the history
Also fixes an erroneous error in `CHECKSIG`. (Wrong-length pubkey should
return false, not error out.)

Closes #40
  • Loading branch information
bobg authored and iampogo committed Oct 25, 2016
1 parent 727c52b commit 8e52c04
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
6 changes: 2 additions & 4 deletions cmd/cored/main.go
Expand Up @@ -33,7 +33,6 @@ import (
"chain/core/txbuilder"
"chain/core/txdb"
"chain/crypto/ed25519"
"chain/crypto/ed25519/chainkd"
"chain/database/pg"
"chain/database/sql"
"chain/env"
Expand Down Expand Up @@ -313,8 +312,7 @@ func remoteSignerInfo(ctx context.Context, processID, buildTag, blockchainID str
if err != nil {
chainlog.Fatal(ctx, chainlog.KeyError, err)
}
k, err := chainkd.NewEd25519PublicKey(signer.Pubkey)
if err != nil {
if len(signer.Pubkey) != ed25519.PublicKeySize {
chainlog.Fatal(ctx, chainlog.KeyError, errors.Wrap(err), "at", "decoding signer public key")
}
client := &rpc.Client{
Expand All @@ -325,7 +323,7 @@ func remoteSignerInfo(ctx context.Context, processID, buildTag, blockchainID str
BuildTag: buildTag,
BlockchainID: blockchainID,
}
a = append(a, &remoteSigner{Client: client, Key: k})
a = append(a, &remoteSigner{Client: client, Key: ed25519.PublicKey(signer.Pubkey)})
}
return a
}
Expand Down
6 changes: 2 additions & 4 deletions core/core.go
Expand Up @@ -17,7 +17,6 @@ import (
"chain/core/rpc"
"chain/core/txdb"
"chain/crypto/ed25519"
"chain/crypto/ed25519/chainkd"
"chain/database/pg"
"chain/database/sql"
"chain/errors"
Expand Down Expand Up @@ -206,11 +205,10 @@ func Configure(ctx context.Context, db pg.DB, c *Config) error {
if err != nil {
return errors.Wrap(errBadSignerURL, err.Error())
}
signingKey, err := chainkd.NewEd25519PublicKey(signer.Pubkey)
if err != nil {
if len(signer.Pubkey) != ed25519.PublicKeySize {
return errors.Wrap(errBadSignerPubkey, err.Error())
}
signingKeys = append(signingKeys, signingKey)
signingKeys = append(signingKeys, ed25519.PublicKey(signer.Pubkey))
}

if c.Quorum == 0 && len(signingKeys) > 0 {
Expand Down
21 changes: 4 additions & 17 deletions crypto/ed25519/chainkd/serialize.go
Expand Up @@ -3,15 +3,11 @@ package chainkd
import (
"encoding/hex"
"errors"

"chain/crypto/ed25519"
)

const (
PublicKeySize = ed25519.PublicKeySize
PrivateKeySize = ed25519.PrivateKeySize
ExtendedPublicKeySize = 64
ExtendedPrivateKeySize = 64
extendedPublicKeySize = 64
extendedPrivateKeySize = 64
)

var (
Expand Down Expand Up @@ -40,7 +36,7 @@ func (xprv XPrv) Bytes() []byte {
}

func (xpub *XPub) UnmarshalText(inp []byte) error {
if len(inp) != 2*ExtendedPublicKeySize {
if len(inp) != 2*extendedPublicKeySize {
return ErrBadKeyStr
}
_, err := hex.Decode(xpub[:], inp)
Expand All @@ -52,7 +48,7 @@ func (xpub XPub) String() string {
}

func (xprv *XPrv) UnmarshalText(inp []byte) error {
if len(inp) != 2*ExtendedPrivateKeySize {
if len(inp) != 2*extendedPrivateKeySize {
return ErrBadKeyStr
}
_, err := hex.Decode(xprv[:], inp)
Expand All @@ -62,12 +58,3 @@ func (xprv *XPrv) UnmarshalText(inp []byte) error {
func (xprv XPrv) String() string {
return hex.EncodeToString(xprv.Bytes())
}

// NewEd25519PublicKey checks the input has the right length and produces a
// PublicKey from it.
func NewEd25519PublicKey(b []byte) (ed25519.PublicKey, error) {
if len(b) != PublicKeySize {
return nil, ErrBadKeyLen
}
return ed25519.PublicKey(b), nil
}
15 changes: 6 additions & 9 deletions protocol/vm/crypto.go
Expand Up @@ -9,7 +9,6 @@ import (
"golang.org/x/crypto/sha3"

"chain/crypto/ed25519"
"chain/crypto/ed25519/chainkd"
"chain/math/checked"
)

Expand Down Expand Up @@ -59,6 +58,9 @@ func opCheckSig(vm *virtualMachine) error {
if err != nil {
return err
}
if len(pubkeyBytes) != ed25519.PublicKeySize {
return vm.pushBool(false, true)
}
msg, err := vm.pop(true)
if err != nil {
return err
Expand All @@ -70,11 +72,7 @@ func opCheckSig(vm *virtualMachine) error {
if err != nil {
return err
}
pubkey, err := chainkd.NewEd25519PublicKey(pubkeyBytes)
if err != nil {
return vm.pushBool(false, true)
}
return vm.pushBool(ed25519.Verify(pubkey, msg, sig), true)
return vm.pushBool(ed25519.Verify(ed25519.PublicKey(pubkeyBytes), msg, sig), true)
}

func opCheckMultiSig(vm *virtualMachine) error {
Expand Down Expand Up @@ -123,11 +121,10 @@ func opCheckMultiSig(vm *virtualMachine) error {

pubkeys := make([]ed25519.PublicKey, 0, numPubkeys)
for _, p := range pubkeyByteses {
pubkey, err := chainkd.NewEd25519PublicKey(p)
if err != nil {
if len(p) != ed25519.PublicKeySize {
return vm.pushBool(false, true)
}
pubkeys = append(pubkeys, pubkey)
pubkeys = append(pubkeys, ed25519.PublicKey(p))
}

for len(sigs) > 0 && len(pubkeys) > 0 {
Expand Down
31 changes: 27 additions & 4 deletions protocol/vm/crypto_test.go
Expand Up @@ -14,16 +14,39 @@ func TestCheckSig(t *testing.T) {
ok, err bool
}{
{
"0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x040506 CHECKSIG",
// This one's OK
"0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
true, false,
},
{
// This one has a wrong-length signature
"0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc2 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
false, false,
},
{
"0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0x040506 CHECKSIG",
// This one has a wrong-length message
"0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
false, true,
},
{
"0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
true, false,
// This one has a wrong-length pubkey
"0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584 CHECKSIG",
false, false,
},
{
// This one has a wrong byte in the signature
"0x00ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
false, false,
},
{
// This one has a wrong byte in the message
"0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0002030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
false, false,
},
{
// This one has a wrong byte in the pubkey
"0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x00ca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
false, false,
},
{
"0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x040506 1 1 CHECKMULTISIG",
Expand Down
11 changes: 4 additions & 7 deletions protocol/vmutil/script.go
Expand Up @@ -2,7 +2,6 @@ package vmutil

import (
"chain/crypto/ed25519"
"chain/crypto/ed25519/chainkd"
"chain/errors"
"chain/protocol/vm"
)
Expand Down Expand Up @@ -66,11 +65,10 @@ func ParseBlockMultiSigProgram(script []byte) ([]ed25519.PublicKey, int, error)

pubkeys := make([]ed25519.PublicKey, 0, npubkeys)
for i := firstPubkeyIndex; i < firstPubkeyIndex+int(npubkeys); i++ {
pubkey, err := chainkd.NewEd25519PublicKey(pops[i].Data)
if err != nil {
if len(pops[i].Data) != ed25519.PublicKeySize {
return nil, 0, err
}
pubkeys = append(pubkeys, pubkey)
pubkeys = append(pubkeys, ed25519.PublicKey(pops[i].Data))
}
return pubkeys, int(nrequired), nil
}
Expand Down Expand Up @@ -129,11 +127,10 @@ func ParseP2SPMultiSigProgram(program []byte) ([]ed25519.PublicKey, int, error)

pubkeys := make([]ed25519.PublicKey, 0, npubkeys)
for i := firstPubkeyIndex; i < firstPubkeyIndex+int(npubkeys); i++ {
pubkey, err := chainkd.NewEd25519PublicKey(pops[i].Data)
if err != nil {
if len(pops[i].Data) != ed25519.PublicKeySize {
return nil, 0, err
}
pubkeys = append(pubkeys, pubkey)
pubkeys = append(pubkeys, ed25519.PublicKey(pops[i].Data))
}
return pubkeys, int(nrequired), nil
}
Expand Down

0 comments on commit 8e52c04

Please sign in to comment.