Skip to content

Commit

Permalink
ecdsa: add SigningKey::as_nonzero_scalar/VerifyingKey::as_affine
Browse files Browse the repository at this point in the history
Adds borrowing accessors for the inner `NonZeroScalar` and `AffinePoint`
within `SigningKey` and `VerifyingKey` respectively.

These mirror the corresponding inherent methods on `SecretKey` and
`PublicKey` as defined in the `elliptic-curve` crate.
  • Loading branch information
tarcieri committed Sep 7, 2022
1 parent 91bb9c3 commit a075d7e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
21 changes: 16 additions & 5 deletions ecdsa/src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ where
Ok(Self { inner })
}

/// Serialize this [`SigningKey`] as bytes
pub fn to_bytes(&self) -> FieldBytes<C> {
self.inner.to_repr()
}

/// Borrow the secret [`NonZeroScalar`] value for this key.
///
/// # ⚠️ Warning
///
/// This value is key material.
///
/// Please treat it with the care it deserves!
pub fn as_nonzero_scalar(&self) -> &NonZeroScalar<C> {
&self.inner
}

/// Get the [`VerifyingKey`] which corresponds to this [`SigningKey`]
#[cfg(feature = "verify")]
#[cfg_attr(docsrs, doc(cfg(feature = "verify")))]
Expand All @@ -82,11 +98,6 @@ where
inner: PublicKey::from_secret_scalar(&self.inner),
}
}

/// Serialize this [`SigningKey`] as bytes
pub fn to_bytes(&self) -> FieldBytes<C> {
self.inner.to_repr()
}
}

impl<C> ConstantTimeEq for SigningKey<C>
Expand Down
16 changes: 16 additions & 0 deletions ecdsa/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ where
pub fn to_encoded_point(&self, compress: bool) -> EncodedPoint<C> {
self.inner.to_encoded_point(compress)
}

/// Borrow the inner [`AffinePoint`] for this public key.
pub fn as_affine(&self) -> &AffinePoint<C> {
self.inner.as_affine()
}
}

impl<C> AsRef<AffinePoint<C>> for VerifyingKey<C>
where
C: PrimeCurve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: sec1::ModulusSize,
{
fn as_ref(&self) -> &AffinePoint<C> {
self.as_affine()
}
}

impl<C> Copy for VerifyingKey<C> where C: PrimeCurve + ProjectiveArithmetic {}
Expand Down

0 comments on commit a075d7e

Please sign in to comment.