Skip to content

Commit

Permalink
revert from struct to tuple structs
Browse files Browse the repository at this point in the history
  • Loading branch information
max-zengo committed May 28, 2024
1 parent 7702c9b commit 1268004
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 55 deletions.
14 changes: 5 additions & 9 deletions src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ 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: Scalar
}
pub struct DerivationData(pub(crate) 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 @@ -82,7 +80,7 @@ impl AggPublicKeyAndMusigCoeff {
musig_coefficient: self.musig_coefficient,
location: self.location,
},
DerivationData { scalar: delta },
DerivationData(delta),
)
}

Expand Down Expand Up @@ -112,18 +110,16 @@ impl AggPublicKeyAndMusigCoeff {

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

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

/// Deserialize the aggregated nonce
pub fn deserialize(bytes: [u8; 32]) -> Option<Self> {
edwards_from_bytes(&bytes).map(|x| Self { point: x })
edwards_from_bytes(&bytes).map(Self)
}
}
24 changes: 9 additions & 15 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.point, &agg_public_key.agg_public_key, message);
sig.scalar += derived_data.scalar * challenge;
let challenge = Signature::k(&nonce.0, &agg_public_key.agg_public_key, message);
sig.0 += derived_data.0 * challenge;
}
(sig, nonce)
}
Expand All @@ -93,8 +93,8 @@ impl KeyPair {
// is the sum of partial_nonces[i] from both parties
// NOTE: the number of nonces is v = 2 here!
let sum_R = [
public_partial_nonce[0].point1 + public_partial_nonce[1].point1,
public_partial_nonce[0].point2 + public_partial_nonce[1].point2,
public_partial_nonce[0].0.0 + public_partial_nonce[1].0.0,
public_partial_nonce[0].0.1 + public_partial_nonce[1].0.1,
];

// Compute b as hash of nonces
Expand All @@ -111,16 +111,16 @@ impl KeyPair {
// Compute effective nonce
// The idea is to compute R and r s.t. R = R_0 + b•R_1 and r = r_0 + b•r_1
let effective_R = sum_R[0] + b * sum_R[1];
let effective_r = private_partial_nonce.scalar1 + b * private_partial_nonce.scalar2;
let effective_r = private_partial_nonce.0.0 + b * private_partial_nonce.0.1;

// Compute Fiat-Shamir challenge of signature
let sig_challenge = Signature::k(&effective_R, &agg_public_key.agg_public_key, message);

let partial_signature =
effective_r + (agg_public_key.musig_coefficient * self.private_key * sig_challenge);
(
PartialSignature { scalar: partial_signature },
AggregatedNonce{ point: effective_R },
PartialSignature(partial_signature),
AggregatedNonce(effective_R),
)
}

Expand All @@ -147,14 +147,8 @@ impl KeyPair {
});
let R: [EdwardsPoint; 2] = r.map(|scalar| &scalar * constants::ED25519_BASEPOINT_TABLE);
(
PrivatePartialNonces {
scalar1: r[0],
scalar2: r[1]
},
PublicPartialNonces {
point1:R[0],
point2: R[1]
}
PrivatePartialNonces((r[0],r[1])),
PublicPartialNonces((R[0], R[1]))
)
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/partial_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ 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: Scalar
}
pub struct PartialSignature(pub(crate) Scalar);


impl PartialSignature {
/// Serialize the partial signature
pub fn serialize(&self) -> [u8; 32] {
self.scalar.to_bytes()
self.0.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(|x| Self { scalar: x })
scalar_from_bytes(&bytes).map(Self)
}
}
25 changes: 11 additions & 14 deletions src/private_partial_nonces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,41 @@ use zeroize::Zeroize;
/// 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
pub struct PrivatePartialNonces {
pub(crate) scalar1: Scalar,
pub(crate) scalar2: Scalar
}
pub struct PrivatePartialNonces(pub(crate)(Scalar, Scalar));


impl PrivatePartialNonces {
/// Serialize the private partial nonces for storage.
///
/// SECURITY: Do not reuse the nonces across signing instances. reusing the nonces will leak the private key.
pub fn serialize(&self) -> [u8; 64] {
let mut output = [0u8; 64];
output[..32].copy_from_slice(&self.scalar1.to_bytes());
output[32..64].copy_from_slice(&self.scalar2.to_bytes());
output[..32].copy_from_slice(&self.0.0.to_bytes());
output[32..64].copy_from_slice(&self.0.1.to_bytes());
output
}

/// Deserialize the private nonces,
/// Will return `None` if they're invalid.
pub fn deserialize(bytes: [u8; 64]) -> Option<Self> {
Some(Self{
scalar1: scalar_from_bytes(&bytes[..32])?,
scalar2: scalar_from_bytes(&bytes[32..64])?,
})
let scalar1 = scalar_from_bytes(&bytes[..32])?;
let scalar2 = scalar_from_bytes(&bytes[32..64])?;
Some(Self((scalar1, scalar2)))
}
}

impl zeroize::ZeroizeOnDrop for PrivatePartialNonces {}

impl zeroize::Zeroize for PrivatePartialNonces {
fn zeroize(&mut self) {
self.scalar1.zeroize();
self.scalar2.zeroize();
self.0.0.zeroize();
self.0.1.zeroize();
}
}

impl Drop for PrivatePartialNonces {
fn drop(&mut self) {
self.scalar1.zeroize();
self.scalar2.zeroize();
self.0.0.zeroize();
self.0.1.zeroize();
}
}
16 changes: 6 additions & 10 deletions src/public_partial_nonces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,22 @@ use crate::common::edwards_from_bytes;

/// Public partial nonces, they should be transmitted to the other party in order to generate the aggregated nonce.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PublicPartialNonces{
pub(crate) point1: EdwardsPoint,
pub(crate) point2: EdwardsPoint
}
pub struct PublicPartialNonces(pub(crate) (EdwardsPoint, EdwardsPoint));

impl PublicPartialNonces {
/// Serialize the public partial nonces in order to transmit the other party.
pub fn serialize(&self) -> [u8; 64] {
let mut output = [0u8; 64];
output[..32].copy_from_slice(&self.point1.compress().0[..]);
output[32..64].copy_from_slice(&self.point2.compress().0[..]);
output[..32].copy_from_slice(&self.0.0.compress().0[..]);
output[32..64].copy_from_slice(&self.0.1.compress().0[..]);
output
}

/// Deserialize the public partial nonces.
pub fn deserialize(bytes: [u8; 64]) -> Option<Self> {
Some(Self {
point1: edwards_from_bytes(&bytes[..32])?,
point2: edwards_from_bytes(&bytes[32..64])?,
})
let point1 = edwards_from_bytes(&bytes[..32])?;
let point2 = edwards_from_bytes(&bytes[32..64])?;
Some(Self((point1, point2)))
}
}

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.point,
s: partial_sigs[0].scalar + partial_sigs[1].scalar,
R: aggregated_nonce.0,
s: partial_sigs[0].0 + partial_sigs[1].0,
}
}
/// Verify an ed25519 signature, this is a strict verification and requires both the public key
Expand Down

0 comments on commit 1268004

Please sign in to comment.