use btcec structs instead of ecdsa structs

This commit is contained in:
Jimmy Song 2014-09-29 16:31:26 -05:00 committed by Dave Collins
parent ce0a334329
commit d5cc72dc27
3 changed files with 39 additions and 80 deletions

View file

@ -6,7 +6,6 @@ package btcscript
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"crypto/sha1" "crypto/sha1"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
@ -1812,7 +1811,7 @@ func opcodeCheckSig(op *parsedOpcode, s *Script) error {
hex.Dump(pkStr), pubKey.X, pubKey.Y, hex.Dump(pkStr), pubKey.X, pubKey.Y,
signature.R, signature.S, hex.Dump(hash)) signature.R, signature.S, hex.Dump(hash))
})) }))
ok := ecdsa.Verify(pubKey.ToECDSA(), hash, signature.R, signature.S) ok := signature.Verify(hash, pubKey)
s.dstack.PushBool(ok) s.dstack.PushBool(ok)
return nil return nil
} }
@ -1947,8 +1946,7 @@ func opcodeCheckMultiSig(op *parsedOpcode, s *Script) error {
continue continue
} }
} }
success = ecdsa.Verify(pubKeys[curPk].ToECDSA(), hash, success = signatures[i].s.Verify(hash, pubKeys[curPk])
signatures[i].s.R, signatures[i].s.S)
if success { if success {
break inner break inner
} }

View file

