Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ecdsa: remove NormalizeLow trait #393

Merged
merged 1 commit into from
Nov 17, 2021
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
8 changes: 4 additions & 4 deletions Cargo.lock

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

37 changes: 12 additions & 25 deletions ecdsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ use elliptic_curve::{
};

#[cfg(feature = "arithmetic")]
use elliptic_curve::{group::ff::PrimeField, NonZeroScalar, ProjectiveArithmetic, Scalar};
use elliptic_curve::{ff::PrimeField, IsHigh, NonZeroScalar, ScalarArithmetic};

/// Size of a fixed sized signature for the given elliptic curve.
pub type SignatureSize<C> = <FieldSize<C> as Add>::Output;
Expand Down Expand Up @@ -184,7 +184,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
impl<C> Signature<C>
where
C: PrimeCurve + ProjectiveArithmetic,
C: PrimeCurve + ScalarArithmetic,
SignatureSize<C>: ArrayLength<u8>,
{
/// Get the `r` component of this signature
Expand All @@ -208,15 +208,17 @@ where
/// [BIP 0062: Dealing with Malleability][1].
///
/// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
pub fn normalize_s(&self) -> Option<Self>
where
Scalar<C>: NormalizeLow,
{
self.s().normalize_low().map(|s_low| {
pub fn normalize_s(&self) -> Option<Self> {
let s = self.s();

if s.is_high().into() {
let neg_s = -s;
let mut result = self.clone();
result.bytes[C::UInt::BYTE_SIZE..].copy_from_slice(&s_low.to_repr());
result
})
result.bytes[C::UInt::BYTE_SIZE..].copy_from_slice(&neg_s.to_repr());
Some(result)
} else {
None
}
}
}

Expand Down Expand Up @@ -288,18 +290,3 @@ where
})
}
}

/// Normalize a scalar (i.e. ECDSA S) to the lower half the field, as described
/// in [BIP 0062: Dealing with Malleability][1].
///
/// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
pub trait NormalizeLow: Sized {
/// Normalize scalar to the lower half of the field (i.e. negate it if it's
/// larger than half the curve's order).
///
/// Returns an `Option` with a new scalar if the original wasn't already
/// low-normalized.
///
/// May be implemented to work in variable time.
fn normalize_low(&self) -> Option<Self>;
}