1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![doc = include_str!("../README.md")]
3use core::{marker::PhantomData, ops::Deref, fmt};
7use std::{
8 io::{self, Read, Write},
9 collections::HashMap,
10};
11
12use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
13use rand_core::{RngCore, CryptoRng};
14
15use transcript::{Transcript, RecommendedTranscript};
16
17use multiexp::{multiexp_vartime, BatchVerifier};
18use ciphersuite::{
19 group::{
20 ff::{Field, PrimeField},
21 Group, GroupEncoding,
22 },
23 Ciphersuite,
24};
25
26use schnorr::SchnorrSignature;
27
28pub use dkg::*;
29
30mod encryption;
31pub use encryption::*;
32
33#[cfg(test)]
34mod tests;
35
36#[derive(Clone, PartialEq, Eq, Debug, thiserror::Error)]
38pub enum PedPoPError<C: Ciphersuite> {
39 #[error("incorrect amount of participants (expected {expected}, found {found})")]
41 IncorrectAmountOfParticipants { expected: usize, found: usize },
42 #[error("invalid proof of knowledge (participant {0})")]
44 InvalidCommitments(Participant),
45 #[error("invalid share (participant {participant}, blame {blame})")]
47 InvalidShare { participant: Participant, blame: Option<EncryptionKeyProof<C>> },
48 #[error("missing participant {0}")]
50 MissingParticipant(Participant),
51 #[error("error from dkg ({0})")]
53 DkgError(DkgError),
54}
55
56fn validate_map<T, C: Ciphersuite>(
58 map: &HashMap<Participant, T>,
59 included: &[Participant],
60 ours: Participant,
61) -> Result<(), PedPoPError<C>> {
62 if (map.len() + 1) != included.len() {
63 Err(PedPoPError::IncorrectAmountOfParticipants {
64 expected: included.len(),
65 found: map.len() + 1,
66 })?;
67 }
68
69 for included in included {
70 if *included == ours {
71 if map.contains_key(included) {
72 Err(PedPoPError::DkgError(DkgError::DuplicatedParticipant(*included)))?;
73 }
74 continue;
75 }
76
77 if !map.contains_key(included) {
78 Err(PedPoPError::MissingParticipant(*included))?;
79 }
80 }
81
82 Ok(())
83}
84
85#[allow(non_snake_case)]
86fn challenge<C: Ciphersuite>(context: [u8; 32], l: Participant, R: &[u8], Am: &[u8]) -> C::F {
87 let mut transcript = RecommendedTranscript::new(b"DKG PedPoP v0.2");
88 transcript.domain_separate(b"schnorr_proof_of_knowledge");
89 transcript.append_message(b"context", context);
90 transcript.append_message(b"participant", l.to_bytes());
91 transcript.append_message(b"nonce", R);
92 transcript.append_message(b"commitments", Am);
93 C::hash_to_F(b"DKG-PedPoP-proof_of_knowledge-0", &transcript.challenge(b"schnorr"))
94}
95
96#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
103pub struct Commitments<C: Ciphersuite> {
104 commitments: Vec<C::G>,
105 cached_msg: Vec<u8>,
106 sig: SchnorrSignature<C>,
107}
108
109impl<C: Ciphersuite> ReadWrite for Commitments<C> {
110 fn read<R: Read>(reader: &mut R, params: ThresholdParams) -> io::Result<Self> {
111 let mut commitments = Vec::with_capacity(params.t().into());
112 let mut cached_msg = vec![];
113
114 #[allow(non_snake_case)]
115 let mut read_G = || -> io::Result<C::G> {
116 let mut buf = <C::G as GroupEncoding>::Repr::default();
117 reader.read_exact(buf.as_mut())?;
118 let point = C::read_G(&mut buf.as_ref())?;
119 cached_msg.extend(buf.as_ref());
120 Ok(point)
121 };
122
123 for _ in 0 .. params.t() {
124 commitments.push(read_G()?);
125 }
126
127 Ok(Commitments { commitments, cached_msg, sig: SchnorrSignature::read(reader)? })
128 }
129
130 fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
131 writer.write_all(&self.cached_msg)?;
132 self.sig.write(writer)
133 }
134}
135
136#[derive(Debug, Zeroize)]
138pub struct KeyGenMachine<C: Ciphersuite> {
139 params: ThresholdParams,
140 context: [u8; 32],
141 _curve: PhantomData<C>,
142}
143
144impl<C: Ciphersuite> KeyGenMachine<C> {
145 pub fn new(params: ThresholdParams, context: [u8; 32]) -> KeyGenMachine<C> {
149 KeyGenMachine { params, context, _curve: PhantomData }
150 }
151
152 pub fn generate_coefficients<R: RngCore + CryptoRng>(
157 self,
158 rng: &mut R,
159 ) -> (SecretShareMachine<C>, EncryptionKeyMessage<C, Commitments<C>>) {
160 let t = usize::from(self.params.t());
161 let mut coefficients = Vec::with_capacity(t);
162 let mut commitments = Vec::with_capacity(t);
163 let mut cached_msg = vec![];
164
165 for i in 0 .. t {
166 coefficients.push(Zeroizing::new(C::random_nonzero_F(&mut *rng)));
168 commitments.push(C::generator() * coefficients[i].deref());
170 cached_msg.extend(commitments[i].to_bytes().as_ref());
171 }
172
173 let r = Zeroizing::new(C::random_nonzero_F(rng));
175 let nonce = C::generator() * r.deref();
176 let sig = SchnorrSignature::<C>::sign(
177 &coefficients[0],
178 r,
183 challenge::<C>(self.context, self.params.i(), nonce.to_bytes().as_ref(), &cached_msg),
184 );
185
186 let encryption = Encryption::new(self.context, self.params.i(), rng);
188
189 let msg =
191 encryption.registration(Commitments { commitments: commitments.clone(), cached_msg, sig });
192 (
193 SecretShareMachine {
194 params: self.params,
195 context: self.context,
196 coefficients,
197 our_commitments: commitments,
198 encryption,
199 },
200 msg,
201 )
202 }
203}
204
205fn polynomial<F: PrimeField + Zeroize>(
206 coefficients: &[Zeroizing<F>],
207 l: Participant,
208) -> Zeroizing<F> {
209 let l = F::from(u64::from(u16::from(l)));
210 assert!(l != F::ZERO, "zero participant passed to polynomial");
212 let mut share = Zeroizing::new(F::ZERO);
213 for (idx, coefficient) in coefficients.iter().rev().enumerate() {
214 *share += coefficient.deref();
215 if idx != (coefficients.len() - 1) {
216 *share *= l;
217 }
218 }
219 share
220}
221
222#[derive(Clone, PartialEq, Eq)]
231pub struct SecretShare<F: PrimeField>(F::Repr);
232impl<F: PrimeField> AsRef<[u8]> for SecretShare<F> {
233 fn as_ref(&self) -> &[u8] {
234 self.0.as_ref()
235 }
236}
237impl<F: PrimeField> AsMut<[u8]> for SecretShare<F> {
238 fn as_mut(&mut self) -> &mut [u8] {
239 self.0.as_mut()
240 }
241}
242impl<F: PrimeField> fmt::Debug for SecretShare<F> {
243 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
244 fmt.debug_struct("SecretShare").finish_non_exhaustive()
245 }
246}
247impl<F: PrimeField> Zeroize for SecretShare<F> {
248 fn zeroize(&mut self) {
249 self.0.as_mut().zeroize()
250 }
251}
252impl<F: PrimeField> Drop for SecretShare<F> {
257 fn drop(&mut self) {
258 self.zeroize();
259 }
260}
261impl<F: PrimeField> ZeroizeOnDrop for SecretShare<F> {}
262
263impl<F: PrimeField> ReadWrite for SecretShare<F> {
264 fn read<R: Read>(reader: &mut R, _: ThresholdParams) -> io::Result<Self> {
265 let mut repr = F::Repr::default();
266 reader.read_exact(repr.as_mut())?;
267 Ok(SecretShare(repr))
268 }
269
270 fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
271 writer.write_all(self.0.as_ref())
272 }
273}
274
275#[derive(Zeroize)]
277pub struct SecretShareMachine<C: Ciphersuite> {
278 params: ThresholdParams,
279 context: [u8; 32],
280 coefficients: Vec<Zeroizing<C::F>>,
281 our_commitments: Vec<C::G>,
282 encryption: Encryption<C>,
283}
284
285impl<C: Ciphersuite> fmt::Debug for SecretShareMachine<C> {
286 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
287 fmt
288 .debug_struct("SecretShareMachine")
289 .field("params", &self.params)
290 .field("context", &self.context)
291 .field("our_commitments", &self.our_commitments)
292 .field("encryption", &self.encryption)
293 .finish_non_exhaustive()
294 }
295}
296
297impl<C: Ciphersuite> SecretShareMachine<C> {
298 #[allow(clippy::type_complexity)]
300 fn verify_r1<R: RngCore + CryptoRng>(
301 &mut self,
302 rng: &mut R,
303 mut commitment_msgs: HashMap<Participant, EncryptionKeyMessage<C, Commitments<C>>>,
304 ) -> Result<HashMap<Participant, Vec<C::G>>, PedPoPError<C>> {
305 validate_map(
306 &commitment_msgs,
307 &self.params.all_participant_indexes().collect::<Vec<_>>(),
308 self.params.i(),
309 )?;
310
311 let mut batch = BatchVerifier::<Participant, C::G>::new(commitment_msgs.len());
312 let mut commitments = HashMap::new();
313 for l in self.params.all_participant_indexes() {
314 let Some(msg) = commitment_msgs.remove(&l) else { continue };
315 let mut msg = self.encryption.register(l, msg);
316
317 if msg.commitments.len() != self.params.t().into() {
318 Err(PedPoPError::InvalidCommitments(l))?;
319 }
320
321 msg.sig.batch_verify(
324 rng,
325 &mut batch,
326 l,
327 msg.commitments[0],
328 challenge::<C>(self.context, l, msg.sig.R.to_bytes().as_ref(), &msg.cached_msg),
329 );
330
331 commitments.insert(l, msg.commitments.drain(..).collect::<Vec<_>>());
332 }
333
334 batch.verify_vartime_with_vartime_blame().map_err(PedPoPError::InvalidCommitments)?;
335
336 commitments.insert(self.params.i(), self.our_commitments.drain(..).collect());
337 Ok(commitments)
338 }
339
340 #[allow(clippy::type_complexity)]
347 pub fn generate_secret_shares<R: RngCore + CryptoRng>(
348 mut self,
349 rng: &mut R,
350 commitments: HashMap<Participant, EncryptionKeyMessage<C, Commitments<C>>>,
351 ) -> Result<
352 (KeyMachine<C>, HashMap<Participant, EncryptedMessage<C, SecretShare<C::F>>>),
353 PedPoPError<C>,
354 > {
355 let commitments = self.verify_r1(&mut *rng, commitments)?;
356
357 let mut res = HashMap::new();
359 for l in self.params.all_participant_indexes() {
360 if l == self.params.i() {
363 continue;
364 }
365
366 let mut share = polynomial(&self.coefficients, l);
367 let share_bytes = Zeroizing::new(SecretShare::<C::F>(share.to_repr()));
368 share.zeroize();
369 res.insert(l, self.encryption.encrypt(rng, l, share_bytes));
370 }
371
372 let share = polynomial(&self.coefficients, self.params.i());
374 self.coefficients.zeroize();
375
376 Ok((
377 KeyMachine { params: self.params, secret: share, commitments, encryption: self.encryption },
378 res,
379 ))
380 }
381}
382
383pub struct KeyMachine<C: Ciphersuite> {
389 params: ThresholdParams,
390 secret: Zeroizing<C::F>,
391 commitments: HashMap<Participant, Vec<C::G>>,
392 encryption: Encryption<C>,
393}
394
395impl<C: Ciphersuite> fmt::Debug for KeyMachine<C> {
396 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
397 fmt
398 .debug_struct("KeyMachine")
399 .field("params", &self.params)
400 .field("commitments", &self.commitments)
401 .field("encryption", &self.encryption)
402 .finish_non_exhaustive()
403 }
404}
405
406impl<C: Ciphersuite> Zeroize for KeyMachine<C> {
407 fn zeroize(&mut self) {
408 self.params.zeroize();
409 self.secret.zeroize();
410 for commitments in self.commitments.values_mut() {
411 commitments.zeroize();
412 }
413 self.encryption.zeroize();
414 }
415}
416
417fn exponential<C: Ciphersuite>(i: Participant, values: &[C::G]) -> Vec<(C::F, C::G)> {
421 let i = C::F::from(u16::from(i).into());
422 let mut res = Vec::with_capacity(values.len());
423 (0 .. values.len()).fold(C::F::ONE, |exp, l| {
424 res.push((exp, values[l]));
425 exp * i
426 });
427 res
428}
429
430fn share_verification_statements<C: Ciphersuite>(
431 target: Participant,
432 commitments: &[C::G],
433 mut share: Zeroizing<C::F>,
434) -> Vec<(C::F, C::G)> {
435 let mut values = exponential::<C>(target, commitments);
440
441 let neg_share_pub = C::generator() * -*share;
445 share.zeroize();
446 values.push((C::F::ONE, neg_share_pub));
447
448 values
449}
450
451#[derive(Clone, Copy, Hash, Debug, Zeroize)]
452enum BatchId {
453 Decryption(Participant),
454 Share(Participant),
455}
456
457impl<C: Ciphersuite> KeyMachine<C> {
458 pub fn calculate_share<R: RngCore + CryptoRng>(
464 mut self,
465 rng: &mut R,
466 mut shares: HashMap<Participant, EncryptedMessage<C, SecretShare<C::F>>>,
467 ) -> Result<BlameMachine<C>, PedPoPError<C>> {
468 validate_map(
469 &shares,
470 &self.params.all_participant_indexes().collect::<Vec<_>>(),
471 self.params.i(),
472 )?;
473
474 let mut batch = BatchVerifier::new(shares.len());
475 let mut blames = HashMap::new();
476 for (l, share_bytes) in shares.drain() {
477 let (mut share_bytes, blame) =
478 self.encryption.decrypt(rng, &mut batch, BatchId::Decryption(l), l, share_bytes);
479 let share =
480 Zeroizing::new(Option::<C::F>::from(C::F::from_repr(share_bytes.0)).ok_or_else(|| {
481 PedPoPError::InvalidShare { participant: l, blame: Some(blame.clone()) }
482 })?);
483 share_bytes.zeroize();
484 *self.secret += share.deref();
485
486 blames.insert(l, blame);
487 batch.queue(
488 rng,
489 BatchId::Share(l),
490 share_verification_statements::<C>(self.params.i(), &self.commitments[&l], share),
491 );
492 }
493 batch.verify_with_vartime_blame().map_err(|id| {
494 let (l, blame) = match id {
495 BatchId::Decryption(l) => (l, None),
496 BatchId::Share(l) => (l, Some(blames.remove(&l).unwrap())),
497 };
498 PedPoPError::InvalidShare { participant: l, blame }
499 })?;
500
501 let mut stripes = Vec::with_capacity(usize::from(self.params.t()));
506 for t in 0 .. usize::from(self.params.t()) {
507 stripes.push(self.commitments.values().map(|commitments| commitments[t]).sum());
508 }
509
510 let mut verification_shares = HashMap::new();
512 for i in self.params.all_participant_indexes() {
513 verification_shares.insert(
514 i,
515 if i == self.params.i() {
516 C::generator() * self.secret.deref()
517 } else {
518 multiexp_vartime(&exponential::<C>(i, &stripes))
519 },
520 );
521 }
522
523 let KeyMachine { commitments, encryption, params, secret } = self;
524 Ok(BlameMachine {
525 commitments,
526 encryption: encryption.into_decryption(),
527 result: Some(
528 ThresholdKeys::new(params, Interpolation::Lagrange, secret, verification_shares)
529 .map_err(PedPoPError::DkgError)?,
530 ),
531 })
532 }
533}
534
535pub struct BlameMachine<C: Ciphersuite> {
537 commitments: HashMap<Participant, Vec<C::G>>,
538 encryption: Decryption<C>,
539 result: Option<ThresholdKeys<C>>,
540}
541
542impl<C: Ciphersuite> fmt::Debug for BlameMachine<C> {
543 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
544 fmt
545 .debug_struct("BlameMachine")
546 .field("commitments", &self.commitments)
547 .field("encryption", &self.encryption)
548 .finish_non_exhaustive()
549 }
550}
551
552impl<C: Ciphersuite> Zeroize for BlameMachine<C> {
553 fn zeroize(&mut self) {
554 for commitments in self.commitments.values_mut() {
555 commitments.zeroize();
556 }
557 self.result.zeroize();
558 }
559}
560
561impl<C: Ciphersuite> BlameMachine<C> {
562 pub fn complete(self) -> ThresholdKeys<C> {
572 self.result.unwrap()
573 }
574
575 fn blame_internal(
576 &self,
577 sender: Participant,
578 recipient: Participant,
579 msg: EncryptedMessage<C, SecretShare<C::F>>,
580 proof: Option<EncryptionKeyProof<C>>,
581 ) -> Participant {
582 let share_bytes = match self.encryption.decrypt_with_proof(sender, recipient, msg, proof) {
583 Ok(share_bytes) => share_bytes,
584 Err(DecryptionError::InvalidSignature) => return sender,
586 Err(DecryptionError::InvalidProof) => return recipient,
588 };
589
590 let Some(share) = Option::<C::F>::from(C::F::from_repr(share_bytes.0)) else {
591 return sender;
593 };
594
595 if !bool::from(
597 multiexp_vartime(&share_verification_statements::<C>(
598 recipient,
599 &self.commitments[&sender],
600 Zeroizing::new(share),
601 ))
602 .is_identity(),
603 ) {
604 return sender;
605 }
606
607 recipient
609 }
610
611 pub fn blame(
624 self,
625 sender: Participant,
626 recipient: Participant,
627 msg: EncryptedMessage<C, SecretShare<C::F>>,
628 proof: Option<EncryptionKeyProof<C>>,
629 ) -> (AdditionalBlameMachine<C>, Participant) {
630 let faulty = self.blame_internal(sender, recipient, msg, proof);
631 (AdditionalBlameMachine(self), faulty)
632 }
633}
634
635#[derive(Debug, Zeroize)]
637pub struct AdditionalBlameMachine<C: Ciphersuite>(BlameMachine<C>);
638impl<C: Ciphersuite> AdditionalBlameMachine<C> {
639 pub fn new(
650 context: [u8; 32],
651 n: u16,
652 mut commitment_msgs: HashMap<Participant, EncryptionKeyMessage<C, Commitments<C>>>,
653 ) -> Result<Self, PedPoPError<C>> {
654 let mut commitments = HashMap::new();
655 let mut encryption = Decryption::new(context);
656 for i in 1 ..= n {
657 let i = Participant::new(i).unwrap();
658 let Some(msg) = commitment_msgs.remove(&i) else { Err(PedPoPError::MissingParticipant(i))? };
659 commitments.insert(i, encryption.register(i, msg).commitments);
660 }
661 Ok(AdditionalBlameMachine(BlameMachine { commitments, encryption, result: None }))
662 }
663
664 pub fn blame(
675 &self,
676 sender: Participant,
677 recipient: Participant,
678 msg: EncryptedMessage<C, SecretShare<C::F>>,
679 proof: Option<EncryptionKeyProof<C>>,
680 ) -> Participant {
681 self.0.blame_internal(sender, recipient, msg, proof)
682 }
683}