schnorr_fun 0.13.0

BIP340 Schnorr signatures based on secp256kfun
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
//! SimplePedPop is a bare bones secure distributed key generation algorithm that leaves a lot left
//! up to the application.
//!
//! The application must figure out:
//!
//! - How to secretly transport secret share contribution from each contributor to their intended destination
//! - Checking that each party got the correct output by comparing
//!   [`VerifiedAggKeygenInput::cert_bytes`] on each of them.
//!
//! [`VerifiedAggKeygenInput::cert_bytes`]: VerifiedAggKeygenInput::cert_bytes
use super::Role;
pub use super::{AuxContributor, ShareReceiver};
use crate::{Message, Schnorr, Signature, frost::*};
use alloc::{
    collections::{BTreeMap, BTreeSet},
    vec::Vec,
};
use core::marker::PhantomData;
use secp256kfun::{KeyPair, hash::Hash32, nonce::NonceGen, poly, prelude::*, rand_core};

pub(crate) mod role_sealed {
    pub trait Sealed {
        /// Translate a role-relative index to the absolute slot index in
        /// `[0..n_contributors)`. Returns `None` if `role_index` is out of
        /// range for this role.
        fn slot_index(role_index: u32, n_receivers: u32, n_aux_contributors: u32) -> Option<u32>;
    }
}

impl role_sealed::Sealed for ShareReceiver {
    fn slot_index(role_index: u32, n_receivers: u32, _n_aux_contributors: u32) -> Option<u32> {
        (role_index < n_receivers).then_some(role_index)
    }
}

impl role_sealed::Sealed for AuxContributor {
    fn slot_index(role_index: u32, n_receivers: u32, n_aux_contributors: u32) -> Option<u32> {
        (role_index < n_aux_contributors)
            .then(|| n_receivers.checked_add(role_index))
            .flatten()
    }
}

const POP_DOMAIN_SEP: &str = "BIP DKG/pop message";

/// One party's protocol state for a `simplepedpop` keygen. Construct with
/// [`gen_keygen_input`] and consume with [`verify_agg_input`] (or the
/// `ShareReceiver` variant).
///
/// `R` selects the role-relative meaning of the `my_role_index` argument to
/// [`gen_keygen_input`] — see the [module-level docs] for `R`'s overall job
/// across the three layers.
///
/// Deliberately not serializable: `Contributor` is in-memory protocol state with
/// invariants that can't be re-checked after decode. A tampered blob with
/// consistent-looking fields could trick later checks.
///
/// [`gen_keygen_input`]: Self::gen_keygen_input
/// [`verify_agg_input`]: Contributor::<AuxContributor>::verify_agg_input
/// [module-level docs]: super
#[derive(Clone, Debug, PartialEq)]
pub struct Contributor<R: Role> {
    my_key_contrib: Point,
    my_index: u32,
    threshold: u32,
    n_aux_contributors: u32,
    n_receivers: u32,
    _role: PhantomData<R>,
}

impl<R: Role> Contributor<R> {
    /// Generate this contributor's keygen input for the protocol round.
    ///
    /// `my_role_index` is role-relative: for [`ShareReceiver`] it's the receiver slot
    /// in `[0..n_receivers)` (the polynomial is evaluated at `ShareIndex = my_role_index + 1`);
    /// for [`AuxContributor`] it's in `[0..n_aux_contributors)`.
    pub fn gen_keygen_input<H, NG>(
        schnorr: &Schnorr<H, NG>,
        threshold: u32,
        n_aux_contributors: u32,
        n_receivers: u32,
        my_role_index: u32,
        rng: &mut impl rand_core::RngCore,
    ) -> Result<(Self, KeygenInput, SecretKeygenInput), GenKeygenInputError>
    where
        H: Hash32,
        NG: NonceGen,
    {
        if threshold == 0 || threshold > n_receivers {
            return Err(GenKeygenInputError::InvalidThreshold {
                threshold,
                n_receivers,
            });
        }
        n_aux_contributors
            .checked_add(n_receivers)
            .ok_or(GenKeygenInputError::TooManyContributors)?;
        let my_index = R::slot_index(my_role_index, n_receivers, n_aux_contributors).ok_or(
            GenKeygenInputError::IndexOutOfRange {
                role_index: my_role_index,
            },
        )?;
        let secret_poly = poly::scalar::generate(threshold as usize, rng);
        let com = poly::scalar::to_point_poly(&secret_poly);
        let pop_keypair = KeyPair::new_xonly(secret_poly[0]);
        let pop = schnorr.sign(
            &pop_keypair,
            Message::new(POP_DOMAIN_SEP, &pop_message_bytes(com[0], my_index)),
        );

        let shares = (1..=n_receivers)
            .map(|i| {
                let share_index = ShareIndex::try_from(i).expect("non-zero");
                poly::scalar::eval(&secret_poly, share_index)
            })
            .collect();
        let self_ = Self {
            my_key_contrib: com[0],
            my_index,
            threshold,
            n_aux_contributors,
            n_receivers,
            _role: PhantomData,
        };
        let msg = KeygenInput { com, pop };
        Ok((self_, msg, shares))
    }

