Skip to content

Commit

Permalink
k256: update Signature::normalize_s usages (#1017)
Browse files Browse the repository at this point in the history
It's now infallible and always returns a normalized signature regardless
of if the original one was normalized or not:

RustCrypto/signatures#780
  • Loading branch information
tarcieri committed Jan 17, 2024
1 parent c0129b6 commit 51a1367
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
18 changes: 13 additions & 5 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ members = [
[profile.dev]
opt-level = 2

[patch.crates-io.crypto-bigint]
git = "https://github.com/RustCrypto/crypto-bigint.git"
[patch.crates-io]
crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint.git" }
ecdsa = { git = "https://github.com/RustCrypto/signatures.git" }
9 changes: 4 additions & 5 deletions k256/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ impl SignPrimitive<Secp256k1> for Scalar {
{
let (sig, recid) = hazmat::sign_prehashed::<Secp256k1, K>(self, k, z)?;
let is_y_odd = recid.is_y_odd() ^ bool::from(sig.s().is_high());
let sig_low = sig.normalize_s().unwrap_or(sig);
let recid = RecoveryId::new(is_y_odd, recid.is_x_reduced());
Ok((sig_low, Some(recid)))
Ok((sig.normalize_s(), Some(recid)))
}
}

Expand Down Expand Up @@ -239,7 +238,7 @@ mod tests {
0xfb, 0x42, 0xef, 0x20, 0xe3, 0xc6, 0xad, 0xb2,
].as_slice()).unwrap();

let sig_normalized = sig_hi.normalize_s().unwrap();
let sig_normalized = sig_hi.normalize_s();
assert_eq!(sig_lo, sig_normalized);
}

Expand All @@ -253,7 +252,7 @@ mod tests {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
].as_slice()).unwrap();

assert_eq!(sig.normalize_s(), None);
assert_eq!(sig.normalize_s(), sig);
}
}

Expand Down Expand Up @@ -386,7 +385,7 @@ mod tests {
ecdsa_core::VerifyingKey::from_encoded_point(&q_encoded).unwrap();

let sig = match Signature::<Secp256k1>::from_der(sig) {
Ok(s) => s.normalize_s().unwrap_or(s),
Ok(s) => s.normalize_s(),
Err(_) if !pass => return None,
Err(_) => return Some("failed to parse signature ASN.1"),
};
Expand Down

0 comments on commit 51a1367

Please sign in to comment.