sl-dkls23 1.0.0-beta

DKLs23 implementation for threshold ECDSA signatures
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright (c) Silence Laboratories Pte. Ltd. All Rights Reserved.
// This software is licensed under the Silence Laboratories License Agreement.

//! This module provides utility functions used throughout the DKG protocol implementation,
//! including polynomial operations, key generation setup, and protocol execution helpers.
//! It also includes test utilities for protocol simulation and verification.

use std::collections::HashMap;

#[cfg(any(test, feature = "test-support"))]
use std::sync::Arc;

use k256::{
    elliptic_curve::subtle::ConstantTimeEq, NonZeroScalar, ProjectivePoint,
    Scalar, Secp256k1,
};
#[cfg(any(test, feature = "test-support"))]
use rand::prelude::*;
#[cfg(any(test, feature = "test-support"))]
use sha2::{Digest, Sha256};

use sl_mpc_mate::math::birkhoff_coeffs;

#[cfg(any(test, feature = "test-support"))]
use crate::setup::{keygen::SetupMessage, *};

#[cfg(any(test, feature = "test-support"))]
use crate::setup::{
    keygen::SetupMessage as KeygenSetupMessage,
    quorum_change::SetupMessage as QuorumChangeSetupMessage,
};
use crate::sign::get_lagrange_coeff_list;

#[cfg(any(test, feature = "test-support"))]
use super::Keyshare;

use super::KeygenError;

/// Computes the Lagrange coefficient for a given point in a polynomial.
///
/// This function calculates the Lagrange coefficient for a specific point `x_i`
/// given a list of points `x_i_list` and their corresponding party IDs.
///
/// # Arguments
/// * `x_i` - The point for which to compute the coefficient
/// * `x_i_list` - List of all points in the polynomial
/// * `party_ids` - List of party IDs corresponding to the points
///
/// # Returns
/// The computed Lagrange coefficient as a scalar value
pub(crate) fn get_lagrange_coeff(
    x_i: &NonZeroScalar,
    x_i_list: &[NonZeroScalar],
    party_ids: &[u8],
) -> Scalar {
    let mut coeff = Scalar::ONE;
    let x_i = x_i as &Scalar;
    for &party_id in party_ids {
        let x_j = &x_i_list[party_id as usize] as &Scalar;
        if x_i.ct_ne(x_j).into() {
            let sub = x_j - x_i;
            coeff *= x_j * &sub.invert().unwrap();
        }
    }

    coeff
}

/// Computes Birkhoff coefficients for a set of points with ranks.
///
/// This function calculates the Birkhoff coefficients for a set of points with
/// associated ranks, which are used in the Birkhoff interpolation process.
///
/// # Arguments
/// * `rank_list` - List of ranks for each point
/// * `x_i_list` - List of points in the polynomial
/// * `party_ids` - List of party IDs corresponding to the points
///
/// # Returns
/// A map of party IDs to their corresponding Birkhoff coefficients
pub(crate) fn get_birkhoff_coefficients(
    rank_list: &[u8],
    x_i_list: &[NonZeroScalar],
    party_ids: &[u8],
) -> HashMap<usize, Scalar> {
    let params = party_ids
        .iter()
        .map(|&pid| {
            (x_i_list[pid as usize], rank_list[pid as usize] as usize)
        })
        .collect::<Vec<_>>();

    let betta_vec = birkhoff_coeffs::<Secp256k1>(&params);

    party_ids
        .iter()
        .zip(betta_vec)
        .map(|(&pid, w_i)| (pid as usize, w_i))
        .collect::<HashMap<_, _>>()
}

