dkls23/setup/
finish.rs

1// Copyright (c) Silence Laboratories Pte. Ltd. All Rights Reserved.
2// This software is licensed under the Silence Laboratories License Agreement.
3
4#![allow(missing_docs)]
5
6use std::{marker::PhantomData, time::Duration};
7
8use signature::{SignatureEncoding, Signer, Verifier};
9
10use sl_mpc_mate::message::InstanceId;
11
12use crate::{
13    setup::{
14        keys::{NoSignature, NoSigningKey, NoVerifyingKey},
15        FinalSignSetupMessage, ProtocolParticipant,
16    },
17    sign::PreSign,
18};
19
20/// Default Time-To-Live (TTL) value for messages in seconds
21const DEFAULT_TTL: u64 = 100; // smaller timeout might fail tests
22
23/// A message used for finalizing signing operations in a multi-party computation protocol.
24///
25/// This struct encapsulates all necessary information for completing a signing operation,
26/// including participant information, cryptographic keys, and protocol parameters.
27///
28/// # Type Parameters
29/// * `SK` - The type of signing key used for message signatures
30/// * `VK` - The type of verifying key used to verify message signatures
31/// * `MS` - The type of message signature
32/// * `PS` - The type of pre-signature used in the protocol
33pub struct SetupMessage<
34    SK = NoSigningKey,
35    VK = NoVerifyingKey,
36    MS = NoSignature,
37    PS = PreSign,
38> {
39    /// Index of the current party
40    party_idx: usize,
41    /// Signing key for the current party
42    sk: SK,
43    /// Verifying keys for all participants
44    vk: Vec<VK>,
45    /// Instance identifier for the protocol
46    instance: InstanceId,
47    /// Time-to-live duration for messages
48    ttl: Duration,
49    /// Hash of the message to be signed
50    hash: [u8; 32],
51    /// Pre-signature used in the final signing step
52    pre: PS,
53    /// Phantom data to hold the message signature type
54    marker: PhantomData<MS>,
55}
56
57impl<SK, VK, MS, PS> SetupMessage<SK, VK, MS, PS> {
58    /// Creates a new setup message for finalizing signing operations.
59    ///
60    /// # Arguments
61    /// * `instance` - Instance identifier for the protocol
62    /// * `party_idx` - Index of the current party
63    /// * `sk` - Signing key for the current party
64    /// * `vk` - Vector of verifying keys for all participants
65    /// * `pre` - Pre-signature used in the final signing step
66    ///
67    /// # Returns
68    /// A new `SetupMessage` instance with default TTL and zero hash
69    pub fn new(
70        instance: InstanceId,
71        party_idx: usize,
72        sk: SK,
73        vk: Vec<VK>,
74        pre: PS,
75    ) -> Self {
76        Self {
77            party_idx,
78            sk,
79            vk,
80            instance,
81            pre,
82            ttl: Duration::from_secs(DEFAULT_TTL),
83            hash: [0; 32],
84            marker: PhantomData,
85        }
86    }
87
88    /// Sets the hash of the message to be signed.
89    ///
90    /// # Arguments
91    /// * `hash` - The 32-byte hash of the message
92    ///
93    /// # Returns
94    /// The modified `SetupMessage` instance
95    pub fn with_hash(mut self, hash: [u8; 32]) -> Self {
96        self.hash = hash;
97        self
98    }
99
100    /// Sets a custom time-to-live duration for messages.
101    ///
102    /// # Arguments
103    /// * `ttl` - The new time-to-live duration
104    ///
105    /// # Returns
106    /// The modified `SetupMessage` instance
107    pub fn ttl(mut self, ttl: Duration) -> Self {
108        self.ttl = ttl;
109        self
110    }
111}
112
113impl<SK, VK, MS, PS> ProtocolParticipant for SetupMessage<SK, VK, MS, PS>
114where
115    SK: Signer<MS>,
116    MS: SignatureEncoding,
117    VK: AsRef<[u8]> + Verifier<MS>,
118{
119    type MessageSignature = MS;
120    type MessageSigner = SK;
121    type MessageVerifier = VK;
122
123    /// Returns the signing key for the current participant.
124    fn signer(&self) -> &Self::MessageSigner {
125        &self.sk
126    }
127
128    /// Returns the verifying key for a specific participant.
129    ///
130    /// # Arguments
131    /// * `index` - The index of the participant
132    ///
133    /// # Returns
134    /// A reference to the verifying key
135    fn verifier(&self, index: usize) -> &Self::MessageVerifier {
136        &self.vk[index]
137    }
138
139    /// Returns the instance identifier for the protocol.
140    fn instance_id(&self) -> &InstanceId {
141        &self.instance
142    }
143
144    /// Returns the time-to-live duration for messages.
145    fn message_ttl(&self) -> Duration {
146        self.ttl
147    }
148
149    /// Returns the index of the current participant.
150    fn participant_index(&self) -> usize {
151        self.party_idx
152    }
153
154    /// Returns the total number of participants in the protocol.
155    fn total_participants(&self) -> usize {
156        self.vk.len()
157    }
158}
159
160impl<SK, VK, MS> FinalSignSetupMessage for SetupMessage<SK, VK, MS, PreSign>
161where
162    SK: Signer<MS>,
163    MS: SignatureEncoding,
164    VK: AsRef<[u8]> + Verifier<MS>,
165{
166    /// Returns a reference to the pre-signature.
167    fn pre_signature(&self) -> &PreSign {
168        &self.pre
169    }
170
171    /// Returns the hash of the message to be signed.
172    fn message_hash(&self) -> [u8; 32] {
173        self.hash
174    }
175}