    fn verify_agg_input_inner<H, NG>(
        self,
        schnorr: &Schnorr<H, NG>,
        agg_input: AggKeygenInput,
    ) -> Result<VerifiedAggKeygenInput, VerifyAggInputError>
    where
        H: Hash32,
    {
        if agg_input.key_contrib.len() != self.n_contributors() as usize {
            return Err(VerifyAggInputError::ContributionDidntMatch);
        }
        // Reject zero-coefficient padding that would inflate the apparent threshold.
        if agg_input.agg_poly.len() + 1 != self.threshold as usize {
            return Err(VerifyAggInputError::ContributionDidntMatch);
        }
        let my_got_contrib = agg_input
            .key_contrib
            .get(self.my_index as usize)
            .map(|(point, _)| *point);
        let my_expected_contrib = self.my_key_contrib;
        if Some(my_expected_contrib) != my_got_contrib {
            return Err(VerifyAggInputError::ContributionDidntMatch);
        }

        agg_input.verify_proofs_of_possession(schnorr)?;

        let shared_key = agg_input
            .shared_key()
            .non_zero()
            .ok_or(VerifyAggInputError::SharedKeyIsZero)?;

        Ok(VerifiedAggKeygenInput {
            inner: agg_input,
            shared_key,
        })
    }

    /// Absolute slot index in `[0..n_contributors)`.
    pub fn contributor_index(&self) -> u32 {
        self.my_index
    }

    /// The `my_role_index` this contributor was constructed with.
    pub fn role_index(&self) -> u32 {
        if self.my_index < self.n_receivers {
            self.my_index
        } else {
            self.my_index - self.n_receivers
        }
    }

    /// `n_aux_contributors + n_receivers`.
    pub fn n_contributors(&self) -> u32 {
        self.n_aux_contributors + self.n_receivers
    }

    /// Number of receivers configured at construction.
    pub fn n_receivers(&self) -> u32 {
        self.n_receivers
    }

    /// Threshold configured at construction.
    pub fn threshold(&self) -> u32 {
        self.threshold
    }
}

impl Contributor<AuxContributor> {
    /// Verify the coordinator faithfully included this contributor's input.
    /// Parties must still confirm agreement on the result by comparing
    /// [`VerifiedAggKeygenInput::cert_bytes`] out-of-band.
    pub fn verify_agg_input<H, NG>(
        self,
        schnorr: &Schnorr<H, NG>,
        agg_input: AggKeygenInput,
    ) -> Result<VerifiedAggKeygenInput, VerifyAggInputError>
    where
        H: Hash32,
    {
        self.verify_agg_input_inner(schnorr, agg_input)
    }
}

