// `threshold_sign::Message` triggers this Clippy lint, but `Box` doesn't implement `Rand`. #![allow(clippy::large_enum_variant)] use rand::distributions::{Distribution, Standard}; use rand::{seq::SliceRandom, Rng}; use serde::{Deserialize, Serialize}; use crate::subset; use crate::threshold_decrypt; /// The content of a `HoneyBadger` message. It should be further annotated with an epoch. #[derive(Clone, Debug, Deserialize, Serialize)] pub enum MessageContent { /// A message belonging to the subset algorithm in the given epoch. Subset(subset::Message), /// A decryption share of the output of `proposer_id`. DecryptionShare { /// The ID of the node that proposed the contribution that is being decrypted. proposer_id: N, /// The decryption share: _f + 1_ of these are required to decrypt the contribution. share: threshold_decrypt::Message, }, } impl Distribution> for Standard where Standard: Distribution, { fn sample(&self, rng: &mut R) -> MessageContent { let message_type = *["subset", "dec_share"].choose(rng).unwrap(); match message_type { "subset" => MessageContent::Subset(rng.gen::>()), "dec_share" => MessageContent::DecryptionShare { proposer_id: rng.gen::(), share: rng.gen::(), }, _ => unreachable!(), } } } impl MessageContent { /// Wraps this content in a `Message` with the given epoch. pub fn with_epoch(self, epoch: u64) -> Message { Message { epoch, content: self, } } } /// A message sent to or received from another node's Honey Badger instance. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Message { pub(super) epoch: u64, pub(super) content: MessageContent, } impl Distribution> for Standard where Standard: Distribution, { fn sample(&self, rng: &mut R) -> Message { Message { epoch: rng.gen::(), content: rng.gen::>(), } } } impl Message { /// Returns this message's Honey Badger epoch. pub fn epoch(&self) -> u64 { self.epoch } }