sl-messages 1.2.1

Message exchange for MPC protocols
Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) Silence Laboratories Pte. Ltd. All Rights Reserved.
// This software is licensed under the Silence Laboratories License Agreement.

use std::time::Duration;

use derivation_path::DerivationPath;
use signature::{SignatureEncoding, Signer, Verifier};

use crate::message::{InstanceId, MessageTag, MsgId};

/// An iterator for parties in range 0..total except me.
pub struct AllOtherParties {
    total: usize,
    me: usize,
    curr: usize,
}

impl Iterator for AllOtherParties {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let val = self.curr;

            if val >= self.total {
                return None;
            }

            self.curr += 1;

            if val != self.me {
                return Some(val);
            }
        }
    }
}

impl ExactSizeIterator for AllOtherParties {
    fn len(&self) -> usize {
        self.total - 1
    }
}

/// A type that provides protocol participant details.
///
/// Construction of a value of this type should carefully validate the
/// verifying keys of all parties. It is crucial to recognize the keys
/// of all participants using either a database of known keys or X.509
/// certificates.
///
/// The type defines how messages will be signed and how to verify the
/// signatures.
pub trait ProtocolParticipant {
    /// Type of a signature, added at end of all broadcast messages
    /// passed between participants.
    type MessageSignature: SignatureEncoding;

    /// Type to sign broadcast messages, some kind of a SecretKey.
    type MessageSigner: Signer<Self::MessageSignature>;

    /// Type used to verify signed messages, a verifying key. `AsRef<[u8]>` is
    /// used to get external representation of the key to derive message ID.
    type MessageVerifier: Verifier<Self::MessageSignature> + AsRef<[u8]>;

    /// Returns total number of participants of a distributed protocol.
    fn total_participants(&self) -> usize;

    /// Returns the verifying key for messages from a participant with
    /// the given index.
    fn verifier(&self, index: usize) -> &Self::MessageVerifier;

    /// Returns a signer to sign messages from the participant.
    fn signer(&self) -> &Self::MessageSigner;

    /// Returns an index of the participant in a protocol.
    /// This is a value in range 0..self.total_participants()
    fn participant_index(&self) -> usize;

    /// Returns the protocol's execution instance ID.
    ///
    /// Each execution of a distributed protocol requires a unique
    /// instance ID to derive the IDs of all messages within that
    /// execution.
    fn instance_id(&self) -> &InstanceId;

    /// Returns the message time-to-live.
    fn message_ttl(&self) -> Duration;

    /// Returns a reference to the participant's own verifier.
    fn participant_verifier(&self) -> &Self::MessageVerifier {
        self.verifier(self.participant_index())
    }

    /// Returns an iterator of all participants except self.
    fn all_other_parties(&self) -> AllOtherParties {
        AllOtherParties {
            curr: 0,
            total: self.total_participants(),
            me: self.participant_index(),
        }
    }

    /// Generates an ID for a message from this party to another party,
    /// or for a broadcast message if the receiver is `None`.
    fn msg_id(&self, receiver: Option<usize>, tag: MessageTag) -> MsgId {
        self.msg_id_from(self.participant_index(), receiver, tag)
    }

    /// Generates an ID for a message from a given sender to a given receiver.
    /// The receiver is identified by its index and is `None` for a broadcast
    /// message.
    fn msg_id_from(
        &self,
        sender: usize,
        receiver: Option<usize>,
        tag: MessageTag,
    ) -> MsgId {
        let receiver = receiver
            .map(|p| self.verifier(p))
            .map(AsRef::<[u8]>::as_ref);

        MsgId::new(
            self.instance_id(),
            self.verifier(sender).as_ref(),
            receiver.as_ref().map(AsRef::as_ref),
            tag,
        )
    }

    /// Hash of the setup message received from the initiator that
    /// starts the protocol execution.
    fn setup_hash(&self) -> &[u8] {
        &[]
    }
}

impl<M: ProtocolParticipant> ProtocolParticipant for &M {
    type MessageSignature = M::MessageSignature;
    type MessageSigner = M::MessageSigner;
    type MessageVerifier = M::MessageVerifier;

