tsslib 0.2.7

Easy-to-use threshold signature schemes (FROST, ML-DSA, DKLs23) — wire- and save-data-compatible with the Go tss-lib.
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
//! Threshold-EdDSA key resharing over a `MessageBroker` (old + new committees).
//!
//! Port of Go `eddsatss/resharing.go`. The old committee re-splits its secret to
//! a fresh `new_threshold`-of-`new_party_count` committee while preserving
//! `EDDSAPub`. Unlike `ecdsatss` there are no Paillier/ring parameters and no
//! zero-knowledge proofs: the new committee verifies each old party's VSS share
//! against the committed (hash-opened) polynomial and checks `Vc[0] == EDDSAPub`.
//!
//! Old-committee members' input key may still carry the full keygen party set:
//! [`ResharingParty::new`] transparently narrows it to the old committee via
//! [`Key::subset_for_parties`].
//!
//! Rounds: (old) round1 broadcasts `EDDSAPub` + a VSS commitment; (new) round2
//! acks; (old) round3 unicasts each new party its share and opens the VSS
//! commitment; (new) round4 rebuilds the share/public key and acks; old-only
//! parties then retire their share.

#![allow(dead_code)]

use super::ed::{self, EcPointJson};
use super::key::Key;
use super::vss;
use super::{Error, ed::point_to_json};
use crate::tss::b64::B64Bytes;
use crate::tss::bigint::BigUintDec;
use crate::tss::expect::JsonExpect;
use crate::tss::{JsonMessage, PartyId, ReSharingParameters, json_get, json_wrap};
use purecrypto::ec::edwards25519::hazmat::{EdwardsPoint, Scalar};
use purecrypto::rng::OsRng;
use serde::{Deserialize, Serialize};
use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender, channel};
use std::sync::{Arc, Mutex};
use zeroize::Zeroize;

const TYPE_R1: &str = "eddsa:reshare:round1";
const TYPE_R2: &str = "eddsa:reshare:round2";
const TYPE_R3_1: &str = "eddsa:reshare:round3-1";
const TYPE_R3_2: &str = "eddsa:reshare:round3-2";
const TYPE_R4: &str = "eddsa:reshare:round4";

/// A running threshold-EdDSA resharing session.
pub struct ResharingParty {
    result_rx: MpscReceiver<Result<Key, Error>>,
    _shared: Arc<Shared>,
}

struct Shared {
    params: ReSharingParameters,
    input: Key,
    state: Mutex<State>,
    result_tx: Mutex<Option<MpscSender<Result<Key, Error>>>>,
}

struct State {
    eddsa_pub: Option<EdwardsPoint>,

    // old side
    vd: Vec<Vec<u8>>,
    new_shares: Vec<Scalar>, // aligned to new_parties

    // collected (new side)
    r1msgs: Vec<R1Msg>,
    r1_from: Vec<PartyId>,
    r3m1: Vec<R3Msg1>,
    r3m1_from: Vec<PartyId>,
    r3m2: Vec<R3Msg2>,
    r3m2_from: Vec<PartyId>,
    r3_join: u8,

    new_key: Option<Key>,
}

impl ResharingParty {
    /// Starts resharing.
    pub fn new(params: ReSharingParameters, input: Key) -> Result<ResharingParty, Error> {
        let (tx, rx) = channel();
        // Old-committee members reindex their input to the old committee so the
        // Lagrange lookup in prepare_wi uses old-committee indices rather than
        // keygen-party indices (mirrors Go round1Old's SubsetForParties). The
        // full keygen key may thus be passed as-is. New-only members never index
        // the input's per-party slices (they take EDDSAPub from round-1
        // messages), so their input is left untouched.
        let input = if params.is_old_committee() {
            input.subset_for_parties(params.old_parties())?
        } else {
            input
        };
        let shared = Arc::new(Shared {
            params,
            input,
            state: Mutex::new(State {
                eddsa_pub: None,
                vd: Vec::new(),
                new_shares: Vec::new(),
                r1msgs: Vec::new(),
                r1_from: Vec::new(),
                r3m1: Vec::new(),
                r3m1_from: Vec::new(),
                r3m2: Vec::new(),
                r3m2_from: Vec::new(),
                r3_join: 0,
                new_key: None,
            }),
            result_tx: Mutex::new(Some(tx)),
        });
        if shared.params.is_new_committee() {
            shared.setup_new_round1();
        }
        if shared.params.is_old_committee() {
            shared.round1_old()?;
        }
        Ok(ResharingParty {
            result_rx: rx,
            _shared: shared,
        })
    }

