From 5b26115f81f496f23c7806050965a61d8f3c0a65 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Tue, 7 Mar 2023 03:25:16 -0500 Subject: [PATCH] Add Debug implementations to dkg --- crypto/dkg/src/encryption.rs | 19 +++++++++++--- crypto/dkg/src/frost.rs | 48 +++++++++++++++++++++++++++++------- crypto/dkg/src/lib.rs | 13 ++++++++++ 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/crypto/dkg/src/encryption.rs b/crypto/dkg/src/encryption.rs index e7047065..cbf61a99 100644 --- a/crypto/dkg/src/encryption.rs +++ b/crypto/dkg/src/encryption.rs @@ -1,6 +1,5 @@ -use core::fmt::Debug; +use core::{ops::Deref, fmt}; use std::{ - ops::Deref, io::{self, Read, Write}, collections::HashMap, }; @@ -39,8 +38,8 @@ pub trait ReadWrite: Sized { } } -pub trait Message: Clone + PartialEq + Eq + Debug + Zeroize + ReadWrite {} -impl Message for M {} +pub trait Message: Clone + PartialEq + Eq + fmt::Debug + Zeroize + ReadWrite {} +impl Message for M {} /// Wraps a message with a key to use for encryption in the future. #[derive(Clone, PartialEq, Eq, Debug, Zeroize)] @@ -347,6 +346,18 @@ pub(crate) struct Encryption { enc_keys: HashMap, } +impl fmt::Debug for Encryption { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt + .debug_struct("Encryption") + .field("context", &self.context) + .field("i", &self.i) + .field("enc_pub_key", &self.enc_pub_key) + .field("enc_keys", &self.enc_keys) + .finish_non_exhaustive() + } +} + impl Zeroize for Encryption { fn zeroize(&mut self) { self.enc_key.zeroize(); diff --git a/crypto/dkg/src/frost.rs b/crypto/dkg/src/frost.rs index 054d7bd9..e2e7c64b 100644 --- a/crypto/dkg/src/frost.rs +++ b/crypto/dkg/src/frost.rs @@ -1,8 +1,4 @@ -use core::{ - marker::PhantomData, - ops::Deref, - fmt::{Debug, Formatter}, -}; +use core::{marker::PhantomData, ops::Deref, fmt}; use std::{ io::{self, Read, Write}, collections::HashMap, @@ -85,6 +81,7 @@ impl ReadWrite for Commitments { } /// State machine to begin the key generation protocol. +#[derive(Debug, Zeroize)] pub struct KeyGenMachine { params: ThresholdParams, context: String, @@ -186,8 +183,8 @@ impl AsMut<[u8]> for SecretShare { self.0.as_mut() } } -impl Debug for SecretShare { - fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), core::fmt::Error> { +impl fmt::Debug for SecretShare { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_struct("SecretShare").finish_non_exhaustive() } } @@ -198,7 +195,7 @@ impl Zeroize for SecretShare { } // Still manually implement ZeroizeOnDrop to ensure these don't stick around. // We could replace Zeroizing with a bound M: ZeroizeOnDrop. -// Doing so would potentially fail to highlight thr expected behavior with these and remove a layer +// Doing so would potentially fail to highlight the expected behavior with these and remove a layer // of depth. impl Drop for SecretShare { fn drop(&mut self) { @@ -229,6 +226,18 @@ pub struct SecretShareMachine { encryption: Encryption, } +impl fmt::Debug for SecretShareMachine { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt + .debug_struct("SecretShareMachine") + .field("params", &self.params) + .field("context", &self.context) + .field("our_commitments", &self.our_commitments) + .field("encryption", &self.encryption) + .finish_non_exhaustive() + } +} + impl SecretShareMachine { /// Verify the data from the previous round (canonicity, PoKs, message authenticity) #[allow(clippy::type_complexity)] @@ -322,6 +331,17 @@ pub struct KeyMachine { encryption: Encryption, } +impl fmt::Debug for KeyMachine { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt + .debug_struct("KeyMachine") + .field("params", &self.params) + .field("commitments", &self.commitments) + .field("encryption", &self.encryption) + .finish_non_exhaustive() + } +} + impl Zeroize for KeyMachine { fn zeroize(&mut self) { self.params.zeroize(); @@ -457,6 +477,16 @@ pub struct BlameMachine { result: ThresholdCore, } +impl fmt::Debug for BlameMachine { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt + .debug_struct("BlameMachine") + .field("commitments", &self.commitments) + .field("encryption", &self.encryption) + .finish_non_exhaustive() + } +} + impl Zeroize for BlameMachine { fn zeroize(&mut self) { for (_, commitments) in self.commitments.iter_mut() { @@ -542,7 +572,7 @@ impl BlameMachine { } } -#[derive(Zeroize)] +#[derive(Debug, Zeroize)] pub struct AdditionalBlameMachine(BlameMachine); impl AdditionalBlameMachine { /// Given an accusation of fault, determine the faulty party (either the sender, who sent an diff --git a/crypto/dkg/src/lib.rs b/crypto/dkg/src/lib.rs index 47ffc895..20c3c06a 100644 --- a/crypto/dkg/src/lib.rs +++ b/crypto/dkg/src/lib.rs @@ -344,6 +344,19 @@ pub struct ThresholdView { verification_shares: HashMap, } +impl fmt::Debug for ThresholdView { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt + .debug_struct("ThresholdView") + .field("offset", &self.offset) + .field("group_key", &self.group_key) + .field("included", &self.included) + .field("original_verification_shares", &self.original_verification_shares) + .field("verification_shares", &self.verification_shares) + .finish_non_exhaustive() + } +} + impl Zeroize for ThresholdView { fn zeroize(&mut self) { self.offset.zeroize();