From afddda0fa51da361216cd16a6380c2af6aaa3fcc Mon Sep 17 00:00:00 2001 From: Michael Rosenberg Date: Sat, 4 Sep 2021 18:00:18 -0400 Subject: [PATCH 1/4] Wrote a stateful signer trait --- signature/src/signer.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/signature/src/signer.rs b/signature/src/signer.rs index 0031b73a7..1b079a467 100644 --- a/signature/src/signer.rs +++ b/signature/src/signer.rs @@ -24,6 +24,22 @@ pub trait Signer { fn try_sign(&self, msg: &[u8]) -> Result; } +/// Sign the provided message bytestring using `&mut Self` (e.g., an evolving +/// cryptographic key), returning a digital signature. +pub trait StatefulSigner { + /// Sign the given message, update the state, and return a digital signature + fn sign(&mut self, msg: &[u8]) -> S { + self.try_sign(msg).expect("signature operation failed") + } + + /// Attempt to sign the given message, updating the state, and returning a + /// digital signature on success, or an error if something went wrong. + /// + /// Signing can fail, e.g., if the number of time periods allowed by the + /// current key is exceeded. + fn try_sign(&mut self, msg: &[u8]) -> Result; +} + /// Sign the given prehashed message [`Digest`] using `Self`. /// /// ## Notes From be3784d84e17268b85109f6c2ddc75e21f9eb4ec Mon Sep 17 00:00:00 2001 From: Michael Rosenberg Date: Sat, 4 Sep 2021 18:01:08 -0400 Subject: [PATCH 2/4] Updated CHANGELOG --- signature/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/signature/CHANGELOG.md b/signature/CHANGELOG.md index 847134ef8..33982b4e4 100644 --- a/signature/CHANGELOG.md +++ b/signature/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Re-export `rand_core`. Emit compilation error if unstable functionality is enabled by bypassing the preview features. ([#683]) +- Defined the `StatefulSigner` trait [#683]: https://github.com/RustCrypto/traits/pull/683 From e0942e4e333f9f430f2b4ba26a10655ed63c8905 Mon Sep 17 00:00:00 2001 From: Michael Rosenberg Date: Sat, 4 Sep 2021 22:21:32 -0400 Subject: [PATCH 3/4] Renamed StatefulSigner -> SignerMut --- signature/CHANGELOG.md | 2 +- signature/src/signer.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/signature/CHANGELOG.md b/signature/CHANGELOG.md index 33982b4e4..b21101d9e 100644 --- a/signature/CHANGELOG.md +++ b/signature/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Re-export `rand_core`. Emit compilation error if unstable functionality is enabled by bypassing the preview features. ([#683]) -- Defined the `StatefulSigner` trait +- Defined the `SignerMut` trait [#683]: https://github.com/RustCrypto/traits/pull/683 diff --git a/signature/src/signer.rs b/signature/src/signer.rs index 1b079a467..bbcce56c8 100644 --- a/signature/src/signer.rs +++ b/signature/src/signer.rs @@ -26,7 +26,7 @@ pub trait Signer { /// Sign the provided message bytestring using `&mut Self` (e.g., an evolving /// cryptographic key), returning a digital signature. -pub trait StatefulSigner { +pub trait SignerMut { /// Sign the given message, update the state, and return a digital signature fn sign(&mut self, msg: &[u8]) -> S { self.try_sign(msg).expect("signature operation failed") From ab016e02d1c3fbb56d1b1421e62572dd716a4c9a Mon Sep 17 00:00:00 2001 From: Michael Rosenberg Date: Sun, 5 Sep 2021 10:29:15 -0400 Subject: [PATCH 4/4] Wrote blanket impl for SignerMut for all Signer types --- signature/src/signer.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/signature/src/signer.rs b/signature/src/signer.rs index bbcce56c8..8ec1099c3 100644 --- a/signature/src/signer.rs +++ b/signature/src/signer.rs @@ -40,6 +40,17 @@ pub trait SignerMut { fn try_sign(&mut self, msg: &[u8]) -> Result; } +// Blanket impl of SignerMut for all Signer types +impl SignerMut for T +where + T: Signer, + S: Signature, +{ + fn try_sign(&mut self, msg: &[u8]) -> Result { + T::try_sign(&self, msg) + } +} + /// Sign the given prehashed message [`Digest`] using `Self`. /// /// ## Notes