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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Orchestrator of the DKG/Resharing procedure.
//!
//! # Deployment Options
//!
//! ## Recommended: All Contributors Run the Arbiter
//!
//! Each contributor should run its own instance of the arbiter over a replicated
//! log (deterministic order of events across all contributors) of commitments,
//! acknowledgements, complaints, and resolutions. All correct contributors, when given
//! the same log, will arrive at the same result (will recover the same group polynomial
//! and a share that can generate partial signatures over it). Using a replicated log allows
//! us to provide both reliable broadcast (all honest contributors see all messages from
//! all other honest contributors) and to enforce a "timeout" (using log index) for each
//! phase of DKG/Resharing (needed to support a `2f + 1` threshold in this construction).
//!
//! ## Trusted Alternative: Standalone Process
//!
//! It is possible to run the arbiter as a standalone process that contributors
//! must trust to track commitments, acks, complaints, and reveals. A rogue arbiter
//! could request reveals from all dealers for all participants and recover the group
//! secret key.
//!
//! _For an example of this approach, refer to <https://docs.rs/commonware-vrf>._
//!
//! # Disqualification on Attributable Faults
//!
//! Submitting duplicate and/or unnecessary information (i.e. a dealer submitting the same commitment twice
//! or submitting an acknowledgement for a disqualified dealer) will throw an error but not disqualify the
//! contributor. It may not be possible for contributors to know the latest state of the arbiter when submitting
//! information and penalizing them for this is not helpful (i.e. an acknowledgement may be inflight when another
//! contributor submits a valid complaint).
//!
//! Submitting invalid information (invalid commitment) or refusing to submit required information (not sending a commitment)
//! qualifies as an attributable fault that disqualifies a dealer/recipient from a round of DKG/Resharing. A developer
//! can additionally handle such a fault as they see fit (may warrant additional punishment).
//!
//! # Warning
//!
//! It is up to the developer to authorize interaction with the arbiter. This is purposely
//! not provided by the Arbiter because this authorization function is highly dependent on
//! the context in which the contributor is being used.

use super::utils;
use crate::bls12381::{
    dkg::{ops, Error},
    primitives::{group::Share, poly},
};
use crate::PublicKey;
use std::collections::{HashMap, HashSet};

/// Gather commitments from all contributors.
pub struct P0 {
    threshold: u32,
    previous: Option<poly::Public>,
    concurrency: usize,

    dealers: Vec<PublicKey>,
    dealers_ordered: HashMap<PublicKey, u32>,

    recipients: Vec<PublicKey>,
    recipients_ordered: HashMap<PublicKey, u32>,

    commitments: HashMap<PublicKey, poly::Public>,
    disqualified: HashSet<PublicKey>,
}

impl P0 {
    /// Create a new abiter for a DKG/Resharing procedure.
    pub fn new(
        threshold: u32,
        previous: Option<poly::Public>,
        mut dealers: Vec<PublicKey>,
        mut recipients: Vec<PublicKey>,
        concurrency: usize,
    ) -> Self {
        dealers.sort();
        let dealers_ordered = dealers
            .iter()
            .enumerate()
            .map(|(i, pk)| (pk.clone(), i as u32))
            .collect();
        recipients.sort();
        let recipients_ordered = recipients
            .iter()
            .enumerate()
            .map(|(i, pk)| (pk.clone(), i as u32))
            .collect();
        dealers.sort();
        Self {
            threshold,
            previous,
            concurrency,
            dealers,
            dealers_ordered,
            recipients,
            recipients_ordered,
            commitments: HashMap::new(),
            disqualified: HashSet::new(),
        }
    }

    /// Required number of commitments to continue procedure.
    pub fn required(&self) -> u32 {
        match &self.previous {
            Some(previous) => previous.required(),
            None => self.threshold,
        }
    }

    /// Disqualify a contributor from the DKG for external reason (i.e. sending invalid messages).
    pub fn disqualify(&mut self, contributor: PublicKey) {
        self.disqualified.insert(contributor);
    }

