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
use crate::handlers::{wire, ACK_NAMESPACE};
use commonware_codec::{Decode, Encode};
use commonware_cryptography::{
bls12381::{
dkg::{self},
primitives::{poly, variant::MinSig},
},
PublicKey,
};
use commonware_macros::select;
use commonware_p2p::{Receiver, Recipients, Sender};
use commonware_runtime::{spawn_cell, Clock, ContextCell, Handle, Spawner};
use commonware_utils::set::Ordered;
use std::{
collections::{BTreeMap, HashSet},
time::Duration,
};
use tracing::{debug, info, warn};
pub struct Arbiter<E: Clock + Spawner, C: PublicKey> {
context: ContextCell<E>,
dkg_frequency: Duration,
dkg_phase_timeout: Duration,
contributors: Ordered<C>,
}
/// Implementation of a "trusted arbiter" that tracks commitments,
/// acknowledgements, and complaints during a DKG round.
impl<E: Clock + Spawner, C: PublicKey> Arbiter<E, C> {
pub fn new(
context: E,
dkg_frequency: Duration,
dkg_phase_timeout: Duration,
contributors: Ordered<C>,
) -> Self {
Self {
context: ContextCell::new(context),
dkg_frequency,
dkg_phase_timeout,
contributors,
}
}
async fn run_round(
&self,
round: u64,
previous: Option<poly::Public<MinSig>>,
sender: &mut impl Sender<PublicKey = C>,
receiver: &mut impl Receiver<PublicKey = C>,
) -> (Option<poly::Public<MinSig>>, HashSet<C>) {
// Create a new round
let start = self.context.current();
let timeout = start + 4 * self.dkg_phase_timeout; // start -> commitment/share -> ack -> arbiter
// Send round start message to contributors
if let Some(previous) = &previous {
info!(round, public=?previous, "starting reshare");
} else {
info!(round, "starting key generation");
}
sender
.send(
Recipients::All,
wire::Dkg::<C::Signature> {
round,
payload: wire::Payload::Start {
group: previous.clone(),
},
}
.encode()
.into(),
true,
)
.await
.expect("failed to send start message");
// Collect commitments
let mut arbiter = dkg::Arbiter::<_, MinSig>::new(
previous,
self.contributors.clone(),
self.contributors.clone(),
1,
);
loop {
select! {
_ = self.context.sleep_until(timeout) => {
warn!(round, "timed out waiting for commitments");
break
},
result = receiver.recv() => {
match result {
Ok((peer, msg)) =>{
// Parse msg
let msg = match wire::Dkg::decode_cfg(msg, &self.contributors.len()) {
Ok(msg) => msg,
Err(_) => {
arbiter.disqualify(peer);
continue;
}
};
if msg.round != round {
continue;
}
let wire::Payload::Commitment { commitment, acks, reveals } = msg.payload else {
// Useless message from previous step
continue;
};
// Validate the signature of each ack
if !acks.iter().all(|ack| {
self.contributors.get(ack.player as usize).map(|signer| {
ack.verify::<MinSig, _>(ACK_NAMESPACE, signer, round, &peer, &commitment)
}).unwrap_or(false)
}) {
arbiter.disqualify(peer);
continue;
}
// Check dealer commitment
//
// Any faults here will be considered as a disqualification.
let ack_indices: Vec<u32> = acks.iter().map(|a| a.player).collect();
if let Err(e) = arbiter.commitment(peer.clone(), commitment, ack_indices, reveals) {
warn!(round, error = ?e, ?peer, "failed to process commitment");
break;
}
// If we are ready, break
if arbiter.ready() {
debug!("collected sufficient commitments");
break;
}
},
Err(e) => {
warn!(round, error = ?e, "unable to read message");
break;
}
};
}
}
}
// Finalize
let (result, disqualified) = arbiter.finalize();
let output = match result {
Ok(output) => output,
Err(e) => {
warn!(round, error=?e, "unable to complete");
sender
.send(
Recipients::All,
wire::Dkg {
round,
payload: wire::Payload::<C::Signature>::Abort,
}
.encode()
.into(),
true,
)
.await
.expect("failed to send abort message");
return (None, disqualified);
}
};
// Send commitments and reveals to all contributors
info!(
round,
commitments = ?output.commitments.keys().map(|idx| self.contributors[*idx as usize].to_string()).collect::<Vec<_>>(),
disqualified = ?disqualified
.iter()
.map(|pk| pk.to_string())
.collect::<Vec<_>>(),
"selected commitments"
);
// Broadcast commitments
let mut commitments = BTreeMap::new();
for (dealer_idx, commitment) in output.commitments {
commitments.insert(dealer_idx, commitment);
}
let mut reveals = BTreeMap::new();
for (dealer_idx, shares) in output.reveals {
for share in shares {
reveals
.entry(share.index)
.or_insert_with(BTreeMap::new)
.insert(dealer_idx, share);
}
}
for (player_idx, player) in self.contributors.iter().enumerate() {
let reveals = reveals.remove(&(player_idx as u32)).unwrap_or_default();
sender
.send(
Recipients::One(player.clone()),
wire::Dkg {
round,
payload: wire::Payload::<C::Signature>::Success {
commitments: commitments.clone(),
reveals,
},
}
.encode()
.into(),
true,
)
.await
.expect("failed to send success message");
}
(Some(output.public), disqualified)
}
pub fn start(
mut self,
sender: impl Sender<PublicKey = C>,
receiver: impl Receiver<PublicKey = C>,
) -> Handle<()> {
spawn_cell!(self.context, self.run(sender, receiver).await)
}
async fn run(
self,
mut sender: impl Sender<PublicKey = C>,
mut receiver: impl Receiver<PublicKey = C>,
) {
let mut round = 0;
let mut previous = None;
loop {
let (public, disqualified) = self
.run_round(round, previous.clone(), &mut sender, &mut receiver)
.await;
// Log round results
match public {
Some(public) => {
info!(
round,
?public,
disqualified = ?disqualified.into_iter().map(|pk| pk.to_string()).collect::<Vec<_>>(),
"round complete"
);
// Only update previous if the round was successful
previous = Some(public);
}
None => {
info!(round, disqualified = ?disqualified.into_iter().map(|pk| pk.to_string()).collect::<Vec<_>>(), "round aborted");
}
}
// Update state
round += 1;
// Wait for next round
self.context.sleep(self.dkg_frequency).await;
}
}
}