Use btcec structs instead of ecdsa structs everywhere.
This change should make it so that only btcec relies on the crypto/ecdsa package for secp256k1 math.
This commit is contained in:
parent
d312d47298
commit
ae28fe6d97
4 changed files with 34 additions and 21 deletions
|
@ -5,7 +5,6 @@
|
||||||
package btcec_test
|
package btcec_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ecdsa"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/conformal/btcec"
|
"github.com/conformal/btcec"
|
||||||
|
@ -75,7 +74,7 @@ func BenchmarkSigVerify(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
// Randomly generated keypair.
|
// Randomly generated keypair.
|
||||||
// Private key: 9e0699c91ca1e3b7e3c9ba71eb71c89890872be97576010fe593fbf3fd57e66d
|
// Private key: 9e0699c91ca1e3b7e3c9ba71eb71c89890872be97576010fe593fbf3fd57e66d
|
||||||
pubKey := ecdsa.PublicKey{
|
pubKey := btcec.PublicKey{
|
||||||
Curve: btcec.S256(),
|
Curve: btcec.S256(),
|
||||||
X: fromHex("d2e670a19c6d753d1a6d8b20bd045df8a08fb162cf508956c31268c6d81ffdab"),
|
X: fromHex("d2e670a19c6d753d1a6d8b20bd045df8a08fb162cf508956c31268c6d81ffdab"),
|
||||||
Y: fromHex("ab65528eefbb8057aa85d597258a3fbd481a24633bc9b47a9aa045c91371de52"),
|
Y: fromHex("ab65528eefbb8057aa85d597258a3fbd481a24633bc9b47a9aa045c91371de52"),
|
||||||
|
@ -83,16 +82,18 @@ func BenchmarkSigVerify(b *testing.B) {
|
||||||
|
|
||||||
// Double sha256 of []byte{0x01, 0x02, 0x03, 0x04}
|
// Double sha256 of []byte{0x01, 0x02, 0x03, 0x04}
|
||||||
msgHash := fromHex("8de472e2399610baaa7f84840547cd409434e31f5d3bd71e4d947f283874f9c0")
|
msgHash := fromHex("8de472e2399610baaa7f84840547cd409434e31f5d3bd71e4d947f283874f9c0")
|
||||||
sigR := fromHex("fef45d2892953aa5bbcdb057b5e98b208f1617a7498af7eb765574e29b5d9c2c")
|
sig := btcec.Signature{
|
||||||
sigS := fromHex("d47563f52aac6b04b55de236b7c515eb9311757db01e02cff079c3ca6efb063f")
|
R: fromHex("fef45d2892953aa5bbcdb057b5e98b208f1617a7498af7eb765574e29b5d9c2c"),
|
||||||
|
S: fromHex("d47563f52aac6b04b55de236b7c515eb9311757db01e02cff079c3ca6efb063f"),
|
||||||
|
}
|
||||||
|
|
||||||
if !ecdsa.Verify(&pubKey, msgHash.Bytes(), sigR, sigS) {
|
if !sig.Verify(msgHash.Bytes(), &pubKey) {
|
||||||
b.Errorf("Signature failed to verify")
|
b.Errorf("Signature failed to verify")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
ecdsa.Verify(&pubKey, msgHash.Bytes(), sigR, sigS)
|
sig.Verify(msgHash.Bytes(), &pubKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,6 @@
|
||||||
package btcec_test
|
package btcec_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ecdsa"
|
|
||||||
"crypto/elliptic"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
@ -591,8 +589,8 @@ func BenchmarkBaseMult(b *testing.B) {
|
||||||
|
|
||||||
// Test this curve's usage with the ecdsa package.
|
// Test this curve's usage with the ecdsa package.
|
||||||
|
|
||||||
func testKeyGeneration(t *testing.T, c elliptic.Curve, tag string) {
|
func testKeyGeneration(t *testing.T, c *btcec.KoblitzCurve, tag string) {
|
||||||
priv, err := ecdsa.GenerateKey(c, rand.Reader)
|
priv, err := btcec.NewPrivateKey(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%s: error: %s", tag, err)
|
t.Errorf("%s: error: %s", tag, err)
|
||||||
return
|
return
|
||||||
|
@ -606,22 +604,23 @@ func TestKeyGeneration(t *testing.T) {
|
||||||
testKeyGeneration(t, btcec.S256(), "S256")
|
testKeyGeneration(t, btcec.S256(), "S256")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSignAndVerify(t *testing.T, c elliptic.Curve, tag string) {
|
func testSignAndVerify(t *testing.T, c *btcec.KoblitzCurve, tag string) {
|
||||||
priv, _ := ecdsa.GenerateKey(c, rand.Reader)
|
priv, _ := btcec.NewPrivateKey(c)
|
||||||
|
pub := priv.PubKey()
|
||||||
|
|
||||||
hashed := []byte("testing")
|
hashed := []byte("testing")
|
||||||
r, s, err := ecdsa.Sign(rand.Reader, priv, hashed)
|
sig, err := priv.Sign(hashed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%s: error signing: %s", tag, err)
|
t.Errorf("%s: error signing: %s", tag, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ecdsa.Verify(&priv.PublicKey, hashed, r, s) {
|
if !sig.Verify(hashed, pub) {
|
||||||
t.Errorf("%s: Verify failed", tag)
|
t.Errorf("%s: Verify failed", tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
hashed[0] ^= 0xff
|
hashed[0] ^= 0xff
|
||||||
if ecdsa.Verify(&priv.PublicKey, hashed, r, s) {
|
if sig.Verify(hashed, pub) {
|
||||||
t.Errorf("%s: Verify always works!", tag)
|
t.Errorf("%s: Verify always works!", tag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -778,7 +777,7 @@ func TestVectors(t *testing.T) {
|
||||||
sha := sha1.New()
|
sha := sha1.New()
|
||||||
|
|
||||||
for i, test := range testVectors {
|
for i, test := range testVectors {
|
||||||
pub := ecdsa.PublicKey{
|
pub := btcec.PublicKey{
|
||||||
Curve: btcec.S256(),
|
Curve: btcec.S256(),
|
||||||
X: fromHex(test.Qx),
|
X: fromHex(test.Qx),
|
||||||
Y: fromHex(test.Qy),
|
Y: fromHex(test.Qy),
|
||||||
|
@ -787,9 +786,8 @@ func TestVectors(t *testing.T) {
|
||||||
sha.Reset()
|
sha.Reset()
|
||||||
sha.Write(msg)
|
sha.Write(msg)
|
||||||
hashed := sha.Sum(nil)
|
hashed := sha.Sum(nil)
|
||||||
r := fromHex(test.r)
|
sig := btcec.Signature{R: fromHex(test.r), S: fromHex(test.s)}
|
||||||
s := fromHex(test.s)
|
if fuck := sig.Verify(hashed, &pub); fuck != test.ok {
|
||||||
if fuck := ecdsa.Verify(&pub, hashed, r, s); fuck != test.ok {
|
|
||||||
//t.Errorf("%d: bad result %v %v", i, pub, hashed)
|
//t.Errorf("%d: bad result %v %v", i, pub, hashed)
|
||||||
t.Errorf("%d: bad result %v instead of %v", i, fuck,
|
t.Errorf("%d: bad result %v instead of %v", i, fuck,
|
||||||
test.ok)
|
test.ok)
|
||||||
|
|
15
privkey.go
15
privkey.go
|
@ -33,6 +33,21 @@ func PrivKeyFromBytes(curve *KoblitzCurve, pk []byte) (*PrivateKey,
|
||||||
return (*PrivateKey)(priv), (*PublicKey)(&priv.PublicKey)
|
return (*PrivateKey)(priv), (*PublicKey)(&priv.PublicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPrivateKey is a wrapper for ecdsa.GenerateKey that returns a PrivateKey
|
||||||
|
// instead of the normal ecdsa.PrivateKey.
|
||||||
|
func NewPrivateKey(curve *KoblitzCurve) (*PrivateKey, error) {
|
||||||
|
key, err := ecdsa.GenerateKey(curve, rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return (*PrivateKey)(key), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PubKey returns the PublicKey corresponding to this private key.
|
||||||
|
func (p *PrivateKey) PubKey() *PublicKey {
|
||||||
|
return (*PublicKey)(&p.PublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
// ToECDSA returns the private key as a *ecdsa.PrivateKey.
|
// ToECDSA returns the private key as a *ecdsa.PrivateKey.
|
||||||
func (p *PrivateKey) ToECDSA() *ecdsa.PrivateKey {
|
func (p *PrivateKey) ToECDSA() *ecdsa.PrivateKey {
|
||||||
return (*ecdsa.PrivateKey)(p)
|
return (*ecdsa.PrivateKey)(p)
|
||||||
|
|
|
@ -6,7 +6,6 @@ package btcec_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/ecdsa"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
@ -427,7 +426,7 @@ func TestSignatureSerialize(t *testing.T) {
|
||||||
|
|
||||||
func testSignCompact(t *testing.T, tag string, curve *btcec.KoblitzCurve,
|
func testSignCompact(t *testing.T, tag string, curve *btcec.KoblitzCurve,
|
||||||
data []byte, isCompressed bool) {
|
data []byte, isCompressed bool) {
|
||||||
tmp, _ := ecdsa.GenerateKey(curve, rand.Reader)
|
tmp, _ := btcec.NewPrivateKey(curve)
|
||||||
priv := (*btcec.PrivateKey)(tmp)
|
priv := (*btcec.PrivateKey)(tmp)
|
||||||
|
|
||||||
hashed := []byte("testing")
|
hashed := []byte("testing")
|
||||||
|
|
Loading…
Reference in a new issue