    /// Verify and track a commitment from a dealer.
    pub fn commitment(&mut self, dealer: PublicKey, commitment: poly::Public) -> Result<(), Error> {
        // Check if contributor is disqualified
        if self.disqualified.contains(&dealer) {
            return Err(Error::ContributorDisqualified);
        }

        // Find the index of the contributor
        let idx = match self.dealers_ordered.get(&dealer) {
            Some(idx) => *idx,
            None => return Err(Error::ContirbutorInvalid),
        };

        // Check if commitment already exists
        if self.commitments.contains_key(&dealer) {
            return Err(Error::DuplicateCommitment);
        }

        // Verify the commitment is valid
        match ops::verify_commitment(self.previous.as_ref(), idx, &commitment, self.threshold) {
            Ok(()) => {
                self.commitments.insert(dealer, commitment);
            }
            Err(e) => {
                self.disqualified.insert(dealer);
                return Err(e);
            }
        }
        Ok(())
    }

    /// If there exist at least `required()` commitments, proceed to `P1`.
    pub fn finalize(mut self) -> (Option<P1>, HashSet<PublicKey>) {
        // Disqualify any contributors who did not submit a commitment
        for contributor in self.dealers.iter() {
            if !self.commitments.contains_key(contributor) {
                self.disqualified.insert(contributor.clone());
            }
        }

        // Ensure we have enough commitments to proceed
        if self.commitments.len() < self.required() as usize {
            return (None, self.disqualified);
        }

        // Allow the arbiter to proceed
        (
            Some(P1 {
                threshold: self.threshold,
                previous: self.previous,
                concurrency: self.concurrency,
                dealers: self.dealers,
                dealers_ordered: self.dealers_ordered,
                recipients: self.recipients,
                recipients_ordered: self.recipients_ordered,
                commitments: self.commitments,
                disqualified: self.disqualified.clone(),
                acks: HashMap::new(),
            }),
            self.disqualified,
        )
    }
}

/// Collect acknowledgements and complaints from all recipients.
pub struct P1 {
    threshold: u32,
    previous: Option<poly::Public>,
    concurrency: usize,

    dealers: Vec<PublicKey>,
    dealers_ordered: HashMap<PublicKey, u32>,

    recipients: Vec<PublicKey>,
    recipients_ordered: HashMap<PublicKey, u32>,

    commitments: HashMap<PublicKey, poly::Public>,
    disqualified: HashSet<PublicKey>,

    acks: HashMap<u32, HashSet<u32>>,
}

/// Alias for a commitment from a dealer.
pub type Commitment = (u32, PublicKey, poly::Public);

/// Alias for a request for a missing share from a dealer
/// for a recipient.
pub type Request = (u32, u32);

impl P1 {
    /// Required number of commitments to continue procedure.
    pub fn required(&self) -> u32 {
        match &self.previous {
            Some(previous) => previous.required(),
            None => self.threshold,
        }
    }

    /// Disqualify a contributor from the DKG for external reason (i.e. sending invalid messages).
    pub fn disqualify(&mut self, contributor: PublicKey) {
        self.disqualified.insert(contributor);
    }

    /// Get the public key of a dealer.
    pub fn dealer(&self, dealer: u32) -> Option<PublicKey> {
        self.dealers.get(dealer as usize).cloned()
    }

    /// Return all tracked commitments.
    pub fn commitments(&self) -> Vec<Commitment> {
        self.commitments
            .iter()
            .filter_map(|(contributor, commitment)| {
                if self.disqualified.contains(contributor) {
                    return None;
                }

                let idx = self.dealers_ordered.get(contributor).unwrap();
                Some((*idx, contributor.clone(), commitment.clone()))
            })
            .collect()
    }