/// Verifies that a secret can be recovered from the given shares.
///
/// This function checks whether the provided shares can be used to reconstruct
/// the original secret by verifying that the reconstructed public key matches
/// the expected public key.
///
/// # Arguments
/// * `x_i_list` - List of x-coordinates for the shares
/// * `rank_list` - List of ranks for each share
/// * `big_s_list` - List of public key components
/// * `public_key` - The expected public key
///
/// # Returns
/// `Ok(())` if the secret can be recovered, or an error if verification fails
#[allow(dead_code)]
pub(crate) fn check_secret_recovery(
    x_i_list: &[NonZeroScalar],
    rank_list: &[u8],
    big_s_list: &[ProjectivePoint],
    public_key: &ProjectivePoint,
) -> Result<(), KeygenError> {
    // If ranks are all zero, then we use lagrange interpolation
    let exp_public_key = if rank_list.iter().all(|&r| r == 0) {
        let coeff_vector = get_lagrange_coeff_list(x_i_list, |x| x);
        big_s_list
            .iter()
            .zip(coeff_vector)
            .fold(ProjectivePoint::IDENTITY, |acc, (point, betta_i)| {
                acc + point * &betta_i
            })
    } else {
        // Otherwise, we use Birkhoff interpolation
        let mut party_params_list = x_i_list
            .iter()
            .zip(rank_list)
            .zip(big_s_list)
            .collect::<Vec<((&NonZeroScalar, &u8), &ProjectivePoint)>>();

        party_params_list.sort_by_key(|((_, &n_i), _)| n_i);

        let params = party_params_list
            .iter()
            .map(|((&x_i, &n_i), _)| (x_i, n_i as usize))
            .collect::<Vec<_>>();

        let betta_vector = birkhoff_coeffs(&params);

        party_params_list
            .into_iter()
            .map(|((_, _), &big_s_i)| big_s_i)
            .zip(betta_vector)
            .fold(ProjectivePoint::IDENTITY, |acc, (point, betta_i)| {
                acc + point * betta_i
            })
    };

    (public_key == &exp_public_key)
        .then_some(())
        .ok_or(KeygenError::PublicKeyMismatch)
}

/// Generates setup messages and seeds for DKG parties.
///
/// This function creates the necessary setup messages and seeds for all parties
/// participating in the DKG protocol. It handles both the case where ranks are
/// provided and where they are not.
///
/// # Arguments
/// * `instance` - Optional instance identifier for the protocol
/// * `t` - Threshold value for the protocol
/// * `n` - Total number of parties
/// * `ranks` - Optional list of ranks for each party
///
/// # Returns
/// A vector of tuples containing setup messages and seeds for each party
#[cfg(any(test, feature = "test-support"))]
pub fn setup_keygen(
    instance: Option<[u8; 32]>,
    t: u8,
    n: u8,
    ranks: Option<&[u8]>,
) -> Vec<(KeygenSetupMessage, [u8; 32])> {
    use std::time::Duration;

    use sl_mpc_mate::message::InstanceId;

    let ranks = if let Some(ranks) = ranks {
        assert_eq!(ranks.len(), n as usize);
        ranks.to_vec()
    } else {
        vec![0u8; n as usize]
    };

    let instance = instance.unwrap_or_else(rand::random);

    // a signing key for each party.
    let party_sk: Vec<NoSigningKey> = std::iter::repeat_with(|| NoSigningKey)
        .take(n as usize)
        .collect();

    let party_vk: Vec<NoVerifyingKey> = party_sk
        .iter()
        .enumerate()
        .map(|(party_id, _)| NoVerifyingKey::new(party_id))
        .collect();

    party_sk
        .into_iter()
        .enumerate()
        .map(|(party_id, sk)| {
            SetupMessage::new(
                InstanceId::new(instance),
                sk,
                party_id,
                party_vk.clone(),
                &ranks,
                t as usize,
            )
            .with_ttl(Duration::from_secs(1000)) // for dkls-metrics benchmarks
        })
        .map(|setup| {
            let mixin = [setup.participant_index() as u8 + 1];

            (
                setup,
                Sha256::new()
                    .chain_update(instance)
                    .chain_update(b"party-seed")
                    .chain_update(mixin)
                    .finalize()
                    .into(),
            )
        })
        .collect::<Vec<_>>()
}

