Skip to content

Commit 32edd0d

Browse files
authored
ecdsa: make Signature::normalize_s infallible (#780)
Unconditionally returns a normalized signature, regardless of whether the signature was normalized to begin with. Closes #736
1 parent 83359e1 commit 32edd0d

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

ecdsa/src/lib.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ use alloc::vec::Vec;
9696
#[cfg(feature = "arithmetic")]
9797
use {
9898
core::str,
99-
elliptic_curve::{scalar::IsHigh, CurveArithmetic, NonZeroScalar},
99+
elliptic_curve::{
100+
scalar::IsHigh, subtle::ConditionallySelectable, CurveArithmetic, NonZeroScalar,
101+
},
100102
};
101103

102104
#[cfg(feature = "digest")]
@@ -313,16 +315,11 @@ where
313315
/// [BIP 0062: Dealing with Malleability][1].
314316
///
315317
/// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
316-
pub fn normalize_s(&self) -> Option<Self> {
317-
let s = self.s();
318-
319-
if s.is_high().into() {
320-
let mut result = self.clone();
321-
result.s = ScalarPrimitive::from(-s);
322-
Some(result)
323-
} else {
324-
None
325-
}
318+
pub fn normalize_s(&self) -> Self {
319+
let mut result = self.clone();
320+
let s_inv = ScalarPrimitive::from(-self.s());
321+
result.s.conditional_assign(&s_inv, self.s.is_high());
322+
result
326323
}
327324
}
328325

0 commit comments

Comments
 (0)