keep-core 0.7.5

Core library for Keep - sovereign key management for Nostr and Bitcoin
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// SPDX-FileCopyrightText: © 2026 PrivKey LLC
// SPDX-License-Identifier: MIT

//! In-process DKG (#454) using the standard frost-secp256k1-tr 3-round protocol.
//!
//! `keep frost network dkg` today requires `--hardware <path>` so the private
//! polynomial coefficients live entirely on an attached signer (M5Stack ESP32,
//! etc.). That is safer for production but blocks any user without hardware
//! from participating and blocks CI from exercising DKG end-to-end (#436).
//!
//! [`SoftwareDkgSession`] wraps `frost-secp256k1-tr::keys::dkg::{part1,part2,
//! part3}` behind the same six-step state machine `keep-cli` uses to drive
//! the hardware signer (`init` -> `round1` -> `round1_peer` -> `round2` ->
//! `receive_share` -> `finalize`), so the CLI can dispatch on the presence
//! of `--hardware` without any protocol-level branching.
//!
//! ## Wire compatibility
//!
//! This module speaks a **software-only** wire format ([`SOFTWARE_DKG_VERSION`]
//! is emitted alongside every round1 package). The keep-esp32 firmware ships
//! its own binary format the software side does not know how to parse, and
//! the mirror is also true, so software peers and hardware peers cannot DKG
//! with each other on this branch. Mixed groups must all be one or the
//! other; the CLI marks this explicitly so a bystander does not silently
//! DKG-with-nobody.
//!
//! ## Threat model reminder
//!
//! Software DKG puts every participant's polynomial + coefficient
//! commitments in that participant's process memory for the duration of
//! the run. That is the same trust surface `frost generate` (trusted
//! dealer) has for the full group secret, but SPLIT across the group and
//! reset once `part3` finishes: no single machine ever sees any other
//! participant's polynomial. That is materially better than trusted-dealer,
//! materially worse than hardware DKG. Operators of high-value groups
//! should still prefer hardware; software is here to unblock everyone else
//! and to give CI a real DKG path.

use std::collections::BTreeMap;

use frost_secp256k1_tr::{
    keys::dkg::{part1, part2, part3, round1, round2},
    keys::{KeyPackage, PublicKeyPackage},
    rand_core::OsRng,
    Identifier,
};
use zeroize::Zeroizing;

use crate::error::{KeepError, Result};

/// Wire discriminator. Bumped when the serialized package layout changes so
/// old peers reject the newer packages loudly rather than mis-decoding.
pub const SOFTWARE_DKG_VERSION: u8 = 1;

/// One participant's software DKG state machine. Same six-step public
/// interface `keep-cli` drives against the hardware signer, so the CLI
/// dispatches on the presence of `--hardware` without any protocol branching.
///
/// The state machine is linear and validates every transition, so an
/// out-of-order call (e.g. `dkg_round2` before every peer's round1 arrived)
/// returns a clear error rather than silently producing garbage.
pub struct SoftwareDkgSession {
    identifier: Identifier,
    max_signers: u16,
    min_signers: u16,
    our_index: u16,
    state: DkgState,
}

enum DkgState {
    Init,
    AwaitingRound1 {
        secret: round1::SecretPackage,
        our_round1: round1::Package,
        peer_round1: BTreeMap<Identifier, round1::Package>,
    },
    AwaitingRound2 {
        secret: round2::SecretPackage,
        round1: BTreeMap<Identifier, round1::Package>,
        peer_round2: BTreeMap<Identifier, round2::Package>,
    },
    Finalized,
}

/// Wire-format round1 package emitted by [`SoftwareDkgSession::round1`]. The
/// CLI encodes this as JSON and publishes it on the DKG topic; peers running
/// this same version decode it back via [`SoftwareDkgSession::round1_peer`].
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SoftwareRound1Wire {
    /// Discriminator so hardware peers reject the payload rather than
    /// silently mis-decoding. Must equal [`SOFTWARE_DKG_VERSION`].
    pub software_dkg_version: u8,
    /// The 1-indexed participant identifier that produced this package.
    pub sender_index: u16,
    /// Hex-encoded `frost_secp256k1_tr::keys::dkg::round1::Package::serialize`
    /// output.
    pub package_hex: String,
}

