Add an api to get a pubkey and privkey from a privkey byte string.

ok and some tweaks from @jrick.
This commit is contained in:
Owain G. Ainsworth 2014-03-25 14:46:20 +00:00
parent de670bd5b2
commit d0a4086e29
4 changed files with 55 additions and 30 deletions

28
privkey.go Normal file
View file

@ -0,0 +1,28 @@
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcec
import (
"crypto/ecdsa"
"math/big"
)
// PrivKeyFromBytes returns a private and public key for `curve' based on the
// private key passed as an argument as a byte slice.
func PrivKeyFromBytes(curve *KoblitzCurve, pk []byte) (*ecdsa.PrivateKey,
*PublicKey) {
x, y := curve.ScalarBaseMult(pk)
priv := &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: curve,
X: x,
Y: y,
},
D: new(big.Int).SetBytes(pk),
}
return priv, (*PublicKey)(&priv.PublicKey)
}

View file

@ -6,7 +6,6 @@ package btcec_test
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"github.com/conformal/btcec" "github.com/conformal/btcec"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"testing" "testing"
@ -220,13 +219,10 @@ var pubKeyTests = []pubKeyTest{
func TestPrivKeys(t *testing.T) { func TestPrivKeys(t *testing.T) {
for _, test := range privKeyTests { for _, test := range privKeyTests {
x, y := btcec.S256().ScalarBaseMult(test.key) _, pub := btcec.PrivKeyFromBytes(btcec.S256(), test.key)
pub := (*btcec.PublicKey)(&ecdsa.PublicKey{
Curve: btcec.S256(), _, err := btcec.ParsePubKey(
X: x, pub.SerializeUncompressed(), btcec.S256())
Y: y,
})
_, err := btcec.ParsePubKey(pub.SerializeUncompressed(), btcec.S256())
if err != nil { if err != nil {
t.Errorf("%s privkey: %v", test.name, err) t.Errorf("%s privkey: %v", test.name, err)
continue continue

View file

@ -437,7 +437,7 @@ func testSignCompact(t *testing.T, tag string, curve *btcec.KoblitzCurve,
pk, wasCompressed, err := btcec.RecoverCompact(curve, sig, hashed) pk, wasCompressed, err := btcec.RecoverCompact(curve, sig, hashed)
if err != nil { if err != nil {
t.Errorf("%s: error recovering: %s", tag, err) t.Errorf("%s: error recovering: %s", tag, err)
return return
} }
if pk.X.Cmp(priv.X) != 0 || pk.Y.Cmp(priv.Y) != 0 { if pk.X.Cmp(priv.X) != 0 || pk.Y.Cmp(priv.Y) != 0 {

View file

@ -10,55 +10,56 @@ github.com/conformal/btcec/field.go fieldVal.PutBytes 100.00% (32/32)
github.com/conformal/btcec/btcec.go KoblitzCurve.addZ1EqualsZ2 100.00% (30/30) github.com/conformal/btcec/btcec.go KoblitzCurve.addZ1EqualsZ2 100.00% (30/30)
github.com/conformal/btcec/btcec.go KoblitzCurve.addZ1AndZ2EqualsOne 100.00% (29/29) github.com/conformal/btcec/btcec.go KoblitzCurve.addZ1AndZ2EqualsOne 100.00% (29/29)
github.com/conformal/btcec/btcec.go KoblitzCurve.addJacobian 100.00% (22/22) github.com/conformal/btcec/btcec.go KoblitzCurve.addJacobian 100.00% (22/22)
github.com/conformal/btcec/btcec.go KoblitzCurve.doubleGeneric 100.00% (18/18)
github.com/conformal/btcec/btcec.go KoblitzCurve.doubleZ1EqualsOne 100.00% (18/18) github.com/conformal/btcec/btcec.go KoblitzCurve.doubleZ1EqualsOne 100.00% (18/18)
github.com/conformal/btcec/btcec.go KoblitzCurve.doubleGeneric 100.00% (18/18)
github.com/conformal/btcec/signature.go Signature.Serialize 100.00% (13/13) github.com/conformal/btcec/signature.go Signature.Serialize 100.00% (13/13)
github.com/conformal/btcec/btcec.go KoblitzCurve.fieldJacobianToBigAffine 100.00% (12/12)
github.com/conformal/btcec/field.go fieldVal.MulInt 100.00% (12/12) github.com/conformal/btcec/field.go fieldVal.MulInt 100.00% (12/12)
github.com/conformal/btcec/field.go fieldVal.Add 100.00% (11/11) github.com/conformal/btcec/btcec.go KoblitzCurve.fieldJacobianToBigAffine 100.00% (12/12)
github.com/conformal/btcec/field.go fieldVal.Add2 100.00% (11/11) github.com/conformal/btcec/field.go fieldVal.Add2 100.00% (11/11)
github.com/conformal/btcec/field.go fieldVal.SetBytes 100.00% (11/11) github.com/conformal/btcec/field.go fieldVal.Add 100.00% (11/11)
github.com/conformal/btcec/field.go fieldVal.NegateVal 100.00% (11/11) github.com/conformal/btcec/field.go fieldVal.NegateVal 100.00% (11/11)
github.com/conformal/btcec/field.go fieldVal.Zero 100.00% (10/10) github.com/conformal/btcec/field.go fieldVal.SetBytes 100.00% (11/11)
github.com/conformal/btcec/btcec.go KoblitzCurve.Add 100.00% (10/10)
github.com/conformal/btcec/btcec.go KoblitzCurve.ScalarMult 100.00% (10/10) github.com/conformal/btcec/btcec.go KoblitzCurve.ScalarMult 100.00% (10/10)
github.com/conformal/btcec/btcec.go KoblitzCurve.Add 100.00% (10/10)
github.com/conformal/btcec/field.go fieldVal.Zero 100.00% (10/10)
github.com/conformal/btcec/btcec.go KoblitzCurve.doubleJacobian 100.00% (9/9) github.com/conformal/btcec/btcec.go KoblitzCurve.doubleJacobian 100.00% (9/9)
github.com/conformal/btcec/btcec.go initS256 100.00% (9/9) github.com/conformal/btcec/btcec.go initS256 100.00% (9/9)
github.com/conformal/btcec/pubkey.go PublicKey.SerializeHybrid 100.00% (8/8) github.com/conformal/btcec/pubkey.go PublicKey.SerializeHybrid 100.00% (8/8)
github.com/conformal/btcec/signature.go canonicalizeInt 100.00% (8/8) github.com/conformal/btcec/signature.go canonicalizeInt 100.00% (8/8)
github.com/conformal/btcec/pubkey.go PublicKey.SerializeCompressed 100.00% (7/7)
github.com/conformal/btcec/btcec.go KoblitzCurve.Double 100.00% (7/7) github.com/conformal/btcec/btcec.go KoblitzCurve.Double 100.00% (7/7)
github.com/conformal/btcec/pubkey.go PublicKey.SerializeUncompressed 100.00% (5/5) github.com/conformal/btcec/pubkey.go PublicKey.SerializeCompressed 100.00% (7/7)
github.com/conformal/btcec/field.go fieldVal.SetByteSlice 100.00% (5/5) github.com/conformal/btcec/privkey.go PrivKeyFromBytes 100.00% (6/6)
github.com/conformal/btcec/pubkey.go pad 100.00% (5/5) github.com/conformal/btcec/pubkey.go pad 100.00% (5/5)
github.com/conformal/btcec/field.go fieldVal.SetByteSlice 100.00% (5/5)
github.com/conformal/btcec/pubkey.go PublicKey.SerializeUncompressed 100.00% (5/5)
github.com/conformal/btcec/signature.go canonicalPadding 100.00% (4/4)
github.com/conformal/btcec/btcec.go KoblitzCurve.bigAffineToField 100.00% (4/4) github.com/conformal/btcec/btcec.go KoblitzCurve.bigAffineToField 100.00% (4/4)
github.com/conformal/btcec/field.go fieldVal.SetHex 100.00% (4/4) github.com/conformal/btcec/field.go fieldVal.SetHex 100.00% (4/4)
github.com/conformal/btcec/signature.go canonicalPadding 100.00% (4/4)
github.com/conformal/btcec/btcec.go KoblitzCurve.IsOnCurve 100.00% (4/4) github.com/conformal/btcec/btcec.go KoblitzCurve.IsOnCurve 100.00% (4/4)
github.com/conformal/btcec/field.go fieldVal.SetInt 100.00% (3/3) github.com/conformal/btcec/field.go fieldVal.SetInt 100.00% (3/3)
github.com/conformal/btcec/field.go fieldVal.Bytes 100.00% (3/3) github.com/conformal/btcec/field.go fieldVal.Bytes 100.00% (3/3)
github.com/conformal/btcec/field.go fieldVal.IsZero 100.00% (2/2)
github.com/conformal/btcec/field.go fieldVal.AddInt 100.00% (2/2)
github.com/conformal/btcec/field.go fieldVal.Equals 100.00% (2/2)
github.com/conformal/btcec/field.go fieldVal.Set 100.00% (2/2)
github.com/conformal/btcec/field.go fieldVal.String 100.00% (2/2) github.com/conformal/btcec/field.go fieldVal.String 100.00% (2/2)
github.com/conformal/btcec/field.go fieldVal.Equals 100.00% (2/2)
github.com/conformal/btcec/field.go fieldVal.IsZero 100.00% (2/2)
github.com/conformal/btcec/btcec.go S256 100.00% (2/2) github.com/conformal/btcec/btcec.go S256 100.00% (2/2)
github.com/conformal/btcec/field.go fieldVal.AddInt 100.00% (2/2)
github.com/conformal/btcec/field.go fieldVal.Set 100.00% (2/2)
github.com/conformal/btcec/pubkey.go isOdd 100.00% (1/1) github.com/conformal/btcec/pubkey.go isOdd 100.00% (1/1)
github.com/conformal/btcec/field.go fieldVal.Negate 100.00% (1/1)
github.com/conformal/btcec/btcec.go initAll 100.00% (1/1)
github.com/conformal/btcec/signature.go ParseSignature 100.00% (1/1)
github.com/conformal/btcec/signature.go ParseDERSignature 100.00% (1/1) github.com/conformal/btcec/signature.go ParseDERSignature 100.00% (1/1)
github.com/conformal/btcec/btcec.go KoblitzCurve.ScalarBaseMult 100.00% (1/1)
github.com/conformal/btcec/field.go fieldVal.Mul 100.00% (1/1) github.com/conformal/btcec/field.go fieldVal.Mul 100.00% (1/1)
github.com/conformal/btcec/btcec.go KoblitzCurve.Params 100.00% (1/1) github.com/conformal/btcec/btcec.go KoblitzCurve.Params 100.00% (1/1)
github.com/conformal/btcec/field.go fieldVal.Square 100.00% (1/1) github.com/conformal/btcec/field.go fieldVal.Square 100.00% (1/1)
github.com/conformal/btcec/field.go fieldVal.Negate 100.00% (1/1)
github.com/conformal/btcec/field.go fieldVal.IsOdd 100.00% (1/1) github.com/conformal/btcec/field.go fieldVal.IsOdd 100.00% (1/1)
github.com/conformal/btcec/btcec.go initAll 100.00% (1/1) github.com/conformal/btcec/btcec.go KoblitzCurve.ScalarBaseMult 100.00% (1/1)
github.com/conformal/btcec/btcec.go KoblitzCurve.QPlus1Div4 100.00% (1/1) github.com/conformal/btcec/btcec.go KoblitzCurve.QPlus1Div4 100.00% (1/1)
github.com/conformal/btcec/signature.go ParseSignature 100.00% (1/1) github.com/conformal/btcec/pubkey.go ParsePubKey 92.86% (26/28)
github.com/conformal/btcec/pubkey.go ParsePubKey 96.15% (25/26)
github.com/conformal/btcec/signature.go SignCompact 90.91% (20/22)
github.com/conformal/btcec/pubkey.go decompressPoint 88.89% (8/9) github.com/conformal/btcec/pubkey.go decompressPoint 88.89% (8/9)
github.com/conformal/btcec/signature.go recoverKeyFromSignature 86.96% (20/23) github.com/conformal/btcec/signature.go recoverKeyFromSignature 86.96% (20/23)
github.com/conformal/btcec/signature.go SignCompact 86.36% (19/22)
github.com/conformal/btcec/signature.go RecoverCompact 77.78% (7/9) github.com/conformal/btcec/signature.go RecoverCompact 77.78% (7/9)
github.com/conformal/btcec/signature.go hashToInt 77.78% (7/9) github.com/conformal/btcec/signature.go hashToInt 77.78% (7/9)
github.com/conformal/btcec ------------------------------------- 98.80% (903/914) github.com/conformal/btcec ------------------------------------- 98.59% (909/922)