impl Contributor<ShareReceiver> {
    /// Verify the aggregated input and extract this contributor's paired secret share
    /// atomically. `secret_share` is already summed across contributors; if you have
    /// per-contributor inputs use [`verify_agg_input_combining`] instead. Parties must
    /// still confirm agreement out-of-band via [`VerifiedAggKeygenInput::cert_bytes`].
    ///
    /// [`verify_agg_input_combining`]: Self::verify_agg_input_combining
    pub fn verify_agg_input<H, NG>(
        self,
        schnorr: &Schnorr<H, NG>,
        agg_input: AggKeygenInput,
        secret_share: Scalar<Secret, Zero>,
    ) -> Result<(VerifiedAggKeygenInput, PairedSecretShare), ShareReceiverError>
    where
        H: Hash32,
    {
        let my_position = self.my_index;
        let verified = self.verify_agg_input_inner(schnorr, agg_input)?;
        let my_share_index = ShareIndex::try_from(my_position + 1).expect("non-zero");
        let secret_share = SecretShare {
            index: my_share_index,
            share: secret_share,
        };
        let paired = verified
            .shared_key
            .pair_secret_share(secret_share)
            .ok_or(ShareReceiverError::InvalidSecretShare)?;
        Ok((verified, paired))
    }

    /// Like [`verify_agg_input`] but sums per-contributor share contributions first.
    ///
    /// Pass the `i`th entry from every contributor's [`SecretKeygenInput`] in
    /// slot order; `secret_share_inputs.len()` must equal `n_contributors`.
    ///
    /// [`verify_agg_input`]: Self::verify_agg_input
    pub fn verify_agg_input_combining<H, NG>(
        self,
        schnorr: &Schnorr<H, NG>,
        agg_input: AggKeygenInput,
        secret_share_inputs: &[Scalar<Secret, Zero>],
    ) -> Result<(VerifiedAggKeygenInput, PairedSecretShare), ShareReceiverError>
    where
        H: Hash32,
    {
        let n_contributors = self.n_contributors();
        if secret_share_inputs.len() != n_contributors as usize {
            return Err(ShareReceiverError::WrongShareInputCount {
                expected: n_contributors,
                got: secret_share_inputs.len() as u32,
            });
        }
        let mut secret_share = s!(0);
        for share in secret_share_inputs {
            secret_share += share;
        }
        self.verify_agg_input(schnorr, agg_input, secret_share)
    }
}

/// Produced by [`Contributor::gen_keygen_input`]. This is sent from the each
/// `Contributor` to the *coordinator*.
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(
    feature = "serde",
    derive(crate::fun::serde::Deserialize, crate::fun::serde::Serialize),
    serde(crate = "crate::fun::serde")
)]
#[derive(Clone, Debug, PartialEq)]
pub struct KeygenInput {
    /// The polynomial commitment of the contributor.
    pub com: Vec<Point>,
    /// Their proof-of-possession signature on the first coefficient.
    pub pop: Signature,
}

/// One [`Contributor`]'s share contributions, indexed by receiver slot
/// (position `i` is the share for `ShareIndex = i + 1`). The application
/// must transport each entry to the corresponding receiver out-of-band —
/// `simplepedpop` does not encrypt them; use [`encpedpop`] for that.
///
/// [`encpedpop`]: super::encpedpop
pub type SecretKeygenInput = Vec<Scalar<Secret, Zero>>;

/// Stores the state of the coordinator as it aggregates inputs from [`Contributor`]s.
#[derive(Clone, Debug, PartialEq)]
pub struct Coordinator {
    threshold: u32,
    n_receivers: u32,
    inputs: BTreeMap<super::Party, Option<KeygenInput>>,
}

impl Coordinator {
    /// Total contributor slots = `n_aux_contributors + n_receivers`; receivers
    /// occupy `[0..n_receivers)`, aux contributors fill the rest.
    pub fn new(threshold: u32, n_aux_contributors: u32, n_receivers: u32) -> Self {
        assert!(threshold > 0);
        let inputs = (0..n_receivers)
            .map(super::Party::Receiver)
            .chain((0..n_aux_contributors).map(super::Party::AuxContributor))
            .map(|party| (party, None))
            .collect();
        Self {
            threshold,
            n_receivers,
            inputs,
        }
    }