/// Wire-format round2 share, one per recipient. Encrypted per-recipient at
/// the CLI level (nip44) before publishing.
///
/// `package_hex` is a secret signing share, so `Debug` is redacted and must
/// stay that way: this struct crosses `#[tracing::instrument]` boundaries in
/// the CLI where a derived `Debug` would leak the share into logs.
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct SoftwareRound2Wire {
    /// Discriminator, same rule as [`SoftwareRound1Wire`].
    pub software_dkg_version: u8,
    /// The 1-indexed participant identifier that produced this share.
    pub sender_index: u16,
    /// Hex-encoded `round2::Package` for the specific recipient. Secret.
    pub package_hex: String,
}

impl std::fmt::Debug for SoftwareRound2Wire {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SoftwareRound2Wire")
            .field("software_dkg_version", &self.software_dkg_version)
            .field("sender_index", &self.sender_index)
            .field("package_hex", &"<redacted>")
            .finish()
    }
}

impl Drop for SoftwareRound2Wire {
    fn drop(&mut self) {
        // `package_hex` is a plaintext signing share. Scrub it on drop so the
        // hex string does not linger in freed heap; this covers both the wire
        // we produce in `round2()` and the one serde deserializes on receive.
        use zeroize::Zeroize;
        self.package_hex.zeroize();
    }
}

/// Result returned from [`SoftwareDkgSession::finalize`], mirroring the
/// hardware finalize shape so the CLI can share printing / storage code.
pub struct SoftwareDkgResult {
    /// This participant's finalized KeyPackage. Store via
    /// `Keep::frost_store_dkg_share`.
    pub key_package: KeyPackage,
    /// The group's PublicKeyPackage. Also stored via the same helper so the
    /// verifying shares for every peer are available during aggregation.
    pub public_key_package: PublicKeyPackage,
    /// The BIP-340 x-only 32-byte group pubkey the shares reconstruct to.
    pub group_pubkey: [u8; 32],
    /// This participant's 1-indexed share number.
    pub our_index: u16,
}

impl SoftwareDkgSession {
    /// Set up state for a `threshold`-of-`participants` DKG run where this
    /// process holds share `our_index` (1-indexed). Rejects thresholds
    /// outside `2..=participants` and indexes outside `1..=participants` so
    /// a caller cannot pass a validated config to `part1` and get an
    /// `UnknownIdentifier` late in the run.
    pub fn init(threshold: u16, participants: u16, our_index: u16) -> Result<Self> {
        if threshold < 2 || threshold > participants {
            return Err(KeepError::Frost(format!(
                "threshold must satisfy 2 <= t ({threshold}) <= n ({participants})"
            )));
        }
        if our_index < 1 || our_index > participants {
            return Err(KeepError::Frost(format!(
                "share index must satisfy 1..={participants}, got {our_index}"
            )));
        }
        let identifier = Identifier::try_from(our_index).map_err(|e| {
            KeepError::Frost(format!(
                "share index {our_index} is not a valid FROST identifier: {e}"
            ))
        })?;
        Ok(Self {
            identifier,
            max_signers: participants,
            min_signers: threshold,
            our_index,
            state: DkgState::Init,
        })
    }

    /// Run frost-core's `part1` and return the round1 wire package this
    /// participant broadcasts to every peer. Must be called before any
    /// `round1_peer` step and exactly once per session.
    pub fn round1(&mut self) -> Result<SoftwareRound1Wire> {
        if !matches!(self.state, DkgState::Init) {
            return Err(KeepError::Frost(
                "SoftwareDkgSession::round1 called out of order (already past round 1)".into(),
            ));
        }
        let (secret, package) = part1(self.identifier, self.max_signers, self.min_signers, OsRng)
            .map_err(|e| KeepError::Frost(format!("DKG part1 failed: {e}")))?;
        let package_bytes = package
            .serialize()
            .map_err(|e| KeepError::Frost(format!("serialize round1 package: {e}")))?;
        let wire = SoftwareRound1Wire {
            software_dkg_version: SOFTWARE_DKG_VERSION,
            sender_index: self.our_index,
            package_hex: hex::encode(&package_bytes),
        };
        self.state = DkgState::AwaitingRound1 {
            secret,
            our_round1: package,
            peer_round1: BTreeMap::new(),
        };
        Ok(wire)
    }