@ -6,7 +6,6 @@ package btcscript
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"crypto/rand" "crypto/rand"
"encoding/binary" "encoding/binary"
"errors" "errors"
@ -1100,7 +1099,7 @@ func MultiSigScript(pubkeys []*btcutil.AddressPubKey, nrequired int) ([]byte, er
// serialized in either a compressed or uncompressed format based on // serialized in either a compressed or uncompressed format based on
// compress. This format must match the same format used to generate // compress. This format must match the same format used to generate
// the payment address, or the script validation will fail. // the payment address, or the script validation will fail.
func SignatureScript(tx *btcwire.MsgTx, idx int, subscript []byte, hashType SigHashType, privKey *ecdsa.PrivateKey, compress bool) ([]byte, error) { func SignatureScript(tx *btcwire.MsgTx, idx int, subscript []byte, hashType SigHashType, privKey *btcec.PrivateKey, compress bool) ([]byte, error) {
sig, err := signTxOutput(tx, idx, subscript, hashType, privKey) sig, err := signTxOutput(tx, idx, subscript, hashType, privKey)
if err != nil { if err != nil {
return nil, err return nil, err
@ -1118,29 +1117,28 @@ func SignatureScript(tx *btcwire.MsgTx, idx int, subscript []byte, hashType SigH
} }
func signTxOutput(tx *btcwire.MsgTx, idx int, subScript []byte, hashType SigHashType, func signTxOutput(tx *btcwire.MsgTx, idx int, subScript []byte, hashType SigHashType,
key *ecdsa.PrivateKey) ([]byte, error) { key *btcec.PrivateKey) ([]byte, error) {
return signTxOutputCustomReader(rand.Reader, tx, idx, subScript, return signTxOutputCustomReader(rand.Reader, tx, idx, subScript,
hashType, key) hashType, key)
} }
func signTxOutputCustomReader(reader io.Reader, tx *btcwire.MsgTx, idx int, func signTxOutputCustomReader(reader io.Reader, tx *btcwire.MsgTx, idx int,
subScript []byte, hashType SigHashType, key *ecdsa.PrivateKey) ([]byte, error) { subScript []byte, hashType SigHashType, key *btcec.PrivateKey) ([]byte, error) {
parsedScript, err := parseScript(subScript) parsedScript, err := parseScript(subScript)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse output script: %v", err) return nil, fmt.Errorf("cannot parse output script: %v", err)
} }
hash := calcScriptHash(parsedScript, hashType, tx, idx) hash := calcScriptHash(parsedScript, hashType, tx, idx)
r, s, err := ecdsa.Sign(reader, key, hash) signature, err := key.Sign(hash)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot sign tx input: %s", err) return nil, fmt.Errorf("cannot sign tx input: %s", err)
} }
return append((&btcec.Signature{R: r, S: s}).Serialize(), return append(signature.Serialize(), byte(hashType)), nil
byte(hashType)), nil
} }
func p2pkSignatureScript(tx *btcwire.MsgTx, idx int, subScript []byte, hashType SigHashType, privKey *ecdsa.PrivateKey) ([]byte, error) { func p2pkSignatureScript(tx *btcwire.MsgTx, idx int, subScript []byte, hashType SigHashType, privKey *btcec.PrivateKey) ([]byte, error) {
sig, err := signTxOutput(tx, idx, subScript, hashType, privKey) sig, err := signTxOutput(tx, idx, subScript, hashType, privKey)
if err != nil { if err != nil {
return nil, err return nil, err
@ -1384,9 +1382,7 @@ sigLoop:
// If it matches we put it in the map. We only // If it matches we put it in the map. We only
// can take one signature per public key so if we // can take one signature per public key so if we
// already have one, we can throw this away. // already have one, we can throw this away.
if ecdsa.Verify(pubKey.ToECDSA(), hash, if pSig.Verify(hash, pubKey) {
pSig.R, pSig.S) {
aStr := addr.EncodeAddress() aStr := addr.EncodeAddress()
if _, ok := addrToSig[aStr]; !ok { if _, ok := addrToSig[aStr]; !ok {
addrToSig[aStr] = sig addrToSig[aStr] = sig
@ -1424,14 +1420,14 @@ sigLoop:
// KeyDB is an interface type provided to SignTxOutput, it encapsulates // KeyDB is an interface type provided to SignTxOutput, it encapsulates
// any user state required to get the private keys for an address. // any user state required to get the private keys for an address.
type KeyDB interface { type KeyDB interface {
GetKey(btcutil.Address) (*ecdsa.PrivateKey, bool, error) GetKey(btcutil.Address) (*btcec.PrivateKey, bool, error)
} }
// KeyClosure implements KeyDB with a closure // KeyClosure implements ScriptDB with a closure
type KeyClosure func(btcutil.Address) (*ecdsa.PrivateKey, bool, error) type KeyClosure func(btcutil.Address) (*btcec.PrivateKey, bool, error)
// GetKey implements KeyDB by returning the result of calling the closure // GetKey implements KeyDB by returning the result of calling the closure
func (kc KeyClosure) GetKey(address btcutil.Address) (*ecdsa.PrivateKey, func (kc KeyClosure) GetKey(address btcutil.Address) (*btcec.PrivateKey,
bool, error) { bool, error) {
return kc(address) return kc(address)
} }

View file

@ -6,11 +6,8 @@ package btcscript_test
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"crypto/rand"
"errors" "errors"
"fmt" "fmt"
"math/big"
"testing" "testing"
"github.com/conformal/btcec" "github.com/conformal/btcec"
@ -2766,17 +2763,7 @@ var SigScriptTests = []TstSigScript{
// created for the MsgTxs in txTests, since they come from the blockchain // created for the MsgTxs in txTests, since they come from the blockchain
// and we don't have the private keys. // and we don't have the private keys.
func TestSignatureScript(t *testing.T) { func TestSignatureScript(t *testing.T) {
privKey := &ecdsa.PrivateKey{ privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyD)
PublicKey: ecdsa.PublicKey{
Curve: btcec.S256(),
X: new(big.Int),
Y: new(big.Int),
},
D: new(big.Int),
}
privKey.D.SetBytes(privKeyD)
privKey.PublicKey.X.SetBytes(pubkeyX)
privKey.PublicKey.Y.SetBytes(pubkeyY)
nexttest: nexttest:
for i := range SigScriptTests { for i := range SigScriptTests {
@ -3272,18 +3259,18 @@ func checkScripts(msg string, tx *btcwire.MsgTx, idx int,
} }
type addressToKey struct { type addressToKey struct {
key *ecdsa.PrivateKey key *btcec.PrivateKey
compressed bool compressed bool
} }
func mkGetKey(keys map[string]addressToKey) btcscript.KeyDB { func mkGetKey(keys map[string]addressToKey) btcscript.KeyDB {
if keys == nil { if keys == nil {
return btcscript.KeyClosure(func(addr btcutil.Address) (*ecdsa.PrivateKey, return btcscript.KeyClosure(func(addr btcutil.Address) (*btcec.PrivateKey,
bool, error) { bool, error) {
return nil, false, errors.New("nope") return nil, false, errors.New("nope")
}) })
} }
return btcscript.KeyClosure(func(addr btcutil.Address) (*ecdsa.PrivateKey, return btcscript.KeyClosure(func(addr btcutil.Address) (*btcec.PrivateKey,
bool, error) { bool, error) {
a2k, ok := keys[addr.EncodeAddress()] a2k, ok := keys[addr.EncodeAddress()]
if !ok { if !ok {
@ -3366,8 +3353,7 @@ func TestSignTxOutput(t *testing.T) {
for _, hashType := range hashTypes { for _, hashType := range hashTypes {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3404,8 +3390,7 @@ func TestSignTxOutput(t *testing.T) {
for _, hashType := range hashTypes { for _, hashType := range hashTypes {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3466,8 +3451,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3505,8 +3489,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3567,8 +3550,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3606,8 +3588,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3668,8 +3649,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3707,8 +3687,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3769,8 +3748,7 @@ func TestSignTxOutput(t *testing.T) {
for _, hashType := range hashTypes { for _, hashType := range hashTypes {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3827,8 +3805,7 @@ func TestSignTxOutput(t *testing.T) {
for _, hashType := range hashTypes { for _, hashType := range hashTypes {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3910,8 +3887,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -3968,8 +3944,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -4050,8 +4025,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -4108,8 +4082,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -4190,8 +4163,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -4248,8 +4220,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key, err := ecdsa.GenerateKey(btcec.S256(), key, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -4330,8 +4301,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key1, err := ecdsa.GenerateKey(btcec.S256(), key1, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -4348,8 +4318,7 @@ func TestSignTxOutput(t *testing.T) {
break break
} }
key2, err := ecdsa.GenerateKey(btcec.S256(), key2, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey 2 for %s: %v", t.Errorf("failed to make privKey 2 for %s: %v",
msg, err) msg, err)
@ -4409,8 +4378,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key1, err := ecdsa.GenerateKey(btcec.S256(), key1, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -4427,8 +4395,7 @@ func TestSignTxOutput(t *testing.T) {
break break
} }
key2, err := ecdsa.GenerateKey(btcec.S256(), key2, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey 2 for %s: %v", t.Errorf("failed to make privKey 2 for %s: %v",
msg, err) msg, err)
@ -4518,8 +4485,7 @@ func TestSignTxOutput(t *testing.T) {
for i := range tx.TxIn { for i := range tx.TxIn {
msg := fmt.Sprintf("%d:%d", hashType, i) msg := fmt.Sprintf("%d:%d", hashType, i)
key1, err := ecdsa.GenerateKey(btcec.S256(), key1, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey for %s: %v", t.Errorf("failed to make privKey for %s: %v",
msg, err) msg, err)
@ -4536,8 +4502,7 @@ func TestSignTxOutput(t *testing.T) {
break break
} }
key2, err := ecdsa.GenerateKey(btcec.S256(), key2, err := btcec.NewPrivateKey(btcec.S256())
rand.Reader)
if err != nil { if err != nil {
t.Errorf("failed to make privKey 2 for %s: %v", t.Errorf("failed to make privKey 2 for %s: %v",
msg, err) msg, err)