-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconfiguration.go
More file actions
281 lines (227 loc) · 7.55 KB
/
Copy pathconfiguration.go
File metadata and controls
281 lines (227 loc) · 7.55 KB
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
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2026 Daniel Bourdrez. All Rights Reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree or at
// https://spdx.org/licenses/MIT.html
package frost
import (
"errors"
"fmt"
"math/big"
"slices"
"github.com/bytemare/ecc"
"github.com/bytemare/secret-sharing/keys"
"github.com/bytemare/frost/internal"
)
// Configuration holds the Configuration for a signing session.
type Configuration struct {
VerificationKey *ecc.Element `json:"verificationKey"`
SignerPublicKeyShares []*keys.PublicKeyShare `json:"signerPublicKeyShares"`
Threshold uint16 `json:"threshold"`
MaxSigners uint16 `json:"maxSigners"`
Ciphersuite Ciphersuite `json:"ciphersuite"`
group ecc.Group
verified bool
keysVerified bool
}
var (
errInvalidThresholdParameter = errors.New("threshold is 0 or higher than maxSigners")
errInvalidMaxSignersOrder = errors.New("maxSigners is higher than group order")
errInvalidNumberOfPublicKeys = errors.New("invalid number of public keys (lower than threshold or above maximum)")
errKeyShareNotMatch = errors.New(
"the key share's group public key does not match the one in the configuration",
)
errInvalidSecretKey = errors.New("provided key share has invalid secret key")
errKeyShareNil = errors.New("provided key share is nil")
errInvalidKeyShare = errors.New("provided key share has non-matching secret and public keys")
errInvalidKeyShareUnknownID = errors.New(
"provided key share has no registered signer identifier in the configuration",
)
errPublicKeyShareNoMatch = errors.New(
"provided key share has a different public key than the one registered for that signer in the configuration",
)
)
// Init verifies whether the configuration's components are valid, in which case it initializes internal values, or
// returns an error otherwise.
func (c *Configuration) Init() error {
if !c.verified {
if err := c.verifyConfiguration(); err != nil {
return err
}
}
if !c.keysVerified {
if err := c.verifySignerPublicKeyShares(); err != nil {
return err
}
}
return nil
}
// Signer returns a new participant of the protocol instantiated from the Configuration and the signer's key share.
func (c *Configuration) Signer(keyShare *keys.KeyShare) (*Signer, error) {
if !c.verified || !c.keysVerified {
if err := c.Init(); err != nil {
return nil, err
}
}
if err := c.ValidateKeyShare(keyShare); err != nil {
return nil, err
}
return &Signer{
KeyShare: keyShare,
LambdaRegistry: make(internal.LambdaRegistry),
NonceCommitments: make(map[uint64]*Nonce),
HidingRandom: nil,
BindingRandom: nil,
Configuration: c,
}, nil
}
// ValidatePublicKeyShare returns an error if they PublicKeyShare has invalid components or properties that not
// compatible with the configuration.
func (c *Configuration) ValidatePublicKeyShare(pks *keys.PublicKeyShare) error {
if !c.verified {
if err := c.verifyConfiguration(); err != nil {
return err
}
}
if pks == nil {
return errors.New("public key share is nil")
}
if pks.Group() != c.group {
return fmt.Errorf("key share has invalid group parameter, want %s got %d", c.group, pks.Group())
}
if err := c.validateIdentifier(pks.Identifier()); err != nil {
return fmt.Errorf("invalid identifier for public key share, the %w", err)
}
if err := c.validateGroupElement(pks.PublicKey()); err != nil {
return fmt.Errorf("invalid public key for participant %d, the key %w", pks.Identifier(), err)
}
return nil
}
// ValidateKeyShare returns an error if they KeyShare has invalid components or properties that not compatible with the
// configuration.
func (c *Configuration) ValidateKeyShare(keyShare *keys.KeyShare) error {
if !c.verified || !c.keysVerified {
if err := c.Init(); err != nil {
return err
}
}
if keyShare == nil {
return errKeyShareNil
}
if err := c.ValidatePublicKeyShare(keyShare.PublicKeyShare()); err != nil {
return err
}
if !c.VerificationKey.Equal(keyShare.VerificationKey()) {
return errKeyShareNotMatch
}
secret := keyShare.SecretKey()
if secret == nil || secret.IsZero() {
return errInvalidSecretKey
}
publicKey := keyShare.PublicKey()
if !c.group.Base().Multiply(secret).Equal(publicKey) {
return errInvalidKeyShare
}
pk := c.getSignerPubKey(keyShare.Identifier())
if pk == nil {
return errInvalidKeyShareUnknownID
}
if !pk.Equal(publicKey) {
return errPublicKeyShareNoMatch
}
return nil
}
func (c *Configuration) verifySignerPublicKeyShares() error {
length := len(c.SignerPublicKeyShares)
if length < int(c.Threshold) || length > int(c.MaxSigners) {
return errInvalidNumberOfPublicKeys
}
// Sets to detect duplicates.
pkSet := make(map[string]uint16, len(c.SignerPublicKeyShares))
idSet := make(map[uint16]struct{}, len(c.SignerPublicKeyShares))
for i, pks := range c.SignerPublicKeyShares {
if pks == nil {
return fmt.Errorf("empty public key share at index %d", i)
}
if err := c.ValidatePublicKeyShare(pks); err != nil {
return err
}
// Verify whether the ID has duplicates
id := pks.Identifier()
if _, exists := idSet[id]; exists {
return fmt.Errorf("found duplicate identifier for signer %d", id)
}
// Verify whether the public key has duplicates
s := string(pks.PublicKey().Encode())
if id2, exists := pkSet[s]; exists {
return fmt.Errorf("found duplicate public keys for signers %d and %d", pks.Identifier(), id2)
}
pkSet[s] = id
idSet[id] = struct{}{}
}
c.keysVerified = true
return nil
}
func getOrder(g ecc.Group) *big.Int {
bytes := g.Order()
if g == ecc.Ristretto255Sha512 || g == ecc.Edwards25519Sha512 {
slices.Reverse(bytes)
}
return big.NewInt(0).SetBytes(bytes)
}
func (c *Configuration) verifyConfiguration() error {
if !c.Ciphersuite.Available() {
return internal.ErrInvalidCiphersuite
}
g := ecc.Group(c.Ciphersuite)
if c.Threshold == 0 || c.Threshold > c.MaxSigners {
return errInvalidThresholdParameter
}
order := getOrder(g)
maxSigners := new(big.Int).SetUint64(uint64(c.MaxSigners))
// This is unlikely to happen, as the usual Group orders cannot be represented in a uint64.
// Only a new, unregistered Group would make it fail here.
if order.Cmp(maxSigners) != 1 {
return errInvalidMaxSignersOrder
}
if err := c.validateGroupElement(c.VerificationKey); err != nil {
return fmt.Errorf("invalid group public key, the key %w", err)
}
c.group = g
c.verified = true
return nil
}
func (c *Configuration) getSignerPubKey(id uint16) *ecc.Element {
for _, pks := range c.SignerPublicKeyShares {
if pks.Identifier() == id {
return pks.PublicKey()
}
}
return nil
}
func (c *Configuration) validateIdentifier(id uint16) error {
switch {
case id == 0:
return internal.ErrIdentifierIs0
case id > c.MaxSigners:
return fmt.Errorf("identifier %d is above authorized range [1:%d]", id, c.MaxSigners)
}
return nil
}
func (c *Configuration) validateGroupElement(e *ecc.Element) error {
switch {
case e == nil:
return errors.New("is nil")
case e.IsIdentity():
return errors.New("is the identity element")
case ecc.Group(c.Ciphersuite).Base().Equal(e):
return errors.New("is the group generator (base element)")
}
return nil
}
func (c *Configuration) challenge(lambda *ecc.Scalar, message []byte, groupCommitment *ecc.Element) *ecc.Scalar {
chall := SchnorrChallenge(c.group, message, groupCommitment, c.VerificationKey)
return chall.Multiply(lambda)
}