    /// Consume a peer's round1 wire package. Rejects a duplicate sender, a
    /// self-echo, and a version mismatch. Returns `true` if this call
    /// completed the round1 set (`peers = participants - 1`).
    pub fn round1_peer(&mut self, wire: &SoftwareRound1Wire) -> Result<bool> {
        let peer_round1 = match &mut self.state {
            DkgState::AwaitingRound1 { peer_round1, .. } => peer_round1,
            _ => {
                return Err(KeepError::Frost(
                    "SoftwareDkgSession::round1_peer called out of order".into(),
                ));
            }
        };
        if wire.software_dkg_version != SOFTWARE_DKG_VERSION {
            return Err(KeepError::Frost(format!(
                "peer round1 package uses unsupported software_dkg_version {}, expected {}",
                wire.software_dkg_version, SOFTWARE_DKG_VERSION
            )));
        }
        if wire.sender_index == self.our_index {
            return Err(KeepError::Frost(
                "peer round1 package echoes our own share index; refusing to self-mix".into(),
            ));
        }
        if wire.sender_index < 1 || wire.sender_index > self.max_signers {
            return Err(KeepError::Frost(format!(
                "peer round1 sender_index {} out of range [1, {}]",
                wire.sender_index, self.max_signers,
            )));
        }
        let peer_id = Identifier::try_from(wire.sender_index).map_err(|e| {
            KeepError::Frost(format!(
                "peer index {} is not a valid FROST identifier: {e}",
                wire.sender_index
            ))
        })?;
        if peer_round1.contains_key(&peer_id) {
            return Err(KeepError::Frost(format!(
                "duplicate round1 package from peer {}",
                wire.sender_index
            )));
        }
        let bytes = hex::decode(&wire.package_hex)
            .map_err(|e| KeepError::Frost(format!("decode round1 package hex: {e}")))?;
        let package = round1::Package::deserialize(&bytes)
            .map_err(|e| KeepError::Frost(format!("deserialize round1 package: {e}")))?;
        peer_round1.insert(peer_id, package);
        Ok(peer_round1.len() as u16 + 1 == self.max_signers)
    }

    /// Run frost-core's `part2` and return the per-recipient round2 wire
    /// packages. Refuses to run until every peer's round1 package has
    /// landed.
    pub fn round2(&mut self) -> Result<Vec<(u16, SoftwareRound2Wire)>> {
        let (secret, our_round1, peer_round1) =
            match std::mem::replace(&mut self.state, DkgState::Finalized) {
                DkgState::AwaitingRound1 {
                    secret,
                    our_round1,
                    peer_round1,
                } if (peer_round1.len() as u16) + 1 == self.max_signers => {
                    (secret, our_round1, peer_round1)
                }
                other => {
                    // Restore the pre-call state on an out-of-order call so the
                    // caller keeps its round1 collection. This recovery only
                    // covers the guard-mismatch arm; once the guard passes below,
                    // the state has already advanced to `Finalized`.
                    self.state = other;
                    return Err(KeepError::Frost(
                        "SoftwareDkgSession::round2 called before every peer's round1 arrived"
                            .into(),
                    ));
                }
            };
        // Past this point the state is already `Finalized`: a `part2` failure is
        // a terminal protocol error, so the session is intentionally not rewound.
        let (round2_secret, round2_packages) =
            part2(secret, &peer_round1).map_err(|e| KeepError::Frost(format!("DKG part2: {e}")))?;

        let mut wires: Vec<(u16, SoftwareRound2Wire)> = Vec::new();
        for (peer_id, package) in &round2_packages {
            let recipient_index = identifier_to_u16(peer_id).ok_or_else(|| {
                KeepError::Frost("DKG part2 returned package for unknown identifier".into())
            })?;
            // `bytes` is a plaintext signing share; keep it in a scrubbed
            // buffer so it does not linger in freed heap after this loop.
            let bytes = Zeroizing::new(
                package
                    .serialize()
                    .map_err(|e| KeepError::Frost(format!("serialize round2 package: {e}")))?,
            );
            wires.push((
                recipient_index,
                SoftwareRound2Wire {
                    software_dkg_version: SOFTWARE_DKG_VERSION,
                    sender_index: self.our_index,
                    package_hex: hex::encode(&bytes),
                },
            ));
        }

        // Rebuild the full round1 map with our own entry, which `part3` needs.
        let mut round1_all = peer_round1;
        round1_all.insert(self.identifier, our_round1);

        self.state = DkgState::AwaitingRound2 {
            secret: round2_secret,
            round1: round1_all,
            peer_round2: BTreeMap::new(),
        };
        Ok(wires)
    }

