-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmpc.go
396 lines (340 loc) · 10.5 KB
/
mpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package ecdsa
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"encoding/json"
"fmt"
"math"
"math/big"
"sync"
"time"
"github.com/bnb-chain/tss-lib/v2/common"
"github.com/bnb-chain/tss-lib/v2/ecdsa/keygen"
"github.com/bnb-chain/tss-lib/v2/ecdsa/signing"
"github.com/bnb-chain/tss-lib/v2/tss"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
)
const (
// defaultSafePrimeGenTimeout is the default time allocated for the library to sample the Paillier public key
defaultSafePrimeGenTimeout = 5 * time.Minute
)
func init() {
tss.RegisterCurve("elliptic.p256Curve", elliptic.P256())
}
var (
msgURL2Round = map[string]uint8{
// DKG
"type.googleapis.com/binance.tsslib.ecdsa.keygen.KGRound1Message": 1,
"type.googleapis.com/binance.tsslib.ecdsa.keygen.KGRound2Message1": 2,
"type.googleapis.com/binance.tsslib.ecdsa.keygen.KGRound2Message2": 3,
"type.googleapis.com/binance.tsslib.ecdsa.keygen.KGRound3Message": 4,
// Signing
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound1Message1": 5,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound1Message2": 6,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound2Message": 7,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound3Message": 8,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound4Message": 9,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound5Message": 10,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound6Message": 11,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound7Message": 12,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound8Message": 13,
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound9Message": 14,
}
broadcastMessages = map[string]struct{}{
// DKG
"type.googleapis.com/binance.tsslib.ecdsa.keygen.KGRound1Message": {},
"type.googleapis.com/binance.tsslib.ecdsa.keygen.KGRound2Message2": {},
"type.googleapis.com/binance.tsslib.ecdsa.keygen.KGRound3Message": {},
// Signing
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound1Message2": {},
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound3Message": {},
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound4Message": {},
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound5Message": {},
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound6Message": {},
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound7Message": {},
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound8Message": {},
"type.googleapis.com/binance.tsslib.ecdsa.signing.SignRound9Message": {},
}
)
type Sender func(msg []byte, isBroadcast bool, to uint16)
type parties []*party
func (parties parties) numericIDs() []uint16 {
var res []uint16
for _, p := range parties {
res = append(res, uint16(big.NewInt(0).SetBytes(p.id.Key).Uint64()))
}
return res
}
type Logger interface {
Debugf(format string, a ...interface{})
Warnf(format string, a ...interface{})
Errorf(format string, a ...interface{})
}
type party struct {
logger Logger
sendMsg Sender
id *tss.PartyID
params *tss.Parameters
out chan tss.Message
in chan tss.Message
shareData *keygen.LocalPartySaveData
closeChan chan struct{}
}
func NewParty(id uint16, logger Logger) *party {
return &party{
logger: logger,
id: tss.NewPartyID(fmt.Sprintf("%d", id), "", big.NewInt(int64(id))),
out: make(chan tss.Message, 1000),
in: make(chan tss.Message, 1000),
}
}
func (p *party) ID() *tss.PartyID {
return p.id
}
func (p *party) locatePartyIndex(id *tss.PartyID) int {
for index, p := range p.params.Parties().IDs() {
if bytes.Equal(p.Key, id.Key) {
return index
}
}
return -1
}
func (p *party) ClassifyMsg(msgBytes []byte) (uint8, bool, error) {
msg := &any.Any{}
if err := proto.Unmarshal(msgBytes, msg); err != nil {
p.logger.Warnf("Received invalid message: %v", err)
return 0, false, err
}
_, isBroadcast := broadcastMessages[msg.TypeUrl]
round := msgURL2Round[msg.TypeUrl]
if round > 4 {
round = round - 4
}
return round, isBroadcast, nil
}
func (p *party) OnMsg(msgBytes []byte, from uint16, broadcast bool) {
id := tss.NewPartyID(fmt.Sprintf("%d", from), "", big.NewInt(int64(from)))
id.Index = p.locatePartyIndex(id)
msg, err := tss.ParseWireMessage(msgBytes, id, broadcast)
if err != nil {
p.logger.Warnf("Received invalid message (%s) of %d bytes from %d: %v", base64.StdEncoding.EncodeToString(msgBytes), len(msgBytes), from, err)
return
}
key := msg.GetFrom().KeyInt()
if key == nil || key.Cmp(big.NewInt(int64(math.MaxUint16))) >= 0 {
p.logger.Warnf("Message received from invalid key: %v", key)
return
}
claimedFrom := uint16(key.Uint64())
if claimedFrom != from {
p.logger.Warnf("Message claimed to be from %d but was received from %d", claimedFrom, from)
return
}
p.in <- msg
}
func (p *party) TPubKey() (*ecdsa.PublicKey, error) {
if p.shareData == nil {
return nil, fmt.Errorf("must call SetShareData() before attempting to sign")
}
pk := p.shareData.ECDSAPub
return &ecdsa.PublicKey{
Curve: pk.Curve(),
X: pk.X(),
Y: pk.Y(),
}, nil
}
func (p *party) ThresholdPK() ([]byte, error) {
pk, err := p.TPubKey()
if err != nil {
return nil, err
}
return x509.MarshalPKIXPublicKey(pk)
}
func (p *party) SetShareData(shareData []byte) error {
var localSaveData keygen.LocalPartySaveData
err := json.Unmarshal(shareData, &localSaveData)
if err != nil {
return fmt.Errorf("failed deserializing shares: %w", err)
}
localSaveData.ECDSAPub.SetCurve(elliptic.P256())
for _, xj := range localSaveData.BigXj {
xj.SetCurve(elliptic.P256())
}
p.shareData = &localSaveData
return nil
}
func (p *party) Init(parties []uint16, threshold int, sendMsg func(msg []byte, isBroadcast bool, to uint16)) {
partyIDs := partyIDsFromNumbers(parties)
ctx := tss.NewPeerContext(partyIDs)
p.params = tss.NewParameters(elliptic.P256(), ctx, p.id, len(parties), threshold)
p.id.Index = p.locatePartyIndex(p.id)
p.sendMsg = sendMsg
p.closeChan = make(chan struct{})
go p.sendMessages()
}
func partyIDsFromNumbers(parties []uint16) []*tss.PartyID {
var partyIDs []*tss.PartyID
for _, p := range parties {
pID := tss.NewPartyID(fmt.Sprintf("%d", p), "", big.NewInt(int64(p)))
partyIDs = append(partyIDs, pID)
}
return tss.SortPartyIDs(partyIDs)
}
func (p *party) Sign(ctx context.Context, msgHash []byte) ([]byte, error) {
if p.shareData == nil {
return nil, fmt.Errorf("must call SetShareData() before attempting to sign")
}
p.logger.Debugf("Starting signing")
defer p.logger.Debugf("Finished signing")
defer close(p.closeChan)
end := make(chan *common.SignatureData, 1)
msgToSign := hashToInt(msgHash, elliptic.P256())
party := signing.NewLocalParty(msgToSign, p.params, *p.shareData, p.out, end)
var endWG sync.WaitGroup
endWG.Add(1)
go func() {
defer endWG.Done()
err := party.Start()
if err != nil {
p.logger.Errorf("Failed signing: %v", err)
}
}()
defer endWG.Wait()
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("signing timed out: %w", ctx.Err())
case sigOut := <-end:
if !bytes.Equal(sigOut.M, msgToSign.Bytes()) {
return nil, fmt.Errorf("message we requested to sign is %s but actual message signed is %s",
base64.StdEncoding.EncodeToString(msgHash),
base64.StdEncoding.EncodeToString(sigOut.M))
}
var sig struct {
R, S *big.Int
}
sig.R = big.NewInt(0)
sig.S = big.NewInt(0)
sig.R.SetBytes(sigOut.R)
sig.S.SetBytes(sigOut.S)
sigRaw, err := asn1.Marshal(sig)
if err != nil {
return nil, fmt.Errorf("failed marshaling ECDSA signature: %w", err)
}
return sigRaw, nil
case msg := <-p.in:
raw, routing, err := msg.WireBytes()
if err != nil {
p.logger.Warnf("Received error when serializing message: %v", err)
continue
}
p.logger.Debugf("%s Got message from %s", p.id.Id, routing.From.Id)
ok, err := party.UpdateFromBytes(raw, routing.From, routing.IsBroadcast)
if !ok {
p.logger.Warnf("Received error when updating party: %v", err.Error())
continue
}
}
}
}
func (p *party) KeyGen(ctx context.Context) ([]byte, error) {
p.logger.Debugf("Starting DKG")
defer p.logger.Debugf("Finished DKG")
defer close(p.closeChan)
preParamGenTimeout := defaultSafePrimeGenTimeout
deadline, deadlineExists := ctx.Deadline()
if deadlineExists {
preParamGenTimeout = deadline.Sub(time.Now())
}
preParams, err := keygen.GeneratePreParams(preParamGenTimeout)
if err != nil {
panic(err)
}
end := make(chan *keygen.LocalPartySaveData, 1)
party := keygen.NewLocalParty(p.params, p.out, end, *preParams)
var endWG sync.WaitGroup
endWG.Add(1)
go func() {
defer endWG.Done()
err := party.Start()
if err != nil {
p.logger.Errorf("Failed generating key: %v", err)
}
}()
defer endWG.Wait()
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("DKG timed out: %w", ctx.Err())
case dkgOut := <-end:
dkgRawOut, err := json.Marshal(*dkgOut)
if err != nil {
return nil, fmt.Errorf("failed serializing DKG output: %w", err)
}
return dkgRawOut, nil
case msg := <-p.in:
raw, routing, err := msg.WireBytes()
if err != nil {
p.logger.Warnf("Received error when serializing message: %v", err)
continue
}
p.logger.Debugf("%s Got message from %s", p.id.Id, routing.From.Id)
ok, err := party.UpdateFromBytes(raw, routing.From, routing.IsBroadcast)
if !ok {
p.logger.Warnf("Received error when updating party: %v", err.Error())
continue
}
}
}
}
func (p *party) sendMessages() {
for {
select {
case <-p.closeChan:
return
case msg := <-p.out:
msgBytes, routing, err := msg.WireBytes()
if err != nil {
p.logger.Warnf("Failed marshaling message: %v", err)
continue
}
if routing.IsBroadcast {
p.sendMsg(msgBytes, routing.IsBroadcast, 0)
} else {
for _, to := range msg.GetTo() {
p.sendMsg(msgBytes, routing.IsBroadcast, uint16(big.NewInt(0).SetBytes(to.Key).Uint64()))
}
}
}
}
}
// hashToInt is taken as-is from the Go ECDSA standard library
func hashToInt(hash []byte, c elliptic.Curve) *big.Int {
orderBits := c.Params().N.BitLen()
orderBytes := (orderBits + 7) / 8
if len(hash) > orderBytes {
hash = hash[:orderBytes]
}
ret := new(big.Int).SetBytes(hash)
excess := len(hash)*8 - orderBits
if excess > 0 {
ret.Rsh(ret, uint(excess))
}
return ret
}
func digest(in []byte) []byte {
h := sha256.New()
h.Write(in)
return h.Sum(nil)
}