    /// Verify and track an acknowledgement from a recipient for a dealer.
    pub fn ack(&mut self, recipient: PublicKey, dealer: u32) -> Result<(), Error> {
        // Check if contributor is disqualified
        if self.disqualified.contains(&recipient) {
            return Err(Error::ContributorDisqualified);
        }

        // Find the index of the recipient
        let idx = match self.recipients_ordered.get(&recipient) {
            Some(idx) => *idx,
            None => return Err(Error::ContirbutorInvalid),
        };

        {
            // Get dealer that submitted commitment
            let dealer = self
                .dealers
                .get(dealer as usize)
                .ok_or(Error::DealerInvalid)?;

            // Check if commitment is still valid
            if self.disqualified.contains(dealer) | !self.commitments.contains_key(dealer) {
                // We don't disqualify the submitter here as this could have happened
                // without their knowledge.
                return Err(Error::CommitmentDisqualified);
            }

            // Ensure we aren't sending a self-ack
            if recipient == *dealer {
                self.disqualified.insert(recipient);
                return Err(Error::SelfAck);
            }
        }

        // Store ack
        let entry = self.acks.entry(dealer).or_default();
        if entry.contains(&idx) {
            Err(Error::DuplicateAck)
        } else {
            entry.insert(idx);
            Ok(())
        }
    }

    /// Verify a complaint from a recipient for a dealer.
    ///
    /// If a complaint is valid, the dealer is disqualified. If a
    /// complaint is invalid, the recipient is disqualified.
    pub fn complaint(
        &mut self,
        recipient: PublicKey,
        dealer: u32,
        share: &Share,
    ) -> Result<(), Error> {
        // Check if contributor is disqualified
        if self.disqualified.contains(&recipient) {
            return Err(Error::ContributorDisqualified);
        }

        // Find the index of the contributor
        let idx = match self.recipients_ordered.get(&recipient) {
            Some(idx) => *idx,
            None => return Err(Error::ContirbutorInvalid),
        };

        // Find the dealer that submitted the commitment
        let dealer_key = self
            .dealers
            .get(dealer as usize)
            .ok_or(Error::DealerInvalid)?;

        if dealer_key == &recipient {
            return Err(Error::SelfComplaint);
        }

        // Check if commitment is still valid
        if self.disqualified.contains(dealer_key) | !self.commitments.contains_key(dealer_key) {
            // We don't disqualify the submitter here as this could have happened
            // without their knowledge.
            return Err(Error::CommitmentDisqualified);
        }

        // Verify complaint
        let commitment = self.commitments.get(dealer_key).unwrap();
        match ops::verify_share(
            self.previous.as_ref(),
            dealer,
            commitment,
            self.threshold,
            idx,
            share,
        ) {
            Ok(_) => {
                // Submitting a useless complaint is a disqualifying offense
                self.disqualified.insert(recipient);
                Err(Error::ComplaintInvalid)
            }
            Err(_) => {
                // Disqualify the dealer
                self.disqualified.insert(dealer_key.clone());
                Ok(())
            }
        }
    }

    /// Request missing dealings.
    fn requests(&self) -> (HashMap<u32, HashSet<u32>>, Vec<Request>) {
        // Compute missing shares
        let mut missing_dealings = HashMap::new(); // dealer -> {recipient}
        let mut required_reveals = HashMap::new(); // recipient -> {dealer}
        for (dealer, acks) in self.acks.iter() {
            for (recipient, recipient_bytes) in self.recipients.iter().enumerate() {
                // Skip any recipients that are disqualified or have already acked
                let dealer_bytes = self.dealers[*dealer as usize].clone();
                if *recipient_bytes == dealer_bytes {
                    continue;
                }
                if self.disqualified.contains(recipient_bytes) {
                    continue;
                }
                let recipient = recipient as u32;
                if acks.contains(&recipient) {
                    continue;
                }

                // Add dealer -> recipient to tracker
                let entry = missing_dealings.entry(*dealer).or_insert_with(HashSet::new);
                entry.insert(recipient);
                let entry = required_reveals
                    .entry(recipient)
                    .or_insert_with(HashSet::new);
                entry.insert(*dealer);
            }
        }

        // Do not request reveals for recipients with more than `max_reveals` missing shares
        let max_reveals = utils::max_reveals(self.threshold);
        for (recipient, dealers) in required_reveals.iter() {
            if dealers.len() <= max_reveals as usize {
                continue;
            }

            // Remove recipient from missing dealings
            for dealer in dealers.iter() {
                if let Some(recipients) = missing_dealings.get_mut(dealer) {
                    recipients.remove(recipient);
                }
            }

            // We do not disqualify dealers that would otherwise need to distribute
            // shares because a particular recipient may just be refusing to participate.
        }

        // Construct required reveals
        let mut reveals = Vec::new();
        for (dealer, recipients) in missing_dealings.iter() {
            for recipient in recipients.iter() {
                reveals.push((*dealer, *recipient));
            }
        }
        (missing_dealings, reveals)
    }