    /// Consume a peer's round2 share targeted at us. Same version + range +
    /// duplicate checks as `round1_peer`. Returns `true` if this call
    /// completed the round2 set.
    pub fn receive_share(&mut self, wire: &SoftwareRound2Wire) -> Result<bool> {
        let peer_round2 = match &mut self.state {
            DkgState::AwaitingRound2 { peer_round2, .. } => peer_round2,
            _ => {
                return Err(KeepError::Frost(
                    "SoftwareDkgSession::receive_share called out of order".into(),
                ));
            }
        };
        if wire.software_dkg_version != SOFTWARE_DKG_VERSION {
            return Err(KeepError::Frost(format!(
                "peer round2 share uses unsupported software_dkg_version {}, expected {}",
                wire.software_dkg_version, SOFTWARE_DKG_VERSION,
            )));
        }
        if wire.sender_index == self.our_index {
            return Err(KeepError::Frost(
                "peer round2 share echoes our own share index; refusing to self-mix".into(),
            ));
        }
        if wire.sender_index < 1 || wire.sender_index > self.max_signers {
            return Err(KeepError::Frost(format!(
                "peer round2 sender_index {} out of range [1, {}]",
                wire.sender_index, self.max_signers,
            )));
        }
        let peer_id = Identifier::try_from(wire.sender_index).map_err(|e| {
            KeepError::Frost(format!(
                "peer index {} is not a valid FROST identifier: {e}",
                wire.sender_index
            ))
        })?;
        if peer_round2.contains_key(&peer_id) {
            return Err(KeepError::Frost(format!(
                "duplicate round2 share from peer {}",
                wire.sender_index
            )));
        }
        let bytes = Zeroizing::new(
            hex::decode(&wire.package_hex)
                .map_err(|e| KeepError::Frost(format!("decode round2 share hex: {e}")))?,
        );
        let package = round2::Package::deserialize(&bytes)
            .map_err(|e| KeepError::Frost(format!("deserialize round2 share: {e}")))?;
        peer_round2.insert(peer_id, package);
        Ok(peer_round2.len() as u16 + 1 == self.max_signers)
    }

