-
Notifications
You must be signed in to change notification settings - Fork 106
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: refactor Signature
constructors and improve docs
#730
Merged
tarcieri
merged 1 commit into
master
from
ecdsa/refactor-constructors-and-improve-documentation
Jul 10, 2023
Merged
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
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 |
---|---|---|
|
@@ -85,7 +85,7 @@ pub use crate::verifying::VerifyingKey; | |
|
||
use core::{fmt, ops::Add}; | ||
use elliptic_curve::{ | ||
generic_array::{sequence::Concat, typenum::Unsigned, ArrayLength, GenericArray}, | ||
generic_array::{typenum::Unsigned, ArrayLength, GenericArray}, | ||
FieldBytes, FieldBytesSize, ScalarPrimitive, | ||
}; | ||
|
||
|
@@ -176,9 +176,11 @@ pub type SignatureBytes<C> = GenericArray<u8, SignatureSize<C>>; | |
/// - `r`: field element size for the given curve, big-endian | ||
/// - `s`: field element size for the given curve, big-endian | ||
/// | ||
/// Both `r` and `s` MUST be non-zero. | ||
/// | ||
/// For example, in a curve with a 256-bit modulus like NIST P-256 or | ||
/// secp256k1, `r` and `s` will both be 32-bytes, resulting in a signature | ||
/// with a total of 64-bytes. | ||
/// secp256k1, `r` and `s` will both be 32-bytes and serialized as big endian, | ||
/// resulting in a signature with a total of 64-bytes. | ||
/// | ||
/// ASN.1 DER-encoded signatures also supported via the | ||
/// [`Signature::from_der`] and [`Signature::to_der`] methods. | ||
|
@@ -202,17 +204,19 @@ where | |
C: PrimeCurve, | ||
SignatureSize<C>: ArrayLength<u8>, | ||
{ | ||
/// Parse a signature from fixed-with bytes. | ||
/// Parse a signature from fixed-width bytes, i.e. 2 * the size of | ||
/// [`FieldBytes`] for a particular curve. | ||
/// | ||
/// # Returns | ||
/// - `Ok(signature)` if the `r` and `s` components are both in the valid | ||
/// range `1..n` when serialized as concatenated big endian integers. | ||
/// - `Err(err)` if the `r` and/or `s` component of the signature is | ||
/// out-of-range when interpreted as a big endian integer. | ||
pub fn from_bytes(bytes: &SignatureBytes<C>) -> Result<Self> { | ||
let (r_bytes, s_bytes) = bytes.split_at(C::FieldBytesSize::USIZE); | ||
let r = ScalarPrimitive::from_slice(r_bytes).map_err(|_| Error::new())?; | ||
let s = ScalarPrimitive::from_slice(s_bytes).map_err(|_| Error::new())?; | ||
|
||
if r.is_zero().into() || s.is_zero().into() { | ||
return Err(Error::new()); | ||
} | ||
|
||
Ok(Self { r, s }) | ||
let r = FieldBytes::<C>::clone_from_slice(r_bytes); | ||
let s = FieldBytes::<C>::clone_from_slice(s_bytes); | ||
Self::from_scalars(r, s) | ||
} | ||
|
||
/// Parse a signature from a byte slice. | ||
|
@@ -236,8 +240,21 @@ where | |
|
||
/// Create a [`Signature`] from the serialized `r` and `s` scalar values | ||
/// which comprise the signature. | ||
/// | ||
/// # Returns | ||
/// - `Ok(signature)` if the `r` and `s` components are both in the valid | ||
/// range `1..n` when serialized as concatenated big endian integers. | ||
/// - `Err(err)` if the `r` and/or `s` component of the signature is | ||
/// out-of-range when interpreted as a big endian integer. | ||
pub fn from_scalars(r: impl Into<FieldBytes<C>>, s: impl Into<FieldBytes<C>>) -> Result<Self> { | ||
Self::try_from(r.into().concat(s.into()).as_slice()) | ||
let r = ScalarPrimitive::from_slice(&r.into()).map_err(|_| Error::new())?; | ||
let s = ScalarPrimitive::from_slice(&s.into()).map_err(|_| Error::new())?; | ||
Comment on lines
+250
to
+251
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the next breaking release it might make sense to change the |
||
|
||
if r.is_zero().into() || s.is_zero().into() { | ||
return Err(Error::new()); | ||
} | ||
|
||
Ok(Self { r, s }) | ||
} | ||
|
||
/// Split the signature into its `r` and `s` components, represented as bytes. | ||
|
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.
@fjarri perhaps you could add a comment like this in RustCrypto/elliptic-curves#907 ?