    /// If there exist at least `threshold - 1` acks each for `required()` dealers, proceed to `P2`.
    pub fn finalize(mut self) -> (Option<(P2, Vec<Request>)>, HashSet<PublicKey>) {
        // Remove acks of disqualified recipients
        for acks in self.acks.values_mut() {
            for disqualified in self.disqualified.iter() {
                if let Some(idx) = self.recipients_ordered.get(disqualified) {
                    acks.remove(idx);
                }

                // If we can't find the recipient, it is probably a disqualified dealer.
            }
        }

        // Disqualify any commitments without at least `self.threshold` acks
        for dealer in self.commitments.keys() {
            let idx = self.dealers_ordered.get(dealer).unwrap();
            let acks = match self.acks.get(idx) {
                Some(acks) => acks.len(),
                None => 0,
            };

            // Check against `self.threshold - 1` because we don't send an
            // ack for ourselves.
            if acks < (self.threshold - 1) as usize {
                self.disqualified.insert(dealer.clone());
            }
        }

        // Remove disqualified commitments
        for disqualified in self.disqualified.iter() {
            self.commitments.remove(disqualified);
            self.acks
                .remove(self.dealers_ordered.get(disqualified).unwrap());
        }

        // If there are not `self.required()` dealings with at least `self.required()` acks,
        // we cannot proceed.
        if self.acks.len() < self.required() as usize {
            return (None, self.disqualified);
        }

        // Allow the arbiter to proceed
        let (missing_dealings, requests) = self.requests();
        (
            Some((
                P2 {
                    threshold: self.threshold,
                    previous: self.previous,
                    concurrency: self.concurrency,
                    dealers: self.dealers,
                    dealers_ordered: self.dealers_ordered,
                    commitments: self.commitments,
                    disqualified: self.disqualified.clone(),
                    acks: self.acks,
                    missing_dealings,
                    resolutions: HashMap::new(),
                },
                requests,
            )),
            self.disqualified,
        )
    }
}

/// Output of the DKG/Resharing procedure.
#[derive(Clone)]
pub struct Output {
    pub public: poly::Public,
    pub commitments: Vec<u32>,
    pub resolutions: HashMap<(u32, u32), Share>,
}

/// Collect missing shares (if any) and recover the public polynomial.
pub struct P2 {
    threshold: u32,
    previous: Option<poly::Public>,
    concurrency: usize,

    dealers: Vec<PublicKey>,
    dealers_ordered: HashMap<PublicKey, u32>,

    commitments: HashMap<PublicKey, poly::Public>,
    disqualified: HashSet<PublicKey>,

    acks: HashMap<u32, HashSet<u32>>,

    missing_dealings: HashMap<u32, HashSet<u32>>,
    resolutions: HashMap<(u32, u32), Share>,
}

impl P2 {
    /// Required number of commitments to continue procedure.
    pub fn required(&self) -> u32 {
        match &self.previous {
            Some(previous) => previous.required(),
            None => self.threshold,
        }
    }

    /// Disqualify a contributor from the DKG for external reason (i.e. sending invalid messages).
    pub fn disqualify(&mut self, contributor: PublicKey) {
        self.disqualified.insert(contributor);
    }

