Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ecdsa: implement signers with a macro #569

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions src/ecdsa/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{object, Client};
use ecdsa::{
der,
elliptic_curve::{
consts::{U32, U48},
generic_array::ArrayLength,
point::PointCompression,
sec1::{self, FromEncodedPoint, ToEncodedPoint},
Expand Down Expand Up @@ -122,39 +121,31 @@ where
type VerifyingKey = VerifyingKey<C>;
}

impl PrehashSigner<Signature<NistP256>> for Signer<NistP256> {
/// Compute a fixed-size P-256 ECDSA signature of a digest output.
fn sign_prehash(&self, prehash: &[u8]) -> Result<Signature<NistP256>, Error> {
self.sign_prehash_ecdsa(prehash)
}
}

impl<D> DigestSigner<D, Signature<NistP256>> for Signer<NistP256>
where
D: Digest<OutputSize = U32> + Default,
{
/// Compute a fixed-sized P-256 ECDSA signature of the given digest
fn try_sign_digest(&self, digest: D) -> Result<Signature<NistP256>, Error> {
self.sign_prehash(&digest.finalize())
}
}

impl PrehashSigner<Signature<NistP384>> for Signer<NistP384> {
/// Compute a fixed-size P-384 ECDSA signature of a digest output.
fn sign_prehash(&self, prehash: &[u8]) -> Result<Signature<NistP384>, Error> {
self.sign_prehash_ecdsa(prehash)
}
}

impl<D> DigestSigner<D, Signature<NistP384>> for Signer<NistP384>
where
D: Digest<OutputSize = U48> + Default,
{
/// Compute a fixed-sized P-384 ECDSA signature of the given digest
fn try_sign_digest(&self, digest: D) -> Result<Signature<NistP384>, Error> {
self.sign_prehash(&digest.finalize())
}
}
macro_rules! impl_signer {
($curve:ty) => {
impl PrehashSigner<Signature<$curve>> for Signer<$curve> {
/// Compute a fixed-size ECDSA signature of a digest output.
fn sign_prehash(&self, prehash: &[u8]) -> Result<Signature<$curve>, Error> {
self.sign_prehash_ecdsa(prehash)
}
}

impl DigestSigner<<$curve as DigestPrimitive>::Digest, Signature<$curve>>
for Signer<$curve>
{
/// Compute a fixed-sized ECDSA signature of the given digest
fn try_sign_digest(
&self,
digest: <$curve as DigestPrimitive>::Digest,
) -> Result<Signature<$curve>, Error> {
self.sign_prehash(&digest.finalize())
}
}
};
}

impl_signer!(NistP256);
impl_signer!(NistP384);

#[cfg(feature = "secp256k1")]
impl PrehashSigner<Signature<Secp256k1>> for Signer<Secp256k1> {
Expand All @@ -179,24 +170,28 @@ impl PrehashSigner<(Signature<Secp256k1>, RecoveryId)> for Signer<Secp256k1> {
}

#[cfg(feature = "secp256k1")]
impl<D> DigestSigner<D, Signature<Secp256k1>> for Signer<Secp256k1>
where
D: Digest<OutputSize = U32> + Default,
impl DigestSigner<<Secp256k1 as DigestPrimitive>::Digest, Signature<Secp256k1>>
for Signer<Secp256k1>
{
/// Compute a fixed-size secp256k1 ECDSA signature of the given digest
fn try_sign_digest(&self, digest: D) -> Result<Signature<Secp256k1>, Error> {
fn try_sign_digest(
&self,
digest: <Secp256k1 as DigestPrimitive>::Digest,
) -> Result<Signature<Secp256k1>, Error> {
self.sign_prehash(&digest.finalize())
}
}

#[cfg(feature = "secp256k1")]
impl<D> DigestSigner<D, (Signature<Secp256k1>, RecoveryId)> for Signer<Secp256k1>
where
D: Digest<OutputSize = U32> + Default,
impl DigestSigner<<Secp256k1 as DigestPrimitive>::Digest, (Signature<Secp256k1>, RecoveryId)>
for Signer<Secp256k1>
{
/// Compute a fixed-size secp256k1 ECDSA signature of the given digest along with the recovery
/// ID.
fn try_sign_digest(&self, digest: D) -> Result<(Signature<Secp256k1>, RecoveryId), Error> {
fn try_sign_digest(
&self,
digest: <Secp256k1 as DigestPrimitive>::Digest,
) -> Result<(Signature<Secp256k1>, RecoveryId), Error> {
self.sign_prehash(&digest.finalize())
}
}
Expand Down
Loading