    /// Blocks until resharing completes (new key, or retired input for old-only).
    /// Non-blocking peek at the ceremony result: `Some(_)` once the result (or
    /// error) is ready, `None` while rounds are still pending. Unlike [`wait`](Self::wait)
    /// it never blocks, so a single-threaded async driver (e.g. wasm/browser) can
    /// poll it after feeding each inbound message.
    pub fn try_result(&self) -> Option<Result<Key, Error>> {
        self.result_rx.try_recv().ok()
    }

    pub fn wait(&self) -> Result<Key, Error> {
        self.result_rx
            .recv()
            .unwrap_or_else(|_| Err(Error::Validation("resharing dropped without result".into())))
    }
}

impl Shared {
    fn deliver(&self, r: Result<Key, Error>) {
        if let Some(tx) = self.result_tx.lock().unwrap().take() {
            let _ = tx.send(r);
        }
    }

    fn fail(&self, m: impl Into<String>) {
        self.deliver(Err(Error::Validation(m.into())));
    }

    // --- old committee ---

    fn round1_old(self: &Arc<Self>) -> Result<(), Error> {
        let mut rng = OsRng;
        let new_t = self.params.new_threshold();
        let wi = self.prepare_wi()?;

        let new_ids = self.params.new_parties().to_vec();
        let new_ks: Vec<Scalar> = new_ids.iter().map(|p| ed::scalar_from_be(&p.key)).collect();
        let (vi, shares) = vss::create(new_t, &wi, &new_ks, &mut rng);
        let (vc, vd) = super::commit::commit(&flatten_points(&vi), &mut rng);

        let pub_pt = self
            .input
            .eddsa_pub_point()
            .ok_or_else(|| Error::Validation("resharing: input EDDSAPub off curve".into()))?;
        let (px, py) = ed::coords_be(&pub_pt);

        {
            let mut st = self.state.lock().unwrap();
            st.vd = vd;
            st.new_shares = shares.iter().map(|s| s.value.clone()).collect();
            st.eddsa_pub = Some(pub_pt);
        }

        let r1 = R1Msg {
            eddsa_pub_x: B64Bytes(px),
            eddsa_pub_y: B64Bytes(py),
            v_commitment: B64Bytes(vc),
        };
        let new_others = self.new_others();
        for pj in &new_others {
            self.send_to(TYPE_R1, &r1, pj)?;
        }

        if new_others.is_empty() {
            self.round3_old();
        } else {
            self.connect(TYPE_R2, &new_others, {
                let me = Arc::clone(self);
                move |_| me.round3_old()
            });
        }
        Ok(())
    }

    fn round3_old(self: &Arc<Self>) {
        let new_ids = self.params.new_parties().to_vec();
        let (new_shares, vd) = {
            let st = self.state.lock().unwrap();
            (st.new_shares.clone(), st.vd.clone())
        };
        for (j, pj) in new_ids.iter().enumerate() {
            let r3m1 = R3Msg1 {
                share: B64Bytes(ed::scalar_to_be(&new_shares[j])),
            };
            if let Err(e) = self.send_to(TYPE_R3_1, &r3m1, pj) {
                return self.deliver(Err(e));
            }
        }
        let r3m2 = R3Msg2 {
            v_decommitment: vd.into_iter().map(B64Bytes).collect(),
        };
        for pj in &new_ids {
            if let Err(e) = self.send_to(TYPE_R3_2, &r3m2, pj) {
                return self.deliver(Err(e));
            }
        }

        let new_others = self.new_others();
        if new_others.is_empty() {
            self.round5_old();
        } else {
            self.connect(TYPE_R4, &new_others, {
                let me = Arc::clone(self);
                move |_| me.round5_old()
            });
        }
    }

