ecdsa: rename Curve::ElementSize => FieldSize (#137)

See: https://github.com/RustCrypto/traits/pull/282
This commit is contained in:
Tony Arcieri
2020-09-02 08:27:58 -07:00
committed by GitHub
parent 7974228f83
commit 416e6cc793
8 changed files with 39 additions and 39 deletions
Generated
+1 -1
View File
@@ -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",
+19 -19
View File
@@ -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>>;
@@ -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>,
@@ -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> {
@@ -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 {
@@ -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
@@ -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()]
@@ -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")
@@ -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;
@@ -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<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;
}
@@ -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 {
+1 -1
View File
@@ -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 {
+1 -1
View File
@@ -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;
}
+12 -12
View File
@@ -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>>;
@@ -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 }
@@ -111,9 +111,9 @@ 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)
}
@@ -121,21 +121,21 @@ where
/// 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()..])
}
}
@@ -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
@@ -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();
+2 -2
View File
@@ -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`)
@@ -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
+1 -1
View File
@@ -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();
+2 -2
View File
@@ -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>,
@@ -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>,
{