From cc2bd545447e4cda8b84f73d442dbcc5d3248945 Mon Sep 17 00:00:00 2001 From: Stan Bondi Date: Wed, 23 Nov 2022 11:20:27 +0400 Subject: [PATCH] fix: make schnorr sig impls more general --- src/signatures/schnorr.rs | 44 +++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/signatures/schnorr.rs b/src/signatures/schnorr.rs index cf096c8e..d929d657 100644 --- a/src/signatures/schnorr.rs +++ b/src/signatures/schnorr.rs @@ -7,6 +7,7 @@ use std::{ cmp::Ordering, + hash::{Hash, Hasher}, marker::PhantomData, ops::{Add, Mul}, }; @@ -41,7 +42,7 @@ pub enum SchnorrSignatureError { /// /// More details on Schnorr signatures can be found at [TLU](https://tlu.tarilabs.com/cryptography/introduction-schnorr-signatures). #[allow(non_snake_case)] -#[derive(PartialEq, Eq, Copy, Debug, Clone, Serialize, Deserialize, Hash)] +#[derive(Copy, Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))] pub struct SchnorrSignature { public_nonce: P, @@ -229,12 +230,13 @@ where } } -impl<'a, 'b, P, K> Add<&'b SchnorrSignature> for &'a SchnorrSignature +impl<'a, 'b, P, K, H> Add<&'b SchnorrSignature> for &'a SchnorrSignature where P: PublicKey, &'a P: Add<&'b P, Output = P>, K: SecretKey, &'a K: Add<&'b K, Output = K>, + H: DomainSeparation, { type Output = SchnorrSignature; @@ -245,12 +247,13 @@ where } } -impl<'a, P, K> Add> for &'a SchnorrSignature +impl<'a, P, K, H> Add> for &'a SchnorrSignature where P: PublicKey, for<'b> &'a P: Add<&'b P, Output = P>, K: SecretKey, for<'b> &'a K: Add<&'b K, Output = K>, + H: DomainSeparation, { type Output = SchnorrSignature; @@ -261,17 +264,18 @@ where } } -impl Default for SchnorrSignature +impl Default for SchnorrSignature where P: PublicKey, K: SecretKey, + H: DomainSeparation, { fn default() -> Self { SchnorrSignature::new(P::default(), K::default()) } } -impl Ord for SchnorrSignature +impl Ord for SchnorrSignature where P: Eq + Ord, K: Eq + ByteArray, @@ -289,7 +293,7 @@ where } } -impl PartialOrd for SchnorrSignature +impl PartialOrd for SchnorrSignature where P: Eq + Ord, K: Eq + ByteArray, @@ -299,6 +303,34 @@ where } } +impl Eq for SchnorrSignature +where + P: Eq, + K: Eq, +{ +} + +impl PartialEq for SchnorrSignature +where + P: PartialEq, + K: PartialEq, +{ + fn eq(&self, other: &Self) -> bool { + self.public_nonce.eq(&other.public_nonce) && self.signature.eq(&other.signature) + } +} + +impl Hash for SchnorrSignature +where + P: Hash, + K: Hash, +{ + fn hash(&self, state: &mut T) { + self.public_nonce.hash(state); + self.signature.hash(state); + } +} + #[cfg(test)] mod test { use crate::{hashing::DomainSeparation, signatures::SchnorrSigChallenge};