    fn round5_old(self: &Arc<Self>) {
        // New members deliver the new key from round4_new; old-only members
        // retire their share.
        if !self.params.is_new_committee() {
            let mut retired = self.input.clone();
            retired.xi = BigUintDec::from_be_bytes(&[]);
            self.deliver(Ok(retired));
        }
    }

    // --- new committee ---

    fn setup_new_round1(self: &Arc<Self>) {
        let old_ids = self.params.old_parties().to_vec();
        self.connect(TYPE_R1, &old_ids, {
            let me = Arc::clone(self);
            let old_ids = old_ids.clone();
            move |msgs| me.round2_new(&old_ids, msgs)
        });
    }

    fn round2_new(self: &Arc<Self>, old_ids: &[PartyId], msgs: Vec<JsonMessage>) {
        let decoded: Result<Vec<R1Msg>, _> = msgs.iter().map(json_get).collect();
        let r1msgs = match decoded {
            Ok(d) => d,
            Err(e) => return self.deliver(Err(e.into())),
        };

        // EDDSAPub must agree across all old parties.
        let mut eddsa_pub: Option<EdwardsPoint> = None;
        for m in &r1msgs {
            let cand = match ed::point_from_affine_be(&m.eddsa_pub_x.0, &m.eddsa_pub_y.0) {
                Some(p) => p,
                None => return self.fail("resharing: EDDSAPub off curve"),
            };
            match &eddsa_pub {
                None => eddsa_pub = Some(cand),
                Some(p) if !ed::eq(p, &cand) => return self.fail("resharing: EDDSAPub mismatch"),
                _ => {}
            }
        }
        {
            let mut st = self.state.lock().unwrap();
            st.eddsa_pub = eddsa_pub;
            st.r1msgs = r1msgs;
            st.r1_from = old_ids.to_vec();
        }

        // Ack to old parties.
        for pj in old_ids {
            if pj.key != self.params.party_id().key {
                let _ = self.send_to(TYPE_R2, &R2Msg {}, pj);
            }
        }

        self.connect(TYPE_R3_1, old_ids, {
            let me = Arc::clone(self);
            let old_ids = old_ids.to_vec();
            move |msgs| me.on_r3m1(&old_ids, msgs)
        });
        self.connect(TYPE_R3_2, old_ids, {
            let me = Arc::clone(self);
            let old_ids = old_ids.to_vec();
            move |msgs| me.on_r3m2(&old_ids, msgs)
        });
    }

    fn on_r3m1(self: &Arc<Self>, from: &[PartyId], msgs: Vec<JsonMessage>) {
        let decoded: Result<Vec<R3Msg1>, _> = msgs.iter().map(json_get).collect();
        let ready = {
            let mut st = self.state.lock().unwrap();
            match decoded {
                Ok(d) => st.r3m1 = d,
                Err(e) => return self.deliver(Err(e.into())),
            }
            st.r3m1_from = from.to_vec();
            st.r3_join += 1;
            st.r3_join == 2
        };
        if ready {
            self.round4_new();
        }
    }

    fn on_r3m2(self: &Arc<Self>, from: &[PartyId], msgs: Vec<JsonMessage>) {
        let decoded: Result<Vec<R3Msg2>, _> = msgs.iter().map(json_get).collect();
        let ready = {
            let mut st = self.state.lock().unwrap();
            match decoded {
                Ok(d) => st.r3m2 = d,
                Err(e) => return self.deliver(Err(e.into())),
            }
            st.r3m2_from = from.to_vec();
            st.r3_join += 1;
            st.r3_join == 2
        };
        if ready {
            self.round4_new();
        }
    }

