|
| 1 | +# Distributed Key Generation (DKG) |
| 2 | + |
| 3 | +The DKG module supports generating FROST key shares in a distributed manner, |
| 4 | +without a trusted dealer. |
| 5 | + |
| 6 | +Before starting, each participant needs an unique identifier, which can be built from |
| 7 | +a `u16`. The process in which these identifiers are allocated is up to the application. |
| 8 | + |
| 9 | +The distributed key generation process has 3 parts, with 2 communication rounds |
| 10 | +between them, in which each participant needs to send a "package" to every other |
| 11 | +participant. In the first round, each participant sends the same package |
| 12 | +(a [`round1::Package`]) to every other. In the second round, each receiver gets |
| 13 | +their own package (a [`round2::Package`]). |
| 14 | + |
| 15 | +Between part 1 and 2, each participant needs to hold onto a [`round1::SecretPackage`] |
| 16 | +that MUST be kept secret. Between part 2 and 3, each participant needs to hold |
| 17 | +onto a [`round2::SecretPackage`]. |
| 18 | + |
| 19 | +After the third part, each participant will get a [`KeyPackage`] with their |
| 20 | +long-term secret share that must be kept secret, and a [`PublicKeyPackage`] |
| 21 | +that is public (and will be the same between all participants). With those |
| 22 | +they can proceed to sign messages with FROST. |
| 23 | + |
| 24 | + |
| 25 | +## Example |
| 26 | + |
| 27 | +```rust |
| 28 | +# // ANCHOR: dkg_import |
| 29 | +use rand::thread_rng; |
| 30 | +use std::collections::BTreeMap; |
| 31 | + |
| 32 | +use frost_secp256k1_tr as frost; |
| 33 | + |
| 34 | +let mut rng = thread_rng(); |
| 35 | + |
| 36 | +let max_signers = 5; |
| 37 | +let min_signers = 3; |
| 38 | +# // ANCHOR_END: dkg_import |
| 39 | + |
| 40 | +//////////////////////////////////////////////////////////////////////////// |
| 41 | +// Key generation, Round 1 |
| 42 | +//////////////////////////////////////////////////////////////////////////// |
| 43 | + |
| 44 | +// Keep track of each participant's round 1 secret package. |
| 45 | +// In practice each participant will keep its copy; no one |
| 46 | +// will have all the participant's packages. |
| 47 | +let mut round1_secret_packages = BTreeMap::new(); |
| 48 | + |
| 49 | +// Keep track of all round 1 packages sent to the given participant. |
| 50 | +// This is used to simulate the broadcast; in practice the packages |
| 51 | +// will be sent through some communication channel. |
| 52 | +let mut received_round1_packages = BTreeMap::new(); |
| 53 | + |
| 54 | +// For each participant, perform the first part of the DKG protocol. |
| 55 | +// In practice, each participant will perform this on their own environments. |
| 56 | +for participant_index in 1..=max_signers { |
| 57 | + let participant_identifier = participant_index.try_into().expect("should be nonzero"); |
| 58 | + # // ANCHOR: dkg_part1 |
| 59 | + let (round1_secret_package, round1_package) = frost::keys::dkg::part1( |
| 60 | + participant_identifier, |
| 61 | + max_signers, |
| 62 | + min_signers, |
| 63 | + &mut rng, |
| 64 | + )?; |
| 65 | + # // ANCHOR_END: dkg_part1 |
| 66 | + |
| 67 | + // Store the participant's secret package for later use. |
| 68 | + // In practice each participant will store it in their own environment. |
| 69 | + round1_secret_packages.insert(participant_identifier, round1_secret_package); |
| 70 | + |
| 71 | + // "Send" the round 1 package to all other participants. In this |
| 72 | + // test this is simulated using a BTreeMap; in practice this will be |
| 73 | + // sent through some communication channel. |
| 74 | + for receiver_participant_index in 1..=max_signers { |
| 75 | + if receiver_participant_index == participant_index { |
| 76 | + continue; |
| 77 | + } |
| 78 | + let receiver_participant_identifier: frost::Identifier = receiver_participant_index |
| 79 | + .try_into() |
| 80 | + .expect("should be nonzero"); |
| 81 | + received_round1_packages |
| 82 | + .entry(receiver_participant_identifier) |
| 83 | + .or_insert_with(BTreeMap::new) |
| 84 | + .insert(participant_identifier, round1_package.clone()); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +//////////////////////////////////////////////////////////////////////////// |
| 89 | +// Key generation, Round 2 |
| 90 | +//////////////////////////////////////////////////////////////////////////// |
| 91 | + |
| 92 | +// Keep track of each participant's round 2 secret package. |
| 93 | +// In practice each participant will keep its copy; no one |
| 94 | +// will have all the participant's packages. |
| 95 | +let mut round2_secret_packages = BTreeMap::new(); |
| 96 | + |
| 97 | +// Keep track of all round 2 packages sent to the given participant. |
| 98 | +// This is used to simulate the broadcast; in practice the packages |
| 99 | +// will be sent through some communication channel. |
| 100 | +let mut received_round2_packages = BTreeMap::new(); |
| 101 | + |
| 102 | +// For each participant, perform the second part of the DKG protocol. |
| 103 | +// In practice, each participant will perform this on their own environments. |
| 104 | +for participant_index in 1..=max_signers { |
| 105 | + let participant_identifier = participant_index.try_into().expect("should be nonzero"); |
| 106 | + let round1_secret_package = round1_secret_packages |
| 107 | + .remove(&participant_identifier) |
| 108 | + .unwrap(); |
| 109 | + let round1_packages = &received_round1_packages[&participant_identifier]; |
| 110 | + # // ANCHOR: dkg_part2 |
| 111 | + let (round2_secret_package, round2_packages) = |
| 112 | + frost::keys::dkg::part2(round1_secret_package, round1_packages)?; |
| 113 | + # // ANCHOR_END: dkg_part2 |
| 114 | + |
| 115 | + // Store the participant's secret package for later use. |
| 116 | + // In practice each participant will store it in their own environment. |
| 117 | + round2_secret_packages.insert(participant_identifier, round2_secret_package); |
| 118 | + |
| 119 | + // "Send" the round 2 package to all other participants. In this |
| 120 | + // test this is simulated using a BTreeMap; in practice this will be |
| 121 | + // sent through some communication channel. |
| 122 | + // Note that, in contrast to the previous part, here each other participant |
| 123 | + // gets its own specific package. |
| 124 | + for (receiver_identifier, round2_package) in round2_packages { |
| 125 | + received_round2_packages |
| 126 | + .entry(receiver_identifier) |
| 127 | + .or_insert_with(BTreeMap::new) |
| 128 | + .insert(participant_identifier, round2_package); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +//////////////////////////////////////////////////////////////////////////// |
| 133 | +// Key generation, final computation |
| 134 | +//////////////////////////////////////////////////////////////////////////// |
| 135 | + |
| 136 | +// Keep track of each participant's long-lived key package. |
| 137 | +// In practice each participant will keep its copy; no one |
| 138 | +// will have all the participant's packages. |
| 139 | +let mut key_packages = BTreeMap::new(); |
| 140 | + |
| 141 | +// Keep track of each participant's public key package. |
| 142 | +// In practice, if there is a Coordinator, only they need to store the set. |
| 143 | +// If there is not, then all candidates must store their own sets. |
| 144 | +// All participants will have the same exact public key package. |
| 145 | +let mut pubkey_packages = BTreeMap::new(); |
| 146 | + |
| 147 | +// For each participant, perform the third part of the DKG protocol. |
| 148 | +// In practice, each participant will perform this on their own environments. |
| 149 | +for participant_index in 1..=max_signers { |
| 150 | + let participant_identifier = participant_index.try_into().expect("should be nonzero"); |
| 151 | + let round2_secret_package = &round2_secret_packages[&participant_identifier]; |
| 152 | + let round1_packages = &received_round1_packages[&participant_identifier]; |
| 153 | + let round2_packages = &received_round2_packages[&participant_identifier]; |
| 154 | + # // ANCHOR: dkg_part3 |
| 155 | + let (key_package, pubkey_package) = frost::keys::dkg::part3( |
| 156 | + round2_secret_package, |
| 157 | + round1_packages, |
| 158 | + round2_packages, |
| 159 | + )?; |
| 160 | + # // ANCHOR_END: dkg_part3 |
| 161 | + key_packages.insert(participant_identifier, key_package); |
| 162 | + pubkey_packages.insert(participant_identifier, pubkey_package); |
| 163 | +} |
| 164 | + |
| 165 | +// With its own key package and the pubkey package, each participant can now proceed |
| 166 | +// to sign with FROST. |
| 167 | +# Ok::<(), frost::Error>(()) |
| 168 | +``` |
0 commit comments