/// Executes the DKG protocol with the given parameters.
///
/// This function runs the DKG protocol for a set of parties with the specified
/// threshold and number of participants. It returns the generated key shares.
///
/// # Arguments
/// * `t` - Threshold value for the protocol
/// * `n` - Total number of parties
/// * `ranks` - Optional list of ranks for each party
///
/// # Returns
/// A vector of key shares, one for each party
#[cfg(any(test, feature = "test-support"))]
pub async fn gen_keyshares(
    t: u8,
    n: u8,
    ranks: Option<&[u8]>,
) -> Vec<Arc<Keyshare>> {
    let coord = sl_mpc_mate::coord::SimpleMessageRelay::new();

    let mut parties = tokio::task::JoinSet::new();
    for (setup, seed) in setup_keygen(None, t, n, ranks) {
        parties.spawn({
            let relay = coord.connect();
            crate::keygen::run(setup, seed, relay)
        });
    }

    let mut shares = vec![];

    while let Some(fini) = parties.join_next().await {
        if let Err(ref err) = fini {
            println!("error {err:?}");
        } else {
            match fini.unwrap() {
                Err(err) => panic!("err {:?}", err),
                Ok(share) => shares.push(Arc::new(share)),
            }
        }
    }

    shares.sort_by_key(|share| share.party_id);

    shares
}

/// Generates setup messages and seeds for quorum change parties.
///
/// This function creates setup messages and seeds for all parties participating
/// in a quorum change protocol, including both existing and new parties.
///
/// # Arguments
/// * `old_keyshares` - Existing key shares from the current quorum
/// * `new_threshold` - New threshold value for the protocol
/// * `new_n_i_list` - List of ranks for new parties
///
/// # Returns
/// A vector of tuples containing setup messages and seeds for each party
#[cfg(any(test, feature = "test-support"))]
pub fn setup_quorum_change(
    old_keyshares: &[Arc<Keyshare>],
    new_threshold: u8,
    new_n_i_list: &[u8],
) -> Vec<(QuorumChangeSetupMessage, [u8; 32])> {
    let old_threshold = old_keyshares[0].threshold as usize;
    let old_participants = old_keyshares.len();
    assert!(old_keyshares.len() >= old_threshold);

    let public_key = old_keyshares[0].public_key();

    let total_parties = old_participants + new_n_i_list.len();

    let old_parties = (0..old_participants).collect::<Vec<usize>>();
    let new_parties = new_n_i_list
        .iter()
        .enumerate()
        .map(|(p, &r)| ((p + old_participants), r))
        .collect::<Vec<_>>();

    let mut rng = rand::thread_rng();

    let instance = rng.gen::<[u8; 32]>().into();

    // a signing key for each party.
    let party_sk: Vec<NoSigningKey> = std::iter::repeat_with(|| NoSigningKey)
        .take(total_parties)
        .collect();

    let party_vk: Vec<NoVerifyingKey> = party_sk
        .iter()
        .enumerate()
        .map(|(p, _)| NoVerifyingKey::new(p))
        .collect();

    party_sk
        .into_iter()
        .enumerate()
        .map(|(p, sk)| {
            // first old_parties_n with Keyshare
            let keyshare = if p < old_participants {
                Some(old_keyshares[p].clone())
            } else {
                None
            };

            QuorumChangeSetupMessage::new(
                instance,
                p,
                &old_parties,
                &new_parties,
                new_threshold as usize,
                sk,
                party_vk.clone(),
                public_key,
            )
            .with_keyshare_opt(keyshare)
        })
        .map(|setup| (setup, rng.gen()))
        .collect::<Vec<_>>()
}

