Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions ecdsa/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub type MaxOverhead = U9;

/// Maximum size of an ASN.1 DER encoded signature for the given elliptic curve.
pub type MaxSize<C> =
<<<C as elliptic_curve::Curve>::ElementSize as Add>::Output as Add<MaxOverhead>>::Output;
<<<C as elliptic_curve::Curve>::FieldSize as Add>::Output as Add<MaxOverhead>>::Output;

/// Byte array containing a serialized ASN.1 signature
type DocumentBytes<C> = GenericArray<u8, MaxSize<C>>;
Expand All @@ -52,9 +52,9 @@ const SEQUENCE_TAG: u8 = 0x30;
pub struct Signature<C>
where
C: Curve,
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
{
/// ASN.1 DER-encoded signature data
bytes: DocumentBytes<C>,
Expand All @@ -69,9 +69,9 @@ where
impl<C> signature::Signature for Signature<C>
where
C: Curve,
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
{
/// Parse an ASN.1 DER-encoded ECDSA signature from a byte slice
fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
Expand All @@ -83,9 +83,9 @@ where
impl<C> Signature<C>
where
C: Curve,
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
{
/// Get the length of the signature in bytes
pub fn len(&self) -> usize {
Expand All @@ -96,7 +96,7 @@ where
pub(crate) fn from_scalars(r: &ElementBytes<C>, s: &ElementBytes<C>) -> Self {
let r_len = int_length(r);
let s_len = int_length(s);
let scalar_size = C::ElementSize::to_usize();
let scalar_size = C::FieldSize::to_usize();
let mut bytes = DocumentBytes::<C>::default();

// SEQUENCE header
Expand Down Expand Up @@ -139,9 +139,9 @@ where
impl<C> AsRef<[u8]> for Signature<C>
where
C: Curve,
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
{
fn as_ref(&self) -> &[u8] {
&self.bytes.as_slice()[..self.len()]
Expand All @@ -151,9 +151,9 @@ where
impl<C> fmt::Debug for Signature<C>
where
C: Curve,
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("asn1::Signature")
Expand All @@ -166,9 +166,9 @@ where
impl<C> TryFrom<&[u8]> for Signature<C>
where
C: Curve,
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
{
type Error = Error;

Expand Down Expand Up @@ -205,12 +205,12 @@ where
}

// First INTEGER (r)
let r_range = parse_int(&bytes[offset..], C::ElementSize::to_usize())?;
let r_range = parse_int(&bytes[offset..], C::FieldSize::to_usize())?;
let r_start = offset.checked_add(r_range.start).unwrap();
let r_end = offset.checked_add(r_range.end).unwrap();

// Second INTEGER (s)
let s_range = parse_int(&bytes[r_end..], C::ElementSize::to_usize())?;
let s_range = parse_int(&bytes[r_end..], C::FieldSize::to_usize())?;
let s_start = r_end.checked_add(s_range.start).unwrap();
let s_end = r_end.checked_add(s_range.end).unwrap();

Expand Down Expand Up @@ -239,9 +239,9 @@ where
impl<C> signature::PrehashSignature for Signature<C>
where
C: Curve + crate::hazmat::DigestPrimitive,
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
{
type Digest = C::Digest;
}
Expand Down Expand Up @@ -336,7 +336,7 @@ mod tests {
pub struct ExampleCurve;

impl elliptic_curve::Curve for ExampleCurve {
type ElementSize = U32;
type FieldSize = U32;
}

impl elliptic_curve::weierstrass::Curve for ExampleCurve {
Expand Down
2 changes: 1 addition & 1 deletion ecdsa/src/dev/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use elliptic_curve::{
pub struct ExampleCurve;

impl elliptic_curve::Curve for ExampleCurve {
type ElementSize = U32;
type FieldSize = U32;
}

impl elliptic_curve::weierstrass::Curve for ExampleCurve {
Expand Down
2 changes: 1 addition & 1 deletion ecdsa/src/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub trait DigestPrimitive: Curve {
#[cfg(feature = "digest")]
impl<C: DigestPrimitive> PrehashSignature for Signature<C>
where
<C::ElementSize as core::ops::Add>::Output: ArrayLength<u8>,
<C::FieldSize as core::ops::Add>::Output: ArrayLength<u8>,
{
type Digest = C::Digest;
}
24 changes: 12 additions & 12 deletions ecdsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use elliptic_curve::{Arithmetic, ElementBytes, FromBytes};
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};

/// Size of a fixed sized signature for the given elliptic curve.
pub type SignatureSize<C> = <<C as elliptic_curve::Curve>::ElementSize as Add>::Output;
pub type SignatureSize<C> = <<C as elliptic_curve::Curve>::FieldSize as Add>::Output;

/// Fixed-size byte array containing an ECDSA signature
pub type SignatureBytes<C> = GenericArray<u8, SignatureSize<C>>;
Expand Down Expand Up @@ -102,7 +102,7 @@ where
/// Create a [`Signature`] from the serialized `r` and `s` components
pub fn from_scalars(r: &ElementBytes<C>, s: &ElementBytes<C>) -> Self {
let mut bytes = SignatureBytes::<C>::default();
let scalar_size = C::ElementSize::to_usize();
let scalar_size = C::FieldSize::to_usize();
bytes[..scalar_size].copy_from_slice(r.as_slice());
bytes[scalar_size..].copy_from_slice(s.as_slice());
Signature { bytes }
Expand All @@ -111,31 +111,31 @@ where
/// Parse a signature from ASN.1 DER
pub fn from_asn1(bytes: &[u8]) -> Result<Self, Error>
where
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
asn1::MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
{
asn1::Signature::<C>::try_from(bytes).map(Into::into)
}

/// Serialize this signature as ASN.1 DER
pub fn to_asn1(&self) -> asn1::Signature<C>
where
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
asn1::MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
{
asn1::Signature::from_scalars(self.r(), self.s())
}

/// Get the `r` component of this signature
pub fn r(&self) -> &ElementBytes<C> {
ElementBytes::<C>::from_slice(&self.bytes[..C::ElementSize::to_usize()])
ElementBytes::<C>::from_slice(&self.bytes[..C::FieldSize::to_usize()])
}

/// Get the `s` component of this signature
pub fn s(&self) -> &ElementBytes<C> {
ElementBytes::<C>::from_slice(&self.bytes[C::ElementSize::to_usize()..])
ElementBytes::<C>::from_slice(&self.bytes[C::FieldSize::to_usize()..])
}
}

Expand All @@ -150,7 +150,7 @@ where
///
/// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
pub fn normalize_s(&mut self) -> Result<bool, Error> {
let s_bytes = GenericArray::from_mut_slice(&mut self.bytes[C::ElementSize::to_usize()..]);
let s_bytes = GenericArray::from_mut_slice(&mut self.bytes[C::FieldSize::to_usize()..]);
let s_option = C::Scalar::from_bytes(s_bytes);

// Not constant time, but we're operating on public values
Expand Down Expand Up @@ -228,13 +228,13 @@ where
impl<C> From<asn1::Signature<C>> for Signature<C>
where
C: Curve,
C::ElementSize: Add + ArrayLength<u8>,
C::FieldSize: Add + ArrayLength<u8>,
asn1::MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
{
fn from(doc: asn1::Signature<C>) -> Signature<C> {
let mut bytes = SignatureBytes::<C>::default();
let scalar_size = C::ElementSize::to_usize();
let scalar_size = C::FieldSize::to_usize();
let r_begin = scalar_size.checked_sub(doc.r().len()).unwrap();
let s_begin = bytes.len().checked_sub(doc.s().len()).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions ecdsa/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<C, D> DigestSigner<D, Signature<C>> for Signer<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
D: FixedOutput<OutputSize = C::ElementSize> + BlockInput + Clone + Default + Reset + Update,
D: FixedOutput<OutputSize = C::FieldSize> + BlockInput + Clone + Default + Reset + Update,
SignatureSize<C>: ArrayLength<u8>,
{
/// Sign message prehash using a deterministic ephemeral scalar (`k`)
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<C, D> RandomizedDigestSigner<D, Signature<C>> for Signer<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
D: FixedOutput<OutputSize = C::ElementSize> + BlockInput + Clone + Default + Reset + Update,
D: FixedOutput<OutputSize = C::FieldSize> + BlockInput + Clone + Default + Reset + Update,
SignatureSize<C>: ArrayLength<u8>,
{
/// Sign message prehash using an ephemeral scalar (`k`) derived according
Expand Down
2 changes: 1 addition & 1 deletion ecdsa/src/signer/rfc6979.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn generate_k<C, D>(
where
C: Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + Zeroize,
D: FixedOutput<OutputSize = C::ElementSize> + BlockInput + Clone + Default + Reset + Update,
D: FixedOutput<OutputSize = C::FieldSize> + BlockInput + Clone + Default + Reset + Update,
{
let mut x = secret_scalar.to_bytes();
let h1: ElementBytes<C> = C::Scalar::from_digest(msg_digest).into();
Expand Down
4 changes: 2 additions & 2 deletions ecdsa/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ where
impl<C, D> DigestVerifier<D, Signature<C>> for Verifier<C>
where
C: Curve + Arithmetic,
D: Digest<OutputSize = C::ElementSize>,
D: Digest<OutputSize = C::FieldSize>,
C::AffinePoint: VerifyPrimitive<C>,
C::Scalar: FromDigest<C>,
SignatureSize<C>: ArrayLength<u8>,
Expand All @@ -63,7 +63,7 @@ impl<C> signature::Verifier<Signature<C>> for Verifier<C>
where
C: Curve + Arithmetic + DigestPrimitive,
C::AffinePoint: VerifyPrimitive<C>,
C::Digest: Digest<OutputSize = C::ElementSize>,
C::Digest: Digest<OutputSize = C::FieldSize>,
C::Scalar: FromDigest<C>,
SignatureSize<C>: ArrayLength<u8>,
{
Expand Down