    /// Adds an `input` from a [`Contributor`]. Authentication of the sender —
    /// confirming `from` is who you think it is — is up to the application.
    pub fn add_input<H: Hash32, NG>(
        &mut self,
        schnorr: &Schnorr<H, NG>,
        from: super::Party,
        input: KeygenInput,
    ) -> Result<(), AddInputError> {
        let entry = match self.inputs.get_mut(&from) {
            Some(maybe_input) => match maybe_input {
                Some(_) => return Err(AddInputError::DuplicateInput { from }),
                none => none,
            },
            None => return Err(AddInputError::UnknownContributor { from }),
        };
        if input.com.len() != self.threshold as usize {
            return Err(AddInputError::WrongThreshold {
                expected: self.threshold,
                got: input.com.len() as u32,
            });
        }

        let slot = from.slot_index(self.n_receivers);
        let (first_coeff_even_y, _) = input.com[0].into_point_with_even_y();
        if !schnorr.verify(
            &first_coeff_even_y,
            Message::new(POP_DOMAIN_SEP, &pop_message_bytes(input.com[0], slot)),
            &input.pop,
        ) {
            return Err(AddInputError::InvalidProofOfPossession);
        }
        *entry = Some(input);

        Ok(())
    }

    /// Which [`Contributor`]s are we missing input from.
    pub fn missing_from(&self) -> BTreeSet<super::Party> {
        self.inputs
            .iter()
            .filter_map(|(party, input)| input.is_none().then_some(*party))
            .collect()
    }

    /// Has the coordinator received input from each [`Contributor`].
    pub fn is_finished(&self) -> bool {
        self.inputs.values().all(|v| v.is_some())
    }

    /// Try and finish input aggregation step.
    ///
    /// Returns `None` if [`is_finished`] returns `false`.
    ///
    /// [`is_finished`]: Self::is_finished
    pub fn finish(self) -> Option<AggKeygenInput> {
        if !self.is_finished() {
            return None;
        }
        let inputs = self.inputs.into_values().flatten().collect::<Vec<_>>();
        // The "key contributions" are separated out and treated specially since they can't be
        // aggregated by the coordinator since each one needs to be validated against a
        // proof-of-possesson.
        let key_contrib = inputs
            .iter()
            .map(|message| (message.com[0], message.pop))
            .collect();

        // The rest of the coefficients can be aggregated
        let mut agg_poly = vec![Point::<NonNormal, Public, _>::zero(); self.threshold as usize - 1];
        for message in inputs {
            for (i, com) in message.com[1..].iter().enumerate() {
                agg_poly[i] += com
            }
        }

        let agg_poly = poly::point::normalize(agg_poly).collect::<Vec<_>>();

        Some(AggKeygenInput {
            key_contrib,
            agg_poly,
        })
    }
}

/// Key generation inputs after being aggregated by the coordinator
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[cfg_attr(
    feature = "serde",
    derive(crate::fun::serde::Deserialize, crate::fun::serde::Serialize),
    serde(crate = "crate::fun::serde")
)]
pub struct AggKeygenInput {
    /// The key contribution from each [`Contributor`]
    key_contrib: Vec<(Point, Signature)>,
    /// The aggregated non-constant term polynomial
    agg_poly: Vec<Point<Normal, Public, Zero>>,
}

impl AggKeygenInput {
    pub(in crate::frost::chilldkg) fn n_contributors(&self) -> usize {
        self.key_contrib.len()
    }

    fn shared_key(&self) -> SharedKey<Normal, Zero> {
        let public_key = self
            .key_contrib
            .iter()
            .fold(Point::zero(), |agg, (point, _)| g!(agg + point))
            .normalize();
        let mut poly = self.agg_poly.clone();
        poly.insert(0, public_key);
        SharedKey::from_poly(poly)
    }

    /// Grind `fingerprint` into the aggregated polynomial coefficients. Returns
    /// the tweak polynomial so [`encpedpop`] can apply the same tweak
    /// homomorphically to encrypted shares.
    ///
    /// [`encpedpop`]: super::encpedpop
    pub(in crate::frost::chilldkg) fn grind_fingerprint<H: Hash32>(
        &mut self,
        fingerprint: Fingerprint,
    ) -> Vec<Scalar<Public, Zero>> {
        let mut shared_key = self.shared_key();
        let tweak_poly = shared_key.grind_fingerprint::<H>(fingerprint);
        self.agg_poly = shared_key.point_polynomial()[1..].to_vec();
        debug_assert!(
            self.shared_key()
                .check_fingerprint::<H>(fingerprint)
                .is_some()
        );
        tweak_poly
    }