    fn total_participants(&self) -> usize {
        (**self).total_participants()
    }

    fn verifier(&self, index: usize) -> &Self::MessageVerifier {
        (**self).verifier(index)
    }

    fn signer(&self) -> &Self::MessageSigner {
        (**self).signer()
    }

    fn participant_index(&self) -> usize {
        (**self).participant_index()
    }

    fn participant_verifier(&self) -> &Self::MessageVerifier {
        (**self).participant_verifier()
    }

    fn instance_id(&self) -> &InstanceId {
        (**self).instance_id()
    }

    fn message_ttl(&self) -> Duration {
        (**self).message_ttl()
    }

    fn setup_hash(&self) -> &[u8] {
        (**self).setup_hash()
    }
}

/// A type that provides details for key generation protocols.
///
/// Construction of a value of this type is an approval to execute a
/// key generation protocol. An application should carefully validate
/// the source data, usually a setup message received from an
/// initiator of protocol execution.
///
pub trait KeygenSetupMessage: ProtocolParticipant {
    /// Threshold parameter.
    fn threshold(&self) -> u8;

    /// Returns rank of a participant with the given index.
    /// May panic if index is out of range.
    fn participant_rank(&self, _party_index: usize) -> u8 {
        0
    }

    /// Returns the `key_id` of the newly generated key share using
    /// the public key of the generated distributed key.
    ///
    /// The implementation might return `hash(public_key)`.
    ///
    fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32];

    /// Additional data to incorporate into the resulting KeyShare.
    ///
    /// This mechanism allows incorporating application-specific data
    /// into the key share. Later, during signature generation, an
    /// application may use this data to execute access control
    /// policies.
    fn keyshare_extra(&self) -> &[u8] {
        &[]
    }
}

impl<M: KeygenSetupMessage> KeygenSetupMessage for &M {
    fn threshold(&self) -> u8 {
        (**self).threshold()
    }

    fn participant_rank(&self, party_index: usize) -> u8 {
        (**self).participant_rank(party_index)
    }

    fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32] {
        (**self).derive_key_id(public_key)
    }

    fn keyshare_extra(&self) -> &[u8] {
        (**self).keyshare_extra()
    }
}

/// A type that provides details for pre-signature generation protocols.
///
/// Construction of a value of this type constitutes approval to
/// execute a pre-signature protocol. An application must carefully
/// validate the source data, usually a setup message received from an
/// initiator of signature generation. In particular, the setup
/// message must contain the `key_id` of the key share. Usually, an
/// application executes some access control policy to decide whether
/// the initiator has the right to access the key share.
///
/// An application is responsible for loading the key share and may
/// use `keyshare_extra()` as input for the access control policy.
///
pub trait PreSignSetupMessage<KS>: ProtocolParticipant {
    /// A shared reference to a key share.
    fn keyshare(&self) -> &KS;

    /// Returns the key chain path for this signature.
    fn chain_path(&self) -> &DerivationPath;

    /// Additional data to incorporate into the resulting pre-signature.
    fn presignature_extra(&self) -> &[u8] {
        &[]
    }

    /// Returns the list of banned parties.
    fn banned_parties(&self) -> &[u8] {
        &[]
    }
}

/// A type that provides details for finalizing signature generation
/// from a pre-calculated pre-signature object within the protocol.
///
/// Construction of a value of this type constitutes approval to
/// execute the protocol. An application must carefully validate the
/// source data, usually a setup message received from an initiator of
/// signature generation.
///
/// **Critical Security Warning:** Pay extra attention to the
/// implementation of the `message_hash()` method.
///
/// Always send the full raw message to be signed in the setup message
/// and always calculate the hash function independently by each
/// participant.
///
/// Using a message hash from the setup message received over the
/// network is *strongly discouraged* and can lead to severe security
/// vulnerabilities.
///
pub trait FinalSignSetupMessage<PS>: ProtocolParticipant {
    /// Returns the pre-signature created by `sign::pre_signature()`.
    fn pre_signature(&self) -> &PS;

