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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use anyhow::{anyhow, Context};
use async_trait::async_trait;
use hex::ToHex;
use slog::{info, trace, warn, Logger};
use std::path::PathBuf;
use thiserror::Error;

use mithril_common::crypto_helper::{KESPeriod, ProtocolInitializer};
use mithril_common::entities::{
    PartyId, ProtocolMessage, ProtocolParameters, SingleSignatures, Stake,
};
use mithril_common::logging::LoggerExtensions;
use mithril_common::protocol::{SignerBuilder, SingleSigner as ProtocolSingleSigner};
use mithril_common::{StdError, StdResult};

use crate::dependency_injection::EpochServiceWrapper;

/// This is responsible for creating new instances of ProtocolInitializer.
pub struct MithrilProtocolInitializerBuilder {}

impl MithrilProtocolInitializerBuilder {
    /// Create a ProtocolInitializer instance.
    pub fn build(
        stake: &Stake,
        protocol_parameters: &ProtocolParameters,
        kes_secret_key_path: Option<PathBuf>,
        kes_period: Option<KESPeriod>,
    ) -> StdResult<ProtocolInitializer> {
        let mut rng = rand_core::OsRng;
        let protocol_initializer = ProtocolInitializer::setup(
            protocol_parameters.to_owned().into(),
            kes_secret_key_path,
            kes_period,
            stake.to_owned(),
            &mut rng,
        )?;

        Ok(protocol_initializer)
    }
}

/// The SingleSigner is the structure responsible for issuing SingleSignatures.
#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait SingleSigner: Sync + Send {
    /// Computes single signatures
    async fn compute_single_signatures(
        &self,
        protocol_message: &ProtocolMessage,
    ) -> StdResult<Option<SingleSignatures>>;

    /// Get party id
    fn get_party_id(&self) -> PartyId;
}

/// SingleSigner error structure.
#[derive(Error, Debug)]
pub enum SingleSignerError {
    /// Cryptographic Signer creation error.
    #[error("the protocol signer creation failed")]
    ProtocolSignerCreationFailure(#[source] StdError),

    /// Signature Error
    #[error("Signature Error")]
    SignatureFailed(#[source] StdError),

    /// Avk computation Error
    #[error("Aggregate verification key computation Error")]
    AggregateVerificationKeyComputationFailed(#[source] StdError),
}

/// Implementation of the SingleSigner.
pub struct MithrilSingleSigner {
    party_id: PartyId,
    epoch_service: EpochServiceWrapper,
    logger: Logger,
}

impl MithrilSingleSigner {
    /// Create a new instance of the MithrilSingleSigner.
    pub fn new(party_id: PartyId, epoch_service: EpochServiceWrapper, logger: Logger) -> Self {
        Self {
            party_id,
            epoch_service,
            logger: logger.new_with_component_name::<Self>(),
        }
    }

    async fn build_protocol_single_signer(&self) -> StdResult<ProtocolSingleSigner> {
        let epoch_service = self.epoch_service.read().await;
        let protocol_initializer =
            epoch_service
                .protocol_initializer()?
                .as_ref()
                .ok_or(anyhow!(
                    "Can not Sign or Compute AVK, No protocol initializer found for party_id: '{}'",
                    self.party_id.clone()
                ))?;

        let builder = SignerBuilder::new(
            &epoch_service.current_signers_with_stake().await?,
            &protocol_initializer.get_protocol_parameters().into(),
        )
        .with_context(|| "Mithril Single Signer can not build signer")
        .map_err(SingleSignerError::ProtocolSignerCreationFailure)?;

        let single_signer = builder
            .restore_signer_from_initializer(self.party_id.clone(), protocol_initializer.clone())
            .with_context(|| {
                format!(
                    "Mithril Single Signer can not restore signer with party_id: '{}'",
                    self.party_id.clone()
                )
            })
            .map_err(SingleSignerError::ProtocolSignerCreationFailure)?;

        Ok(single_signer)
    }
}

#[async_trait]
impl SingleSigner for MithrilSingleSigner {
    async fn compute_single_signatures(
        &self,
        protocol_message: &ProtocolMessage,
    ) -> StdResult<Option<SingleSignatures>> {
        let protocol_single_signer = self.build_protocol_single_signer().await?;

        info!(
            self.logger, "Signing protocol message";
            "protocol_message" =>  #?protocol_message,
            "signed message" => protocol_message.compute_hash().encode_hex::<String>()
        );
        let signatures = protocol_single_signer
            .sign(protocol_message)
            .with_context(|| {
                format!(
                    "Mithril Single Signer can not sign protocol_message: '{:?}'",
                    protocol_message
                )
            })
            .map_err(SingleSignerError::SignatureFailed)?;

        match &signatures {
            Some(signature) => {
                trace!(
                    self.logger,
                    "Party #{}: lottery #{:?} won",
                    signature.party_id,
                    &signature.won_indexes
                );
            }
            None => {
                warn!(
                    self.logger,
                    "No signature computed, all lotteries were lost"
                );
            }
        };

        Ok(signatures)
    }

    /// Get party id
    fn get_party_id(&self) -> PartyId {
        self.party_id.clone()
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use tokio::sync::RwLock;

    use crate::database::repository::{ProtocolInitializerRepository, StakePoolStore};
    use crate::database::test_helper::main_db_connection;
    use crate::services::MithrilEpochService;
    use crate::test_tools::TestLogger;
    use mithril_common::crypto_helper::ProtocolClerk;
    use mithril_common::entities::{Epoch, ProtocolMessagePartKey};
    use mithril_common::test_utils::MithrilFixtureBuilder;
    use mithril_persistence::store::StakeStorer;

    use super::*;

    #[tokio::test]
    async fn compute_single_signature_success() {
        let snapshot_digest = "digest".to_string();
        let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
        let current_signer = &fixture.signers_fixture()[0];
        let clerk = ProtocolClerk::from_signer(&current_signer.protocol_signer);
        let avk = clerk.compute_avk();
        let logger = TestLogger::stdout();
        let connection = Arc::new(main_db_connection().unwrap());
        let stake_store = {
            let store = Arc::new(StakePoolStore::new(connection.clone(), None));
            store
                .save_stakes(
                    Epoch(10).offset_to_signer_retrieval_epoch().unwrap(),
                    fixture.stake_distribution(),
                )
                .await
                .unwrap();
            store
        };
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(connection, None));
        let epoch_service =
            MithrilEpochService::new(stake_store, protocol_initializer_store, logger.clone())
                .set_data_to_default_or_fake(Epoch(10))
                .alter_data(|data| {
                    data.protocol_initializer = Some(current_signer.protocol_initializer.clone());
                    data.current_signers = fixture.signers();
                });

        let single_signer = MithrilSingleSigner::new(
            current_signer.party_id(),
            Arc::new(RwLock::new(epoch_service)),
            logger,
        );

        let mut protocol_message = ProtocolMessage::new();
        protocol_message.set_message_part(ProtocolMessagePartKey::SnapshotDigest, snapshot_digest);
        let sign_result = single_signer
            .compute_single_signatures(&protocol_message)
            .await
            .expect("single signer should not fail")
            .expect("single signer should produce a signature here");

        let expected_message = protocol_message.compute_hash().as_bytes().to_vec();
        let decoded_sig = sign_result.to_protocol_signature();
        assert!(
            decoded_sig
                .verify(
                    &fixture.protocol_parameters().into(),
                    &current_signer.protocol_signer.verification_key(),
                    &current_signer.protocol_signer.get_stake(),
                    &avk,
                    &expected_message
                )
                .is_ok(),
            "produced single signature should be valid"
        );
    }
}