    fn verify_proofs_of_possession<H, NG>(
        &self,
        schnorr: &Schnorr<H, NG>,
    ) -> Result<(), VerifyAggInputError>
    where
        H: Hash32,
    {
        for (i, (key_contrib, pop)) in self.key_contrib.iter().enumerate() {
            let (first_coeff_even_y, _) = key_contrib.into_point_with_even_y();
            if !schnorr.verify(
                &first_coeff_even_y,
                Message::new(POP_DOMAIN_SEP, &pop_message_bytes(*key_contrib, i as u32)),
                pop,
            ) {
                return Err(VerifyAggInputError::InvalidProofOfPossession { from: i as u32 });
            }
        }

        Ok(())
    }
}

/// An [`AggKeygenInput`] that this contributor has verified against its own protocol state.
#[derive(Clone, Debug, PartialEq)]
pub struct VerifiedAggKeygenInput {
    inner: AggKeygenInput,
    shared_key: SharedKey,
}

impl VerifiedAggKeygenInput {
    /// The raw aggregate that was verified.
    pub fn agg_input(&self) -> &AggKeygenInput {
        &self.inner
    }

    /// The [`SharedKey`] this aggregated input produces.
    pub fn shared_key(&self) -> SharedKey {
        self.shared_key.clone()
    }

    /// Bytes that uniquely identify this verified aggregate. Compare this
    /// out-of-band with every other party — if you all match, you all
    /// produced the same key. Outer layers ([`encpedpop`], [`certpedpop`])
    /// extend these bytes to bind their own state.
    ///
    /// [`encpedpop`]: super::encpedpop
    /// [`certpedpop`]: super::certpedpop
    pub fn cert_bytes(&self) -> Vec<u8> {
        let shared_key = self.shared_key();
        let poly = shared_key.point_polynomial();
        let cert_bytes = (poly.len() as u32)
            .to_be_bytes()
            .into_iter()
            .chain(poly.iter().flat_map(|coeff| coeff.to_bytes()));

        cert_bytes.collect()
    }
}

/// Test/example helper: run every `simplepedpop` party in-process. Not for production.
pub fn simulate_keygen<H, NG>(
    schnorr: &Schnorr<H, NG>,
    threshold: u32,
    n_receivers: u32,
    n_aux_contributors: u32,
    rng: &mut impl rand_core::RngCore,
) -> (SharedKey, Vec<PairedSecretShare>)
where
    H: Hash32,
    NG: NonceGen,
{
    let mut aggregator = Coordinator::new(threshold, n_aux_contributors, n_receivers);
    let mut receivers: Vec<Contributor<ShareReceiver>> = vec![];
    let mut auxes: Vec<Contributor<AuxContributor>> = vec![];
    // Per-receiver list of share contributions (indexed by receiver position).
    let mut secret_inputs: Vec<Vec<Scalar<Secret, Zero>>> = vec![vec![]; n_receivers as usize];

    for receiver_idx in 0..n_receivers {
        let (contributor, to_coordinator, shares) = Contributor::<ShareReceiver>::gen_keygen_input(
            schnorr,
            threshold,
            n_aux_contributors,
            n_receivers,
            receiver_idx,
            rng,
        )
        .unwrap();
        aggregator
            .add_input(
                schnorr,
                super::Party::Receiver(receiver_idx),
                to_coordinator,
            )
            .unwrap();
        for (receiver_position, share) in shares.into_iter().enumerate() {
            secret_inputs[receiver_position].push(share);
        }
        receivers.push(contributor);
    }
    for aux_idx in 0..n_aux_contributors {
        let (contributor, to_coordinator, shares) =
            Contributor::<AuxContributor>::gen_keygen_input(
                schnorr,
                threshold,
                n_aux_contributors,
                n_receivers,
                aux_idx,
                rng,
            )
            .unwrap();
        aggregator
            .add_input(
                schnorr,
                super::Party::AuxContributor(aux_idx),
                to_coordinator,
            )
            .unwrap();
        for (receiver_position, share) in shares.into_iter().enumerate() {
            secret_inputs[receiver_position].push(share);
        }
        auxes.push(contributor);
    }

    let agg_input = aggregator.finish().unwrap();

    let mut verified: Option<VerifiedAggKeygenInput> = None;
    let mut paired_shares = vec![];
    for (receiver_position, contributor) in receivers.into_iter().enumerate() {
        let contributions = core::mem::take(&mut secret_inputs[receiver_position]);
        let (v, paired) = contributor
            .verify_agg_input_combining(schnorr, agg_input.clone(), &contributions)
            .unwrap();
        paired_shares.push(paired);
        verified.get_or_insert(v);
    }
    for contributor in auxes {
        verified.get_or_insert(
            contributor
                .verify_agg_input(schnorr, agg_input.clone())
                .unwrap(),
        );
    }
    let verified = verified.expect("at least one contributor");

    (verified.shared_key(), paired_shares)
}