    fn round4_new(self: &Arc<Self>) {
        let new_t = self.params.new_threshold();
        let new_ids = self.params.new_parties().to_vec();
        let me_id = ed::scalar_from_be(&self.params.party_id().key);

        let (eddsa_pub, r1msgs, r1_from, r3m1, r3m1_from, r3m2, r3m2_from) = {
            let st = self.state.lock().unwrap();
            (
                st.eddsa_pub.unwrap(),
                st.r1msgs.clone(),
                st.r1_from.clone(),
                st.r3m1.clone(),
                st.r3m1_from.clone(),
                st.r3m2.clone(),
                st.r3m2_from.clone(),
            )
        };

        let mut new_xi = Scalar::ZERO;
        let mut vc_agg: Vec<Option<EdwardsPoint>> = (0..=new_t).map(|_| None).collect();

        for (k, r3m1_msg) in r3m1.iter().enumerate() {
            let oid = &r3m1_from[k];
            let r1pos = match r1_from.iter().position(|p| p.key == oid.key) {
                Some(p) => p,
                None => return self.fail("resharing: missing round1 for old party"),
            };
            let r2pos = match r3m2_from.iter().position(|p| p.key == oid.key) {
                Some(p) => p,
                None => return self.fail("resharing: missing round3-2 for old party"),
            };
            let vc = r1msgs[r1pos].v_commitment.0.clone();
            let d: Vec<Vec<u8>> = r3m2[r2pos]
                .v_decommitment
                .iter()
                .map(|b| b.0.clone())
                .collect();
            let flat = match super::commit::decommit(&vc, &d) {
                Some(v) if v.len() == (new_t + 1) * 2 => v,
                _ => return self.fail("resharing: VSS decommitment failed"),
            };
            let vj = match unflatten_points(&flat) {
                Some(v) => v,
                None => return self.fail("resharing: VSS commitments off curve"),
            };
            let share = ed::scalar_from_be(&r3m1_msg.share.0);
            if !vss::verify(&me_id, &share, new_t, &vj) {
                return self.fail("resharing: VSS share verification failed");
            }
            new_xi = new_xi.add(&share);
            for (c, slot) in vc_agg.iter_mut().enumerate() {
                *slot = Some(match slot {
                    None => vj[c],
                    Some(acc) => ed::add(acc, &vj[c]),
                });
            }
        }

        let vc0 = match vc_agg[0] {
            Some(p) => p,
            None => return self.fail("resharing: no old shares received"),
        };
        if !ed::eq(&vc0, &eddsa_pub) {
            return self.fail("resharing: reconstructed key != EDDSAPub");
        }

        let mut new_big_xjs = Vec::with_capacity(new_ids.len());
        for pj in &new_ids {
            let kj = ed::scalar_from_be(&pj.key);
            let mut bx = vc0;
            let mut z = Scalar::ONE;
            for slot in vc_agg.iter().take(new_t + 1).skip(1) {
                z = z.mul(&kj);
                bx = ed::add(&bx, &ed::mul(&slot.unwrap(), &z));
            }
            new_big_xjs.push(bx);
        }

        // Wipe the transient big-endian copy of the new secret share once it
        // has been captured by the Key (which zeroizes its own copy on drop).
        let mut new_xi_be = ed::scalar_to_be(&new_xi);
        let new_key = Key {
            xi: BigUintDec::from_be_bytes(&new_xi_be),
            share_id: BigUintDec::from_be_bytes(&self.params.party_id().key),
            ks: new_ids
                .iter()
                .map(|p| BigUintDec::from_be_bytes(&p.key))
                .collect(),
            big_xj: new_big_xjs.iter().map(ec_point).collect(),
            eddsa_pub: ec_point(&eddsa_pub),
        };
        new_xi_be.zeroize();
        self.state.lock().unwrap().new_key = Some(new_key.clone());

        // Ack round4 to every other old+new party.
        let mut all = self.params.old_parties().to_vec();
        all.extend(new_ids.iter().cloned());
        for pj in &all {
            if pj.key != self.params.party_id().key {
                let _ = self.send_to(TYPE_R4, &R4Msg {}, pj);
            }
        }

        let new_others = self.new_others();
        if new_others.is_empty() {
            self.deliver(Ok(new_key));
        } else {
            self.connect(TYPE_R4, &new_others, {
                let me = Arc::clone(self);
                move |_| {
                    let k = me.state.lock().unwrap().new_key.clone();
                    if let Some(k) = k {
                        me.deliver(Ok(k));
                    }
                }
            });
        }
    }

