1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use anyhow::{anyhow, Context};
use async_trait::async_trait;
use slog_scope::{debug, warn};

use mithril_common::{
    crypto_helper::{ProtocolAggregationError, ProtocolMultiSignature},
    entities::{self},
    StdResult,
};

use crate::dependency_injection::EpochServiceWrapper;
use crate::entities::OpenMessage;

#[cfg(test)]
use mockall::automock;

/// MultiSigner is the cryptographic engine in charge of producing multi signatures from individual signatures
#[cfg_attr(test, automock)]
#[async_trait]
pub trait MultiSigner: Sync + Send {
    /// Verify a single signature
    async fn verify_single_signature(
        &self,
        message: &entities::ProtocolMessage,
        signatures: &entities::SingleSignatures,
    ) -> StdResult<()>;

    /// Creates a multi signature from single signatures
    async fn create_multi_signature(
        &self,
        open_message: &OpenMessage,
    ) -> StdResult<Option<ProtocolMultiSignature>>;
}

/// MultiSignerImpl is an implementation of the MultiSigner
pub struct MultiSignerImpl {
    epoch_service: EpochServiceWrapper,
}

impl MultiSignerImpl {
    /// MultiSignerImpl factory
    pub fn new(epoch_service: EpochServiceWrapper) -> Self {
        debug!("New MultiSignerImpl created");
        Self { epoch_service }
    }
}

#[async_trait]
impl MultiSigner for MultiSignerImpl {
    /// Verify a single signature
    async fn verify_single_signature(
        &self,
        message: &entities::ProtocolMessage,
        single_signature: &entities::SingleSignatures,
    ) -> StdResult<()> {
        debug!(
            "Verify single signature from {} at indexes {:?} for message {:?}",
            single_signature.party_id, single_signature.won_indexes, message
        );

        let epoch_service = self.epoch_service.read().await;
        let protocol_multi_signer = epoch_service.protocol_multi_signer().with_context(|| {
            "Multi Signer could not get protocol multi-signer from epoch service"
        })?;

        protocol_multi_signer
            .verify_single_signature(message, single_signature)
            .with_context(|| {
                format!("Multi Signer can not verify single signature for message '{message:?}'")
            })
    }

    /// Creates a multi signature from single signatures
    async fn create_multi_signature(
        &self,
        open_message: &OpenMessage,
    ) -> StdResult<Option<ProtocolMultiSignature>> {
        debug!("MultiSigner:create_multi_signature({open_message:?})");

        let epoch_service = self.epoch_service.read().await;
        let protocol_multi_signer = epoch_service.protocol_multi_signer().with_context(|| {
            "Multi Signer could not get protocol multi-signer from epoch service"
        })?;

        match protocol_multi_signer.aggregate_single_signatures(
            &open_message.single_signatures,
            &open_message.protocol_message,
        ) {
            Ok(multi_signature) => Ok(Some(multi_signature)),
            Err(ProtocolAggregationError::NotEnoughSignatures(actual, expected)) => {
                warn!("Could not compute multi-signature: Not enough signatures. Got only {} out of {}.", actual, expected);
                Ok(None)
            }
            Err(err) => Err(anyhow!(err).context(format!(
                "Multi Signer can not create multi-signature for entity type '{:?}'",
                open_message.signed_entity_type
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::services::FakeEpochService;
    use mithril_common::entities::SignerWithStake;
    use mithril_common::{
        crypto_helper::tests_setup::*,
        entities::{CardanoDbBeacon, Epoch, SignedEntityType},
        test_utils::{fake_data, MithrilFixtureBuilder},
    };
    use std::sync::Arc;
    use tokio::sync::RwLock;

    fn take_signatures_until_quorum_is_almost_reached(
        signatures: &mut Vec<entities::SingleSignatures>,
        quorum: usize,
    ) -> Vec<entities::SingleSignatures> {
        signatures.sort_by(|l, r| l.won_indexes.len().cmp(&r.won_indexes.len()));

        let mut result = vec![];
        let mut nb_won_indexes = 0;

        while let Some(signature) = signatures.first() {
            if signature.won_indexes.len() + nb_won_indexes >= quorum {
                break;
            }
            nb_won_indexes += signature.won_indexes.len();
            result.push(signatures.remove(0));
        }

        result
    }

    #[tokio::test]
    async fn test_multi_signer_multi_signature_ok() {
        let epoch = Epoch(5);
        let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
        let protocol_parameters = fixture.protocol_parameters();
        let multi_signer = MultiSignerImpl::new(Arc::new(RwLock::new(
            FakeEpochService::from_fixture(epoch, &fixture),
        )));

        let message = setup_message();

        let mut signatures = Vec::new();

        let mut expected_certificate_signers: Vec<SignerWithStake> = Vec::new();
        for signer_fixture in fixture.signers_fixture() {
            if let Some(signature) = signer_fixture
                .protocol_signer
                .sign(message.compute_hash().as_bytes())
            {
                let won_indexes = signature.indexes.clone();

                signatures.push(entities::SingleSignatures::new(
                    signer_fixture.signer_with_stake.party_id.to_owned(),
                    signature.into(),
                    won_indexes,
                ));

                expected_certificate_signers.push(signer_fixture.signer_with_stake.to_owned())
            }
        }

        for signature in &signatures {
            multi_signer
                .verify_single_signature(&message, signature)
                .await
                .expect("single signature should be valid");
        }

        let signatures_to_almost_reach_quorum = take_signatures_until_quorum_is_almost_reached(
            &mut signatures,
            protocol_parameters.k as usize,
        );
        assert!(
            !signatures_to_almost_reach_quorum.is_empty(),
            "they should be at least one signature that can be registered without reaching the quorum"
        );

        let mut open_message = OpenMessage {
            epoch,
            signed_entity_type: SignedEntityType::CardanoImmutableFilesFull(CardanoDbBeacon {
                epoch,
                ..fake_data::beacon()
            }),
            protocol_message: message.clone(),
            is_certified: false,
            single_signatures: Vec::new(),
            ..OpenMessage::dummy()
        };

        // No signatures registered: multi-signer can't create the multi-signature
        assert!(multi_signer
            .create_multi_signature(&open_message)
            .await
            .expect("create multi signature should not fail")
            .is_none());

        // Add some signatures but not enough to reach the quorum: multi-signer should not create the multi-signature
        open_message.single_signatures = signatures_to_almost_reach_quorum;

        assert!(multi_signer
            .create_multi_signature(&open_message)
            .await
            .expect("create multi signature should not fail")
            .is_none());

        // Add the remaining signatures to reach the quorum: multi-signer should create a multi-signature
        open_message.single_signatures.append(&mut signatures);

        assert!(
            multi_signer
                .create_multi_signature(&open_message)
                .await
                .expect("create multi signature should not fail")
                .is_some(),
            "no multi-signature were computed"
        );
    }
}