mithril_stm/aggregate_signature/
clerk.rs

1use blake2::digest::{Digest, FixedOutput};
2
3use crate::{
4    AggregateSignature, AggregateVerificationKey, AggregationError, BasicVerifier,
5    ClosedKeyRegistration, Index, Parameters, Signer, SingleSignature,
6    SingleSignatureWithRegisteredParty, Stake, VerificationKey,
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> 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.4.9",
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.4.9", 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 for their corresponding indices.
60    ///
61    /// This function first deduplicates the repeated signatures, and if there are enough signatures, it collects the merkle tree indexes of unique signatures.
62    /// The list of merkle tree indexes is used to create a batch proof, to prove that all signatures are from eligible signers.
63    ///
64    /// It returns an instance of `AggregateSignature`.
65    pub fn aggregate_signatures(
66        &self,
67        sigs: &[SingleSignature],
68        msg: &[u8],
69    ) -> Result<AggregateSignature<D>, AggregationError> {
70        let sig_reg_list = sigs
71            .iter()
72            .map(|sig| SingleSignatureWithRegisteredParty {
73                sig: sig.clone(),
74                reg_party: self.closed_reg.reg_parties[sig.signer_index as usize],
75            })
76            .collect::<Vec<SingleSignatureWithRegisteredParty>>();
77
78        let avk = AggregateVerificationKey::from(&self.closed_reg);
79        let msgp = avk.get_merkle_tree_batch_commitment().concatenate_with_message(msg);
80        let mut unique_sigs = BasicVerifier::select_valid_signatures_for_k_indices(
81            &self.closed_reg.total_stake,
82            &self.params,
83            &msgp,
84            &sig_reg_list,
85        )?;
86
87        unique_sigs.sort_unstable();
88
89        let mt_index_list = unique_sigs
90            .iter()
91            .map(|sig_reg| sig_reg.sig.signer_index as usize)
92            .collect::<Vec<usize>>();
93
94        let batch_proof = self
95            .closed_reg
96            .merkle_tree
97            .compute_merkle_tree_batch_path(mt_index_list);
98
99        Ok(AggregateSignature {
100            signatures: unique_sigs,
101            batch_proof,
102        })
103    }
104
105    /// Aggregate a set of signatures for their corresponding indices.
106    ///
107    /// This function first deduplicates the repeated signatures, and if there are enough signatures, it collects the merkle tree indexes of unique signatures.
108    /// The list of merkle tree indexes is used to create a batch proof, to prove that all signatures are from eligible signers.
109    ///
110    /// It returns an instance of `AggregateSignature`.
111    #[deprecated(since = "0.4.9", note = "Use `aggregate_signatures` instead")]
112    pub fn aggregate(
113        &self,
114        sigs: &[SingleSignature],
115        msg: &[u8],
116    ) -> Result<AggregateSignature<D>, AggregationError> {
117        Self::aggregate_signatures(self, sigs, msg)
118    }
119
120    /// Compute the `AggregateVerificationKey` related to the used registration.
121    pub fn compute_aggregate_verification_key(&self) -> AggregateVerificationKey<D> {
122        AggregateVerificationKey::from(&self.closed_reg)
123    }
124
125    /// Compute the `AggregateVerificationKey` related to the used registration.
126    #[deprecated(
127        since = "0.4.9",
128        note = "Use `compute_aggregate_verification_key` instead"
129    )]
130    pub fn compute_avk(&self) -> AggregateVerificationKey<D> {
131        Self::compute_aggregate_verification_key(self)
132    }
133
134    /// Get the (VK, stake) of a party given its index.
135    pub fn get_registered_party_for_index(
136        &self,
137        party_index: &Index,
138    ) -> Option<(VerificationKey, Stake)> {
139        self.closed_reg
140            .reg_parties
141            .get(*party_index as usize)
142            .map(|&r| r.into())
143    }
144
145    /// Get the (VK, stake) of a party given its index.
146    #[deprecated(since = "0.4.9", note = "Use `get_registered_party_for_index` instead")]
147    pub fn get_reg_party(&self, party_index: &Index) -> Option<(VerificationKey, Stake)> {
148        Self::get_registered_party_for_index(self, party_index)
149    }
150}