mithril_common/entities/
single_signature.rs

1use mithril_stm::SingleSignature as StmSingleSignature;
2use serde::{Deserialize, Serialize};
3use std::fmt::{Debug, Formatter};
4
5use crate::{
6    crypto_helper::ProtocolSingleSignature,
7    entities::{LotteryIndex, PartyId},
8};
9
10/// SingleSignatures represent single signatures originating from a participant in the network
11/// for a digest at won lottery indexes
12#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub struct SingleSignature {
14    /// The unique identifier of the signer
15    pub party_id: PartyId,
16
17    /// The single signature of the digest
18    pub signature: ProtocolSingleSignature,
19
20    /// The indexes of the won lotteries that lead to the single signatures
21    #[serde(rename = "indexes")]
22    pub won_indexes: Vec<LotteryIndex>,
23
24    /// Status of the authentication of the signer that emitted the signature
25    #[serde(skip)]
26    pub authentication_status: SingleSignatureAuthenticationStatus,
27}
28
29/// Status of the authentication of the signer that emitted the signature
30#[derive(Debug, Copy, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
31pub enum SingleSignatureAuthenticationStatus {
32    /// The signer that emitted the signature is authenticated
33    Authenticated,
34    /// The signer that emitted the signature is not authenticated
35    #[default]
36    Unauthenticated,
37}
38
39impl SingleSignature {
40    /// `SingleSignatures` factory
41    pub fn new<T: Into<PartyId>>(
42        party_id: T,
43        signature: ProtocolSingleSignature,
44        won_indexes: Vec<LotteryIndex>,
45    ) -> SingleSignature {
46        SingleSignature {
47            party_id: party_id.into(),
48            signature,
49            won_indexes,
50            authentication_status: SingleSignatureAuthenticationStatus::Unauthenticated,
51        }
52    }
53
54    /// Convert this [SingleSignature] to its corresponding [MithrilStm Signature][SingleSignature].
55    pub fn to_protocol_signature(&self) -> StmSingleSignature {
56        self.signature.clone().into()
57    }
58
59    /// Check that the signer that emitted the signature is authenticated
60    pub fn is_authenticated(&self) -> bool {
61        self.authentication_status == SingleSignatureAuthenticationStatus::Authenticated
62    }
63}
64
65impl Debug for SingleSignature {
66    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
67        let is_pretty_printing = f.alternate();
68        let mut debug = f.debug_struct("SingleSignatures");
69        debug
70            .field("party_id", &self.party_id)
71            .field("won_indexes", &format_args!("{:?}", self.won_indexes));
72
73        match is_pretty_printing {
74            true => debug
75                .field("signature", &format_args!("{:?}", self.signature))
76                .finish(),
77            false => debug.finish_non_exhaustive(),
78        }
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use crate::test::{builder::MithrilFixtureBuilder, crypto_helper::setup_message};
85
86    use super::*;
87
88    #[test]
89    fn single_signatures_should_convert_to_protocol_signatures() {
90        let message = setup_message();
91        let fixture = MithrilFixtureBuilder::default().with_signers(1).build();
92        let signer = &fixture.signers_fixture()[0];
93        let protocol_sigs = signer
94            .protocol_signer
95            .sign(message.compute_hash().as_bytes())
96            .unwrap();
97
98        let signature = SingleSignature::new(
99            signer.signer_with_stake.party_id.to_owned(),
100            protocol_sigs.clone().into(),
101            protocol_sigs.indexes.clone(),
102        );
103
104        assert_eq!(protocol_sigs, signature.to_protocol_signature());
105    }
106}