Skip to content

Commit 8488daf

Browse files
committed
upgrade to latest github.com/Roasbeef/btcd BIP340 branch, upgrade deps
1 parent 3a7d0bf commit 8488daf

22 files changed

+117
-132
lines changed

cmd/tss-benchsign/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/binance-chain/tss-lib/ecdsa/signing"
1919
"github.com/binance-chain/tss-lib/test"
2020
"github.com/binance-chain/tss-lib/tss"
21-
"github.com/btcsuite/btcd/btcec"
21+
"github.com/decred/dcrd/dcrec/secp256k1/v2"
2222
"github.com/ipfs/go-log"
2323
"github.com/olekukonko/tablewriter"
2424
"github.com/pkg/errors"
@@ -196,8 +196,8 @@ outer:
196196
); !ok {
197197
panic("ECDSA signature verification did not pass")
198198
}
199-
btcecSig := &btcec.Signature{R: r, S: s}
200-
if ok = btcecSig.Verify(msg.Bytes(), (*btcec.PublicKey)(&pk)); !ok {
199+
btcecSig := secp256k1.NewSignature(r, s)
200+
if ok = btcecSig.Verify(msg.Bytes(), (*secp256k1.PublicKey)(&pk)); !ok {
201201
panic("ECDSA signature verification 2 did not pass")
202202
}
203203
break outer

common/signature.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crypto/ckd/child_key_derivation.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package ckd
44

55
import (
66
"bytes"
7-
"crypto/ecdsa"
87
"crypto/elliptic"
98
"crypto/hmac"
109
"crypto/rand"
@@ -17,13 +16,13 @@ import (
1716

1817
"github.com/binance-chain/tss-lib/common"
1918
"github.com/binance-chain/tss-lib/crypto"
20-
"github.com/btcsuite/btcd/btcec"
19+
"github.com/btcsuite/btcd/btcec/v2"
2120
"github.com/btcsuite/btcutil/base58"
2221
"golang.org/x/crypto/ripemd160"
2322
)
2423

2524
type ExtendedKey struct {
26-
ecdsa.PublicKey
25+
*btcec.PublicKey
2726
Depth uint8
2827
ChildIndex uint32
2928
ChainCode []byte // 32 bytes
@@ -71,7 +70,7 @@ func (k *ExtendedKey) String() string {
7170
serializedBytes = append(serializedBytes, k.ParentFP...)
7271
serializedBytes = append(serializedBytes, childNumBytes[:]...)
7372
serializedBytes = append(serializedBytes, k.ChainCode...)
74-
pubKeyBytes := serializeCompressed(k.PublicKey.X, k.PublicKey.Y)
73+
pubKeyBytes := serializeCompressed(k.PublicKey.X(), k.PublicKey.Y())
7574
serializedBytes = append(serializedBytes, pubKeyBytes...)
7675

7776
checkSum := doubleHashB(serializedBytes)[:4]
@@ -104,23 +103,21 @@ func NewExtendedKeyFromString(key string, curve elliptic.Curve) (*ExtendedKey, e
104103
chainCode := payload[13:45]
105104
keyData := payload[45:78]
106105

107-
var pubKey ecdsa.PublicKey
108-
109-
if c, ok := curve.(*btcec.KoblitzCurve); ok {
106+
var pubKey *btcec.PublicKey
107+
if _, ok := curve.(*btcec.KoblitzCurve); ok {
110108
// Ensure the public key parses correctly and is actually on the
111109
// secp256k1 curve.
112-
pk, err := btcec.ParsePubKey(keyData, c)
110+
pk, err := btcec.ParsePubKey(keyData)
113111
if err != nil {
114112
return nil, err
115113
}
116-
pubKey = ecdsa.PublicKey(*pk)
114+
pubKey = pk
117115
} else {
116+
var x, y btcec.FieldVal
118117
px, py := elliptic.Unmarshal(curve, keyData)
119-
pubKey = ecdsa.PublicKey{
120-
Curve: curve,
121-
X: px,
122-
Y: py,
123-
}
118+
x.SetByteSlice(px.Bytes())
119+
y.SetByteSlice(py.Bytes())
120+
pubKey = btcec.NewPublicKey(&x, &y)
124121
}
125122

126123
return &ExtendedKey{
@@ -207,13 +204,13 @@ func DeriveChildKey(index uint32, pk *ExtendedKey, curve elliptic.Curve) (*big.I
207204
return nil, nil, errors.New("cannot derive key beyond max depth")
208205
}
209206

210-
cryptoPk, err := crypto.NewECPoint(curve, pk.X, pk.Y)
207+
cryptoPk, err := crypto.NewECPoint(curve, pk.X(), pk.Y())
211208
if err != nil {
212209
common.Logger.Error("error getting pubkey from extendedkey")
213210
return nil, nil, err
214211
}
215212

216-
pkPublicKeyBytes := serializeCompressed(pk.X, pk.Y)
213+
pkPublicKeyBytes := serializeCompressed(pk.X(), pk.Y())
217214

218215
data := make([]byte, 37)
219216
copy(data, pkPublicKeyBytes)
@@ -247,7 +244,7 @@ func DeriveChildKey(index uint32, pk *ExtendedKey, curve elliptic.Curve) (*big.I
247244
}
248245

249246
childPk := &ExtendedKey{
250-
PublicKey: *childCryptoPk.ToECDSAPubKey(),
247+
PublicKey: childCryptoPk.ToSecp256k1PubKey(),
251248
Depth: pk.Depth + 1,
252249
ChildIndex: index,
253250
ChainCode: childChainCode,
@@ -257,6 +254,8 @@ func DeriveChildKey(index uint32, pk *ExtendedKey, curve elliptic.Curve) (*big.I
257254
return ilNum, childPk, nil
258255
}
259256

257+
// GenerateSeed
258+
// TODO: Is this being used?
260259
func GenerateSeed(length uint8) ([]byte, error) {
261260
// Per [BIP32], the seed must be in range [MinSeedBytes, MaxSeedBytes].
262261
if length < MinSeedBytes || length > MaxSeedBytes {

crypto/ckd/child_key_derivation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111

1212
. "github.com/binance-chain/tss-lib/crypto/ckd"
13-
"github.com/btcsuite/btcd/btcec"
13+
"github.com/btcsuite/btcd/btcec/v2"
1414
)
1515

1616
func TestPublicDerivation(t *testing.T) {

crypto/ecpoint.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818

1919
"github.com/binance-chain/tss-lib/tss"
2020
"github.com/btcsuite/btcd/btcec/v2"
21+
"github.com/decred/dcrd/dcrec/edwards/v2"
22+
"github.com/decred/dcrd/dcrec/secp256k1/v4"
2123
)
2224

2325
// ECPoint convenience helper
@@ -59,20 +61,28 @@ func (p *ECPoint) ScalarMult(k *big.Int) *ECPoint {
5961
return newP
6062
}
6163

62-
func (p *ECPoint) ToECDSAPubKey() *ecdsa.PublicKey {
63-
return &ecdsa.PublicKey{
64-
Curve: p.curve,
65-
X: p.X(),
66-
Y: p.Y(),
67-
}
64+
func (p *ECPoint) ToBtcecPubKey() *btcec.PublicKey {
65+
var x, y btcec.FieldVal
66+
x.SetByteSlice(p.X().Bytes())
67+
y.SetByteSlice(p.Y().Bytes())
68+
return btcec.NewPublicKey(&x, &y)
6869
}
6970

70-
func (p *ECPoint) ToBtcecPubKey() *btcec.PublicKey {
71+
func (p *ECPoint) ToSecp256k1PubKey() *secp256k1.PublicKey {
7172
var x, y btcec.FieldVal
7273
x.SetByteSlice(p.X().Bytes())
7374
y.SetByteSlice(p.Y().Bytes())
75+
return secp256k1.NewPublicKey(&x, &y)
76+
}
7477

75-
return btcec.NewPublicKey(&x, &y)
78+
func (p *ECPoint) ToEdwardsPubKey() *edwards.PublicKey {
79+
ecdsaPK := ecdsa.PublicKey{
80+
Curve: p.curve,
81+
X: p.X(),
82+
Y: p.Y(),
83+
}
84+
pk := edwards.PublicKey(ecdsaPK)
85+
return &pk
7686
}
7787

7888
func (p *ECPoint) IsOnCurve() bool {

crypto/ecpoint_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"reflect"
1414
"testing"
1515

16-
"github.com/btcsuite/btcd/btcec"
16+
"github.com/btcsuite/btcd/btcec/v2"
1717
"github.com/decred/dcrd/dcrec/edwards/v2"
1818
"github.com/stretchr/testify/assert"
1919

@@ -126,10 +126,10 @@ func TestS256EcpointJsonSerialization(t *testing.T) {
126126

127127
pubKeyBytes, err := hex.DecodeString("03935336acb03b2b801d8f8ac5e92c56c4f6e93319901fdfffba9d340a874e2879")
128128
assert.NoError(t, err)
129-
pbk, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256())
129+
pbk, err := btcec.ParsePubKey(pubKeyBytes)
130130
assert.NoError(t, err)
131131

132-
point, err := NewECPoint(ec, pbk.X, pbk.Y)
132+
point, err := NewECPoint(ec, pbk.X(), pbk.Y())
133133
assert.NoError(t, err)
134134
bz, err := json.Marshal(point)
135135
assert.NoError(t, err)
@@ -144,7 +144,7 @@ func TestS256EcpointJsonSerialization(t *testing.T) {
144144
}
145145

146146
func TestEdwardsEcpointJsonSerialization(t *testing.T) {
147-
ec := edwards.Edwards()
147+
ec := tss.Edwards()
148148
tss.RegisterCurve("ed25519", ec)
149149

150150
pubKeyBytes, err := hex.DecodeString("ae1e5bf5f3d6bf58b5c222088671fcbe78b437e28fae944c793897b26091f249")

ecdsa/keygen/ecdsa-keygen.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecdsa/resharing/ecdsa-resharing.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecdsa/signing/ecdsa-signing.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecdsa/signing/key_derivation_test.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"sync/atomic"
1111
"testing"
1212

13-
"github.com/btcsuite/btcd/btcec"
1413
"github.com/stretchr/testify/assert"
1514

1615
"github.com/binance-chain/tss-lib/common"
@@ -39,12 +38,7 @@ func TestHDKeyDerivation(t *testing.T) {
3938
assert.NotNil(t, keys[0].ECDSAPub, "the first ECDSA public key must not be null")
4039

4140
// build ecdsa key pair
42-
parentPkX, parentPkY := keys[0].ECDSAPub.X(), keys[0].ECDSAPub.Y()
43-
pk := ecdsa.PublicKey{
44-
Curve: tss.EC(),
45-
X: parentPkX,
46-
Y: parentPkY,
47-
}
41+
pk := keys[0].ECDSAPub.ToSecp256k1PubKey()
4842

4943
// setting the chain code to a random positive number smaller than the maximum allowed of 32 bytes
5044
chainCode := make([]byte, 32)
@@ -66,7 +60,7 @@ func TestHDKeyDerivation(t *testing.T) {
6660

6761
keyDerivationDelta := il
6862

69-
err = UpdatePublicKeyAndAdjustBigXj(keyDerivationDelta, keys, &extendedChildPk.PublicKey, tss.EC())
63+
err = UpdatePublicKeyAndAdjustBigXj(keyDerivationDelta, keys, extendedChildPk.PublicKey, tss.EC())
7064
assert.NoErrorf(t, err, "there should not be an error setting the derived keys")
7165

7266
// PHASE: signing
@@ -117,7 +111,6 @@ signing:
117111
bigRX, bigRY := parties[0].temp.BigR.X(), parties[0].temp.BigR.Y()
118112
bigR := crypto.NewECPointNoCurveCheck(tss.EC(), bigRX, bigRY)
119113

120-
r := parties[0].temp.Rx
121114
// fmt.Printf("sign result: R(%s, %s), r=%s\n", bigR.X().String(), bigR.Y().String(), r.String())
122115

123116
modN := common.ModInt(tss.EC().Params().N)
@@ -130,13 +123,14 @@ signing:
130123
// fmt.Printf("S: %s\n", sumS.String())
131124
// END check s correctness
132125

133-
ok := ecdsa.Verify(&extendedChildPk.PublicKey, msg.Bytes(), bigR.X(), sumS)
126+
ecdsaPK := &ecdsa.PublicKey{
127+
Curve: tss.EC(),
128+
X: extendedChildPk.X(),
129+
Y: extendedChildPk.Y(),
130+
}
131+
ok := ecdsa.Verify(ecdsaPK, msg.Bytes(), bigR.X(), sumS)
134132
assert.True(t, ok, "ecdsa verify must pass")
135133

136-
btcecSig := &btcec.Signature{R: r, S: sumS}
137-
btcecSig.Verify(msg.Bytes(), (*btcec.PublicKey)(&extendedChildPk.PublicKey))
138-
assert.True(t, ok, "ecdsa verify 2 must pass")
139-
140134
t.Log("ECDSA signing test done.")
141135
// END ECDSA verify
142136

0 commit comments

Comments
 (0)