/// Reasons [`Contributor::gen_keygen_input`] may fail.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GenKeygenInputError {
    /// `threshold == 0` or `threshold > n_receivers`. A threshold above the
    /// receiver count produces an un-reconstructible key.
    InvalidThreshold {
        /// The supplied threshold.
        threshold: u32,
        /// The supplied number of receivers.
        n_receivers: u32,
    },
    /// `my_role_index` was out of range for the role. For [`ShareReceiver`] the
    /// max is `n_receivers`; for [`AuxContributor`] it is `n_aux_contributors`.
    IndexOutOfRange {
        /// The supplied role-relative index.
        role_index: u32,
    },
    /// `n_aux_contributors + n_receivers` overflowed `u32`.
    TooManyContributors,
}

impl core::fmt::Display for GenKeygenInputError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            GenKeygenInputError::InvalidThreshold {
                threshold,
                n_receivers,
            } => write!(
                f,
                "threshold {threshold} is invalid for {n_receivers} receivers"
            ),
            GenKeygenInputError::IndexOutOfRange { role_index } => write!(
                f,
                "role-relative index {role_index} is out of range for this role"
            ),
            GenKeygenInputError::TooManyContributors => {
                write!(f, "n_aux_contributors + n_receivers overflowed u32")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for GenKeygenInputError {}

/// Reasons [`Contributor::verify_agg_input`] may fail.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VerifyAggInputError {
    /// This contributor's input was not faithfully included in the aggregated input.
    ContributionDidntMatch,
    /// A key contribution had an invalid proof of possession.
    InvalidProofOfPossession {
        /// The contributor slot whose PoP failed.
        from: u32,
    },
    /// The aggregated key contributions summed to the point at infinity —
    /// negligible with one honest random contributor, so this indicates malice.
    SharedKeyIsZero,
}

impl core::fmt::Display for VerifyAggInputError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            VerifyAggInputError::ContributionDidntMatch => write!(
                f,
                "the contribution assigned to us was not what we contributed"
            ),
            VerifyAggInputError::InvalidProofOfPossession { from } => {
                write!(f, "invalid proof of possession from contributor {from}")
            }
            VerifyAggInputError::SharedKeyIsZero => write!(
                f,
                "aggregated key contributions summed to the point at infinity"
            ),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for VerifyAggInputError {}

/// Reasons [`Contributor::<ShareReceiver>::verify_agg_input`] (or its
/// summing variant [`verify_agg_input_combining`]) may fail.
///
/// [`verify_agg_input_combining`]: Contributor::<ShareReceiver>::verify_agg_input_combining
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ShareReceiverError {
    /// The aggregated input failed to verify.
    VerifyAggInput(VerifyAggInputError),
    /// The supplied `secret_share` did not pair with the verified aggregate.
    InvalidSecretShare,
    /// `secret_share_inputs.len()` passed to `verify_agg_input_combining`
    /// did not match `n_contributors`.
    WrongShareInputCount {
        /// The configured number of contributors.
        expected: u32,
        /// The number of share inputs actually supplied.
        got: u32,
    },
}

impl From<VerifyAggInputError> for ShareReceiverError {
    fn from(err: VerifyAggInputError) -> Self {
        ShareReceiverError::VerifyAggInput(err)
    }
}

impl core::fmt::Display for ShareReceiverError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ShareReceiverError::VerifyAggInput(err) => write!(f, "{err}"),
            ShareReceiverError::InvalidSecretShare => {
                write!(
                    f,
                    "the secret share did not pair with the verified aggregate"
                )
            }
            ShareReceiverError::WrongShareInputCount { expected, got } => write!(
                f,
                "expected {expected} secret share inputs (one per contributor), got {got}"
            ),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ShareReceiverError {}

