Skip to content
Open
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
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

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

39 changes: 39 additions & 0 deletions pkgs/pkc/src/ecdsa/sig_rec_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type_cvrt!(From<RecoverableSignature> for EcdsaRecSignature, |rec| {
#[cfg(test)]
#[expect(clippy::unwrap_used, reason = "test code")]
mod tests {
use crate::ecdsa::curve_consts::ORDER;
use crate::ecdsa::tests::*;
use crate::ecdsa::{
Compression, EcdsaPublicKey, EcdsaRecSigBytes, EcdsaRecSignature, EcdsaSecretKey, EcdsaSigBytes, EcdsaSignature,
Expand Down Expand Up @@ -211,6 +212,44 @@ mod tests {
assert_eq!(EcdsaPublicKey::recover(&MSG, &normalized).unwrap(), alice_pk);
}

/// Half the curve order, rounded down, which is the largest low S.
fn half_order() -> [u8; 32] {
let mut half = [0u8; 32];
let mut carry = 0;
for (h, o) in half.iter_mut().zip(ORDER) {
*h = (carry << 7) | (o >> 1);
carry = o & 1;
}
half
}

/// A recoverable signature read from its 65 bytes tells a high S from its
/// low-S twin, which verification and recovery both accept.
#[rstest]
fn tells_high_s_from_its_low_s_twin(alice_pk: EcdsaPublicKey, alice_rec_sig: EcdsaRecSignature) {
let bytes = EcdsaRecSigBytes::from(alice_rec_sig).to_bytes();
let read = |s: &[u8; 32], recovery_id: u8| {
let mut out = bytes;
out[0] = 27 + (bytes[0] - 27) / 4 * 4 + recovery_id;
out[33..].copy_from_slice(s);
EcdsaRecSignature::try_from(EcdsaRecSigBytes::from_bytes(out).unwrap()).unwrap()
};
let id = alice_rec_sig.recovery_id();

let half = half_order();
assert!(read(&half, id).signature().is_low_s());
assert!(!read(&negate_scalar(&half), id).signature().is_low_s());

let low = read(&bytes[33..].try_into().unwrap(), id);
let high = read(&negate_scalar(&bytes[33..]), id ^ 1);
assert_eq!(low, alice_rec_sig);
assert!(low.signature().is_low_s());
assert!(!high.signature().is_low_s());
assert_eq!(high.normalize_s(), Some(low));
assert_eq!(alice_pk.verify_digest(&MSG, high), Ok(()));
assert_eq!(EcdsaPublicKey::recover(&MSG, &high).unwrap(), alice_pk);
}

#[rstest]
fn verifies_without_downcast(alice_pk: EcdsaPublicKey, alice_rec_sig: EcdsaRecSignature) {
assert!(alice_pk.verify_digest(&MSG, alice_rec_sig).is_ok());
Expand Down
3 changes: 3 additions & 0 deletions pkgs/primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[package]
name = "dash-primitives"
version = "0.0.0"
exclude = [
"contrib",
]
readme = "README.md"
authors.workspace = true
edition.workspace = true
Expand Down
Loading
Loading