    /// Finalize the DKG. Refuses to run until every peer's round2 share has
    /// arrived. On success the session state is consumed; further method
    /// calls return an error.
    pub fn finalize(&mut self) -> Result<SoftwareDkgResult> {
        let (secret, round1, round2) = match std::mem::replace(&mut self.state, DkgState::Finalized)
        {
            DkgState::AwaitingRound2 {
                secret,
                round1,
                peer_round2,
            } if (peer_round2.len() as u16) + 1 == self.max_signers => {
                (secret, round1, peer_round2)
            }
            other => {
                self.state = other;
                return Err(KeepError::Frost(
                    "SoftwareDkgSession::finalize called before every peer's round2 arrived".into(),
                ));
            }
        };

        // part3 wants round1 packages from peers only (not our own), so strip
        // our identifier out before handing the map in.
        let mut round1_peers = round1;
        round1_peers.remove(&self.identifier);

        let (key_package, public_key_package) = part3(&secret, &round1_peers, &round2)
            .map_err(|e| KeepError::Frost(format!("DKG part3: {e}")))?;

        // Single source of truth for x-only group pubkey extraction, shared with
        // the trusted-dealer path so both handle the 32- and 33-byte encodings.
        let group_pubkey = super::dealer::extract_group_pubkey(&public_key_package)?;

        Ok(SoftwareDkgResult {
            key_package,
            public_key_package,
            group_pubkey,
            our_index: self.our_index,
        })
    }

    /// This participant's 1-indexed share number.
    pub fn our_index(&self) -> u16 {
        self.our_index
    }
}

