|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::hash::Hash; |
| 3 | + |
| 4 | +use blake2::digest::{Digest, FixedOutput}; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use strum::{Display, EnumDiscriminants}; |
| 7 | + |
| 8 | +use crate::error::StmAggregateSignatureError; |
| 9 | +use crate::merkle_tree::MerkleBatchPath; |
| 10 | +use crate::{AggregateVerificationKey, Parameters, SingleSignatureWithRegisteredParty}; |
| 11 | + |
1 | 12 | use super::ConcatenationProof;
|
2 | 13 |
|
3 |
| -pub type AggregateSignature<D> = ConcatenationProof<D>; |
| 14 | +/// A STM aggregate signature. |
| 15 | +#[derive(Debug, Clone, Serialize, Deserialize, EnumDiscriminants)] |
| 16 | +#[strum(serialize_all = "PascalCase")] |
| 17 | +#[strum_discriminants(derive(Serialize, Hash, Display))] // TODO: replace strum with custom implementation |
| 18 | +#[strum_discriminants(name(AggregateSignatureType))] |
| 19 | +#[serde(bound( |
| 20 | + serialize = "MerkleBatchPath<D>: Serialize", |
| 21 | + deserialize = "MerkleBatchPath<D>: Deserialize<'de>" |
| 22 | +))] |
| 23 | +#[serde(untagged)] |
| 24 | +pub enum AggregateSignature<D: Clone + Digest + FixedOutput + Send + Sync> { |
| 25 | + /// Concatenation proof system. |
| 26 | + Concatenation(ConcatenationProof<D>), |
| 27 | +} |
| 28 | + |
| 29 | +impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> { |
| 30 | + /// Verify an aggregate signature |
| 31 | + pub fn verify( |
| 32 | + &self, |
| 33 | + msg: &[u8], |
| 34 | + avk: &AggregateVerificationKey<D>, |
| 35 | + parameters: &Parameters, |
| 36 | + ) -> Result<(), StmAggregateSignatureError<D>> { |
| 37 | + match self { |
| 38 | + AggregateSignature::Concatenation(stm_aggr_sig) => { |
| 39 | + stm_aggr_sig.verify(msg, avk, parameters) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// Batch verify a set of aggregate signatures |
| 45 | + pub fn batch_verify( |
| 46 | + stm_signatures: &[Self], |
| 47 | + msgs: &[Vec<u8>], |
| 48 | + avks: &[AggregateVerificationKey<D>], |
| 49 | + parameters: &[Parameters], |
| 50 | + ) -> Result<(), StmAggregateSignatureError<D>> { |
| 51 | + let stm_signatures: HashMap<AggregateSignatureType, Vec<Self>> = |
| 52 | + stm_signatures.iter().fold(HashMap::new(), |mut acc, sig| { |
| 53 | + acc.entry(sig.into()).or_default().push(sig.clone()); |
| 54 | + acc |
| 55 | + }); |
| 56 | + stm_signatures |
| 57 | + .into_iter() |
| 58 | + .try_for_each( |
| 59 | + |(stm_aggr_sig_type, stm_aggr_sigs)| match stm_aggr_sig_type { |
| 60 | + AggregateSignatureType::Concatenation => ConcatenationProof::batch_verify( |
| 61 | + &stm_aggr_sigs |
| 62 | + .into_iter() |
| 63 | + .filter_map(|s| match s { |
| 64 | + Self::Concatenation(stm_aggr_sig) => Some(stm_aggr_sig), |
| 65 | + }) |
| 66 | + .collect::<Vec<_>>(), |
| 67 | + msgs, |
| 68 | + avks, |
| 69 | + parameters, |
| 70 | + ), |
| 71 | + }, |
| 72 | + ) |
| 73 | + .map_err(|_| StmAggregateSignatureError::BatchInvalid) |
| 74 | + } |
| 75 | + |
| 76 | + /// Convert an aggregate signature to bytes |
| 77 | + pub fn to_bytes(&self) -> Vec<u8> { |
| 78 | + match self { |
| 79 | + AggregateSignature::Concatenation(stm_aggr_sig) => stm_aggr_sig.to_bytes(), |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// Extract an aggregate signature from a byte slice. |
| 84 | + pub fn from_bytes(bytes: &[u8]) -> Result<Self, StmAggregateSignatureError<D>> { |
| 85 | + // TODO: Move first byte of concatenation proof here to identify the proof system |
| 86 | + Ok(Self::Concatenation(ConcatenationProof::from_bytes(bytes)?)) |
| 87 | + } |
| 88 | + |
| 89 | + /// Extract the list of signatures. |
| 90 | + pub fn signatures(&self) -> Vec<SingleSignatureWithRegisteredParty> { |
| 91 | + match self { |
| 92 | + AggregateSignature::Concatenation(stm_aggr_sig) => stm_aggr_sig.signatures.clone(), |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// Extract the list of unique merkle tree nodes that covers path for all signatures. |
| 97 | + // TODO: transfer this function to the concatenation proof |
| 98 | + pub fn batch_proof(&self) -> MerkleBatchPath<D> { |
| 99 | + match self { |
| 100 | + AggregateSignature::Concatenation(stm_aggr_sig) => stm_aggr_sig.batch_proof.clone(), |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// Extract the list of unique merkle tree nodes that covers path for all signatures. (test only) |
| 105 | + // TODO: transfer this function to the concatenation proof |
| 106 | + #[cfg(test)] |
| 107 | + pub(crate) fn set_batch_proof(&mut self, batch_proof: MerkleBatchPath<D>) { |
| 108 | + match self { |
| 109 | + AggregateSignature::Concatenation(stm_aggr_sig) => { |
| 110 | + stm_aggr_sig.batch_proof = batch_proof |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments