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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! Test for Refreshing shares
#![cfg(feature = "serialization")]
use rand_core::{CryptoRng, RngCore};
use crate::keys::dkg::{round1, round2};
use crate::keys::generate_with_dealer;
use crate::keys::refresh::{
compute_refreshing_shares, refresh_dkg_part1, refresh_dkg_part2, refresh_share,
};
use crate::{self as frost};
use crate::{
keys::{KeyPackage, PublicKeyPackage, SecretShare},
Ciphersuite, Error, Identifier, Signature, VerifyingKey,
};
use crate::tests::ciphersuite_generic::check_part3_different_participants;
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use super::ciphersuite_generic::check_sign;
/// We want to test that recovered share matches the original share
pub fn check_refresh_shares_with_dealer<C: Ciphersuite, R: RngCore + CryptoRng>(mut rng: R) {
// Compute shares
////////////////////////////////////////////////////////////////////////////
// Old Key generation
////////////////////////////////////////////////////////////////////////////
const MAX_SIGNERS: u16 = 5;
const MIN_SIGNERS: u16 = 3;
let (old_shares, pub_key_package) = generate_with_dealer(
MAX_SIGNERS,
MIN_SIGNERS,
frost::keys::IdentifierList::Default,
&mut rng,
)
.unwrap();
let mut old_key_packages: BTreeMap<frost::Identifier<C>, KeyPackage<C>> = BTreeMap::new();
for (k, v) in old_shares {
let key_package = KeyPackage::try_from(v).unwrap();
old_key_packages.insert(k, key_package);
}
////////////////////////////////////////////////////////////////////////////
// New Key generation
////////////////////////////////////////////////////////////////////////////
// Signer 2 will be removed and Signers 1, 3, 4 & 5 will remain
let remaining_ids = vec![
Identifier::try_from(1).unwrap(),
Identifier::try_from(3).unwrap(),
Identifier::try_from(4).unwrap(),
Identifier::try_from(5).unwrap(),
];
// Trusted Dealer generates zero keys and new public key package
let (zero_shares, new_pub_key_package) =
compute_refreshing_shares(pub_key_package, &remaining_ids, &mut rng).unwrap();
// Simulate serialization / deserialization to ensure it works
let new_pub_key_package =
frost::keys::PublicKeyPackage::deserialize(&new_pub_key_package.serialize().unwrap())
.unwrap();
// Each participant refreshes their share
let mut new_shares = BTreeMap::new();
for i in 0..remaining_ids.len() {
let identifier = remaining_ids[i];
let current_share = &old_key_packages[&identifier];
// Do a serialization roundtrip to simulate real usage
let zero_share = SecretShare::deserialize(&zero_shares[i].serialize().unwrap()).unwrap();
let new_share = refresh_share(zero_share, current_share).unwrap();
new_shares.insert(identifier, new_share);
}
let mut key_packages: BTreeMap<frost::Identifier<C>, KeyPackage<C>> = BTreeMap::new();
for (k, v) in new_shares {
// Simulate serialization / deserialization to ensure it works
let v = KeyPackage::<C>::deserialize(&v.serialize().unwrap()).unwrap();
key_packages.insert(k, v);
}
check_sign(MIN_SIGNERS, key_packages, rng, new_pub_key_package).unwrap();
}
/// We want to check that shares are refreshed with valid signers
pub fn check_refresh_shares_with_dealer_fails_with_invalid_signers<
C: Ciphersuite,
R: RngCore + CryptoRng,
>(
identifiers: &[Identifier<C>],
error: Error<C>,
mut rng: R,
) {
let (_old_shares, pub_key_package) =
generate_with_dealer::<C, R>(5, 2, frost::keys::IdentifierList::Default, &mut rng).unwrap();
let out = compute_refreshing_shares(pub_key_package, identifiers, &mut rng);
assert!(out.is_err());
assert!(out == Err(error))
}
/// We want to test that refresh share fails if the identifiers don't match the
/// identifiers in the public key package
pub fn check_refresh_shares_with_dealer_fails_with_invalid_public_key_package<
C: Ciphersuite,
R: RngCore + CryptoRng,
>(
mut rng: R,
) {
// Compute shares
////////////////////////////////////////////////////////////////////////////
// Old Key generation
////////////////////////////////////////////////////////////////////////////
const MAX_SIGNERS: u16 = 3;
const MIN_SIGNERS: u16 = 2;
let (old_shares, incorrect_pub_key_package) = generate_with_dealer(
MAX_SIGNERS,
MIN_SIGNERS,
frost::keys::IdentifierList::Default,
&mut rng,
)
.unwrap();
let mut old_key_packages: BTreeMap<frost::Identifier<C>, KeyPackage<C>> = BTreeMap::new();
for (k, v) in old_shares {
let key_package = KeyPackage::try_from(v).unwrap();
old_key_packages.insert(k, key_package);
}
////////////////////////////////////////////////////////////////////////////
// New Key generation
////////////////////////////////////////////////////////////////////////////
// Signer 2 will be removed and Signers 1, 3, 4 & 5 will remain
let remaining_ids = vec![
Identifier::try_from(1).unwrap(),
Identifier::try_from(3).unwrap(),
Identifier::try_from(4).unwrap(),
Identifier::try_from(5).unwrap(),
];
// Trusted Dealer generates zero keys and new public key package
let e =
compute_refreshing_shares(incorrect_pub_key_package, &remaining_ids, &mut rng).unwrap_err();
assert_eq!(e, Error::UnknownIdentifier)
}
/// Check serialisation
#[cfg(feature = "serialization")]
pub fn check_refresh_shares_with_dealer_serialisation<C: Ciphersuite, R: RngCore + CryptoRng>(
mut rng: R,
) {
////////////////////////////////////////////////////////////////////////////
// Old Key generation
////////////////////////////////////////////////////////////////////////////
const MAX_SIGNERS: u16 = 5;
const MIN_SIGNERS: u16 = 3;
let (_old_shares, pub_key_package) = generate_with_dealer(
MAX_SIGNERS,
MIN_SIGNERS,
frost::keys::IdentifierList::Default,
&mut rng,
)
.unwrap();
////////////////////////////////////////////////////////////////////////////
// New Key generation
//
// Zero key is calculated by trusted dealer
// Participant 2 will be removed and Participants 1, 3, 4 & 5 will remain
////////////////////////////////////////////////////////////////////////////
let remaining_ids = vec![
Identifier::try_from(1).unwrap(),
Identifier::try_from(3).unwrap(),
Identifier::try_from(4).unwrap(),
Identifier::try_from(5).unwrap(),
];
let (zero_shares, new_pub_key_package) =
compute_refreshing_shares(pub_key_package, &remaining_ids, &mut rng).unwrap();
// Trusted dealer serialises zero shares and key package
let zero_shares_serialised = SecretShare::<C>::serialize(&zero_shares[0]);
assert!(zero_shares_serialised.is_ok());
let new_pub_key_package_serialised = PublicKeyPackage::<C>::serialize(&new_pub_key_package);
assert!(new_pub_key_package_serialised.is_ok());
// Participant 1 deserialises zero share and key package
let zero_share = SecretShare::<C>::deserialize(&zero_shares_serialised.unwrap());
assert!(zero_share.is_ok());
let new_pub_key_package =
PublicKeyPackage::<C>::deserialize(&new_pub_key_package_serialised.unwrap());
assert!(new_pub_key_package.is_ok());
// Participant 1 checks Key Package can be created from Secret Share
let key_package = KeyPackage::<C>::try_from(zero_share.unwrap());
assert!(key_package.is_ok());
}
/// Test FROST signing with DKG with a Ciphersuite.
pub fn check_refresh_shares_with_dkg<C: Ciphersuite + PartialEq, R: RngCore + CryptoRng>(
mut rng: R,
) -> (Vec<u8>, Signature<C>, VerifyingKey<C>)
where
C::Group: core::cmp::PartialEq,
{
////////////////////////////////////////////////////////////////////////////
// Old Key generation
////////////////////////////////////////////////////////////////////////////
let old_max_signers = 5;
let min_signers = 3;
let (old_shares, pub_key_package) = generate_with_dealer(
old_max_signers,
min_signers,
frost::keys::IdentifierList::Default,
&mut rng,
)
.unwrap();
let mut old_key_packages: BTreeMap<frost::Identifier<C>, KeyPackage<C>> = BTreeMap::new();
for (k, v) in old_shares {
let key_package = KeyPackage::try_from(v).unwrap();
old_key_packages.insert(k, key_package);
}
////////////////////////////////////////////////////////////////////////////
// Key generation, Round 1
////////////////////////////////////////////////////////////////////////////
let max_signers = 4;
let min_signers = 3;
let remaining_ids = vec![
Identifier::try_from(4).unwrap(),
Identifier::try_from(2).unwrap(),
Identifier::try_from(3).unwrap(),
Identifier::try_from(1).unwrap(),
];
// Keep track of each participant's round 1 secret package.
// In practice each participant will keep its copy; no one
// will have all the participant's packages.
let mut round1_secret_packages: BTreeMap<
frost::Identifier<C>,
frost::keys::dkg::round1::SecretPackage<C>,
> = BTreeMap::new();
// Keep track of all round 1 packages sent to the given participant.
// This is used to simulate the broadcast; in practice the packages
// will be sent through some communication channel.
let mut received_round1_packages: BTreeMap<
frost::Identifier<C>,
BTreeMap<frost::Identifier<C>, frost::keys::dkg::round1::Package<C>>,
> = BTreeMap::new();
// For each participant, perform the first part of the DKG protocol.
// In practice, each participant will perform this on their own environments.
for participant_identifier in remaining_ids.clone() {
let (round1_secret_package, round1_package) =
refresh_dkg_part1(participant_identifier, max_signers, min_signers, &mut rng).unwrap();
// Simulate serialization / deserialization to ensure it works
let round1_secret_package = frost::keys::dkg::round1::SecretPackage::<C>::deserialize(
&round1_secret_package.serialize().unwrap(),
)
.unwrap();
let round1_package = frost::keys::dkg::round1::Package::<C>::deserialize(
&round1_package.serialize().unwrap(),
)
.unwrap();
// Store the participant's secret package for later use.
// In practice each participant will store it in their own environment.
round1_secret_packages.insert(
participant_identifier,
// Serialization roundtrip to simulate storage for later
round1::SecretPackage::deserialize(&round1_secret_package.serialize().unwrap())
.unwrap(),
);
// "Send" the round 1 package to all other participants. In this
// test this is simulated using a BTreeMap; in practice this will be
// sent through some communication channel.
for receiver_participant_identifier in remaining_ids.clone() {
if receiver_participant_identifier == participant_identifier {
continue;
}
received_round1_packages
.entry(receiver_participant_identifier)
.or_default()
.insert(
participant_identifier,
// Serialization roundtrip to simulate communication
round1::Package::deserialize(&round1_package.serialize().unwrap()).unwrap(),
);
}
}
////////////////////////////////////////////////////////////////////////////
// Key generation, Round 2
////////////////////////////////////////////////////////////////////////////
// Keep track of each participant's round 2 secret package.
// In practice each participant will keep its copy; no one
// will have all the participant's packages.
let mut round2_secret_packages = BTreeMap::new();
// Keep track of all round 2 packages sent to the given participant.
// This is used to simulate the broadcast; in practice the packages
// will be sent through some communication channel.
let mut received_round2_packages = BTreeMap::new();
// For each participant, perform the second part of the DKG protocol.
// In practice, each participant will perform this on their own environments.
for participant_identifier in remaining_ids.clone() {
let round1_secret_package = round1_secret_packages
.remove(&participant_identifier)
.unwrap();
let round1_packages = &received_round1_packages[&participant_identifier];
let (round2_secret_package, round2_packages) =
refresh_dkg_part2(round1_secret_package, round1_packages).expect("should work");
// Simulate serialization / deserialization to ensure it works
let round2_secret_package = frost::keys::dkg::round2::SecretPackage::<C>::deserialize(
&round2_secret_package.serialize().unwrap(),
)
.unwrap();
// Store the participant's secret package for later use.
// In practice each participant will store it in their own environment.
round2_secret_packages.insert(
participant_identifier,
// Serialization roundtrip to simulate storage for later
round2::SecretPackage::deserialize(&round2_secret_package.serialize().unwrap())
.unwrap(),
);
// "Send" the round 2 package to all other participants. In this
// test this is simulated using a BTreeMap; in practice this will be
// sent through some communication channel.
// Note that, in contrast to the previous part, here each other participant
// gets its own specific package.
for (receiver_identifier, round2_package) in round2_packages {
// Simulate serialization / deserialization to ensure it works
let round2_package = frost::keys::dkg::round2::Package::<C>::deserialize(
&round2_package.serialize().unwrap(),
)
.unwrap();
received_round2_packages
.entry(receiver_identifier)
.or_insert_with(BTreeMap::new)
.insert(
participant_identifier,
// Serialization roundtrip to simulate communication
round2::Package::deserialize(&round2_package.serialize().unwrap()).unwrap(),
);
}
}
////////////////////////////////////////////////////////////////////////////
// Key generation, final computation
////////////////////////////////////////////////////////////////////////////
// Keep track of each participant's long-lived key package.
// In practice each participant will keep its copy; no one
// will have all the participant's packages.
let mut key_packages = BTreeMap::new();
// Map of the verifying share of each participant.
// Used by the signing test that follows.
let mut verifying_shares = BTreeMap::new();
// The group public key, used by the signing test that follows.
let mut verifying_key = None;
// For each participant, store the set of verifying keys they have computed.
// This is used to check if the set is correct (the same) for all participants.
// In practice, if there is a Coordinator, only they need to store the set.
// If there is not, then all candidates must store their own sets.
// The verifying keys are used to verify the signature shares produced
// for each signature before being aggregated.
let mut pubkey_packages_by_participant = BTreeMap::new();
check_part3_different_participants(
max_signers,
round2_secret_packages.clone(),
received_round1_packages.clone(),
received_round2_packages.clone(),
);
// For each participant, this is where they refresh their shares
// In practice, each participant will perform this on their own environments.
for participant_identifier in remaining_ids.clone() {
let (key_package, pubkey_package_for_participant) =
frost::keys::refresh::refresh_dkg_shares(
&round2_secret_packages[&participant_identifier],
&received_round1_packages[&participant_identifier],
&received_round2_packages[&participant_identifier],
pub_key_package.clone(),
old_key_packages[&participant_identifier].clone(),
)
.unwrap();
// Simulate serialization / deserialization to ensure it works
let key_package = KeyPackage::deserialize(&key_package.serialize().unwrap()).unwrap();
let pubkey_package_for_participant = frost::keys::PublicKeyPackage::deserialize(
&pubkey_package_for_participant.serialize().unwrap(),
)
.unwrap();
verifying_shares.insert(participant_identifier, key_package.verifying_share);
// Test if all verifying_key are equal
if let Some(previous_verifying_key) = verifying_key {
assert_eq!(previous_verifying_key, key_package.verifying_key)
}
verifying_key = Some(key_package.verifying_key);
key_packages.insert(participant_identifier, key_package);
pubkey_packages_by_participant
.insert(participant_identifier, pubkey_package_for_participant);
}
// Test if the set of verifying keys is correct for all participants.
for verifying_keys_for_participant in pubkey_packages_by_participant.values() {
assert!(verifying_keys_for_participant.verifying_shares == verifying_shares);
}
let pubkeys = pubkey_packages_by_participant
.first_key_value()
.unwrap()
.1
.clone();
// Simulate serialization / deserialization to ensure it works
let pubkeys =
frost::keys::PublicKeyPackage::deserialize(&pubkeys.serialize().unwrap()).unwrap();
// Proceed with the signing test.
check_sign(min_signers, key_packages, rng, pubkeys).unwrap()
}
/// Test FROST signing with DKG with a Ciphersuite, using a smaller
/// threshold than the original one.
pub fn check_refresh_shares_with_dkg_smaller_threshold<
C: Ciphersuite + PartialEq,
R: RngCore + CryptoRng,
>(
mut rng: R,
) where
C::Group: core::cmp::PartialEq,
{
////////////////////////////////////////////////////////////////////////////
// Old Key generation
////////////////////////////////////////////////////////////////////////////
let old_max_signers = 5;
let min_signers = 3;
let (old_shares, pub_key_package) = generate_with_dealer(
old_max_signers,
min_signers,
frost::keys::IdentifierList::Default,
&mut rng,
)
.unwrap();
let mut old_key_packages: BTreeMap<frost::Identifier<C>, KeyPackage<C>> = BTreeMap::new();
for (k, v) in old_shares {
let key_package = KeyPackage::try_from(v).unwrap();
old_key_packages.insert(k, key_package);
}
////////////////////////////////////////////////////////////////////////////
// Key generation, Round 1
////////////////////////////////////////////////////////////////////////////
let max_signers = 4;
// Use a smaller threshold than the original
let min_signers = 2;
let remaining_ids = vec![
Identifier::try_from(4).unwrap(),
Identifier::try_from(2).unwrap(),
Identifier::try_from(3).unwrap(),
Identifier::try_from(1).unwrap(),
];
// Keep track of each participant's round 1 secret package.
// In practice each participant will keep its copy; no one
// will have all the participant's packages.
let mut round1_secret_packages: BTreeMap<
frost::Identifier<C>,
frost::keys::dkg::round1::SecretPackage<C>,
> = BTreeMap::new();
// Keep track of all round 1 packages sent to the given participant.
// This is used to simulate the broadcast; in practice the packages
// will be sent through some communication channel.
let mut received_round1_packages: BTreeMap<
frost::Identifier<C>,
BTreeMap<frost::Identifier<C>, frost::keys::dkg::round1::Package<C>>,
> = BTreeMap::new();
// For each participant, perform the first part of the DKG protocol.
// In practice, each participant will perform this on their own environments.
for participant_identifier in remaining_ids.clone() {
let (round1_secret_package, round1_package) =
refresh_dkg_part1(participant_identifier, max_signers, min_signers, &mut rng).unwrap();
// Store the participant's secret package for later use.
// In practice each participant will store it in their own environment.
round1_secret_packages.insert(participant_identifier, round1_secret_package);
// "Send" the round 1 package to all other participants. In this
// test this is simulated using a BTreeMap; in practice this will be
// sent through some communication channel.
for receiver_participant_identifier in remaining_ids.clone() {
if receiver_participant_identifier == participant_identifier {
continue;
}
received_round1_packages
.entry(receiver_participant_identifier)
.or_default()
.insert(participant_identifier, round1_package.clone());
}
}
////////////////////////////////////////////////////////////////////////////
// Key generation, Round 2
////////////////////////////////////////////////////////////////////////////
// Keep track of each participant's round 2 secret package.
// In practice each participant will keep its copy; no one
// will have all the participant's packages.
let mut round2_secret_packages = BTreeMap::new();
// Keep track of all round 2 packages sent to the given participant.
// This is used to simulate the broadcast; in practice the packages
// will be sent through some communication channel.
let mut received_round2_packages = BTreeMap::new();
// For each participant, perform the second part of the DKG protocol.
// In practice, each participant will perform this on their own environments.
for participant_identifier in remaining_ids.clone() {
let round1_secret_package = round1_secret_packages
.remove(&participant_identifier)
.unwrap();
let round1_packages = &received_round1_packages[&participant_identifier];
let (round2_secret_package, round2_packages) =
refresh_dkg_part2(round1_secret_package, round1_packages).expect("should work");
// Store the participant's secret package for later use.
// In practice each participant will store it in their own environment.
round2_secret_packages.insert(participant_identifier, round2_secret_package);
// "Send" the round 2 package to all other participants. In this
// test this is simulated using a BTreeMap; in practice this will be
// sent through some communication channel.
// Note that, in contrast to the previous part, here each other participant
// gets its own specific package.
for (receiver_identifier, round2_package) in round2_packages {
received_round2_packages
.entry(receiver_identifier)
.or_insert_with(BTreeMap::new)
.insert(participant_identifier, round2_package);
}
}
////////////////////////////////////////////////////////////////////////////
// Key generation, final computation
////////////////////////////////////////////////////////////////////////////
// For each participant, this is where they refresh their shares
// In practice, each participant will perform this on their own environments.
let mut results = Vec::new();
for participant_identifier in remaining_ids.clone() {
results.push(frost::keys::refresh::refresh_dkg_shares(
&round2_secret_packages[&participant_identifier],
&received_round1_packages[&participant_identifier],
&received_round2_packages[&participant_identifier],
pub_key_package.clone(),
old_key_packages[&participant_identifier].clone(),
));
}
assert!(results
.iter()
.all(|r| matches!(r, Err(Error::InvalidMinSigners))));
}