/// Generates setup messages and seeds for extending the quorum with new parties.
///
/// This function creates setup messages and seeds for a quorum change that
/// adds new parties to the existing quorum.
///
/// # Arguments
/// * `old_keyshares` - Existing key shares from the current quorum
/// * `new_threshold` - New threshold value for the protocol
/// * `new_participants_len` - Number of new participants to add
/// * `new_n_i_list` - List of ranks for new parties
///
/// # Returns
/// A vector of tuples containing setup messages and seeds for each party
#[cfg(any(test, feature = "test-support"))]
pub fn setup_quorum_change_extend_parties(
    old_keyshares: &[Arc<Keyshare>],
    new_threshold: u8,
    new_participants_len: u8,
    new_n_i_list: &[u8],
) -> Vec<(QuorumChangeSetupMessage, [u8; 32])> {
    let new_n = old_keyshares.len() + new_participants_len as usize;

    let old_threshold = old_keyshares[0].threshold as usize;
    let old_participants = old_keyshares.len();
    assert!(old_keyshares.len() >= old_threshold);

    let public_key = old_keyshares[0].public_key();

    let total_parties = new_n;

    let old_parties = (0..old_keyshares.len()).collect::<Vec<_>>();
    let new_parties = new_n_i_list
        .iter()
        .enumerate()
        .map(|(p, &r)| (p, r))
        .collect::<Vec<_>>();

    let mut rng = rand::thread_rng();

    let instance = rng.gen::<[u8; 32]>().into();

    // a signing key for each party.
    let party_sk: Vec<NoSigningKey> = std::iter::repeat_with(|| NoSigningKey)
        .take(total_parties)
        .collect();
    let party_vk: Vec<NoVerifyingKey> = party_sk
        .iter()
        .enumerate()
        .map(|(p, _)| NoVerifyingKey::new(p))
        .collect();

    party_sk
        .into_iter()
        .enumerate()
        .map(|(p, sk)| {
            // first old_parties_n with Keyshare
            let keyshare = if p < old_participants {
                Some(old_keyshares[p].clone())
            } else {
                None
            };

            QuorumChangeSetupMessage::new(
                instance,
                p,
                &old_parties,
                &new_parties,
                new_threshold as usize,
                sk,
                party_vk.clone(),
                public_key,
            )
            .with_keyshare_opt(keyshare)
        })
        .map(|setup| (setup, rng.gen()))
        .collect::<Vec<_>>()
}

/// Generates setup messages and seeds for changing the quorum threshold.
///
/// This function creates setup messages and seeds for a quorum change that
/// modifies the threshold value while keeping the same set of parties.
///
/// # Arguments
/// * `old_keyshares` - Existing key shares from the current quorum
/// * `new_threshold` - New threshold value for the protocol
/// * `new_n_i_list` - List of ranks for the parties
///
/// # Returns
/// A vector of tuples containing setup messages and seeds for each party
#[cfg(any(test, feature = "test-support"))]
pub fn setup_quorum_change_threshold(
    old_keyshares: &[Arc<Keyshare>],
    new_threshold: u8,
    new_n_i_list: &[u8],
) -> Vec<(QuorumChangeSetupMessage, [u8; 32])> {
    assert!(old_keyshares.len() >= old_keyshares[0].threshold as usize);

    let total_parties = old_keyshares.len();

    let old_participants = old_keyshares.len();

    let public_key = old_keyshares[0].public_key();

    let old_parties = (0..old_keyshares.len()).collect::<Vec<_>>();

    let new_parties =
        new_n_i_list.iter().copied().enumerate().collect::<Vec<_>>();

    let mut rng = rand::thread_rng();

    let instance = rng.gen::<[u8; 32]>().into();

    // a signing key for each party.
    let party_sk: Vec<NoSigningKey> = std::iter::repeat_with(|| NoSigningKey)
        .take(total_parties)
        .collect();
    let party_vk: Vec<NoVerifyingKey> = party_sk
        .iter()
        .enumerate()
        .map(|(p, _)| NoVerifyingKey::new(p))
        .collect();

    party_sk
        .into_iter()
        .enumerate()
        .map(|(p, sk)| {
            // first old_parties_n with Keyshare
            let keyshare = if p < old_participants {
                Some(old_keyshares[p].clone())
            } else {
                None
            };

            QuorumChangeSetupMessage::new(
                instance,
                p,
                &old_parties,
                &new_parties,
                new_threshold as usize,
                sk,
                party_vk.clone(),
                public_key,
            )
            .with_keyshare_opt(keyshare)
        })
        .map(|setup| (setup, rng.gen()))
        .collect::<Vec<_>>()
}