From 405260769d2af0781a63c819df36883d73a54f4d Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 4 Aug 2020 20:10:12 -0700 Subject: [PATCH] ecdsa: have NormalizeLow return a bool ...to indicate whether normalization occurred or not, and have this propagate via `Signature::normalize_s`. --- ecdsa/src/lib.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ecdsa/src/lib.rs b/ecdsa/src/lib.rs index 70cbfff4..2e5a872a 100644 --- a/ecdsa/src/lib.rs +++ b/ecdsa/src/lib.rs @@ -149,16 +149,20 @@ where /// [BIP 0062: Dealing with Malleability][1]. /// /// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki - pub fn normalize_s(&mut self) -> Result<(), Error> { + pub fn normalize_s(&mut self) -> Result { let s_bytes = GenericArray::from_mut_slice(&mut self.bytes[C::ElementSize::to_usize()..]); let s_option = C::Scalar::from_bytes(s_bytes); // Not constant time, but we're operating on public values if s_option.is_some().into() { let mut s = s_option.unwrap(); - s.normalize_low(); - s_bytes.copy_from_slice(&s.into()); - Ok(()) + + if s.normalize_low() { + s_bytes.copy_from_slice(&s.into()); + Ok(true) + } else { + Ok(false) + } } else { Err(Error::new()) } @@ -240,5 +244,5 @@ where pub trait NormalizeLow { /// Normalize scalar to the lower half of the field (i.e. negate it if it's /// larger than half the curve's order) - fn normalize_low(&mut self); + fn normalize_low(&mut self) -> bool; }