    // --- helpers ---

    fn prepare_wi(&self) -> Result<Scalar, Error> {
        let i = self
            .params
            .old_index()
            .ok_or_else(|| Error::Validation("resharing: not an old-committee member".into()))?;
        let ks = self.input.ks_scalars();
        if self.params.old_threshold() + 1 > ks.len() {
            return Err(Error::Validation("resharing: old t+1 > parties".into()));
        }
        if i >= ks.len() {
            return Err(Error::Validation(
                "resharing: old party index out of range of key Ks".into(),
            ));
        }
        let mut wi = self.input.xi_scalar();
        for (j, ksj) in ks.iter().enumerate() {
            if j == i {
                continue;
            }
            let denom = ksj.sub(&ks[i]);
            wi = wi.mul(&ksj.mul(&denom.invert()));
        }
        Ok(wi)
    }

    fn new_others(&self) -> Vec<PartyId> {
        let me = self.params.party_id();
        self.params
            .new_parties()
            .iter()
            .filter(|p| p.key != me.key)
            .cloned()
            .collect()
    }

    fn connect<F>(self: &Arc<Self>, typ: &str, others: &[PartyId], cb: F)
    where
        F: FnOnce(Vec<JsonMessage>) + Send + 'static,
    {
        let exp = JsonExpect::new(typ, others.to_vec(), Box::new(cb));
        self.params.broker().connect(typ, Arc::new(exp));
    }

    fn send_to<T: Serialize>(&self, typ: &str, body: &T, to: &PartyId) -> Result<(), Error> {
        let msg = json_wrap(
            typ,
            body,
            Some(self.params.party_id().clone()),
            Some(to.clone()),
        )?;
        self.params
            .broker()
            .receive(&msg)
            .map_err(|e| Error::Validation(format!("broker delivery failed: {e}")))
    }
}

// --- helpers ---------------------------------------------------------------

fn flatten_points(vs: &[EdwardsPoint]) -> Vec<Vec<u8>> {
    let mut out = Vec::with_capacity(vs.len() * 2);
    for p in vs {
        let (x, y) = ed::coords_be(p);
        out.push(x);
        out.push(y);
    }
    out
}

fn unflatten_points(flat: &[Vec<u8>]) -> Option<Vec<EdwardsPoint>> {
    if !flat.len().is_multiple_of(2) {
        return None;
    }
    let mut out = Vec::with_capacity(flat.len() / 2);
    for pair in flat.chunks(2) {
        out.push(ed::eight_inv_eight(&ed::point_from_affine_be(
            &pair[0], &pair[1],
        )?));
    }
    Some(out)
}

fn ec_point(p: &EdwardsPoint) -> EcPointJson {
    point_to_json(p)
}

// --- wire types ------------------------------------------------------------

#[derive(Clone, Serialize, Deserialize)]
struct R1Msg {
    #[serde(rename = "eddsa_pub_x")]
    eddsa_pub_x: B64Bytes,
    #[serde(rename = "eddsa_pub_y")]
    eddsa_pub_y: B64Bytes,
    #[serde(rename = "v_commitment")]
    v_commitment: B64Bytes,
}

#[derive(Clone, Serialize, Deserialize)]
struct R2Msg {}

#[derive(Clone, Serialize, Deserialize)]
struct R3Msg1 {
    #[serde(rename = "share")]
    share: B64Bytes,
}

#[derive(Clone, Serialize, Deserialize)]
struct R3Msg2 {
    #[serde(rename = "v_decommitment")]
    v_decommitment: Vec<B64Bytes>,
}

#[derive(Clone, Serialize, Deserialize)]
struct R4Msg {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::eddsatss::import::import_key;
    use crate::eddsatss::signing::{SignatureData, SigningParty};
    use crate::tss::testhub::ReshareHub;
    use crate::tss::{Parameters, testhub::TestHub};
    use purecrypto::ec::{Ed25519PublicKey, Ed25519Signature};

