mithril_stm/protocol/aggregate_signature/
aggregate_key.rs

1use blake2::digest::{Digest, FixedOutput};
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    ClosedKeyRegistration, Stake,
6    membership_commitment::{MerkleBatchPath, MerkleTreeBatchCommitment},
7};
8
9/// Stm aggregate key (batch compatible), which contains the merkle tree commitment and the total stake of the system.
10/// Batch Compat Merkle tree commitment includes the number of leaves in the tree in order to obtain batch path.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(bound(
13    serialize = "MerkleBatchPath<D>: Serialize",
14    deserialize = "MerkleBatchPath<D>: Deserialize<'de>"
15))]
16pub struct AggregateVerificationKey<D: Clone + Digest + FixedOutput> {
17    mt_commitment: MerkleTreeBatchCommitment<D>,
18    total_stake: Stake,
19}
20
21impl<D: Digest + Clone + FixedOutput> AggregateVerificationKey<D> {
22    pub(crate) fn get_merkle_tree_batch_commitment(&self) -> MerkleTreeBatchCommitment<D> {
23        self.mt_commitment.clone()
24    }
25
26    #[deprecated(
27        since = "0.5.0",
28        note = "Use `get_merkle_tree_batch_commitment` instead"
29    )]
30    pub fn get_mt_commitment(&self) -> MerkleTreeBatchCommitment<D> {
31        Self::get_merkle_tree_batch_commitment(self)
32    }
33
34    pub fn get_total_stake(&self) -> Stake {
35        self.total_stake
36    }
37}
38
39impl<D: Digest + Clone + FixedOutput> PartialEq for AggregateVerificationKey<D> {
40    fn eq(&self, other: &Self) -> bool {
41        self.mt_commitment == other.mt_commitment && self.total_stake == other.total_stake
42    }
43}
44
45impl<D: Digest + Clone + FixedOutput> Eq for AggregateVerificationKey<D> {}
46
47impl<D: Clone + Digest + FixedOutput> From<&ClosedKeyRegistration<D>>
48    for AggregateVerificationKey<D>
49{
50    fn from(reg: &ClosedKeyRegistration<D>) -> Self {
51        Self {
52            mt_commitment: reg.merkle_tree.to_merkle_tree_batch_commitment(),
53            total_stake: reg.total_stake,
54        }
55    }
56}