    /// Get the ID of a dealer.
    pub fn dealer(&self, dealer: &PublicKey) -> Option<u32> {
        self.dealers_ordered.get(dealer).cloned()
    }

    /// Return all tracked commitments.
    pub fn commitments(&self) -> Vec<Commitment> {
        self.commitments
            .iter()
            .filter_map(|(contributor, commitment)| {
                if self.disqualified.contains(contributor) {
                    return None;
                }

                let idx = self.dealers_ordered.get(contributor).unwrap();
                Some((*idx, contributor.clone(), commitment.clone()))
            })
            .collect()
    }

    /// Verify and track a forced resolution from a dealer.
    pub fn reveal(&mut self, dealer: PublicKey, share: Share) -> Result<(), Error> {
        // Check if contributor is disqualified
        if self.disqualified.contains(&dealer) {
            return Err(Error::ContributorDisqualified);
        }

        // Find the index of the contributor
        let idx = match self.dealers_ordered.get(&dealer) {
            Some(idx) => *idx,
            None => return Err(Error::ContirbutorInvalid),
        };

        // Check if commitment is still valid
        if self.disqualified.contains(&dealer) | !self.commitments.contains_key(&dealer) {
            // We don't disqualify the submitter here as this could have happened
            // without their knowledge.
            return Err(Error::CommitmentDisqualified);
        }

        // Verify share
        let commitment = self.commitments.get(&dealer).unwrap();
        if let Err(e) = ops::verify_share(
            self.previous.as_ref(),
            idx,
            commitment,
            self.threshold,
            share.index,
            &share,
        ) {
            // Disqualify the dealer
            self.disqualified.insert(dealer);
            return Err(e);
        }

        // Store that resolution was successful
        let missing = match self.missing_dealings.get_mut(&idx) {
            Some(missing) => missing,
            None => {
                return Err(Error::UnexpectedReveal);
            }
        };
        if missing.remove(&share.index) {
            self.resolutions.insert((idx, share.index), share);
            Ok(())
        } else {
            Err(Error::UnexpectedReveal)
        }
    }

    /// If there exist at least `threshold` resolutions for `required()` dealers, recover
    /// the group public polynomial.
    pub fn finalize(mut self) -> (Result<Output, Error>, HashSet<PublicKey>) {
        // Remove any dealers that did not distribute all required shares (may not be `n`)
        for (dealer, recipients) in &self.missing_dealings {
            if recipients.is_empty() {
                continue;
            }
            self.disqualified
                .insert(self.dealers[*dealer as usize].clone());
        }

        // Remove any disqualified dealers
        for disqualified in self.disqualified.iter() {
            self.commitments.remove(disqualified);
            self.acks
                .remove(self.dealers_ordered.get(disqualified).unwrap());
        }

        // Determine if we have enough resolutions
        let required = self.required();
        if self.acks.len() < required as usize {
            return (Err(Error::InsufficientDealings), self.disqualified);
        }

        // Recover group
        let public = match self.previous {
            Some(previous) => {
                let commitments = self
                    .commitments
                    .iter()
                    .map(|(contributor, commitment)| {
                        let idx = self.dealers_ordered.get(contributor).unwrap();
                        (*idx, commitment.clone())
                    })
                    .collect();
                match ops::recover_public(&previous, commitments, self.threshold, self.concurrency)
                {
                    Ok(public) => public,
                    Err(e) => return (Err(e), self.disqualified),
                }
            }
            None => {
                let commitments = self.commitments.values().cloned().collect();
                match ops::construct_public(commitments, required) {
                    Ok(public) => public,
                    Err(e) => return (Err(e), self.disqualified),
                }
            }
        };

        // Generate output
        let output = Output {
            public,
            commitments: self
                .commitments
                .keys()
                .map(|contributor| *self.dealers_ordered.get(contributor).unwrap())
                .collect(),
            resolutions: self.resolutions,
        };
        (Ok(output), self.disqualified)
    }
}