fn identifier_to_u16(id: &Identifier) -> Option<u16> {
    // frost-core's `Identifier` serialize is a big-endian 32-byte scalar
    // where the value we care about lives in the last two bytes for
    // participant indexes 1..=u16::MAX.
    let bytes = id.serialize();
    if bytes.len() < 32 {
        return None;
    }
    for &b in &bytes[..30] {
        if b != 0 {
            return None;
        }
    }
    Some(u16::from_be_bytes([bytes[30], bytes[31]]))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::frost::signing::sign_with_local_shares;
    use crate::frost::SharePackage;

    /// Run a full 2-of-3 software DKG and verify the resulting shares can
    /// aggregate a valid signature under the derived group pubkey.
    #[test]
    fn end_to_end_2_of_3_software_dkg() {
        run_group(2, 3);
    }

    /// A larger 3-of-5 group, exercising deeper participant sets and
    /// verifying the aggregated signature still verifies under the derived
    /// group pubkey.
    #[test]
    fn end_to_end_3_of_5_software_dkg() {
        run_group(3, 5);
    }

    fn run_group(threshold: u16, participants: u16) {
        let mut sessions: Vec<SoftwareDkgSession> = (1..=participants)
            .map(|idx| SoftwareDkgSession::init(threshold, participants, idx).unwrap())
            .collect();

        // Round 1: every session emits a wire package.
        let round1_wires: Vec<SoftwareRound1Wire> =
            sessions.iter_mut().map(|s| s.round1().unwrap()).collect();

        // Every session ingests every OTHER session's round1 wire.
        for (i, session) in sessions.iter_mut().enumerate() {
            for (j, wire) in round1_wires.iter().enumerate() {
                if i == j {
                    continue;
                }
                session.round1_peer(wire).unwrap();
            }
        }

        // Round 2: each session emits per-recipient wires.
        let mut per_session_round2: Vec<Vec<(u16, SoftwareRound2Wire)>> =
            sessions.iter_mut().map(|s| s.round2().unwrap()).collect();

        // Deliver each round2 share to its intended recipient.
        for (sender_i, wires) in per_session_round2.drain(..).enumerate() {
            for (recipient_index, wire) in wires {
                let recipient_i = (recipient_index - 1) as usize;
                assert_ne!(recipient_i, sender_i, "no self-shares in round 2");
                sessions[recipient_i].receive_share(&wire).unwrap();
            }
        }

        // Finalize every session and check group_pubkey agreement.
        let results: Vec<SoftwareDkgResult> =
            sessions.iter_mut().map(|s| s.finalize().unwrap()).collect();
        let first_group = results[0].group_pubkey;
        for r in &results {
            assert_eq!(r.group_pubkey, first_group);
        }

        // Assemble SharePackages for the threshold set and produce a real
        // signature. If any DKG step corrupted a share this would fail at
        // aggregation time.
        let share_packages: Vec<SharePackage> = results
            .iter()
            .take(threshold as usize)
            .map(|r| {
                let metadata = crate::frost::ShareMetadata::new(
                    r.our_index,
                    threshold,
                    participants,
                    r.group_pubkey,
                    "dkg-test".into(),
                );
                SharePackage::new(metadata, &r.key_package, &r.public_key_package).unwrap()
            })
            .collect();

        let message = b"software dkg produces spendable shares";
        let sig = sign_with_local_shares(&share_packages, message).unwrap();
        assert_eq!(sig.len(), 64);
    }

    /// A version-tag mismatch on a peer's round1 package MUST refuse rather
    /// than silently mis-decoding a future / stranger wire format.
    #[test]
    fn round1_peer_refuses_bad_version() {
        let mut a = SoftwareDkgSession::init(2, 2, 1).unwrap();
        let mut b = SoftwareDkgSession::init(2, 2, 2).unwrap();
        let _ = a.round1().unwrap();
        let mut b_wire = b.round1().unwrap();
        b_wire.software_dkg_version = 99;
        let err = a
            .round1_peer(&b_wire)
            .expect_err("bad version must be refused");
        assert!(matches!(err, KeepError::Frost(_)));
    }

    /// A peer sending two round1 packages is refused; otherwise a hostile
    /// peer could bias the resulting key with a late substitution.
    #[test]
    fn duplicate_round1_package_from_peer_refused() {
        let mut a = SoftwareDkgSession::init(2, 2, 1).unwrap();
        let mut b = SoftwareDkgSession::init(2, 2, 2).unwrap();
        let _ = a.round1().unwrap();
        let b_wire = b.round1().unwrap();
        a.round1_peer(&b_wire).unwrap();
        let err = a
            .round1_peer(&b_wire)
            .expect_err("duplicate round1 must be refused");
        assert!(matches!(err, KeepError::Frost(_)));
    }

    /// Calling `round2` before every peer's round1 lands must refuse without
    /// destroying the state we already have.
    #[test]
    fn round2_before_full_round1_refuses() {
        let mut s = SoftwareDkgSession::init(2, 3, 1).unwrap();
        let _ = s.round1().unwrap();
        let err = s
            .round2()
            .expect_err("incomplete round1 -> round2 must be refused");
        assert!(matches!(err, KeepError::Frost(_)));
    }

    /// Invalid config (threshold above participants, threshold below 2, or
    /// out-of-range index) fails at init.
    #[test]
    fn init_rejects_bad_config() {
        assert!(SoftwareDkgSession::init(1, 3, 1).is_err());
        assert!(SoftwareDkgSession::init(4, 3, 1).is_err());
        assert!(SoftwareDkgSession::init(2, 3, 0).is_err());
        assert!(SoftwareDkgSession::init(2, 3, 4).is_err());
    }

    /// An n-of-n group (threshold == participants) completes DKG and its full
    /// set of shares aggregates a valid signature.
    #[test]
    fn end_to_end_3_of_3_software_dkg() {
        run_group(3, 3);
    }

    type Round2Wires = Vec<(u16, SoftwareRound2Wire)>;

    /// Assert `err` is a `KeepError::Frost` whose message names the specific
    /// guard, so a negative test cannot pass on a different Frost error (bad
    /// hex, deserialize failure, wrong state) than the one it means to pin.
    fn assert_frost_reason(err: KeepError, needle: &str) {
        match err {
            KeepError::Frost(msg) => assert!(
                msg.contains(needle),
                "expected Frost error containing {needle:?}, got: {msg}"
            ),
            other => panic!("expected KeepError::Frost, got {other:?}"),
        }
    }

    /// Two 2-of-2 sessions advanced through the round1 exchange and round2
    /// emission, each in `AwaitingRound2` and holding the other's round2 wires.
    /// `a`'s wires carry `sender_index == 1`, `b`'s carry `sender_index == 2`.
    fn advanced_2of2() -> (
        SoftwareDkgSession,
        SoftwareDkgSession,
        Round2Wires,
        Round2Wires,
    ) {
        let mut a = SoftwareDkgSession::init(2, 2, 1).unwrap();
        let mut b = SoftwareDkgSession::init(2, 2, 2).unwrap();
        let aw = a.round1().unwrap();
        let bw = b.round1().unwrap();
        a.round1_peer(&bw).unwrap();
        b.round1_peer(&aw).unwrap();
        let a2 = a.round2().unwrap();
        let b2 = b.round2().unwrap();
        (a, b, a2, b2)
    }

    /// Peer drop mid-round-2: a participant that never receives one peer's
    /// round2 share must refuse to finalize rather than derive a key from an
    /// incomplete set.
    #[test]
    fn finalize_refuses_when_a_peer_round2_share_is_missing() {
        let mut sessions: Vec<SoftwareDkgSession> = (1..=3)
            .map(|idx| SoftwareDkgSession::init(2, 3, idx).unwrap())
            .collect();
        let round1_wires: Vec<SoftwareRound1Wire> =
            sessions.iter_mut().map(|s| s.round1().unwrap()).collect();
        for (i, session) in sessions.iter_mut().enumerate() {
            for (j, wire) in round1_wires.iter().enumerate() {
                if i != j {
                    session.round1_peer(wire).unwrap();
                }
            }
        }
        let per_session_round2: Vec<Vec<(u16, SoftwareRound2Wire)>> =
            sessions.iter_mut().map(|s| s.round2().unwrap()).collect();

        // Deliver to participant 1 only participant 2's share; participant 3's
        // share is intentionally dropped.
        for (recipient_index, wire) in &per_session_round2[1] {
            if *recipient_index == 1 {
                sessions[0].receive_share(wire).unwrap();
            }
        }

        // `SoftwareDkgResult` holds key material and is intentionally not
        // `Debug`, so match on the error directly rather than `expect_err`.
        // Pin the pre-`part3` completeness guard by name: without the message
        // check a deleted guard would still be masked by part3's own failure
        // on the incomplete set.
        match sessions[0].finalize() {
            Err(err) => assert_frost_reason(err, "before every peer's round2 arrived"),
            Ok(_) => panic!("a missing peer round2 share must refuse finalize"),
        }
    }

    /// A peer sending two round2 shares is refused; a late substitution could
    /// otherwise bias the recipient's key material.
    #[test]
    fn duplicate_round2_share_refused() {
        let (mut a, _b, _a2, b2) = advanced_2of2();
        // b2 carries b's share destined for recipient 1 (a).
        let (recipient, wire) = &b2[0];
        assert_eq!(*recipient, 1);
        a.receive_share(wire).unwrap();
        let err = a
            .receive_share(wire)
            .expect_err("duplicate round2 share must be refused");
        assert_frost_reason(err, "duplicate round2 share from peer");
    }

    /// A round2 share whose `sender_index` equals our own is refused, so a
    /// participant cannot be tricked into mixing its own share back in.
    #[test]
    fn round2_share_echoing_our_index_refused() {
        let (mut a, _b, a2, _b2) = advanced_2of2();
        // a2 is a's own share (sender_index == 1) destined for recipient 2.
        let (_recipient, own_wire) = &a2[0];
        assert_eq!(own_wire.sender_index, 1);
        let err = a
            .receive_share(own_wire)
            .expect_err("a self-echoed round2 share must be refused");
        assert_frost_reason(err, "echoes our own share index");
    }

    /// `receive_share` before `round2` (wrong state) is refused without
    /// corrupting session state.
    #[test]
    fn receive_share_out_of_order_refused() {
        let (_a, _b, _a2, b2) = advanced_2of2();
        let (_recipient, wire) = &b2[0];
        // A fresh session is still in the initial state, not AwaitingRound2.
        let mut fresh = SoftwareDkgSession::init(2, 2, 1).unwrap();
        let err = fresh
            .receive_share(wire)
            .expect_err("receive_share before round2 must be refused");
        assert_frost_reason(err, "receive_share called out of order");
    }

    #[test]
    fn identifier_round_trip() {
        for idx in [1u16, 2, 5, 42, 255, 300, 1000, 65535] {
            let id = Identifier::try_from(idx).unwrap();
            assert_eq!(identifier_to_u16(&id), Some(idx));
        }
    }
}