Skip to content

Commit

Permalink
for serde change tuple struct to field structs.
Browse files Browse the repository at this point in the history
tuple structs give trouble to typetag crate required by gotham-engine
  • Loading branch information
max-zengo committed May 28, 2024
1 parent 9f93c6a commit 7702c9b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
16 changes: 10 additions & 6 deletions src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub struct AggPublicKeyAndMusigCoeff {
#[derive(Debug, Clone, PartialEq, Eq)]
/// Data required to sign for the derived public key, this is generated when [`AggPublicKeyAndMusigCoeff::derive_key`] is called,
/// and this needs to be passed to [`KeyPair::partial_sign_derived`] when signing
pub struct DerivationData(pub(crate) Scalar);
pub struct DerivationData {
pub(crate) scalar: Scalar
}

impl AggPublicKeyAndMusigCoeff {
/// Aggregate public keys. This creates a combined public key that requires both parties in order to sign messages.
Expand Down Expand Up @@ -80,7 +82,7 @@ impl AggPublicKeyAndMusigCoeff {
musig_coefficient: self.musig_coefficient,
location: self.location,
},
DerivationData(delta),
DerivationData { scalar: delta },
)
}

Expand Down Expand Up @@ -109,17 +111,19 @@ impl AggPublicKeyAndMusigCoeff {
}

/// The aggregated nonce of both parties, required for aggregating the signatures.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AggregatedNonce(pub(crate) EdwardsPoint);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AggregatedNonce {
pub(crate) point: EdwardsPoint
}

impl AggregatedNonce {
/// Serialize the aggregated nonce
pub fn serialize(&self) -> [u8; 32] {
self.0.compress().0
self.point.compress().0
}

/// Deserialize the aggregated nonce
pub fn deserialize(bytes: [u8; 32]) -> Option<Self> {
edwards_from_bytes(&bytes).map(Self)
edwards_from_bytes(&bytes).map(|x| Self { point: x })
}
}
8 changes: 4 additions & 4 deletions src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ impl KeyPair {

// Only one party needs to adjust the signature, so we limit to just the "first" party in the ordered set.
if agg_public_key.location == KeySortedLocation::First {
let challenge = Signature::k(&nonce.0, &agg_public_key.agg_public_key, message);
sig.0 += derived_data.0 * challenge;
let challenge = Signature::k(&nonce.point, &agg_public_key.agg_public_key, message);
sig.scalar += derived_data.scalar * challenge;
}
(sig, nonce)
}
Expand Down Expand Up @@ -119,8 +119,8 @@ impl KeyPair {
let partial_signature =
effective_r + (agg_public_key.musig_coefficient * self.private_key * sig_challenge);
(
PartialSignature(partial_signature),
AggregatedNonce(effective_R),
PartialSignature { scalar: partial_signature },
AggregatedNonce{ point: effective_R },
)
}

Expand Down
8 changes: 5 additions & 3 deletions src/partial_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ use curve25519_dalek::scalar::Scalar;

/// A partial signature, should be aggregated with another partial signature under the same aggregated public key and message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartialSignature(pub(crate) Scalar);
pub struct PartialSignature{
pub(crate) scalar: Scalar
}

impl PartialSignature {
/// Serialize the partial signature
pub fn serialize(&self) -> [u8; 32] {
self.0.to_bytes()
self.scalar.to_bytes()
}

/// Deserialize the partial signature, returns None if the bytes cannot represent a signature.
pub fn deserialize(bytes: [u8; 32]) -> Option<Self> {
scalar_from_bytes(&bytes).map(Self)
scalar_from_bytes(&bytes).map(|x| Self { scalar: x })
}
}
2 changes: 1 addition & 1 deletion src/private_partial_nonces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use zeroize::Zeroize;


#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// Private Partial Nonces, they should be kept until partially signing a message and then they should be discarded.
///
/// SECURITY: Reusing them across signatures will cause the private key to leak
Expand Down
4 changes: 2 additions & 2 deletions src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl Signature {
partial_sigs: [PartialSignature; 2],
) -> Self {
Self {
R: aggregated_nonce.0,
s: partial_sigs[0].0 + partial_sigs[1].0,
R: aggregated_nonce.point,
s: partial_sigs[0].scalar + partial_sigs[1].scalar,
}
}
/// Verify an ed25519 signature, this is a strict verification and requires both the public key
Expand Down

0 comments on commit 7702c9b

Please sign in to comment.