1use std::time::Duration;
8
9use derivation_path::DerivationPath;
10use k256::ProjectivePoint;
11pub use signature::{SignatureEncoding, Signer, Verifier};
12
13use sl_mpc_mate::message::{InstanceId, MessageTag, MsgId};
14
15use crate::{keygen::Keyshare, sign::PreSign};
16
17pub const SETUP_MESSAGE_TAG: MessageTag = MessageTag::tag(0);
19
20pub const ABORT_MESSAGE_TAG: MessageTag = MessageTag::tag(u64::MAX);
24
25pub struct AllOtherParties {
27 total: usize,
28 me: usize,
29 curr: usize,
30}
31
32impl Iterator for AllOtherParties {
33 type Item = usize;
34
35 fn next(&mut self) -> Option<Self::Item> {
36 loop {
37 let val = self.curr;
38
39 if val >= self.total {
40 return None;
41 }
42
43 self.curr += 1;
44
45 if val != self.me {
46 return Some(val);
47 }
48 }
49 }
50}
51
52impl ExactSizeIterator for AllOtherParties {
53 fn len(&self) -> usize {
54 self.total - 1
55 }
56}
57
58pub trait ProtocolParticipant {
60 type MessageSignature: SignatureEncoding;
63
64 type MessageSigner: Signer<Self::MessageSignature>;
66
67 type MessageVerifier: Verifier<Self::MessageSignature> + AsRef<[u8]>;
71
72 fn total_participants(&self) -> usize;
74
75 fn verifier(&self, index: usize) -> &Self::MessageVerifier;
78
79 fn signer(&self) -> &Self::MessageSigner;
81
82 fn participant_index(&self) -> usize;
85
86 fn instance_id(&self) -> &InstanceId;
89
90 fn message_ttl(&self) -> Duration;
92
93 fn participant_verifier(&self) -> &Self::MessageVerifier {
95 self.verifier(self.participant_index())
96 }
97
98 fn all_other_parties(&self) -> AllOtherParties {
100 AllOtherParties {
101 curr: 0,
102 total: self.total_participants(),
103 me: self.participant_index(),
104 }
105 }
106
107 fn msg_id(&self, receiver: Option<usize>, tag: MessageTag) -> MsgId {
110 self.msg_id_from(self.participant_index(), receiver, tag)
111 }
112
113 fn msg_id_from(
117 &self,
118 sender: usize,
119 receiver: Option<usize>,
120 tag: MessageTag,
121 ) -> MsgId {
122 let receiver = receiver
123 .map(|p| self.verifier(p))
124 .map(AsRef::<[u8]>::as_ref);
125
126 MsgId::new(
127 self.instance_id(),
128 self.verifier(sender).as_ref(),
129 receiver.as_ref().map(AsRef::as_ref),
130 tag,
131 )
132 }
133}
134
135impl<M: ProtocolParticipant> ProtocolParticipant for &M {
136 type MessageSignature = M::MessageSignature;
137 type MessageSigner = M::MessageSigner;
138 type MessageVerifier = M::MessageVerifier;
139
140 fn total_participants(&self) -> usize {
141 (**self).total_participants()
142 }
143
144 fn verifier(&self, index: usize) -> &Self::MessageVerifier {
145 (**self).verifier(index)
146 }
147
148 fn signer(&self) -> &Self::MessageSigner {
149 (**self).signer()
150 }
151
152 fn participant_index(&self) -> usize {
153 (**self).participant_index()
154 }
155
156 fn participant_verifier(&self) -> &Self::MessageVerifier {
157 (**self).participant_verifier()
158 }
159
160 fn instance_id(&self) -> &InstanceId {
161 (**self).instance_id()
162 }
163
164 fn message_ttl(&self) -> Duration {
165 (**self).message_ttl()
166 }
167}
168
169pub trait KeygenSetupMessage: ProtocolParticipant {
171 fn threshold(&self) -> u8;
173
174 fn participant_rank(&self, _party_index: usize) -> u8 {
177 0
178 }
179
180 fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32];
182
183 fn keyshare_extra(&self) -> &[u8] {
185 &[]
186 }
187}
188
189impl<M: KeygenSetupMessage> KeygenSetupMessage for &M {
190 fn threshold(&self) -> u8 {
191 (**self).threshold()
192 }
193
194 fn participant_rank(&self, party_index: usize) -> u8 {
195 (**self).participant_rank(party_index)
196 }
197
198 fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32] {
199 (**self).derive_key_id(public_key)
200 }
201
202 fn keyshare_extra(&self) -> &[u8] {
203 (**self).keyshare_extra()
204 }
205}
206
207pub trait PreSignSetupMessage: ProtocolParticipant {
209 fn keyshare(&self) -> &Keyshare;
211
212 fn chain_path(&self) -> &DerivationPath;
214
215 fn presignature_extra(&self) -> &[u8] {
217 &[]
218 }
219}
220
221pub trait FinalSignSetupMessage: ProtocolParticipant {
223 fn pre_signature(&self) -> &PreSign;
225
226 fn message_hash(&self) -> [u8; 32];
228}
229
230pub trait SignSetupMessage: PreSignSetupMessage {
232 fn message_hash(&self) -> [u8; 32];
234}
235
236pub trait KeyExporterSetupMessage<PK, KS>: ProtocolParticipant {
238 fn receiver_public_key(&self) -> &PK;
240
241 fn keyshare(&self) -> &KS;
243}
244
245pub trait KeyExportReceiverSetupMessage<SK>: ProtocolParticipant {
247 fn receiver_private_key(&self) -> &SK;
249
250 fn keyshare(&self) -> &Keyshare;
252}
253
254pub trait QuorumChangeSetupMessage<KS, PK>: ProtocolParticipant {
256 fn old_keyshare(&self) -> Option<&KS>;
258
259 fn new_threshold(&self) -> u8;
261
262 fn new_participant_rank(&self, _party_id: u8) -> u8 {
264 0
265 }
266
267 fn expected_public_key(&self) -> &PK;
269
270 fn new_party_id(&self, index: usize) -> Option<u8> {
272 self.new_party_indices()
273 .iter()
274 .position(|p| p == &index)
275 .map(|p| p as u8)
276 }
277
278 fn old_party_indices(&self) -> &[usize];
280
281 fn new_party_indices(&self) -> &[usize];
285
286 fn keyshare_extra(&self) -> &[u8] {
288 &[]
289 }
290
291 fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32];
293}
294
295impl<KS, PK, M: QuorumChangeSetupMessage<KS, PK>>
296 QuorumChangeSetupMessage<KS, PK> for &M
297{
298 fn old_keyshare(&self) -> Option<&KS> {
299 (**self).old_keyshare()
300 }
301
302 fn new_threshold(&self) -> u8 {
303 (**self).new_threshold()
304 }
305
306 fn expected_public_key(&self) -> &PK {
307 (**self).expected_public_key()
308 }
309
310 fn old_party_indices(&self) -> &[usize] {
311 (**self).old_party_indices()
312 }
313
314 fn new_party_indices(&self) -> &[usize] {
315 (**self).new_party_indices()
316 }
317
318 fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32] {
319 (**self).derive_key_id(public_key)
320 }
321}
322
323pub mod keygen;
325
326pub mod sign;
328
329pub mod finish;
331
332pub mod key_export;
334
335pub use keys::*;
336
337mod keys;
338
339pub mod quorum_change;