dkls23/setup/key_export/exporter.rs
1// Copyright (c) Silence Laboratories Pte. Ltd. All Rights Reserved.
2// This software is licensed under the Silence Laboratories License Agreement.
3
4use super::*;
5
6/// A message used for setting up key export operations in a multi-party computation protocol.
7///
8/// This struct encapsulates all necessary information for exporting a key,
9/// including participant information, cryptographic keys, and protocol parameters.
10///
11/// # Type Parameters
12/// * `SK` - The type of signing key used for message signatures
13/// * `VK` - The type of verifying key used to verify message signatures
14/// * `MS` - The type of message signature
15/// * `KS` - The type of keyshare used in the protocol
16pub struct KeyExporter<
17 SK = NoSigningKey,
18 VK = NoVerifyingKey,
19 MS = NoSignature,
20 KS = Keyshare,
21> {
22 /// ID of the current party
23 party_id: usize,
24 /// Signing key for the current party
25 sk: SK,
26 /// Verifying keys for all participants
27 vk: Vec<VK>,
28 /// Instance identifier for the protocol
29 inst: InstanceId,
30 /// Time-to-live duration for messages
31 ttl: Duration,
32 /// Public key of the receiver
33 pub_key: PublicKey,
34 /// Reference to the keyshare to be exported
35 share: Arc<KS>,
36 /// Phantom data to hold the message signature type
37 marker: PhantomData<MS>,
38}
39
40impl<SK, VK, MS, KS> KeyExporter<SK, VK, MS, KS> {
41 /// Creates a new setup message for key export operations.
42 ///
43 /// # Arguments
44 /// * `inst` - Instance identifier for the protocol
45 /// * `sk` - Signing key for the current party
46 /// * `party_id` - ID of the current party
47 /// * `vk` - Vector of verifying keys for all participants
48 /// * `share` - Reference to the keyshare to be exported
49 /// * `enc_pub_key` - Public key of the receiver
50 ///
51 /// # Returns
52 /// A new `KeyExporter` instance with default TTL
53 pub fn new(
54 inst: InstanceId,
55 sk: SK,
56 party_id: usize,
57 vk: Vec<VK>,
58 share: Arc<KS>,
59 enc_pub_key: PublicKey,
60 ) -> Self {
61 Self {
62 party_id,
63 sk,
64 vk,
65 inst,
66 ttl: Duration::from_secs(DEFAULT_TTL),
67 marker: PhantomData,
68 pub_key: enc_pub_key,
69 share,
70 }
71 }
72
73 /// Returns a reference to the keyshare to be exported.
74 pub fn keyshare(&self) -> &KS {
75 &self.share
76 }
77
78 /// Sets a custom time-to-live duration for messages.
79 ///
80 /// # Arguments
81 /// * `ttl` - The new time-to-live duration
82 ///
83 /// # Returns
84 /// The modified `KeyExporter` instance
85 pub fn with_ttl(mut self, ttl: Duration) -> Self {
86 self.ttl = ttl;
87 self
88 }
89}
90
91impl<SK, VK, MS, KS> ProtocolParticipant for KeyExporter<SK, VK, MS, KS>
92where
93 SK: Signer<MS>,
94 MS: SignatureEncoding,
95 VK: AsRef<[u8]> + Verifier<MS>,
96{
97 type MessageSignature = MS;
98 type MessageSigner = SK;
99 type MessageVerifier = VK;
100
101 /// Returns the total number of participants in the protocol.
102 fn total_participants(&self) -> usize {
103 self.vk.len()
104 }
105
106 /// Returns the index of the current participant.
107 fn participant_index(&self) -> usize {
108 self.party_id
109 }
110
111 /// Returns the instance identifier for the protocol.
112 fn instance_id(&self) -> &InstanceId {
113 &self.inst
114 }
115
116 /// Returns the time-to-live duration for messages.
117 fn message_ttl(&self) -> Duration {
118 self.ttl
119 }
120
121 /// Returns the verifying key for a specific participant.
122 ///
123 /// # Arguments
124 /// * `index` - The index of the participant
125 ///
126 /// # Returns
127 /// A reference to the verifying key
128 fn verifier(&self, index: usize) -> &Self::MessageVerifier {
129 &self.vk[index]
130 }
131
132 /// Returns the signing key for the current participant.
133 fn signer(&self) -> &Self::MessageSigner {
134 &self.sk
135 }
136}
137
138impl<SK, VK, MS, KS> setup::KeyExporterSetupMessage<PublicKey, KS>
139 for KeyExporter<SK, VK, MS, KS>
140where
141 SK: Signer<MS>,
142 MS: SignatureEncoding,
143 VK: AsRef<[u8]> + Verifier<MS>,
144{
145 /// Returns the public key of the receiver.
146 fn receiver_public_key(&self) -> &PublicKey {
147 &self.pub_key
148 }
149
150 /// Returns a reference to the keyshare to be exported.
151 fn keyshare(&self) -> &KS {
152 &self.share
153 }
154}