From c335d68a3d03f145eb341ad01d878ef5b02089a8 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 2 Sep 2020 08:25:10 -0700 Subject: [PATCH] ecdsa: rename Curve::ElementSize => FieldSize See: https://github.com/RustCrypto/traits/pull/282 --- Cargo.lock | 2 +- ecdsa/src/asn1.rs | 38 ++++++++++++++++++------------------- ecdsa/src/dev/curve.rs | 2 +- ecdsa/src/hazmat.rs | 2 +- ecdsa/src/lib.rs | 24 +++++++++++------------ ecdsa/src/signer.rs | 4 ++-- ecdsa/src/signer/rfc6979.rs | 2 +- ecdsa/src/verifier.rs | 4 ++-- 8 files changed, 39 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 21011518..0b2a1c2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,7 +79,7 @@ dependencies = [ [[package]] name = "elliptic-curve" version = "0.5.0" -source = "git+https://github.com/RustCrypto/traits#abff234bfe0ced9254615dc608ece09619a8db38" +source = "git+https://github.com/RustCrypto/traits#a85525d61e1d882d12a4c00387655dded76fd0c2" dependencies = [ "digest", "generic-array", diff --git a/ecdsa/src/asn1.rs b/ecdsa/src/asn1.rs index 8bf653f4..dd8ae6bc 100644 --- a/ecdsa/src/asn1.rs +++ b/ecdsa/src/asn1.rs @@ -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 = - <<::ElementSize as Add>::Output as Add>::Output; + <<::FieldSize as Add>::Output as Add>::Output; /// Byte array containing a serialized ASN.1 signature type DocumentBytes = GenericArray>; @@ -52,9 +52,9 @@ const SEQUENCE_TAG: u8 = 0x30; pub struct Signature where C: Curve, - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { /// ASN.1 DER-encoded signature data bytes: DocumentBytes, @@ -69,9 +69,9 @@ where impl signature::Signature for Signature where C: Curve, - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { /// Parse an ASN.1 DER-encoded ECDSA signature from a byte slice fn from_bytes(bytes: &[u8]) -> Result { @@ -83,9 +83,9 @@ where impl Signature where C: Curve, - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { /// Get the length of the signature in bytes pub fn len(&self) -> usize { @@ -96,7 +96,7 @@ where pub(crate) fn from_scalars(r: &ElementBytes, s: &ElementBytes) -> 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::::default(); // SEQUENCE header @@ -139,9 +139,9 @@ where impl AsRef<[u8]> for Signature where C: Curve, - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { fn as_ref(&self) -> &[u8] { &self.bytes.as_slice()[..self.len()] @@ -151,9 +151,9 @@ where impl fmt::Debug for Signature where C: Curve, - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("asn1::Signature") @@ -166,9 +166,9 @@ where impl TryFrom<&[u8]> for Signature where C: Curve, - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { type Error = Error; @@ -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(); @@ -239,9 +239,9 @@ where impl signature::PrehashSignature for Signature where C: Curve + crate::hazmat::DigestPrimitive, - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { type Digest = C::Digest; } @@ -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 { diff --git a/ecdsa/src/dev/curve.rs b/ecdsa/src/dev/curve.rs index 1a80823c..dd4bd7ff 100644 --- a/ecdsa/src/dev/curve.rs +++ b/ecdsa/src/dev/curve.rs @@ -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 { diff --git a/ecdsa/src/hazmat.rs b/ecdsa/src/hazmat.rs index 278f8626..91846253 100644 --- a/ecdsa/src/hazmat.rs +++ b/ecdsa/src/hazmat.rs @@ -127,7 +127,7 @@ pub trait DigestPrimitive: Curve { #[cfg(feature = "digest")] impl PrehashSignature for Signature where - ::Output: ArrayLength, + ::Output: ArrayLength, { type Digest = C::Digest; } diff --git a/ecdsa/src/lib.rs b/ecdsa/src/lib.rs index ae8d70c2..09e247fd 100644 --- a/ecdsa/src/lib.rs +++ b/ecdsa/src/lib.rs @@ -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 = <::ElementSize as Add>::Output; +pub type SignatureSize = <::FieldSize as Add>::Output; /// Fixed-size byte array containing an ECDSA signature pub type SignatureBytes = GenericArray>; @@ -102,7 +102,7 @@ where /// Create a [`Signature`] from the serialized `r` and `s` components pub fn from_scalars(r: &ElementBytes, s: &ElementBytes) -> Self { let mut bytes = SignatureBytes::::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 } @@ -111,9 +111,9 @@ where /// Parse a signature from ASN.1 DER pub fn from_asn1(bytes: &[u8]) -> Result where - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, asn1::MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { asn1::Signature::::try_from(bytes).map(Into::into) } @@ -121,21 +121,21 @@ where /// Serialize this signature as ASN.1 DER pub fn to_asn1(&self) -> asn1::Signature where - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, asn1::MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { asn1::Signature::from_scalars(self.r(), self.s()) } /// Get the `r` component of this signature pub fn r(&self) -> &ElementBytes { - ElementBytes::::from_slice(&self.bytes[..C::ElementSize::to_usize()]) + ElementBytes::::from_slice(&self.bytes[..C::FieldSize::to_usize()]) } /// Get the `s` component of this signature pub fn s(&self) -> &ElementBytes { - ElementBytes::::from_slice(&self.bytes[C::ElementSize::to_usize()..]) + ElementBytes::::from_slice(&self.bytes[C::FieldSize::to_usize()..]) } } @@ -150,7 +150,7 @@ where /// /// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki pub fn normalize_s(&mut self) -> Result { - 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 @@ -228,13 +228,13 @@ where impl From> for Signature where C: Curve, - C::ElementSize: Add + ArrayLength, + C::FieldSize: Add + ArrayLength, asn1::MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + ::Output: Add + ArrayLength, { fn from(doc: asn1::Signature) -> Signature { let mut bytes = SignatureBytes::::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(); diff --git a/ecdsa/src/signer.rs b/ecdsa/src/signer.rs index 059058d5..dc786c7f 100644 --- a/ecdsa/src/signer.rs +++ b/ecdsa/src/signer.rs @@ -73,7 +73,7 @@ impl DigestSigner> for Signer where C: Curve + Arithmetic, C::Scalar: FromDigest + Invert + SignPrimitive + Zeroize, - D: FixedOutput + BlockInput + Clone + Default + Reset + Update, + D: FixedOutput + BlockInput + Clone + Default + Reset + Update, SignatureSize: ArrayLength, { /// Sign message prehash using a deterministic ephemeral scalar (`k`) @@ -106,7 +106,7 @@ impl RandomizedDigestSigner> for Signer where C: Curve + Arithmetic, C::Scalar: FromDigest + Invert + SignPrimitive + Zeroize, - D: FixedOutput + BlockInput + Clone + Default + Reset + Update, + D: FixedOutput + BlockInput + Clone + Default + Reset + Update, SignatureSize: ArrayLength, { /// Sign message prehash using an ephemeral scalar (`k`) derived according diff --git a/ecdsa/src/signer/rfc6979.rs b/ecdsa/src/signer/rfc6979.rs index 68a77823..e39ad1a2 100644 --- a/ecdsa/src/signer/rfc6979.rs +++ b/ecdsa/src/signer/rfc6979.rs @@ -22,7 +22,7 @@ pub fn generate_k( where C: Arithmetic, C::Scalar: FromDigest + Invert + Zeroize, - D: FixedOutput + BlockInput + Clone + Default + Reset + Update, + D: FixedOutput + BlockInput + Clone + Default + Reset + Update, { let mut x = secret_scalar.to_bytes(); let h1: ElementBytes = C::Scalar::from_digest(msg_digest).into(); diff --git a/ecdsa/src/verifier.rs b/ecdsa/src/verifier.rs index d8f5b9ea..17a77e55 100644 --- a/ecdsa/src/verifier.rs +++ b/ecdsa/src/verifier.rs @@ -48,7 +48,7 @@ where impl DigestVerifier> for Verifier where C: Curve + Arithmetic, - D: Digest, + D: Digest, C::AffinePoint: VerifyPrimitive, C::Scalar: FromDigest, SignatureSize: ArrayLength, @@ -63,7 +63,7 @@ impl signature::Verifier> for Verifier where C: Curve + Arithmetic + DigestPrimitive, C::AffinePoint: VerifyPrimitive, - C::Digest: Digest, + C::Digest: Digest, C::Scalar: FromDigest, SignatureSize: ArrayLength, {