dkg_pedpop/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![doc = include_str!("../README.md")]
3// This crate requires `dleq` which doesn't support no-std via std-shims
4// #![cfg_attr(not(feature = "std"), no_std)]
5
6use core::{marker::PhantomData, ops::Deref, fmt};
7use std::{
8  io::{self, Read, Write},
9  collections::HashMap,
10};
11
12use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
13use rand_core::{RngCore, CryptoRng};
14
15use transcript::{Transcript, RecommendedTranscript};
16
17use multiexp::{multiexp_vartime, BatchVerifier};
18use ciphersuite::{
19  group::{
20    ff::{Field, PrimeField},
21    Group, GroupEncoding,
22  },
23  Ciphersuite,
24};
25
26use schnorr::SchnorrSignature;
27
28pub use dkg::*;
29
30mod encryption;
31pub use encryption::*;
32
33#[cfg(test)]
34mod tests;
35
36/// Errors possible during key generation.
37#[derive(Clone, PartialEq, Eq, Debug, thiserror::Error)]
38pub enum PedPoPError<C: Ciphersuite> {
39  /// An incorrect amount of participants was provided.
40  #[error("incorrect amount of participants (expected {expected}, found {found})")]
41  IncorrectAmountOfParticipants { expected: usize, found: usize },
42  /// An invalid proof of knowledge was provided.
43  #[error("invalid proof of knowledge (participant {0})")]
44  InvalidCommitments(Participant),
45  /// An invalid DKG share was provided.
46  #[error("invalid share (participant {participant}, blame {blame})")]
47  InvalidShare { participant: Participant, blame: Option<EncryptionKeyProof<C>> },
48  /// A participant was missing.
49  #[error("missing participant {0}")]
50  MissingParticipant(Participant),
51  /// An error propagated from the underlying `dkg` crate.
52  #[error("error from dkg ({0})")]
53  DkgError(DkgError),
54}
55
56// Validate a map of values to have the expected included participants
57fn validate_map<T, C: Ciphersuite>(
58  map: &HashMap<Participant, T>,
59  included: &[Participant],
60  ours: Participant,
61) -> Result<(), PedPoPError<C>> {
62  if (map.len() + 1) != included.len() {
63    Err(PedPoPError::IncorrectAmountOfParticipants {
64      expected: included.len(),
65      found: map.len() + 1,
66    })?;
67  }
68
69  for included in included {
70    if *included == ours {
71      if map.contains_key(included) {
72        Err(PedPoPError::DkgError(DkgError::DuplicatedParticipant(*included)))?;
73      }
74      continue;
75    }
76
77    if !map.contains_key(included) {
78      Err(PedPoPError::MissingParticipant(*included))?;
79    }
80  }
81
82  Ok(())
83}
84
85#[allow(non_snake_case)]
86fn challenge<C: Ciphersuite>(context: [u8; 32], l: Participant, R: &[u8], Am: &[u8]) -> C::F {
87  let mut transcript = RecommendedTranscript::new(b"DKG PedPoP v0.2");
88  transcript.domain_separate(b"schnorr_proof_of_knowledge");
89  transcript.append_message(b"context", context);
90  transcript.append_message(b"participant", l.to_bytes());
91  transcript.append_message(b"nonce", R);
92  transcript.append_message(b"commitments", Am);
93  C::hash_to_F(b"DKG-PedPoP-proof_of_knowledge-0", &transcript.challenge(b"schnorr"))
94}
95
96/// The commitments message, intended to be broadcast to all other parties.
97///
98/// Every participant should only provide one set of commitments to all parties. If any
99/// participant sends multiple sets of commitments, they are faulty and should be presumed
100/// malicious. As this library does not handle networking, it is unable to detect if any
101/// participant is so faulty. That responsibility lies with the caller.
102#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
103pub struct Commitments<C: Ciphersuite> {
104  commitments: Vec<C::G>,
105  cached_msg: Vec<u8>,
106  sig: SchnorrSignature<C>,
107}
108
109impl<C: Ciphersuite> ReadWrite for Commitments<C> {
110  fn read<R: Read>(reader: &mut R, params: ThresholdParams) -> io::Result<Self> {
111    let mut commitments = Vec::with_capacity(params.t().into());
112    let mut cached_msg = vec![];
113
114    #[allow(non_snake_case)]
115    let mut read_G = || -> io::Result<C::G> {
116      let mut buf = <C::G as GroupEncoding>::Repr::default();
117      reader.read_exact(buf.as_mut())?;
118      let point = C::read_G(&mut buf.as_ref())?;
119      cached_msg.extend(buf.as_ref());
120      Ok(point)
121    };
122
123    for _ in 0 .. params.t() {
124      commitments.push(read_G()?);
125    }
126
127    Ok(Commitments { commitments, cached_msg, sig: SchnorrSignature::read(reader)? })
128  }
129
130  fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
131    writer.write_all(&self.cached_msg)?;
132    self.sig.write(writer)
133  }
134}
135
136/// State machine to begin the key generation protocol.
137#[derive(Debug, Zeroize)]
138pub struct KeyGenMachine<C: Ciphersuite> {
139  params: ThresholdParams,
140  context: [u8; 32],
141  _curve: PhantomData<C>,
142}
143
144impl<C: Ciphersuite> KeyGenMachine<C> {
145  /// Create a new machine to generate a key.
146  ///
147  /// The context should be unique among multisigs.
148  pub fn new(params: ThresholdParams, context: [u8; 32]) -> KeyGenMachine<C> {
149    KeyGenMachine { params, context, _curve: PhantomData }
150  }
151
152  /// Start generating a key according to the PedPoP DKG specification present in the FROST paper.
153  ///
154  /// Returns a commitments message to be sent to all parties over an authenticated channel. If any
155  /// party submits multiple sets of commitments, they MUST be treated as malicious.
156  pub fn generate_coefficients<R: RngCore + CryptoRng>(
157    self,
158    rng: &mut R,
159  ) -> (SecretShareMachine<C>, EncryptionKeyMessage<C, Commitments<C>>) {
160    let t = usize::from(self.params.t());
161    let mut coefficients = Vec::with_capacity(t);
162    let mut commitments = Vec::with_capacity(t);
163    let mut cached_msg = vec![];
164
165    for i in 0 .. t {
166      // Step 1: Generate t random values to form a polynomial with
167      coefficients.push(Zeroizing::new(C::random_nonzero_F(&mut *rng)));
168      // Step 3: Generate public commitments
169      commitments.push(C::generator() * coefficients[i].deref());
170      cached_msg.extend(commitments[i].to_bytes().as_ref());
171    }
172
173    // Step 2: Provide a proof of knowledge
174    let r = Zeroizing::new(C::random_nonzero_F(rng));
175    let nonce = C::generator() * r.deref();
176    let sig = SchnorrSignature::<C>::sign(
177      &coefficients[0],
178      // This could be deterministic as the PoK is a singleton never opened up to cooperative
179      // discussion
180      // There's no reason to spend the time and effort to make this deterministic besides a
181      // general obsession with canonicity and determinism though
182      r,
183      challenge::<C>(self.context, self.params.i(), nonce.to_bytes().as_ref(), &cached_msg),
184    );
185
186    // Additionally create an encryption mechanism to protect the secret shares
187    let encryption = Encryption::new(self.context, self.params.i(), rng);
188
189    // Step 4: Broadcast
190    let msg =
191      encryption.registration(Commitments { commitments: commitments.clone(), cached_msg, sig });
192    (
193      SecretShareMachine {
194        params: self.params,
195        context: self.context,
196        coefficients,
197        our_commitments: commitments,
198        encryption,
199      },
200      msg,
201    )
202  }
203}
204
205fn polynomial<F: PrimeField + Zeroize>(
206  coefficients: &[Zeroizing<F>],
207  l: Participant,
208) -> Zeroizing<F> {
209  let l = F::from(u64::from(u16::from(l)));
210  // This should never be reached since Participant is explicitly non-zero
211  assert!(l != F::ZERO, "zero participant passed to polynomial");
212  let mut share = Zeroizing::new(F::ZERO);
213  for (idx, coefficient) in coefficients.iter().rev().enumerate() {
214    *share += coefficient.deref();
215    if idx != (coefficients.len() - 1) {
216      *share *= l;
217    }
218  }
219  share
220}
221
222/// The secret share message, to be sent to the party it's intended for over an authenticated
223/// channel.
224///
225/// If any participant sends multiple secret shares to another participant, they are faulty.
226// This should presumably be written as SecretShare(Zeroizing<F::Repr>).
227// It's unfortunately not possible as F::Repr doesn't have Zeroize as a bound.
228// The encryption system also explicitly uses Zeroizing<M> so it can ensure anything being
229// encrypted is within Zeroizing. Accordingly, internally having Zeroizing would be redundant.
230#[derive(Clone, PartialEq, Eq)]
231pub struct SecretShare<F: PrimeField>(F::Repr);
232impl<F: PrimeField> AsRef<[u8]> for SecretShare<F> {
233  fn as_ref(&self) -> &[u8] {
234    self.0.as_ref()
235  }
236}
237impl<F: PrimeField> AsMut<[u8]> for SecretShare<F> {
238  fn as_mut(&mut self) -> &mut [u8] {
239    self.0.as_mut()
240  }
241}
242impl<F: PrimeField> fmt::Debug for SecretShare<F> {
243  fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
244    fmt.debug_struct("SecretShare").finish_non_exhaustive()
245  }
246}
247impl<F: PrimeField> Zeroize for SecretShare<F> {
248  fn zeroize(&mut self) {
249    self.0.as_mut().zeroize()
250  }
251}
252// Still manually implement ZeroizeOnDrop to ensure these don't stick around.
253// We could replace Zeroizing<M> with a bound M: ZeroizeOnDrop.
254// Doing so would potentially fail to highlight the expected behavior with these and remove a layer
255// of depth.
256impl<F: PrimeField> Drop for SecretShare<F> {
257  fn drop(&mut self) {
258    self.zeroize();
259  }
260}
261impl<F: PrimeField> ZeroizeOnDrop for SecretShare<F> {}
262
263impl<F: PrimeField> ReadWrite for SecretShare<F> {
264  fn read<R: Read>(reader: &mut R, _: ThresholdParams) -> io::Result<Self> {
265    let mut repr = F::Repr::default();
266    reader.read_exact(repr.as_mut())?;
267    Ok(SecretShare(repr))
268  }
269
270  fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
271    writer.write_all(self.0.as_ref())
272  }
273}
274
275/// Advancement of the key generation state machine.
276#[derive(Zeroize)]
277pub struct SecretShareMachine<C: Ciphersuite> {
278  params: ThresholdParams,
279  context: [u8; 32],
280  coefficients: Vec<Zeroizing<C::F>>,
281  our_commitments: Vec<C::G>,
282  encryption: Encryption<C>,
283}
284
285impl<C: Ciphersuite> fmt::Debug for SecretShareMachine<C> {
286  fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
287    fmt
288      .debug_struct("SecretShareMachine")
289      .field("params", &self.params)
290      .field("context", &self.context)
291      .field("our_commitments", &self.our_commitments)
292      .field("encryption", &self.encryption)
293      .finish_non_exhaustive()
294  }
295}
296
297impl<C: Ciphersuite> SecretShareMachine<C> {
298  /// Verify the data from the previous round (canonicity, PoKs, message authenticity)
299  #[allow(clippy::type_complexity)]
300  fn verify_r1<R: RngCore + CryptoRng>(
301    &mut self,
302    rng: &mut R,
303    mut commitment_msgs: HashMap<Participant, EncryptionKeyMessage<C, Commitments<C>>>,
304  ) -> Result<HashMap<Participant, Vec<C::G>>, PedPoPError<C>> {
305    validate_map(
306      &commitment_msgs,
307      &self.params.all_participant_indexes().collect::<Vec<_>>(),
308      self.params.i(),
309    )?;
310
311    let mut batch = BatchVerifier::<Participant, C::G>::new(commitment_msgs.len());
312    let mut commitments = HashMap::new();
313    for l in self.params.all_participant_indexes() {
314      let Some(msg) = commitment_msgs.remove(&l) else { continue };
315      let mut msg = self.encryption.register(l, msg);
316
317      if msg.commitments.len() != self.params.t().into() {
318        Err(PedPoPError::InvalidCommitments(l))?;
319      }
320
321      // Step 5: Validate each proof of knowledge
322      // This is solely the prep step for the latter batch verification
323      msg.sig.batch_verify(
324        rng,
325        &mut batch,
326        l,
327        msg.commitments[0],
328        challenge::<C>(self.context, l, msg.sig.R.to_bytes().as_ref(), &msg.cached_msg),
329      );
330
331      commitments.insert(l, msg.commitments.drain(..).collect::<Vec<_>>());
332    }
333
334    batch.verify_vartime_with_vartime_blame().map_err(PedPoPError::InvalidCommitments)?;
335
336    commitments.insert(self.params.i(), self.our_commitments.drain(..).collect());
337    Ok(commitments)
338  }
339
340  /// Continue generating a key.
341  ///
342  /// Takes in everyone else's commitments. Returns a HashMap of encrypted secret shares to be sent
343  /// over authenticated channels to their relevant counterparties.
344  ///
345  /// If any participant sends multiple secret shares to another participant, they are faulty.
346  #[allow(clippy::type_complexity)]
347  pub fn generate_secret_shares<R: RngCore + CryptoRng>(
348    mut self,
349    rng: &mut R,
350    commitments: HashMap<Participant, EncryptionKeyMessage<C, Commitments<C>>>,
351  ) -> Result<
352    (KeyMachine<C>, HashMap<Participant, EncryptedMessage<C, SecretShare<C::F>>>),
353    PedPoPError<C>,
354  > {
355    let commitments = self.verify_r1(&mut *rng, commitments)?;
356
357    // Step 1: Generate secret shares for all other parties
358    let mut res = HashMap::new();
359    for l in self.params.all_participant_indexes() {
360      // Don't insert our own shares to the byte buffer which is meant to be sent around
361      // An app developer could accidentally send it. Best to keep this black boxed
362      if l == self.params.i() {
363        continue;
364      }
365
366      let mut share = polynomial(&self.coefficients, l);
367      let share_bytes = Zeroizing::new(SecretShare::<C::F>(share.to_repr()));
368      share.zeroize();
369      res.insert(l, self.encryption.encrypt(rng, l, share_bytes));
370    }
371
372    // Calculate our own share
373    let share = polynomial(&self.coefficients, self.params.i());
374    self.coefficients.zeroize();
375
376    Ok((
377      KeyMachine { params: self.params, secret: share, commitments, encryption: self.encryption },
378      res,
379    ))
380  }
381}
382
383/// Advancement of the the secret share state machine.
384///
385/// This machine will 'complete' the protocol, by a local perspective. In order to be secure,
386/// the parties must confirm having successfully completed the protocol (an effort out of scope to
387/// this library), yet this is modeled by one more state transition (BlameMachine).
388pub struct KeyMachine<C: Ciphersuite> {
389  params: ThresholdParams,
390  secret: Zeroizing<C::F>,
391  commitments: HashMap<Participant, Vec<C::G>>,
392  encryption: Encryption<C>,
393}
394
395impl<C: Ciphersuite> fmt::Debug for KeyMachine<C> {
396  fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
397    fmt
398      .debug_struct("KeyMachine")
399      .field("params", &self.params)
400      .field("commitments", &self.commitments)
401      .field("encryption", &self.encryption)
402      .finish_non_exhaustive()
403  }
404}
405
406impl<C: Ciphersuite> Zeroize for KeyMachine<C> {
407  fn zeroize(&mut self) {
408    self.params.zeroize();
409    self.secret.zeroize();
410    for commitments in self.commitments.values_mut() {
411      commitments.zeroize();
412    }
413    self.encryption.zeroize();
414  }
415}
416
417// Calculate the exponent for a given participant and apply it to a series of commitments
418// Initially used with the actual commitments to verify the secret share, later used with
419// stripes to generate the verification shares
420fn exponential<C: Ciphersuite>(i: Participant, values: &[C::G]) -> Vec<(C::F, C::G)> {
421  let i = C::F::from(u16::from(i).into());
422  let mut res = Vec::with_capacity(values.len());
423  (0 .. values.len()).fold(C::F::ONE, |exp, l| {
424    res.push((exp, values[l]));
425    exp * i
426  });
427  res
428}
429
430fn share_verification_statements<C: Ciphersuite>(
431  target: Participant,
432  commitments: &[C::G],
433  mut share: Zeroizing<C::F>,
434) -> Vec<(C::F, C::G)> {
435  // This can be insecurely linearized from n * t to just n using the below sums for a given
436  // stripe. Doing so uses naive addition which is subject to malleability. The only way to
437  // ensure that malleability isn't present is to use this n * t algorithm, which runs
438  // per sender and not as an aggregate of all senders, which also enables blame
439  let mut values = exponential::<C>(target, commitments);
440
441  // Perform the share multiplication outside of the multiexp to minimize stack copying
442  // While the multiexp BatchVerifier does zeroize its flattened multiexp, and itself, it still
443  // converts whatever we give to an iterator and then builds a Vec internally, welcoming copies
444  let neg_share_pub = C::generator() * -*share;
445  share.zeroize();
446  values.push((C::F::ONE, neg_share_pub));
447
448  values
449}
450
451#[derive(Clone, Copy, Hash, Debug, Zeroize)]
452enum BatchId {
453  Decryption(Participant),
454  Share(Participant),
455}
456
457impl<C: Ciphersuite> KeyMachine<C> {
458  /// Calculate our share given the shares sent to us.
459  ///
460  /// Returns a BlameMachine usable to determine if faults in the protocol occurred.
461  ///
462  /// This will error on, and return a blame proof for, the first-observed case of faulty behavior.
463  pub fn calculate_share<R: RngCore + CryptoRng>(
464    mut self,
465    rng: &mut R,
466    mut shares: HashMap<Participant, EncryptedMessage<C, SecretShare<C::F>>>,
467  ) -> Result<BlameMachine<C>, PedPoPError<C>> {
468    validate_map(
469      &shares,
470      &self.params.all_participant_indexes().collect::<Vec<_>>(),
471      self.params.i(),
472    )?;
473
474    let mut batch = BatchVerifier::new(shares.len());
475    let mut blames = HashMap::new();
476    for (l, share_bytes) in shares.drain() {
477      let (mut share_bytes, blame) =
478        self.encryption.decrypt(rng, &mut batch, BatchId::Decryption(l), l, share_bytes);
479      let share =
480        Zeroizing::new(Option::<C::F>::from(C::F::from_repr(share_bytes.0)).ok_or_else(|| {
481          PedPoPError::InvalidShare { participant: l, blame: Some(blame.clone()) }
482        })?);
483      share_bytes.zeroize();
484      *self.secret += share.deref();
485
486      blames.insert(l, blame);
487      batch.queue(
488        rng,
489        BatchId::Share(l),
490        share_verification_statements::<C>(self.params.i(), &self.commitments[&l], share),
491      );
492    }
493    batch.verify_with_vartime_blame().map_err(|id| {
494      let (l, blame) = match id {
495        BatchId::Decryption(l) => (l, None),
496        BatchId::Share(l) => (l, Some(blames.remove(&l).unwrap())),
497      };
498      PedPoPError::InvalidShare { participant: l, blame }
499    })?;
500
501    // Stripe commitments per t and sum them in advance. Calculating verification shares relies on
502    // these sums so preprocessing them is a massive speedup
503    // If these weren't just sums, yet the tables used in multiexp, this would be further optimized
504    // As of right now, each multiexp will regenerate them
505    let mut stripes = Vec::with_capacity(usize::from(self.params.t()));
506    for t in 0 .. usize::from(self.params.t()) {
507      stripes.push(self.commitments.values().map(|commitments| commitments[t]).sum());
508    }
509
510    // Calculate each user's verification share
511    let mut verification_shares = HashMap::new();
512    for i in self.params.all_participant_indexes() {
513      verification_shares.insert(
514        i,
515        if i == self.params.i() {
516          C::generator() * self.secret.deref()
517        } else {
518          multiexp_vartime(&exponential::<C>(i, &stripes))
519        },
520      );
521    }
522
523    let KeyMachine { commitments, encryption, params, secret } = self;
524    Ok(BlameMachine {
525      commitments,
526      encryption: encryption.into_decryption(),
527      result: Some(
528        ThresholdKeys::new(params, Interpolation::Lagrange, secret, verification_shares)
529          .map_err(PedPoPError::DkgError)?,
530      ),
531    })
532  }
533}
534
535/// A machine capable of handling blame proofs.
536pub struct BlameMachine<C: Ciphersuite> {
537  commitments: HashMap<Participant, Vec<C::G>>,
538  encryption: Decryption<C>,
539  result: Option<ThresholdKeys<C>>,
540}
541
542impl<C: Ciphersuite> fmt::Debug for BlameMachine<C> {
543  fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
544    fmt
545      .debug_struct("BlameMachine")
546      .field("commitments", &self.commitments)
547      .field("encryption", &self.encryption)
548      .finish_non_exhaustive()
549  }
550}
551
552impl<C: Ciphersuite> Zeroize for BlameMachine<C> {
553  fn zeroize(&mut self) {
554    for commitments in self.commitments.values_mut() {
555      commitments.zeroize();
556    }
557    self.result.zeroize();
558  }
559}
560
561impl<C: Ciphersuite> BlameMachine<C> {
562  /// Mark the protocol as having been successfully completed, returning the generated keys.
563  /// This should only be called after having confirmed, with all participants, successful
564  /// completion.
565  ///
566  /// Confirming successful completion is not necessarily as simple as everyone reporting their
567  /// completion. Everyone must also receive everyone's report of completion, entering into the
568  /// territory of consensus protocols. This library does not handle that nor does it provide any
569  /// tooling to do so. This function is solely intended to force users to acknowledge they're
570  /// completing the protocol, not processing any blame.
571  pub fn complete(self) -> ThresholdKeys<C> {
572    self.result.unwrap()
573  }
574
575  fn blame_internal(
576    &self,
577    sender: Participant,
578    recipient: Participant,
579    msg: EncryptedMessage<C, SecretShare<C::F>>,
580    proof: Option<EncryptionKeyProof<C>>,
581  ) -> Participant {
582    let share_bytes = match self.encryption.decrypt_with_proof(sender, recipient, msg, proof) {
583      Ok(share_bytes) => share_bytes,
584      // If there's an invalid signature, the sender did not send a properly formed message
585      Err(DecryptionError::InvalidSignature) => return sender,
586      // Decryption will fail if the provided ECDH key wasn't correct for the given message
587      Err(DecryptionError::InvalidProof) => return recipient,
588    };
589
590    let Some(share) = Option::<C::F>::from(C::F::from_repr(share_bytes.0)) else {
591      // If this isn't a valid scalar, the sender is faulty
592      return sender;
593    };
594
595    // If this isn't a valid share, the sender is faulty
596    if !bool::from(
597      multiexp_vartime(&share_verification_statements::<C>(
598        recipient,
599        &self.commitments[&sender],
600        Zeroizing::new(share),
601      ))
602      .is_identity(),
603    ) {
604      return sender;
605    }
606
607    // The share was canonical and valid
608    recipient
609  }
610
611  /// Given an accusation of fault, determine the faulty party (either the sender, who sent an
612  /// invalid secret share, or the receiver, who claimed a valid secret share was invalid). No
613  /// matter which, prevent completion of the machine, forcing an abort of the protocol.
614  ///
615  /// The message should be a copy of the encrypted secret share from the accused sender to the
616  /// accusing recipient. This message must have been authenticated as actually having come from
617  /// the sender in question.
618  ///
619  /// In order to enable detecting multiple faults, an `AdditionalBlameMachine` is returned, which
620  /// can be used to determine further blame. These machines will process the same blame statements
621  /// multiple times, always identifying blame. It is the caller's job to ensure they're unique in
622  /// order to prevent multiple instances of blame over a single incident.
623  pub fn blame(
624    self,
625    sender: Participant,
626    recipient: Participant,
627    msg: EncryptedMessage<C, SecretShare<C::F>>,
628    proof: Option<EncryptionKeyProof<C>>,
629  ) -> (AdditionalBlameMachine<C>, Participant) {
630    let faulty = self.blame_internal(sender, recipient, msg, proof);
631    (AdditionalBlameMachine(self), faulty)
632  }
633}
634
635/// A machine capable of handling an arbitrary amount of additional blame proofs.
636#[derive(Debug, Zeroize)]
637pub struct AdditionalBlameMachine<C: Ciphersuite>(BlameMachine<C>);
638impl<C: Ciphersuite> AdditionalBlameMachine<C> {
639  /// Create an AdditionalBlameMachine capable of evaluating Blame regardless of if the caller was
640  /// a member in the DKG protocol.
641  ///
642  /// Takes in the parameters for the DKG protocol and all of the participant's commitment
643  /// messages.
644  ///
645  /// This constructor assumes the full validity of the commitment messages. They must be fully
646  /// authenticated as having come from the supposed party and verified as valid. Usage of invalid
647  /// commitments is considered undefined behavior, and may cause everything from inaccurate blame
648  /// to panics.
649  pub fn new(
650    context: [u8; 32],
651    n: u16,
652    mut commitment_msgs: HashMap<Participant, EncryptionKeyMessage<C, Commitments<C>>>,
653  ) -> Result<Self, PedPoPError<C>> {
654    let mut commitments = HashMap::new();
655    let mut encryption = Decryption::new(context);
656    for i in 1 ..= n {
657      let i = Participant::new(i).unwrap();
658      let Some(msg) = commitment_msgs.remove(&i) else { Err(PedPoPError::MissingParticipant(i))? };
659      commitments.insert(i, encryption.register(i, msg).commitments);
660    }
661    Ok(AdditionalBlameMachine(BlameMachine { commitments, encryption, result: None }))
662  }
663
664  /// Given an accusation of fault, determine the faulty party (either the sender, who sent an
665  /// invalid secret share, or the receiver, who claimed a valid secret share was invalid).
666  ///
667  /// The message should be a copy of the encrypted secret share from the accused sender to the
668  /// accusing recipient. This message must have been authenticated as actually having come from
669  /// the sender in question.
670  ///
671  /// This will process the same blame statement multiple times, always identifying blame. It is
672  /// the caller's job to ensure they're unique in order to prevent multiple instances of blame
673  /// over a single incident.
674  pub fn blame(
675    &self,
676    sender: Participant,
677    recipient: Participant,
678    msg: EncryptedMessage<C, SecretShare<C::F>>,
679    proof: Option<EncryptionKeyProof<C>>,
680  ) -> Participant {
681    self.0.blame_internal(sender, recipient, msg, proof)
682  }
683}