Skip to content

Commit a39e3af

Browse files
committed
core: add more serialization roundtrips to tests
1 parent 0f21162 commit a39e3af

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

frost-core/src/tests/ciphersuite_generic.rs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use rand_core::{CryptoRng, RngCore};
66

77
use crate as frost;
88
use crate::keys::dkg::{round1, round2};
9-
use crate::keys::SigningShare;
9+
use crate::keys::{SecretShare, SigningShare};
10+
use crate::round1::SigningNonces;
1011
use crate::round2::SignatureShare;
1112
use crate::{
1213
keys::PublicKeyPackage, Error, Field, Group, Identifier, Signature, SigningKey, SigningPackage,
@@ -26,6 +27,8 @@ pub fn check_zero_key_fails<C: Ciphersuite>() {
2627
/// Test share generation with a Ciphersuite
2728
pub fn check_share_generation<C: Ciphersuite, R: RngCore + CryptoRng>(mut rng: R) {
2829
let secret = crate::SigningKey::<C>::new(&mut rng);
30+
// Simulate serialization / deserialization to ensure it works
31+
let secret = SigningKey::deserialize(&secret.serialize()).unwrap();
2932

3033
let max_signers = 5;
3134
let min_signers = 3;
@@ -110,20 +113,28 @@ pub fn check_sign_with_dealer<C: Ciphersuite, R: RngCore + CryptoRng>(
110113

111114
let max_signers = 5;
112115
let min_signers = 3;
113-
let (shares, pubkeys) = frost::keys::generate_with_dealer(
116+
let (shares, pub_key_package) = frost::keys::generate_with_dealer(
114117
max_signers,
115118
min_signers,
116119
frost::keys::IdentifierList::Default,
117120
&mut rng,
118121
)
119122
.unwrap();
123+
// Simulate serialization / deserialization to ensure it works
124+
let pub_key_package =
125+
PublicKeyPackage::deserialize(&pub_key_package.serialize().unwrap()).unwrap();
120126

121127
// Verifies the secret shares from the dealer
122128
let mut key_packages: BTreeMap<frost::Identifier<C>, frost::keys::KeyPackage<C>> =
123129
BTreeMap::new();
124130

125131
for (k, v) in shares {
132+
// Simulate serialization / deserialization to ensure it works
133+
let v = SecretShare::<C>::deserialize(&v.serialize().unwrap()).unwrap();
126134
let key_package = frost::keys::KeyPackage::try_from(v).unwrap();
135+
// Simulate serialization / deserialization to ensure it works
136+
let key_package =
137+
frost::keys::KeyPackage::deserialize(&key_package.serialize().unwrap()).unwrap();
127138
key_packages.insert(k, key_package);
128139
}
129140
// Check if it fails with not enough signers. Usually this would return an
@@ -146,11 +157,11 @@ pub fn check_sign_with_dealer<C: Ciphersuite, R: RngCore + CryptoRng>(
146157
})
147158
.collect(),
148159
&mut rng,
149-
pubkeys.clone(),
160+
pub_key_package.clone(),
150161
);
151162
assert_eq!(r, Err(Error::InvalidSignature));
152163

153-
check_sign(min_signers, key_packages, rng, pubkeys).unwrap()
164+
check_sign(min_signers, key_packages, rng, pub_key_package).unwrap()
154165
}
155166

156167
/// Test FROST signing with trusted dealer fails with invalid numbers of signers.
@@ -205,7 +216,10 @@ pub fn check_sign<C: Ciphersuite + PartialEq, R: RngCore + CryptoRng>(
205216
// Round 1: generating nonces and signing commitments for each participant
206217
////////////////////////////////////////////////////////////////////////////
207218

208-
for participant_identifier in key_packages.keys().take(min_signers as usize).cloned() {
219+
for participant_identifier in key_packages.keys().take(min_signers as usize) {
220+
// Simulate serialization / deserialization to ensure it works
221+
let participant_identifier =
222+
Identifier::deserialize(&participant_identifier.serialize()).unwrap();
209223
// Generate one (1) nonce and one SigningCommitments instance for each
210224
// participant, up to _min_signers_.
211225
let (nonces, commitments) = frost::round1::commit(
@@ -215,6 +229,11 @@ pub fn check_sign<C: Ciphersuite + PartialEq, R: RngCore + CryptoRng>(
215229
.signing_share(),
216230
&mut rng,
217231
);
232+
// Simulate serialization / deserialization to ensure it works
233+
let nonces = SigningNonces::deserialize(&nonces.serialize().unwrap()).unwrap();
234+
let commitments =
235+
frost::round1::SigningCommitments::deserialize(&commitments.serialize().unwrap())
236+
.unwrap();
218237
nonces_map.insert(participant_identifier, nonces);
219238
commitments_map.insert(participant_identifier, commitments);
220239
}
@@ -225,6 +244,9 @@ pub fn check_sign<C: Ciphersuite + PartialEq, R: RngCore + CryptoRng>(
225244
let mut signature_shares = BTreeMap::new();
226245
let message = "message to sign".as_bytes();
227246
let signing_package = SigningPackage::new(commitments_map, message);
247+
// Simulate serialization / deserialization to ensure it works
248+
let signing_package =
249+
SigningPackage::deserialize(&signing_package.serialize().unwrap()).unwrap();
228250

229251
////////////////////////////////////////////////////////////////////////////
230252
// Round 2: each participant generates their signature share
@@ -243,6 +265,8 @@ pub fn check_sign<C: Ciphersuite + PartialEq, R: RngCore + CryptoRng>(
243265

244266
// Each participant generates their signature share.
245267
let signature_share = frost::round2::sign(&signing_package, nonces_to_use, key_package)?;
268+
// Simulate serialization / deserialization to ensure it works
269+
let signature_share = SignatureShare::deserialize(&signature_share.serialize()).unwrap();
246270
signature_shares.insert(*participant_identifier, signature_share);
247271
}
248272

@@ -267,6 +291,8 @@ pub fn check_sign<C: Ciphersuite + PartialEq, R: RngCore + CryptoRng>(
267291

268292
// Aggregate (also verifies the signature shares)
269293
let group_signature = frost::aggregate(&signing_package, &signature_shares, &pubkey_package)?;
294+
// Simulate serialization / deserialization to ensure it works
295+
let group_signature = Signature::deserialize(&group_signature.serialize().unwrap()).unwrap();
270296

271297
// Check that the threshold signature can be verified by the group public
272298
// key (the verification key).
@@ -453,6 +479,16 @@ where
453479
frost::keys::dkg::part1(participant_identifier, max_signers, min_signers, &mut rng)
454480
.unwrap();
455481

482+
// Simulate serialization / deserialization to ensure it works
483+
let round1_secret_package = frost::keys::dkg::round1::SecretPackage::<C>::deserialize(
484+
&round1_secret_package.serialize().unwrap(),
485+
)
486+
.unwrap();
487+
let round1_package = frost::keys::dkg::round1::Package::<C>::deserialize(
488+
&round1_package.serialize().unwrap(),
489+
)
490+
.unwrap();
491+
456492
// Store the participant's secret package for later use.
457493
// In practice each participant will store it in their own environment.
458494
round1_secret_packages.insert(
@@ -509,6 +545,12 @@ where
509545
let (round2_secret_package, round2_packages) =
510546
frost::keys::dkg::part2(round1_secret_package, round1_packages).expect("should work");
511547

548+
// Simulate serialization / deserialization to ensure it works
549+
let round2_secret_package = frost::keys::dkg::round2::SecretPackage::<C>::deserialize(
550+
&round2_secret_package.serialize().unwrap(),
551+
)
552+
.unwrap();
553+
512554
// Store the participant's secret package for later use.
513555
// In practice each participant will store it in their own environment.
514556
round2_secret_packages.insert(
@@ -524,6 +566,11 @@ where
524566
// Note that, in contrast to the previous part, here each other participant
525567
// gets its own specific package.
526568
for (receiver_identifier, round2_package) in round2_packages {
569+
// Simulate serialization / deserialization to ensure it works
570+
let round2_package = frost::keys::dkg::round2::Package::<C>::deserialize(
571+
&round2_package.serialize().unwrap(),
572+
)
573+
.unwrap();
527574
received_round2_packages
528575
.entry(receiver_identifier)
529576
.or_insert_with(BTreeMap::new)
@@ -574,6 +621,13 @@ where
574621
&received_round2_packages[&participant_identifier],
575622
)
576623
.unwrap();
624+
// Simulate serialization / deserialization to ensure it works
625+
let key_package =
626+
frost::keys::KeyPackage::deserialize(&key_package.serialize().unwrap()).unwrap();
627+
let pubkey_package_for_participant = frost::keys::PublicKeyPackage::deserialize(
628+
&pubkey_package_for_participant.serialize().unwrap(),
629+
)
630+
.unwrap();
577631
verifying_keys.insert(participant_identifier, key_package.verifying_share);
578632
// Test if all verifying_key are equal
579633
if let Some(previous_verifying_key) = verifying_key {
@@ -591,6 +645,9 @@ where
591645
}
592646

593647
let pubkeys = frost::keys::PublicKeyPackage::new(verifying_keys, verifying_key.unwrap());
648+
// Simulate serialization / deserialization to ensure it works
649+
let pubkeys =
650+
frost::keys::PublicKeyPackage::deserialize(&pubkeys.serialize().unwrap()).unwrap();
594651

595652
// Proceed with the signing test.
596653
check_sign(min_signers, key_packages, rng, pubkeys).unwrap()

frost-core/src/tests/refresh.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ pub fn check_refresh_shares_with_dealer<C: Ciphersuite, R: RngCore + CryptoRng>(
6969
&mut rng,
7070
)
7171
.unwrap();
72+
// Simulate serialization / deserialization to ensure it works
73+
let new_pub_key_package =
74+
frost::keys::PublicKeyPackage::deserialize(&new_pub_key_package.serialize().unwrap())
75+
.unwrap();
7276

7377
// Each participant refreshes their share
7478

@@ -79,14 +83,16 @@ pub fn check_refresh_shares_with_dealer<C: Ciphersuite, R: RngCore + CryptoRng>(
7983
let current_share = &old_key_packages[&identifier];
8084
// Do a serialization roundtrip to simulate real usage
8185
let zero_share = SecretShare::deserialize(&zero_shares[i].serialize().unwrap()).unwrap();
82-
let new_share = refresh_share(zero_share, current_share);
86+
let new_share = refresh_share(zero_share, current_share).unwrap();
8387
new_shares.insert(identifier, new_share);
8488
}
8589

8690
let mut key_packages: BTreeMap<frost::Identifier<C>, KeyPackage<C>> = BTreeMap::new();
8791

8892
for (k, v) in new_shares {
89-
key_packages.insert(k, v.unwrap());
93+
// Simulate serialization / deserialization to ensure it works
94+
let v = KeyPackage::<C>::deserialize(&v.serialize().unwrap()).unwrap();
95+
key_packages.insert(k, v);
9096
}
9197
check_sign(MIN_SIGNERS, key_packages, rng, new_pub_key_package).unwrap();
9298
}
@@ -389,6 +395,16 @@ where
389395
let (round1_secret_package, round1_package) =
390396
refresh_dkg_part_1(participant_identifier, max_signers, min_signers, &mut rng).unwrap();
391397

398+
// Simulate serialization / deserialization to ensure it works
399+
let round1_secret_package = frost::keys::dkg::round1::SecretPackage::<C>::deserialize(
400+
&round1_secret_package.serialize().unwrap(),
401+
)
402+
.unwrap();
403+
let round1_package = frost::keys::dkg::round1::Package::<C>::deserialize(
404+
&round1_package.serialize().unwrap(),
405+
)
406+
.unwrap();
407+
392408
// Store the participant's secret package for later use.
393409
// In practice each participant will store it in their own environment.
394410
round1_secret_packages.insert(
@@ -439,6 +455,12 @@ where
439455
let (round2_secret_package, round2_packages) =
440456
refresh_dkg_part2(round1_secret_package, round1_packages).expect("should work");
441457

458+
// Simulate serialization / deserialization to ensure it works
459+
let round2_secret_package = frost::keys::dkg::round2::SecretPackage::<C>::deserialize(
460+
&round2_secret_package.serialize().unwrap(),
461+
)
462+
.unwrap();
463+
442464
// Store the participant's secret package for later use.
443465
// In practice each participant will store it in their own environment.
444466
round2_secret_packages.insert(
@@ -454,6 +476,11 @@ where
454476
// Note that, in contrast to the previous part, here each other participant
455477
// gets its own specific package.
456478
for (receiver_identifier, round2_package) in round2_packages {
479+
// Simulate serialization / deserialization to ensure it works
480+
let round2_package = frost::keys::dkg::round2::Package::<C>::deserialize(
481+
&round2_package.serialize().unwrap(),
482+
)
483+
.unwrap();
457484
received_round2_packages
458485
.entry(receiver_identifier)
459486
.or_insert_with(BTreeMap::new)
@@ -506,6 +533,12 @@ where
506533
old_key_packages[&participant_identifier].clone(),
507534
)
508535
.unwrap();
536+
// Simulate serialization / deserialization to ensure it works
537+
let key_package = KeyPackage::deserialize(&key_package.serialize().unwrap()).unwrap();
538+
let pubkey_package_for_participant = frost::keys::PublicKeyPackage::deserialize(
539+
&pubkey_package_for_participant.serialize().unwrap(),
540+
)
541+
.unwrap();
509542
verifying_keys.insert(participant_identifier, key_package.verifying_share);
510543
// Test if all verifying_key are equal
511544
if let Some(previous_verifying_key) = verifying_key {
@@ -523,6 +556,9 @@ where
523556
}
524557

525558
let pubkeys = frost::keys::PublicKeyPackage::new(verifying_keys, verifying_key.unwrap());
559+
// Simulate serialization / deserialization to ensure it works
560+
let pubkeys =
561+
frost::keys::PublicKeyPackage::deserialize(&pubkeys.serialize().unwrap()).unwrap();
526562

527563
// Proceed with the signing test.
528564
check_sign(min_signers, key_packages, rng, pubkeys).unwrap()

0 commit comments

Comments
 (0)