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
6 changes: 1 addition & 5 deletions ecdsa/src/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
//! FULL PRIVATE KEY RECOVERY!
//! </div>

use crate::{EcdsaCurve, Error, RecoveryId, Result, Signature, SignatureSize};
use crate::{EcdsaCurve, Error, RecoveryId, Result, Signature};
use elliptic_curve::{
CurveArithmetic, FieldBytes, NonZeroScalar, ProjectivePoint, Scalar,
array::ArraySize,
bigint::{BitOps, Encoding},
ff::PrimeField,
group::{Curve as _, Group},
Expand Down Expand Up @@ -58,7 +57,6 @@ pub fn sign_prehashed<C>(
) -> Result<(Signature<C>, RecoveryId)>
where
C: EcdsaCurve + CurveArithmetic,
SignatureSize<C>: ArraySize,
{
let z = bytes2scalar::<C>(z);

Expand Down Expand Up @@ -109,7 +107,6 @@ pub fn sign_prehashed_rfc6979<C, D>(
where
C: EcdsaCurve + CurveArithmetic,
D: Digest + BlockSizeUser,
SignatureSize<C>: ArraySize,
{
let order = C::ORDER;
let mut kgen = rfc6979::KGenerator::<D, C::Uint>::new(&d.to_repr(), z, ad, &order);
Expand Down Expand Up @@ -137,7 +134,6 @@ where
pub fn verify_prehashed<C>(q: &ProjectivePoint<C>, z: &[u8], sig: &Signature<C>) -> Result<()>
where
C: EcdsaCurve + CurveArithmetic,
SignatureSize<C>: ArraySize,
{
if C::NORMALIZE_S && sig.s().is_high().into() {
return Err(Error::new());
Expand Down
57 changes: 10 additions & 47 deletions ecdsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub use crate::verifying::VerifyingKey;

use core::{fmt, ops::Add};
use elliptic_curve::{
FieldBytes, FieldBytesSize, ScalarValue,
Curve, FieldBytes, FieldBytesSize, ScalarValue,
array::{Array, ArraySize, typenum::Unsigned},
};

Expand Down Expand Up @@ -164,7 +164,9 @@ const SHA384_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.16.840.1.10
const SHA512_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.3");

/// Marker trait for elliptic curves intended for use with ECDSA.
pub trait EcdsaCurve: PrimeCurve {
pub trait EcdsaCurve:
Curve<FieldBytesSize: Add<Output: ArraySize<ArrayType<u8>: Copy>>> + PrimeCurve
{
/// Does this curve use low-S normalized signatures?
///
/// This is typically `false`. See [`Signature::normalize_s`] for more information.
Expand Down Expand Up @@ -203,7 +205,7 @@ pub type SignatureBytes<C> = Array<u8, SignatureSize<C>>;
/// "human readable" text formats, and a binary encoding otherwise.
///
/// [IEEE P1363]: https://en.wikipedia.org/wiki/IEEE_P1363
#[derive(Clone, Eq, PartialEq)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Signature<C: EcdsaCurve> {
r: ScalarValue<C>,
s: ScalarValue<C>,
Expand All @@ -212,7 +214,6 @@ pub struct Signature<C: EcdsaCurve> {
impl<C> Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
/// Parse a signature from fixed-width bytes, i.e. 2 * the size of
/// [`FieldBytes`] for a particular curve.
Expand Down Expand Up @@ -301,7 +302,6 @@ where
impl<C> Signature<C>
where
C: EcdsaCurve + CurveArithmetic,
SignatureSize<C>: ArraySize,
{
/// Get the `r` component of this signature
pub fn r(&self) -> NonZeroScalar<C> {
Expand All @@ -318,30 +318,20 @@ where
(self.r(), self.s())
}

/// Normalize signature into "low S" form as described in
/// [BIP 0062: Dealing with Malleability][1].
/// Normalize signature into "low S" form described in [BIP 0062: Dealing with Malleability][1].
///
/// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
pub fn normalize_s(&self) -> Self {
let mut result = self.clone();
let mut result = *self;
let s_inv = ScalarValue::from(-self.s());
result.s.conditional_assign(&s_inv, self.s.is_high());
result
}
}

impl<C> Copy for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
<SignatureSize<C> as ArraySize>::ArrayType<u8>: Copy,
{
}

impl<C> From<Signature<C>> for SignatureBytes<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn from(signature: Signature<C>) -> SignatureBytes<C> {
signature.to_bytes()
Expand All @@ -351,15 +341,13 @@ where
impl<C> SignatureEncoding for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
type Repr = SignatureBytes<C>;
}

impl<C> TryFrom<&[u8]> for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
type Error = Error;

Expand All @@ -371,7 +359,6 @@ where
impl<C> fmt::Debug for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ecdsa::Signature<{:?}>(", C::default())?;
Expand All @@ -387,7 +374,6 @@ where
impl<C> fmt::Display for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:X}")
Expand All @@ -397,7 +383,6 @@ where
impl<C> core::hash::Hash for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.to_bytes().hash(state);
Expand All @@ -407,7 +392,6 @@ where
impl<C> fmt::LowerHex for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.to_bytes() {
Expand All @@ -420,7 +404,6 @@ where
impl<C> fmt::UpperHex for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.to_bytes() {
Expand All @@ -434,7 +417,6 @@ where
impl<C> str::FromStr for Signature<C>
where
C: EcdsaCurve + CurveArithmetic,
SignatureSize<C>: ArraySize,
{
type Err = Error;

Expand Down Expand Up @@ -494,7 +476,6 @@ where
impl<C> Serialize for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
Expand All @@ -508,7 +489,6 @@ where
impl<'de, C> Deserialize<'de> for Signature<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
Expand Down Expand Up @@ -540,7 +520,7 @@ impl<C: EcdsaCurve> Zeroize for Signature<C> {
///
/// [RFC5758 § 3.2]: https://www.rfc-editor.org/rfc/rfc5758#section-3.2
#[cfg(feature = "digest")]
#[derive(Clone, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SignatureWithOid<C: EcdsaCurve> {
/// Inner signature type.
signature: Signature<C>,
Expand Down Expand Up @@ -596,7 +576,6 @@ where
pub fn from_bytes_with_digest<D>(bytes: &SignatureBytes<C>) -> Result<Self>
where
D: AssociatedOid + Digest,
SignatureSize<C>: ArraySize,
{
Self::new_with_digest::<D>(Signature::<C>::from_bytes(bytes)?)
}
Expand All @@ -605,7 +584,6 @@ where
pub fn from_slice_with_digest<D>(slice: &[u8]) -> Result<Self>
where
D: AssociatedOid + Digest,
SignatureSize<C>: ArraySize,
{
Self::new_with_digest::<D>(Signature::<C>::from_slice(slice)?)
}
Expand Down Expand Up @@ -643,9 +621,7 @@ where

/// Serialize this signature as fixed-width bytes.
pub fn to_bytes(&self) -> SignatureBytes<C>
where
SignatureSize<C>: ArraySize,
{
where {
self.signature.to_bytes()
}

Expand All @@ -660,7 +636,7 @@ where
der::MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<der::MaxOverhead> + ArraySize,
{
self.signature.clone().into()
self.signature.into()
}
}

Expand All @@ -675,20 +651,10 @@ pub trait DigestAlgorithm: EcdsaCurve {
type Digest: BlockSizeUser + Digest + FixedOutput;
}

#[cfg(feature = "digest")]
impl<C> Copy for SignatureWithOid<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
<SignatureSize<C> as ArraySize>::ArrayType<u8>: Copy,
{
}

#[cfg(feature = "digest")]
impl<C> core::hash::Hash for SignatureWithOid<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.signature.hash(state);
Expand All @@ -710,7 +676,6 @@ where
impl<C> From<SignatureWithOid<C>> for SignatureBytes<C>
where
C: EcdsaCurve,
SignatureSize<C>: ArraySize,
{
fn from(signature: SignatureWithOid<C>) -> SignatureBytes<C> {
signature.to_bytes()
Expand Down Expand Up @@ -751,7 +716,6 @@ impl<C> SignatureEncoding for SignatureWithOid<C>
where
C: DigestAlgorithm,
C::Digest: AssociatedOid,
SignatureSize<C>: ArraySize,
{
type Repr = SignatureBytes<C>;
}
Expand All @@ -766,7 +730,6 @@ impl<C> TryFrom<&[u8]> for SignatureWithOid<C>
where
C: DigestAlgorithm,
C::Digest: AssociatedOid,
SignatureSize<C>: ArraySize,
{
type Error = Error;

Expand Down
14 changes: 1 addition & 13 deletions ecdsa/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use crate::{Error, Result};
#[cfg(feature = "algorithm")]
use {
crate::{
DigestAlgorithm, EcdsaCurve, Signature, SignatureSize, SigningKey, VerifyingKey,
DigestAlgorithm, EcdsaCurve, Signature, SigningKey, VerifyingKey,
hazmat::{bytes2scalar, sign_prehashed_rfc6979, verify_prehashed},
},
digest::{Digest, Update},
elliptic_curve::{
AffinePoint, CurveArithmetic, FieldBytes, FieldBytesSize, Group, PrimeField,
ProjectivePoint, Scalar,
array::ArraySize,
bigint::CheckedAdd,
field,
ops::Invert,
Expand Down Expand Up @@ -95,7 +94,6 @@ impl RecoveryId {
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
AffinePoint<C>: DecompressPoint<C> + FromSec1Point<C> + ToSec1Point<C>,
FieldBytesSize<C>: sec1::ModulusSize,
SignatureSize<C>: ArraySize,
{
Self::trial_recovery_from_digest(verifying_key, C::Digest::new_with_prefix(msg), signature)
}
Expand All @@ -113,7 +111,6 @@ impl RecoveryId {
D: Digest,
AffinePoint<C>: DecompressPoint<C> + FromSec1Point<C> + ToSec1Point<C>,
FieldBytesSize<C>: sec1::ModulusSize,
SignatureSize<C>: ArraySize,
{
Self::trial_recovery_from_prehash(verifying_key, &digest.finalize(), signature)
}
Expand All @@ -130,7 +127,6 @@ impl RecoveryId {
C: EcdsaCurve + CurveArithmetic,
AffinePoint<C>: DecompressPoint<C> + FromSec1Point<C> + ToSec1Point<C>,
FieldBytesSize<C>: sec1::ModulusSize,
SignatureSize<C>: ArraySize,
{
// Ensure signature verifies with the provided key
verify_prehashed::<C>(
Expand Down Expand Up @@ -172,7 +168,6 @@ impl<C> SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
/// Sign the given message prehash, using the given rng for the RFC6979 Section 3.6 "additional
/// data", returning a signature and recovery ID.
Expand Down Expand Up @@ -220,7 +215,6 @@ where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: Digest + Update,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
fn try_sign_digest<F: Fn(&mut D) -> Result<()>>(
&self,
Expand All @@ -237,7 +231,6 @@ impl<C> RandomizedPrehashSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
fn sign_prehash_with_rng<R: TryCryptoRng + ?Sized>(
&self,
Expand All @@ -254,7 +247,6 @@ where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: Digest + Update,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
fn try_sign_digest_with_rng<R: TryCryptoRng + ?Sized, F: Fn(&mut D) -> Result<()>>(
&self,
Expand All @@ -272,7 +264,6 @@ impl<C> PrehashSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
fn sign_prehash(&self, prehash: &[u8]) -> Result<(Signature<C>, RecoveryId)> {
self.sign_prehash_recoverable(prehash)
Expand All @@ -284,7 +275,6 @@ impl<C> Signer<(Signature<C>, RecoveryId)> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
fn try_sign(&self, msg: &[u8]) -> Result<(Signature<C>, RecoveryId)> {
self.try_multipart_sign(&[msg])
Expand All @@ -296,7 +286,6 @@ impl<C> MultipartSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
fn try_multipart_sign(&self, msg: &[&[u8]]) -> Result<(Signature<C>, RecoveryId)> {
let mut digest = C::Digest::new();
Expand All @@ -312,7 +301,6 @@ where
C: EcdsaCurve + CurveArithmetic,
AffinePoint<C>: DecompressPoint<C> + FromSec1Point<C> + ToSec1Point<C>,
FieldBytesSize<C>: sec1::ModulusSize,
SignatureSize<C>: ArraySize,
{
/// Recover a [`VerifyingKey`] from the given message, signature, and [`RecoveryId`].
///
Expand Down
Loading