-
Notifications
You must be signed in to change notification settings - Fork 7
/
frost.go
419 lines (336 loc) · 11.3 KB
/
frost.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2024 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 implements FROST, the Flexible Round-Optimized Schnorr Threshold (FROST) signing protocol.
package frost
import (
"errors"
"fmt"
"math/big"
"slices"
"github.com/bytemare/ecc"
"github.com/bytemare/secret-sharing/keys"
"github.com/bytemare/frost/internal"
)
// Ciphersuite identifies the group and hash function to use for FROST.
type Ciphersuite byte
const (
// Default and recommended ciphersuite for FROST.
Default = Ristretto255
// Ristretto255 uses Ristretto255 and SHA-512. This ciphersuite is recommended.
Ristretto255 = Ciphersuite(ecc.Ristretto255Sha512)
// Ed448 uses Edwards448 and SHAKE256, producing Ed448-compliant signatures as specified in RFC8032.
// ed448 = Ciphersuite(2).
// P256 uses P-256 and SHA-256.
P256 = Ciphersuite(ecc.P256Sha256)
// P384 uses P-384 and SHA-384.
P384 = Ciphersuite(ecc.P384Sha384)
// P521 uses P-521 and SHA-512.
P521 = Ciphersuite(ecc.P521Sha512)
// Ed25519 uses Edwards25519 and SHA-512, producing Ed25519-compliant signatures as specified in RFC8032.
Ed25519 = Ciphersuite(ecc.Edwards25519Sha512)
// Secp256k1 uses Secp256k1 and SHA-256.
Secp256k1 = Ciphersuite(ecc.Secp256k1Sha256)
)
// Available returns whether the selected ciphersuite is available.
func (c Ciphersuite) Available() bool {
switch c {
case Ed25519, Ristretto255, P256, P384, P521, Secp256k1:
return true
default:
return false
}
}
// Group returns the elliptic curve group used in the ciphersuite.
func (c Ciphersuite) Group() ecc.Group {
if !c.Available() {
return 0
}
return ecc.Group(c)
}
// 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.ID); 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.ID, 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.Public()); err != nil {
return err
}
if !c.VerificationKey.Equal(keyShare.VerificationKey) {
return errKeyShareNotMatch
}
if keyShare.Secret == nil || keyShare.Secret.IsZero() {
return errInvalidSecretKey
}
if !c.group.Base().Multiply(keyShare.Secret).Equal(keyShare.PublicKey) {
return errInvalidKeyShare
}
pk := c.getSignerPubKey(keyShare.ID)
if pk == nil {
return errInvalidKeyShareUnknownID
}
if !pk.Equal(keyShare.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
if _, exists := idSet[pks.ID]; exists {
return fmt.Errorf("found duplicate identifier for signer %d", pks.ID)
}
// Verify whether the public key has duplicates
s := string(pks.PublicKey.Encode())
if id, exists := pkSet[s]; exists {
return fmt.Errorf("found duplicate public keys for signers %d and %d", pks.ID, id)
}
pkSet[s] = pks.ID
idSet[pks.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.ID == 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)
}
// SchnorrChallenge computes the per-message SchnorrChallenge.
func SchnorrChallenge(g ecc.Group, msg []byte, r, pk *ecc.Element) *ecc.Scalar {
return internal.H2(g, internal.Concatenate(r.Encode(), pk.Encode(), msg))
}
// VerifySignature returns whether the signature of the message is valid under publicKey.
func VerifySignature(c Ciphersuite, message []byte, signature *Signature, publicKey *ecc.Element) error {
g := c.Group()
if g == 0 {
return internal.ErrInvalidCiphersuite
}
ch := SchnorrChallenge(g, message, signature.R, publicKey)
r := signature.R.Copy().Add(publicKey.Copy().Multiply(ch))
l := g.Base().Multiply(signature.Z)
// Clear the cofactor for Edwards25519.
if g == ecc.Edwards25519Sha512 {
cofactor := ecc.Edwards25519Sha512.NewScalar().SetUInt64(8)
l.Multiply(cofactor)
r.Multiply(cofactor)
}
if !l.Equal(r) {
return errInvalidSignature
}
return nil
}
// NewPublicKeyShare returns a PublicKeyShare from separately encoded key material. To deserialize a byte string
// produced by the PublicKeyShare.Encode() method, use the PublicKeyShare.Decode() method.
func NewPublicKeyShare(c Ciphersuite, id uint16, signerPublicKey []byte) (*keys.PublicKeyShare, error) {
if !c.Available() {
return nil, internal.ErrInvalidCiphersuite
}
if id == 0 {
return nil, internal.ErrIdentifierIs0
}
g := c.Group()
pk := g.NewElement()
if err := pk.Decode(signerPublicKey); err != nil {
return nil, fmt.Errorf("could not decode public share: %w", err)
}
return &keys.PublicKeyShare{
PublicKey: pk,
ID: id,
Group: g,
VssCommitment: nil,
}, nil
}
// NewKeyShare returns a KeyShare from separately encoded key material. To deserialize a byte string produced by the
// KeyShare.Encode() method, use the KeyShare.Decode() method.
func NewKeyShare(
c Ciphersuite,
id uint16,
secretShare, signerPublicKey, verificationKey []byte,
) (*keys.KeyShare, error) {
pks, err := NewPublicKeyShare(c, id, signerPublicKey)
if err != nil {
return nil, err
}
g := c.Group()
s := g.NewScalar()
if err = s.Decode(secretShare); err != nil {
return nil, fmt.Errorf("could not decode secret share: %w", err)
}
vpk := g.Base().Multiply(s)
if !vpk.Equal(pks.PublicKey) {
return nil, errInvalidKeyShare
}
gpk := g.NewElement()
if err = gpk.Decode(verificationKey); err != nil {
return nil, fmt.Errorf("could not decode the group public key: %w", err)
}
return &keys.KeyShare{
Secret: s,
VerificationKey: gpk,
PublicKeyShare: *pks,
}, nil
}