Skip to content

Commit 581781f

Browse files
committed
feat: configurable curve and support secp256k1
Signed-off-by: lanford33 <[email protected]>
1 parent 6f32efc commit 581781f

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

mpc/binance/ecdsa/mpc.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import (
2626
"github.com/bnb-chain/tss-lib/v2/ecdsa/keygen"
2727
"github.com/bnb-chain/tss-lib/v2/ecdsa/signing"
2828
"github.com/bnb-chain/tss-lib/v2/tss"
29+
"github.com/btcsuite/btcd/btcec/v2"
30+
s256k1 "github.com/btcsuite/btcd/btcec/v2"
31+
"github.com/decred/dcrd/dcrec/secp256k1/v4"
2932
"github.com/golang/protobuf/proto"
3033
"github.com/golang/protobuf/ptypes/any"
3134
)
@@ -106,14 +109,20 @@ type party struct {
106109
in chan tss.Message
107110
shareData *keygen.LocalPartySaveData
108111
closeChan chan struct{}
112+
curve elliptic.Curve
109113
}
110114

111-
func NewParty(id uint16, logger Logger) *party {
115+
func NewParty(id uint16, curve elliptic.Curve, logger Logger) *party {
116+
if curve == nil {
117+
curve = s256k1.S256()
118+
}
119+
112120
return &party{
113121
logger: logger,
114122
id: tss.NewPartyID(fmt.Sprintf("%d", id), "", big.NewInt(int64(id))),
115123
out: make(chan tss.Message, 1000),
116124
in: make(chan tss.Message, 1000),
125+
curve: curve,
117126
}
118127
}
119128

@@ -190,7 +199,17 @@ func (p *party) ThresholdPK() ([]byte, error) {
190199
if err != nil {
191200
return nil, err
192201
}
193-
return x509.MarshalPKIXPublicKey(pk)
202+
203+
switch p.curve.Params().Name {
204+
case string(tss.Secp256k1):
205+
xFieldVal, yFieldVal := new(secp256k1.FieldVal), new(secp256k1.FieldVal)
206+
xFieldVal.SetByteSlice(pk.X.Bytes())
207+
yFieldVal.SetByteSlice(pk.Y.Bytes())
208+
btcecPubKey := btcec.NewPublicKey(xFieldVal, yFieldVal)
209+
return btcecPubKey.SerializeCompressed(), nil
210+
default:
211+
return x509.MarshalPKIXPublicKey(pk)
212+
}
194213
}
195214

196215
func (p *party) SetShareData(shareData []byte) error {
@@ -199,9 +218,9 @@ func (p *party) SetShareData(shareData []byte) error {
199218
if err != nil {
200219
return fmt.Errorf("failed deserializing shares: %w", err)
201220
}
202-
localSaveData.ECDSAPub.SetCurve(elliptic.P256())
221+
localSaveData.ECDSAPub.SetCurve(p.curve)
203222
for _, xj := range localSaveData.BigXj {
204-
xj.SetCurve(elliptic.P256())
223+
xj.SetCurve(p.curve)
205224
}
206225
p.shareData = &localSaveData
207226
return nil
@@ -210,7 +229,7 @@ func (p *party) SetShareData(shareData []byte) error {
210229
func (p *party) Init(parties []uint16, threshold int, sendMsg func(msg []byte, isBroadcast bool, to uint16)) {
211230
partyIDs := partyIDsFromNumbers(parties)
212231
ctx := tss.NewPeerContext(partyIDs)
213-
p.params = tss.NewParameters(elliptic.P256(), ctx, p.id, len(parties), threshold)
232+
p.params = tss.NewParameters(p.curve, ctx, p.id, len(parties), threshold)
214233
p.id.Index = p.locatePartyIndex(p.id)
215234
p.sendMsg = sendMsg
216235
p.closeChan = make(chan struct{})
@@ -237,7 +256,7 @@ func (p *party) Sign(ctx context.Context, msgHash []byte) ([]byte, error) {
237256

238257
end := make(chan *common.SignatureData, 1)
239258

240-
msgToSign := hashToInt(msgHash, elliptic.P256())
259+
msgToSign := hashToInt(msgHash, p.curve)
241260
party := signing.NewLocalParty(msgToSign, p.params, *p.shareData, p.out, end)
242261

243262
var endWG sync.WaitGroup

mpc/binance/ecdsa/mpc_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package ecdsa
99
import (
1010
"context"
1111
"crypto/ecdsa"
12+
"crypto/elliptic"
1213
"fmt"
1314
"math/big"
1415
"sync"
@@ -108,9 +109,9 @@ func (parties parties) Mapping() map[string]*tss.PartyID {
108109
}
109110

110111
func TestTSS(t *testing.T) {
111-
pA := NewParty(1, logger("pA", t.Name()))
112-
pB := NewParty(2, logger("pB", t.Name()))
113-
pC := NewParty(3, logger("pC", t.Name()))
112+
pA := NewParty(1, elliptic.P256(), logger("pA", t.Name()))
113+
pB := NewParty(2, elliptic.P256(), logger("pB", t.Name()))
114+
pC := NewParty(3, elliptic.P256(), logger("pC", t.Name()))
114115

115116
t.Logf("Created parties")
116117

test/binance/ecdsa_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package binance_test
22

33
import (
44
"crypto/ecdsa"
5+
"crypto/elliptic"
56
"crypto/x509"
67
"testing"
78

89
ecdsa_scheme "github.com/IBM/TSS/mpc/binance/ecdsa"
10+
911
. "github.com/IBM/TSS/types"
12+
1013
"github.com/stretchr/testify/assert"
1114
)
1215

@@ -18,7 +21,9 @@ func TestThresholdBinanceECDSA(t *testing.T) {
1821
var signatureAlgorithms func([]*commLogger) (func(uint16) KeyGenerator, func(uint16) Signer)
1922

2023
verifySig = verifySignatureECDSA
21-
signatureAlgorithms = ecdsaKeygenAndSign
24+
signatureAlgorithms = func(loggers []*commLogger) (func(uint16) KeyGenerator, func(uint16) Signer) {
25+
return ecdsaKeygenAndSign(elliptic.P256(), loggers)
26+
}
2227

2328
testScheme(t, n, signatureAlgorithms, verifySig, false)
2429
}
@@ -31,18 +36,20 @@ func TestFastThresholdBinanceECDSA(t *testing.T) {
3136
var signatureAlgorithms func([]*commLogger) (func(uint16) KeyGenerator, func(uint16) Signer)
3237

3338
verifySig = verifySignatureECDSA
34-
signatureAlgorithms = ecdsaKeygenAndSign
39+
signatureAlgorithms = func(loggers []*commLogger) (func(uint16) KeyGenerator, func(uint16) Signer) {
40+
return ecdsaKeygenAndSign(elliptic.P256(), loggers)
41+
}
3542

3643
testScheme(t, n, signatureAlgorithms, verifySig, true)
3744
}
3845

39-
func ecdsaKeygenAndSign(loggers []*commLogger) (func(id uint16) KeyGenerator, func(id uint16) Signer) {
46+
func ecdsaKeygenAndSign(curve elliptic.Curve, loggers []*commLogger) (func(id uint16) KeyGenerator, func(id uint16) Signer) {
4047
kgf := func(id uint16) KeyGenerator {
41-
return ecdsa_scheme.NewParty(id, loggers[id-1])
48+
return ecdsa_scheme.NewParty(id, curve, loggers[id-1])
4249
}
4350

4451
sf := func(id uint16) Signer {
45-
return ecdsa_scheme.NewParty(id, loggers[id-1])
52+
return ecdsa_scheme.NewParty(id, curve, loggers[id-1])
4653
}
4754
return kgf, sf
4855
}

0 commit comments

Comments
 (0)