diff --git a/signature/CHANGELOG.md b/signature/CHANGELOG.md index 847134ef8..b21101d9e 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 `SignerMut` trait [#683]: https://github.com/RustCrypto/traits/pull/683 diff --git a/signature/src/signer.rs b/signature/src/signer.rs index 0031b73a7..8ec1099c3 100644 --- a/signature/src/signer.rs +++ b/signature/src/signer.rs @@ -24,6 +24,33 @@ 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 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") + } + + /// 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; +} + +// 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