/// Reasons [`Coordinator::add_input`] may reject a contributor's input.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AddInputError {
    /// Input from this contributor has already been recorded.
    DuplicateInput {
        /// The party that already has an input recorded.
        from: super::Party,
    },
    /// No contributor is expected at this party.
    UnknownContributor {
        /// The unexpected party.
        from: super::Party,
    },
    /// Polynomial commitment length doesn't match the configured threshold.
    WrongThreshold {
        /// The threshold the coordinator was configured with.
        expected: u32,
        /// The number of coefficients we actually received.
        got: u32,
    },
    /// Proof-of-possession signature failed to verify.
    InvalidProofOfPossession,
}

impl core::fmt::Display for AddInputError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            AddInputError::DuplicateInput { from } => {
                write!(f, "already have input from {from:?}")
            }
            AddInputError::UnknownContributor { from } => {
                write!(f, "no input expected from {from:?}")
            }
            AddInputError::WrongThreshold { expected, got } => write!(
                f,
                "input polynomial has {got} coefficients but threshold is {expected}"
            ),
            AddInputError::InvalidProofOfPossession => {
                write!(f, "proof-of-possession signature did not verify")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for AddInputError {}

// `parity_byte || contributor_index_be`. The parity byte closes the BIP340
// x-only gap so a replay of `(A, pop)` under `-A` fails PoP verification.
// SPEC DEVIATION: spec only has the contribution_index.
fn pop_message_bytes(first_coeff: Point, contributor_index: u32) -> [u8; 5] {
    let mut bytes = [0u8; 5];
    bytes[0] = u8::from(!first_coeff.is_y_even());
    bytes[1..].copy_from_slice(&contributor_index.to_be_bytes());
    bytes
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::frost::chilldkg::{Party, simplepedpop};

    use proptest::{
        prelude::*,
        test_runner::{RngAlgorithm, TestRng},
    };
    use secp256kfun::proptest;

    proptest! {
        #[test]
        fn simplepedpop_run_simulate_keygen(
            (n_receivers, threshold) in (1u32..=4).prop_flat_map(|n| (Just(n), 1u32..=n)),
            n_aux_contributors in 0u32..5,
        ) {
            let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
            let mut rng = TestRng::deterministic_rng(RngAlgorithm::ChaCha);

            simplepedpop::simulate_keygen(&schnorr, threshold, n_receivers, n_aux_contributors, &mut rng);
        }
    }

    // The PoP must bind both the slot index and the y-parity of com[0]:
    //   (1) Alice's pop for slot 0 must not verify at another slot.
    //   (2) A replay of Alice's pop with the negated first coefficient (-A) must
    //       not verify at any slot, including slot 0.
    #[test]
    fn pop_bound_to_slot_and_parity_rejects_replay() {
        let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
        let mut rng = TestRng::deterministic_rng(RngAlgorithm::ChaCha);
        let threshold = 2u32;
        let n_aux_contributors = 0u32;
        let n_receivers = 2u32;

        let (_alice_state, alice_msg, _alice_shares) =
            simplepedpop::Contributor::<ShareReceiver>::gen_keygen_input(
                &schnorr,
                threshold,
                n_aux_contributors,
                n_receivers,
                0,
                &mut rng,
            )
            .unwrap();

        let mut coord_replay_slot =
            simplepedpop::Coordinator::new(threshold, n_aux_contributors, n_receivers);
        assert_eq!(
            coord_replay_slot.add_input(&schnorr, Party::Receiver(1), alice_msg.clone()),
            Err(AddInputError::InvalidProofOfPossession),
            "Alice's pop for slot 0 must not verify at slot 1"
        );

        let negated_msg = KeygenInput {
            com: core::iter::once(-alice_msg.com[0])
                .chain(alice_msg.com[1..].iter().copied())
                .collect(),
            pop: alice_msg.pop,
        };
        let mut coord_negate_same_slot =
            simplepedpop::Coordinator::new(threshold, n_aux_contributors, n_receivers);
        assert_eq!(
            coord_negate_same_slot.add_input(&schnorr, Party::Receiver(0), negated_msg.clone()),
            Err(AddInputError::InvalidProofOfPossession),
            "negated-key replay at the original slot must now be rejected by the PoP check"
        );

        let mut coord_negate_other_slot =
            simplepedpop::Coordinator::new(threshold, n_aux_contributors, n_receivers);
        assert_eq!(
            coord_negate_other_slot.add_input(&schnorr, Party::Receiver(1), negated_msg),
            Err(AddInputError::InvalidProofOfPossession),
            "negated-key replay at a different slot must be rejected by the PoP check"
        );
    }

    fn run_input_aggregation_stage(
        threshold: u32,
        n_aux_contributors: u32,
        n_receivers: u32,
    ) -> (
        Vec<simplepedpop::Contributor<AuxContributor>>,
        simplepedpop::AggKeygenInput,
    ) {
        let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
        let mut rng = TestRng::deterministic_rng(RngAlgorithm::ChaCha);
        let mut coordinator =
            simplepedpop::Coordinator::new(threshold, n_aux_contributors, n_receivers);
        for receiver_idx in 0..n_receivers {
            let (_, msg, _) = simplepedpop::Contributor::<ShareReceiver>::gen_keygen_input(
                &schnorr,
                threshold,
                n_aux_contributors,
                n_receivers,
                receiver_idx,
                &mut rng,
            )
            .unwrap();
            coordinator
                .add_input(&schnorr, Party::Receiver(receiver_idx), msg)
                .unwrap();
        }
        let mut auxes = vec![];
        for aux_idx in 0..n_aux_contributors {
            let (contributor, msg, _) =
                simplepedpop::Contributor::<AuxContributor>::gen_keygen_input(
                    &schnorr,
                    threshold,
                    n_aux_contributors,
                    n_receivers,
                    aux_idx,
                    &mut rng,
                )
                .unwrap();
            coordinator
                .add_input(&schnorr, Party::AuxContributor(aux_idx), msg)
                .unwrap();
            auxes.push(contributor);
        }
        (auxes, coordinator.finish().unwrap())
    }

    #[test]
    fn verify_agg_input_rejects_tampered_pop() {
        let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
        let (auxes, mut agg_input) = run_input_aggregation_stage(2, 1, 2);
        let wrong_pop = agg_input.key_contrib[1].1;
        agg_input.key_contrib[0].1 = wrong_pop;

        assert_eq!(
            auxes
                .into_iter()
                .next()
                .unwrap()
                .verify_agg_input(&schnorr, agg_input),
            Err(simplepedpop::VerifyAggInputError::InvalidProofOfPossession { from: 0 }),
            "verify_agg_input must reject raw aggregates with tampered PoPs"
        );
    }

    // A malicious aggregator could pad agg_poly with a zero high-order
    // coefficient to inflate the apparent threshold without changing the
    // polynomial's value at any ShareIndex. verify_agg_input must reject this
    // — contributors signed up for a specific threshold and that determines
    // the polynomial degree.
    #[test]
    fn verify_agg_input_rejects_padded_threshold() {
        use secp256kfun::Point;
        let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
        let (auxes, mut agg_input) = run_input_aggregation_stage(2, 1, 2);
        agg_input
            .agg_poly
            .push(Point::<Normal, Public, Zero>::zero());

        assert_eq!(
            auxes
                .into_iter()
                .next()
                .unwrap()
                .verify_agg_input(&schnorr, agg_input),
            Err(simplepedpop::VerifyAggInputError::ContributionDidntMatch),
            "verify_agg_input must reject agg_poly padding that inflates the threshold"
        );
    }

    // A threshold higher than the number of receivers produces an
    // un-reconstructible key generation. gen_keygen_input must reject this up
    // front rather than silently producing a useless aggregate.
    #[test]
    fn gen_keygen_input_rejects_threshold_above_n_receivers() {
        let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
        let mut rng = TestRng::deterministic_rng(RngAlgorithm::ChaCha);

        let result = simplepedpop::Contributor::<ShareReceiver>::gen_keygen_input(
            &schnorr, 3, 0, 2, 0, &mut rng,
        );
        assert_eq!(
            result.err(),
            Some(simplepedpop::GenKeygenInputError::InvalidThreshold {
                threshold: 3,
                n_receivers: 2,
            })
        );
    }
}