-
Notifications
You must be signed in to change notification settings - Fork 105
/
types.go
250 lines (211 loc) · 6.32 KB
/
types.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
package exported
import (
"crypto/ecdsa"
"fmt"
"strings"
"github.com/btcsuite/btcd/btcec/v2"
ec "github.com/btcsuite/btcd/btcec/v2/ecdsa"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/axelarnetwork/axelar-core/utils"
)
// key id length range bounds dictated by tofnd
const (
KeyIDLengthMin = 4
KeyIDLengthMax = 256
)
// KeyID ensures a correctly formatted tss key ID
type KeyID string
// Validate returns an error, if the key ID is too short or too long
func (id KeyID) Validate() error {
if err := utils.ValidateString(string(id)); err != nil {
return sdkerrors.Wrap(err, "invalid key id")
}
if len(id) < KeyIDLengthMin || len(id) > KeyIDLengthMax {
return fmt.Errorf("key id length %d not in range [%d,%d]", len(id), KeyIDLengthMin, KeyIDLengthMax)
}
return nil
}
// KeyRoleFromSimpleStr creates a KeyRole from string
func KeyRoleFromSimpleStr(str string) (KeyRole, error) {
switch strings.ToLower(str) {
case MasterKey.SimpleString():
return MasterKey, nil
case SecondaryKey.SimpleString():
return SecondaryKey, nil
case ExternalKey.SimpleString():
return ExternalKey, nil
default:
return -1, fmt.Errorf("invalid key role %s", str)
}
}
// SimpleString returns a human-readable string
func (x KeyRole) SimpleString() string {
switch x {
case MasterKey:
return "master"
case SecondaryKey:
return "secondary"
case ExternalKey:
return "external"
default:
return "unknown"
}
}
// Validate validates the KeyRole
func (x KeyRole) Validate() error {
switch x {
case MasterKey, SecondaryKey, ExternalKey:
return nil
default:
return fmt.Errorf("invalid key role %d", x)
}
}
// KeyShareDistributionPolicyFromSimpleStr creates a KeyShareDistributionPolicy from string
func KeyShareDistributionPolicyFromSimpleStr(str string) (KeyShareDistributionPolicy, error) {
switch strings.ToLower(str) {
case WeightedByStake.SimpleString():
return WeightedByStake, nil
case OnePerValidator.SimpleString():
return OnePerValidator, nil
default:
return -1, fmt.Errorf("invalid key share distribution policy %s", str)
}
}
// SimpleString returns a human-readable string
func (r KeyShareDistributionPolicy) SimpleString() string {
switch r {
case WeightedByStake:
return "weighted-by-stake"
case OnePerValidator:
return "one-per-validator"
default:
return "unknown"
}
}
// Validate validates the KeyShareDistributionPolicy
func (r KeyShareDistributionPolicy) Validate() error {
switch r {
case WeightedByStake, OnePerValidator:
return nil
default:
return fmt.Errorf("invalid key share distribution policy %d", r)
}
}
// Validate validates the KeyRequirement
func (m KeyRequirement) Validate() error {
if err := m.KeyRole.Validate(); err != nil {
return err
}
if err := m.KeyType.Validate(); err != nil {
return err
}
if m.MinKeygenThreshold.Validate() != nil {
return fmt.Errorf("MinKeygenThreshold must be <=1 and >0")
}
if m.SafetyThreshold.Validate() != nil {
return fmt.Errorf("SafetyThreshold must be <=1 and >0")
}
if err := m.KeyShareDistributionPolicy.Validate(); err != nil {
return err
}
if m.KeygenVotingThreshold.Validate() != nil {
return fmt.Errorf("KeygenVotingThreshold must be <=1 and >0")
}
if m.SignVotingThreshold.Validate() != nil {
return fmt.Errorf("SignVotingThreshold must be <=1 and >0")
}
if m.KeygenTimeout <= 0 {
return fmt.Errorf("KeygenTimeout must be >0")
}
if m.SignTimeout <= 0 {
return fmt.Errorf("SignTimeout must be >0")
}
if m.MinTotalShareCount <= 0 || m.MinTotalShareCount > m.MaxTotalShareCount {
return fmt.Errorf("must satisfy 0 < MinTotalShareCount <= MaxTotalShareCount")
}
for totalShareCount := m.MinTotalShareCount; totalShareCount <= m.MaxTotalShareCount; totalShareCount++ {
corruptionThreshold := ComputeAbsCorruptionThreshold(m.SafetyThreshold, sdk.NewInt(totalShareCount))
if corruptionThreshold < 0 || corruptionThreshold >= totalShareCount {
return fmt.Errorf("invalid safety threshold [%s], and corruption threshold [%d] when total share count is [%d]",
m.SafetyThreshold.String(),
corruptionThreshold,
totalShareCount,
)
}
minSigningThreshold := utils.NewThreshold(corruptionThreshold+1, totalShareCount)
if m.KeygenVotingThreshold.LT(minSigningThreshold) {
return fmt.Errorf("invalid keygen voting threshold [%s], safety threshold [%s], and corruption threshold [%d] when total share count is [%d]",
m.KeygenVotingThreshold.String(),
m.SafetyThreshold.String(),
corruptionThreshold,
totalShareCount,
)
}
if m.SignVotingThreshold.GT(minSigningThreshold) {
return fmt.Errorf("invalid sign voting threshold [%s], safety threshold [%s] and corruption threshold [%d] when total share count is [%d]",
m.SignVotingThreshold.String(),
m.SafetyThreshold.String(),
corruptionThreshold,
totalShareCount,
)
}
}
return nil
}
// ComputeAbsCorruptionThreshold returns absolute corruption threshold to be used by tss.
// (threshold + 1) shares are required to sign
func ComputeAbsCorruptionThreshold(safetyThreshold utils.Threshold, totalShareCount sdk.Int) int64 {
return sdk.NewDec(totalShareCount.Int64()).MulInt64(safetyThreshold.Numerator).QuoInt64(safetyThreshold.Denominator).Ceil().TruncateInt().Int64() - 1
}
// SimpleString returns a human-readable string
func (x KeyType) SimpleString() string {
switch x {
case Threshold:
return "threshold"
case Multisig:
return "multisig"
case None:
return "none"
default:
return "unknown"
}
}
// Validate validates the KeyType
func (x KeyType) Validate() error {
switch x {
case Threshold, Multisig, None:
return nil
default:
return fmt.Errorf("invalid key type %d", x)
}
}
// Validate validates the SigKeyPair
func (m SigKeyPair) Validate() error {
_, err := btcec.ParsePubKey(m.PubKey)
if err != nil {
return err
}
_, err = ec.ParseDERSignature(m.Signature)
if err != nil {
return err
}
return nil
}
// GetKey returns the public key of the SigKeyPair
func (m SigKeyPair) GetKey() (ecdsa.PublicKey, error) {
btcecKey, err := btcec.ParsePubKey(m.PubKey)
if err != nil {
return ecdsa.PublicKey{}, err
}
pk := btcecKey.ToECDSA()
return *pk, nil
}
// GetSig returns the signature of the SigKeyPair
func (m SigKeyPair) GetSig() (ec.Signature, error) {
sig, err := ec.ParseDERSignature(m.Signature)
if err != nil {
return ec.Signature{}, err
}
return *sig, nil
}