-
Notifications
You must be signed in to change notification settings - Fork 198
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
signature v2.0.0-pre #1141
Merged
Merged
signature v2.0.0-pre #1141
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Encoding support. | ||
use crate::{Error, Result}; | ||
|
||
#[cfg(feature = "alloc")] | ||
use alloc::{boxed::Box, vec::Vec}; | ||
|
||
/// Support for decoding/encoding signatures as bytes. | ||
pub trait SignatureEncoding: | ||
Clone + Sized + for<'a> TryFrom<&'a [u8], Error = Error> + Into<Self::Repr> | ||
{ | ||
/// Byte representation of a signature. | ||
type Repr: 'static + AsRef<[u8]> + AsMut<[u8]> + Clone + Default + Send + Sync; | ||
|
||
/// Decode signature from its byte representation. | ||
fn from_bytes(bytes: &Self::Repr) -> Result<Self> { | ||
Self::try_from(bytes.as_ref()) | ||
} | ||
|
||
/// Encode signature as its byte representation. | ||
fn to_bytes(&self) -> Self::Repr { | ||
self.clone().into() | ||
} | ||
|
||
/// Encode signature as a byte vector. | ||
#[cfg(feature = "alloc")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] | ||
fn to_vec(&self) -> Vec<u8> { | ||
self.to_bytes().as_ref().to_vec() | ||
} | ||
|
||
/// Encode the signature as a boxed byte slice. | ||
#[cfg(feature = "alloc")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] | ||
fn to_boxed_slice(&self) -> Box<[u8]> { | ||
tarcieri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.to_vec().into_boxed_slice() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the main argument here that there's no inherent need for someone with access to an
S: Signer
to be able to (de)serialize the resulting signatures? I'd buy that argument. Are there examples in the wild?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is largely a reaction (perhaps an overreaction) to mandatory trait bounds around the
Signature
trait in the v1.0signature
crate being too restrictive and preventing some things that would otherwise be valid. Although debatably theAsRef<[u8]>
one was too onerous.As far as a real-world example of something eliminating the bounds enables, the main thing is probably a heapless core implementation with an
alloc
-dependentSignatureEncoding
impl:https://github.com/RustCrypto/signatures/pull/562/files#diff-23f3fe37b15706957a041307578d2433204986a9eb42dc8d7c4126dcaf9266f1R226-R239
In this case, it's a variable-width ASN.1 DER-encoded type, which knows how to serialize itself into a buffer on heapless platforms, but when the heap is available, it's nice to be able to serialize it into a heap-backed structure (in this case
Box<[u8]>
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. In the above case, couldn't
SignatureEncoding
be implemented without the feature gate? The def simply won't contain theto_vec
part ifalloc
isn't set. Or is the trait simply not useful in that case (for this particular crate)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
SignatureEncoding::Repr
in that example is analloc::boxed::Box<[u8]>
, so no, it cannot be defined without thealloc
feature enabled.