    /// Computes hash of a message to sign.
    fn message_hash(&self) -> [u8; 32];
}

/// A setup message for `sign::run()`.
pub trait SignSetupMessage<KS>: PreSignSetupMessage<KS> {
    /// Hash of a message to sign.
    fn message_hash(&self) -> [u8; 32];
}

/// A setup message for key export.
pub trait KeyExporterSetupMessage<PK, KS>: ProtocolParticipant {
    /// Returns the public key of the receiving party.
    fn receiver_public_key(&self) -> &PK;

    /// A shared reference to a key share.
    fn keyshare(&self) -> &KS;
}

/// A setup message for a receiver of an exported key.
pub trait KeyExportReceiverSetupMessage<KS, SK>: ProtocolParticipant {
    /// Private key to decrypt P2P messages.
    fn receiver_private_key(&self) -> &SK;

    /// A shared reference to a key share.
    fn keyshare(&self) -> &KS;
}

/// A setup message for quorum_change::run()
pub trait QuorumChangeSetupMessage<KS, PK>: ProtocolParticipant {
    /// A shared reference to a key share.
    fn old_keyshare(&self) -> Option<&KS>;

    /// New threshold parameter.
    fn new_threshold(&self) -> u8;

    /// New participant rank. Panics if `party_id` is out of range.
    fn new_participant_rank(&self, _party_id: u8) -> u8 {
        0
    }

    /// Expected public key.
    fn expected_public_key(&self) -> &PK;

    /// Returns `new_party_id` for `party_index`.
    fn new_party_id(&self, index: usize) -> Option<u8> {
        self.new_party_indices()
            .iter()
            .position(|p| p == &index)
            .map(|p| p as u8)
    }

    /// List of old party indices.
    fn old_party_indices(&self) -> &[usize];

    /// List of indices of new parties in a list of protocol
    /// participants. Order of indices defines assignment of party-id
    /// to new key shares.
    fn new_party_indices(&self) -> &[usize];

    /// Additional data to incorporate into the resulting key share.
    fn keyshare_extra(&self) -> &[u8] {
        &[]
    }

    /// Derives a `key_id` from a public key.
    fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32];
}

impl<KS, PK, M: QuorumChangeSetupMessage<KS, PK>>
    QuorumChangeSetupMessage<KS, PK> for &M
{
    fn old_keyshare(&self) -> Option<&KS> {
        (**self).old_keyshare()
    }

    fn new_threshold(&self) -> u8 {
        (**self).new_threshold()
    }

    fn expected_public_key(&self) -> &PK {
        (**self).expected_public_key()
    }

    fn old_party_indices(&self) -> &[usize] {
        (**self).old_party_indices()
    }

    fn new_party_indices(&self) -> &[usize] {
        (**self).new_party_indices()
    }

    fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32] {
        (**self).derive_key_id(public_key)
    }
}

/// A setup message for WTSS DKG.
pub trait WeightedKeygenSetupMessage: ProtocolParticipant {
    /// Returns rank of a participant with the given index.
    /// May panic if index is out of range.
    fn participant_weight(&self, _party_index: usize) -> u16 {
        1
    }

    /// Threshold parameter for weighted TSS.
    fn weighted_threshold(&self) -> u16;

    /// Returns the `key_id` of the newly generated key share using
    /// the public key of the generated distributed key.
    ///
    /// The implementation might return `hash(public_key)`.
    ///
    fn derive_key_id(&self, public_key: &[u8]) -> [u8; 32];

    /// Additional data to incorporate into the resulting KeyShare.
    ///
    /// This mechanism allows incorporating application-specific data
    /// into the key share. Later, during signature generation, an
    /// application may use this data to execute access control
    /// policies.
    fn keyshare_extra(&self) -> &[u8] {
        &[]
    }
}

/// A setup message for WTSS QC.
pub trait WeightedQuorumChangeSetupMessage<KS, PK>:
    QuorumChangeSetupMessage<KS, PK>
{
    /// New participant weight.
    fn new_participant_weight(&self, party_id: usize) -> u16;
}