-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_party.go
executable file
·166 lines (140 loc) · 4.4 KB
/
local_party.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
// Copyright © 2019 Binance
//
// This file is part of Binance. The full Binance copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
package signing
import (
"errors"
"fmt"
"math/big"
"github.com/sodiumlabs/tss-lib/common"
"github.com/sodiumlabs/tss-lib/crypto"
cmt "github.com/sodiumlabs/tss-lib/crypto/commitments"
"github.com/sodiumlabs/tss-lib/eddsa/keygen"
"github.com/sodiumlabs/tss-lib/tss"
)
// Implements Party
// Implements Stringer
var _ tss.Party = (*LocalParty)(nil)
var _ fmt.Stringer = (*LocalParty)(nil)
type (
LocalParty struct {
*tss.BaseParty
params *tss.Parameters
keys keygen.LocalPartySaveData
temp localTempData
data SignatureData
// outbound messaging
out chan<- tss.Message
end chan<- *SignatureData
}
localMessageStore struct {
signRound1Messages,
signRound2Messages,
signRound3Messages []tss.ParsedMessage
}
localTempData struct {
localMessageStore
// temp data (thrown away after sign) / round 1
wi,
m,
ri *big.Int
pointRi *crypto.ECPoint
deCommit cmt.HashDeCommitment
// round 2
cjs []*big.Int
si *[32]byte
// round 3
r *big.Int
}
)
func NewLocalParty(
msg *big.Int,
params *tss.Parameters,
key keygen.LocalPartySaveData,
out chan<- tss.Message,
end chan<- *SignatureData,
) tss.Party {
partyCount := len(params.Parties().IDs())
p := &LocalParty{
BaseParty: new(tss.BaseParty),
params: params,
keys: keygen.BuildLocalSaveDataSubset(key, params.Parties().IDs()),
temp: localTempData{},
data: SignatureData{},
out: out,
end: end,
}
// msgs init
p.temp.signRound1Messages = make([]tss.ParsedMessage, partyCount)
p.temp.signRound2Messages = make([]tss.ParsedMessage, partyCount)
p.temp.signRound3Messages = make([]tss.ParsedMessage, partyCount)
// temp data init
p.temp.m = msg
p.temp.cjs = make([]*big.Int, partyCount)
return p
}
func (p *LocalParty) FirstRound() tss.Round {
return newRound1(p.params, &p.keys, &p.data, &p.temp, p.out, p.end)
}
func (p *LocalParty) Start() *tss.Error {
return tss.BaseStart(p, TaskName, func(round tss.Round) *tss.Error {
round1, ok := round.(*round1)
if !ok {
return round.WrapError(errors.New("unable to Start(). party is in an unexpected round"))
}
if err := round1.prepare(); err != nil {
return round.WrapError(err)
}
return nil
})
}
func (p *LocalParty) Update(msg tss.ParsedMessage) (ok bool, err *tss.Error) {
return tss.BaseUpdate(p, msg, TaskName)
}
func (p *LocalParty) UpdateFromBytes(wireBytes []byte, from *tss.PartyID, isBroadcast bool) (bool, *tss.Error) {
msg, err := tss.ParseWireMessage(wireBytes, from, isBroadcast)
if err != nil {
return false, p.WrapError(err)
}
return p.Update(msg)
}
func (p *LocalParty) ValidateMessage(msg tss.ParsedMessage) (bool, *tss.Error) {
if msg.GetFrom() == nil || !msg.GetFrom().ValidateBasic() {
return false, p.WrapError(fmt.Errorf("received msg with an invalid sender: %s", msg))
}
// check that the message's "from index" will fit into the array
if maxFromIdx := len(p.params.Parties().IDs()) - 1; maxFromIdx < msg.GetFrom().Index {
return false, p.WrapError(fmt.Errorf("received msg with a sender index too great (%d <= %d)",
maxFromIdx, msg.GetFrom().Index), msg.GetFrom())
}
return p.BaseParty.ValidateMessage(msg)
}
func (p *LocalParty) StoreMessage(msg tss.ParsedMessage) (bool, *tss.Error) {
// ValidateBasic is cheap; double-check the message here in case the public StoreMessage was called externally
if ok, err := p.ValidateMessage(msg); !ok || err != nil {
return ok, err
}
fromPIdx := msg.GetFrom().Index
// switch/case is necessary to store any messages beyond current round
// this does not handle message replays. we expect the caller to apply replay and spoofing protection.
switch msg.Content().(type) {
case *SignRound1Message:
p.temp.signRound1Messages[fromPIdx] = msg
case *SignRound2Message:
p.temp.signRound2Messages[fromPIdx] = msg
case *SignRound3Message:
p.temp.signRound3Messages[fromPIdx] = msg
default: // unrecognised message, just ignore!
common.Logger.Warnf("unrecognised message ignored: %v", msg)
return false, nil
}
return true, nil
}
func (p *LocalParty) PartyID() *tss.PartyID {
return p.params.PartyID()
}
func (p *LocalParty) String() string {
return fmt.Sprintf("id: %s, %s", p.PartyID(), p.BaseParty.String())
}