mithril_stm/aggregate_signature/
clerk.rs

1use blake2::digest::{Digest, FixedOutput};
2
3use crate::{
4    AggregateSignature, AggregateSignatureType, AggregateVerificationKey, AggregationError,
5    ClosedKeyRegistration, Index, Parameters, Signer, SingleSignature, Stake, VerificationKey,
6    aggregate_signature::ConcatenationProof,
7};
8
9/// `Clerk` can verify and aggregate `SingleSignature`s and verify `AggregateSignature`s.
10/// Clerks can only be generated with the registration closed.
11/// This avoids that a Merkle Tree is computed before all parties have registered.
12#[derive(Debug, Clone)]
13pub struct Clerk<D: Clone + Digest> {
14    pub(crate) closed_reg: ClosedKeyRegistration<D>,
15    pub(crate) params: Parameters,
16}
17
18impl<D: Digest + Clone + FixedOutput + Send + Sync> Clerk<D> {
19    /// Create a new `Clerk` from a closed registration instance.
20    pub fn new_clerk_from_closed_key_registration(
21        params: &Parameters,
22        closed_reg: &ClosedKeyRegistration<D>,
23    ) -> Self {
24        Self {
25            params: *params,
26            closed_reg: closed_reg.clone(),
27        }
28    }
29
30    /// Create a new `Clerk` from a closed registration instance.
31    #[deprecated(
32        since = "0.5.0",
33        note = "Use `new_clerk_from_closed_key_registration` instead"
34    )]
35    pub fn from_registration(params: &Parameters, closed_reg: &ClosedKeyRegistration<D>) -> Self {
36        Self::new_clerk_from_closed_key_registration(params, closed_reg)
37    }
38
39    /// Create a Clerk from a signer.
40    pub fn new_clerk_from_signer(signer: &Signer<D>) -> Self {
41        let closed_reg = signer
42            .get_closed_key_registration()
43            .clone()
44            .expect("Core signer does not include closed registration. Clerk, and so, the Stm certificate cannot be built without closed registration!")
45            ;
46
47        Self {
48            params: signer.get_parameters(),
49            closed_reg,
50        }
51    }
52
53    /// Create a Clerk from a signer.
54    #[deprecated(since = "0.5.0", note = "Use `new_clerk_from_signer` instead")]
55    pub fn from_signer(signer: &Signer<D>) -> Self {
56        Self::new_clerk_from_signer(signer)
57    }
58
59    /// Aggregate a set of signatures.
60    #[deprecated(since = "0.5.3", note = "Use `aggregate_signatures_with_type` instead")]
61    pub fn aggregate_signatures(
62        &self,
63        sigs: &[SingleSignature],
64        msg: &[u8],
65    ) -> Result<AggregateSignature<D>, AggregationError> {
66        self.aggregate_signatures_with_type(sigs, msg, AggregateSignatureType::default())
67    }
68
69    /// Aggregate a set of signatures with a given proof type.
70    pub fn aggregate_signatures_with_type(
71        &self,
72        sigs: &[SingleSignature],
73        msg: &[u8],
74        aggregate_signature_type: AggregateSignatureType,
75    ) -> Result<AggregateSignature<D>, AggregationError> {
76        match aggregate_signature_type {
77            AggregateSignatureType::Concatenation => Ok(AggregateSignature::Concatenation(
78                ConcatenationProof::aggregate_signatures(self, sigs, msg)?,
79            )),
80            #[cfg(feature = "future_proof_system")]
81            AggregateSignatureType::Future => Err(AggregationError::UnsupportedProofSystem(
82                aggregate_signature_type,
83            )),
84        }
85    }
86
87    /// Aggregate a set of signatures for their corresponding indices.
88    ///
89    /// This function first deduplicates the repeated signatures, and if there are enough signatures, it collects the merkle tree indexes of unique signatures.
90    /// The list of merkle tree indexes is used to create a batch proof, to prove that all signatures are from eligible signers.
91    ///
92    /// It returns an instance of `AggregateSignature`.
93    #[deprecated(since = "0.5.0", note = "Use `aggregate_signatures` instead")]
94    #[allow(deprecated)]
95    pub fn aggregate(
96        &self,
97        sigs: &[SingleSignature],
98        msg: &[u8],
99    ) -> Result<AggregateSignature<D>, AggregationError> {
100        Self::aggregate_signatures(self, sigs, msg)
101    }
102
103    /// Compute the `AggregateVerificationKey` related to the used registration.
104    pub fn compute_aggregate_verification_key(&self) -> AggregateVerificationKey<D> {
105        AggregateVerificationKey::from(&self.closed_reg)
106    }
107
108    /// Compute the `AggregateVerificationKey` related to the used registration.
109    #[deprecated(
110        since = "0.5.0",
111        note = "Use `compute_aggregate_verification_key` instead"
112    )]
113    pub fn compute_avk(&self) -> AggregateVerificationKey<D> {
114        Self::compute_aggregate_verification_key(self)
115    }
116
117    /// Get the (VK, stake) of a party given its index.
118    pub fn get_registered_party_for_index(
119        &self,
120        party_index: &Index,
121    ) -> Option<(VerificationKey, Stake)> {
122        self.closed_reg
123            .reg_parties
124            .get(*party_index as usize)
125            .map(|&r| r.into())
126    }
127
128    /// Get the (VK, stake) of a party given its index.
129    #[deprecated(since = "0.5.0", note = "Use `get_registered_party_for_index` instead")]
130    pub fn get_reg_party(&self, party_index: &Index) -> Option<(VerificationKey, Stake)> {
131        Self::get_registered_party_for_index(self, party_index)
132    }
133}