Skip to content

Commit bf47748

Browse files
authored
signature: replace signature_derive with blanket impls (#1827)
Adds blanket impls of: - `Signer` for types which impl `PrehashSignature + DigestSignature` - `Verifier` for types which impl `PrehashSignature + DigestVerifier` These blanket impls eliminate the need to have a `signature_derive` crate, which only existed to write the `Signer` and `Verifier` impls for this particular case. Since this is a breaking change, bumps the version to `3.0.0-pre`
1 parent ba9dbac commit bf47748

File tree

16 files changed

+55
-885
lines changed

16 files changed

+55
-885
lines changed

.github/workflows/signature.yml

+2-19
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ jobs:
3737
toolchain: ${{ matrix.rust }}
3838
targets: ${{ matrix.target }}
3939
- run: cargo build --target ${{ matrix.target }} --release --no-default-features
40-
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features derive
40+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features digest
4141
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features rand_core
42-
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features derive,rand_core
4342

4443
minimal-versions:
4544
if: false # disabled until we stop using pre-releases
@@ -52,7 +51,7 @@ jobs:
5251
strategy:
5352
matrix:
5453
rust:
55-
- 1.85.0 # Minimum Rust version the tests pass on
54+
- 1.85.0 # MSRV
5655
- stable
5756
steps:
5857
- uses: actions/checkout@v4
@@ -63,19 +62,3 @@ jobs:
6362
- run: cargo test --release --no-default-features
6463
- run: cargo test --release
6564
- run: cargo test --release --all-features
66-
67-
derive:
68-
runs-on: ubuntu-latest
69-
strategy:
70-
matrix:
71-
rust:
72-
- 1.85.0 # MSRV
73-
- stable
74-
steps:
75-
- uses: actions/checkout@v4
76-
- uses: RustCrypto/actions/cargo-cache@master
77-
- uses: dtolnay/rust-toolchain@master
78-
with:
79-
toolchain: ${{ matrix.rust }}
80-
- run: cargo test --release
81-
working-directory: signature_derive

Cargo.lock

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ members = [
1010
"elliptic-curve",
1111
"kem",
1212
"password-hash",
13-
"signature_derive",
1413
"universal-hash",
1514
"signature",
1615
]

crypto/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cipher = { version = "0.5.0-pre.7", path = "../cipher", optional = true }
2121
digest = { version = "0.11.0-pre.9", path = "../digest", optional = true, features = ["mac"] }
2222
elliptic-curve = { version = "0.14.0-rc.1", path = "../elliptic-curve", optional = true }
2323
password-hash = { version = "0.6.0-rc.0", path = "../password-hash", optional = true }
24-
signature = { version = "2.3.0-pre.6", path = "../signature", optional = true, default-features = false }
24+
signature = { version = "3.0.0-pre", path = "../signature", optional = true, default-features = false }
2525
universal-hash = { version = "0.6.0-rc.0", path = "../universal-hash", optional = true }
2626

2727
[features]

signature/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "signature"
3-
version = "2.3.0-pre.7"
3+
version = "3.0.0-pre"
44
authors = ["RustCrypto Developers"]
55
edition = "2024"
66
rust-version = "1.85"
@@ -13,7 +13,6 @@ categories = ["cryptography", "no-std"]
1313
description = "Traits for cryptographic signature algorithms (e.g. ECDSA, Ed25519)"
1414

1515
[dependencies]
16-
derive = { package = "signature_derive", version = "2", optional = true, path = "../signature_derive" }
1716
digest = { version = "=0.11.0-pre.10", optional = true, default-features = false }
1817
rand_core = { version = "0.9", optional = true, default-features = false }
1918

signature/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,6 @@ mod prehash_signature;
144144

145145
pub use crate::{encoding::*, error::*, keypair::*, signer::*, verifier::*};
146146

147-
#[cfg(feature = "derive")]
148-
pub use derive::{Signer, Verifier};
149-
150-
#[cfg(all(feature = "derive", feature = "digest"))]
151-
pub use derive::{DigestSigner, DigestVerifier};
152-
153147
#[cfg(feature = "digest")]
154148
pub use {crate::prehash_signature::*, digest};
155149

signature/src/prehash_signature.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ use crate::{
1919
/// This approach is relatively common in signature schemes based on the
2020
/// [Fiat-Shamir heuristic].
2121
///
22-
/// For signature types that implement this trait, when the `derive` crate
23-
/// feature is enabled a custom derive for [`Signer`] is available for any
24-
/// types that impl [`DigestSigner`], and likewise for deriving [`Verifier`] for
22+
/// For signature types that implement this trait, a blanket impl of the [`Signer`] trait is
23+
/// available for any types that impl [`DigestSigner`], and likewise for the [`Verifier`] for
2524
/// types which impl [`DigestVerifier`].
2625
///
2726
/// [Fiat-Shamir heuristic]: https://en.wikipedia.org/wiki/Fiat%E2%80%93Shamir_heuristic

signature/src/signer.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::error::Error;
44

55
#[cfg(feature = "digest")]
6-
use crate::digest::Digest;
6+
use crate::{PrehashSignature, digest::Digest};
77

88
#[cfg(feature = "rand_core")]
99
use crate::rand_core::{CryptoRng, TryCryptoRng};
@@ -82,6 +82,17 @@ pub trait DigestSigner<D: Digest, S> {
8282
fn try_sign_digest(&self, digest: D) -> Result<S, Error>;
8383
}
8484

85+
#[cfg(feature = "digest")]
86+
impl<S, T> Signer<S> for T
87+
where
88+
S: PrehashSignature,
89+
T: DigestSigner<S::Digest, S>,
90+
{
91+
fn try_sign(&self, msg: &[u8]) -> Result<S, Error> {
92+
self.try_sign_digest(S::Digest::new_with_prefix(msg))
93+
}
94+
}
95+
8596
/// Sign the given message using the provided external randomness source.
8697
#[cfg(feature = "rand_core")]
8798
pub trait RandomizedSigner<S> {
@@ -124,6 +135,21 @@ pub trait RandomizedDigestSigner<D: Digest, S> {
124135
) -> Result<S, Error>;
125136
}
126137

138+
#[cfg(all(feature = "digest", feature = "rand_core"))]
139+
impl<S, T> RandomizedSigner<S> for T
140+
where
141+
S: PrehashSignature,
142+
T: RandomizedDigestSigner<S::Digest, S>,
143+
{
144+
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
145+
&self,
146+
rng: &mut R,
147+
msg: &[u8],
148+
) -> Result<S, Error> {
149+
self.try_sign_digest_with_rng(rng, S::Digest::new_with_prefix(msg))
150+
}
151+
}
152+
127153
/// Sign the provided message bytestring using `&mut Self` (e.g. an evolving
128154
/// cryptographic key such as a stateful hash-based signature), and a per-signature
129155
/// randomizer, returning a digital signature.

signature/src/verifier.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::error::Error;
44

55
#[cfg(feature = "digest")]
6-
use crate::digest::Digest;
6+
use crate::{PrehashSignature, digest::Digest};
77

88
/// Verify the provided message bytestring using `Self` (e.g. a public key)
99
pub trait Verifier<S> {
@@ -39,3 +39,14 @@ pub trait DigestVerifier<D: Digest, S> {
3939
/// Verify the signature against the given [`Digest`] output.
4040
fn verify_digest(&self, digest: D, signature: &S) -> Result<(), Error>;
4141
}
42+
43+
#[cfg(feature = "digest")]
44+
impl<S, T> Verifier<S> for T
45+
where
46+
S: PrehashSignature,
47+
T: DigestVerifier<S::Digest, S>,
48+
{
49+
fn verify(&self, msg: &[u8], signature: &S) -> Result<(), Error> {
50+
self.verify_digest(S::Digest::new_with_prefix(msg), signature)
51+
}
52+
}

signature/tests/derive.rs

-85
This file was deleted.

signature_derive/CHANGELOG.md

-76
This file was deleted.

0 commit comments

Comments
 (0)