Skip to content

Commit 3e37adb

Browse files
committed
Merge rust-bitcoin#749: Add tests for ecdsa::Signature::from_compact
e63c8cb Add tests for ecdsa::Signature::from_compact (Shing Him Ng) Pull request description: Used `Signature::from_compact` on another change and saw there were no unit tests for it, so I added some. Not sure how useful they are since it's just a wrapper though but thought I'd add some real quick. Feel free to close if it's not necessary ACKs for top commit: apoelstra: ACK e63c8cb successfully ran local tests; nice! Tree-SHA512: f99df1b3025737f5de4c892d8e649c0c30fa4126d04e2536da17d6caf9b4ab8ae8b0489bf6e7ddfefe0867277c7a254d8ce27bcf1dbffd23851ed31e5919cd11
2 parents 736adc9 + e63c8cb commit 3e37adb

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/ecdsa/mod.rs

+55
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,58 @@ pub(crate) fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {
429429
}
430430
len <= max_len
431431
}
432+
433+
#[cfg(test)]
434+
mod tests {
435+
use crate::ecdsa::Signature;
436+
use crate::Scalar;
437+
438+
#[test]
439+
fn test_from_compact_min_r_and_min_s() {
440+
// From libsecp256k1: "The signature must consist of a 32-byte big endian R value, followed
441+
// by a 32-byte big endian S value. If R or S fall outside of [0..order-1], the encoding is
442+
// invalid. R and S with value 0 are allowed in the encoding."
443+
let r = Scalar::ZERO;
444+
let s = Scalar::ZERO;
445+
let mut bytes: [u8; 64] = [0; 64];
446+
bytes[..32].copy_from_slice(&r.to_be_bytes());
447+
bytes[32..].copy_from_slice(&s.to_be_bytes());
448+
449+
assert!(Signature::from_compact(&bytes).is_ok())
450+
}
451+
452+
#[test]
453+
fn test_from_compact_max_r_and_max_s() {
454+
let r = Scalar::MAX;
455+
let s = Scalar::MAX;
456+
let mut bytes: [u8; 64] = [0; 64];
457+
bytes[..32].copy_from_slice(&r.to_be_bytes());
458+
bytes[32..].copy_from_slice(&s.to_be_bytes());
459+
460+
assert!(Signature::from_compact(&bytes).is_ok())
461+
}
462+
463+
#[test]
464+
fn test_from_compact_invalid_r() {
465+
let r = Scalar::MAX;
466+
let s = Scalar::MAX;
467+
let mut bytes: [u8; 64] = [0; 64];
468+
bytes[..32].copy_from_slice(&r.to_be_bytes());
469+
bytes[32..].copy_from_slice(&s.to_be_bytes());
470+
bytes[31] += 1;
471+
472+
assert!(Signature::from_compact(&bytes).is_err())
473+
}
474+
475+
#[test]
476+
fn test_from_compact_invalid_s() {
477+
let r = Scalar::MAX;
478+
let s = Scalar::MAX;
479+
let mut bytes: [u8; 64] = [0; 64];
480+
bytes[..32].copy_from_slice(&r.to_be_bytes());
481+
bytes[32..].copy_from_slice(&s.to_be_bytes());
482+
bytes[63] += 1;
483+
484+
assert!(Signature::from_compact(&bytes).is_err())
485+
}
486+
}

0 commit comments

Comments
 (0)