    fn pid(key: u8) -> PartyId {
        PartyId::new(key.to_string(), format!("P{key}"), vec![key])
    }

    #[test]
    fn import_then_reshare_then_sign() {
        // Import a plain Ed25519 key as 1-of-1, reshare to 2-of-3, then sign.
        let d = [0x42u8];
        let old = pid(5);
        let input = import_key(&d, &old.key).unwrap();
        let eddsa_pub = input.eddsa_pub_point().unwrap();

        let old_ids = vec![old.clone()];
        let new_ids = PartyId::sort(vec![pid(11), pid(12), pid(13)], 0);
        let (old_t, new_t) = (0usize, 1usize);

        let mut all = old_ids.clone();
        all.extend(new_ids.iter().cloned());
        let hub = ReshareHub::new(&all);

        let mut sessions: Vec<ResharingParty> = Vec::new();
        sessions.push(
            ResharingParty::new(
                ReSharingParameters::new(
                    old_ids.clone(),
                    new_ids.clone(),
                    old_t,
                    new_t,
                    old.clone(),
                    hub.broker(&old),
                ),
                input.clone(),
            )
            .unwrap(),
        );
        for p in &new_ids {
            sessions.push(
                ResharingParty::new(
                    ReSharingParameters::new(
                        old_ids.clone(),
                        new_ids.clone(),
                        old_t,
                        new_t,
                        p.clone(),
                        hub.broker(p),
                    ),
                    input.clone(),
                )
                .unwrap(),
            );
        }

        let results: Vec<Key> = sessions
            .iter()
            .map(|s| s.wait().expect("resharing succeeds"))
            .collect();
        let new_keys: Vec<Key> = results[1..].to_vec();
        for k in &new_keys {
            k.validate_basic().unwrap();
            assert_eq!(k.eddsa_pub.coords, new_keys[0].eddsa_pub.coords);
        }
        // Public key preserved.
        assert!(ed::eq(&new_keys[0].eddsa_pub_point().unwrap(), &eddsa_pub));

        // Sign with a 2-of-3 subset of the new committee (parties 11, 12).
        let signers: Vec<PartyId> = new_ids[..2].to_vec();
        // Build per-signer keys narrowed to the signing committee.
        let signer_keys: Vec<Key> = (0..2).map(|i| subset_key(&new_keys[i], &signers)).collect();
        let msg = b"reshared eddsa signs";
        let hub2 = TestHub::new(&signers);
        let sparties: Vec<SigningParty> = (0..signers.len())
            .map(|i| {
                let params = Parameters::new(signers.to_vec(), &signers[i], new_t, hub2.broker(i));
                SigningParty::new(params, signer_keys[i].clone(), msg).unwrap()
            })
            .collect();
        let sigs: Vec<SignatureData> = sparties.iter().map(|p| p.wait().unwrap()).collect();

        let pk = Ed25519PublicKey::from_bytes(ed::encode_point(&eddsa_pub));
        let mut s = [0u8; 64];
        s.copy_from_slice(&sigs[0].signature);
        assert!(pk.verify(msg, &Ed25519Signature::from_bytes(s)).is_ok());
    }

    /// Narrows a new-committee key to a signing subset (reindex Ks/BigXj, keep Xi).
    fn subset_key(k: &Key, signers: &[PartyId]) -> Key {
        let idx: Vec<usize> = signers
            .iter()
            .map(|s| {
                k.ks.iter()
                    .position(|kk| kk.as_be_bytes() == s.key.as_slice())
                    .expect("signer in key")
            })
            .collect();
        Key {
            xi: k.xi.clone(),
            share_id: k.share_id.clone(),
            ks: idx.iter().map(|&i| k.ks[i].clone()).collect(),
            big_xj: idx.iter().map(|&i| k.big_xj[i].clone()).collect(),
            eddsa_pub: k.eddsa_pub.clone(),
        }
    }
}