mithril_stm/aggregate_signature/
aggregate_key.rs

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