Skip to content

Commit

Permalink
ecdsa: make Signature::normalize_s infallible (#780)
Browse files Browse the repository at this point in the history
Unconditionally returns a normalized signature, regardless of whether
the signature was normalized to begin with.

Closes #736
  • Loading branch information
tarcieri committed Jan 17, 2024
1 parent 83359e1 commit 32edd0d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions ecdsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ use alloc::vec::Vec;
#[cfg(feature = "arithmetic")]
use {
core::str,
elliptic_curve::{scalar::IsHigh, CurveArithmetic, NonZeroScalar},
elliptic_curve::{
scalar::IsHigh, subtle::ConditionallySelectable, CurveArithmetic, NonZeroScalar,
},
};

#[cfg(feature = "digest")]
Expand Down Expand Up @@ -313,16 +315,11 @@ 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> {
let s = self.s();

if s.is_high().into() {
let mut result = self.clone();
result.s = ScalarPrimitive::from(-s);
Some(result)
} else {
None
}
pub fn normalize_s(&self) -> Self {
let mut result = self.clone();
let s_inv = ScalarPrimitive::from(-self.s());
result.s.conditional_assign(&s_inv, self.s.is_high());
result
}
}

Expand Down

0 comments on commit 32edd0d

Please sign in to comment.