Merge rust-bitcoin/rust-secp256k1#749: Add tests for ecdsa::Signature::from_compact

e63c8cbcfc 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 e63c8cbcfc successfully ran local tests; nice!

Tree-SHA512: f99df1b3025737f5de4c892d8e649c0c30fa4126d04e2536da17d6caf9b4ab8ae8b0489bf6e7ddfefe0867277c7a254d8ce27bcf1dbffd23851ed31e5919cd11
This commit is contained in:
merge-script 2024-09-30 23:00:48 +00:00
commit 3e37adb384
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 55 additions and 0 deletions

View File

@ -429,3 +429,58 @@ pub(crate) fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {
}
len <= max_len
}
#[cfg(test)]
mod tests {
use crate::ecdsa::Signature;
use crate::Scalar;
#[test]
fn test_from_compact_min_r_and_min_s() {
// From libsecp256k1: "The signature must consist of a 32-byte big endian R value, followed
// by a 32-byte big endian S value. If R or S fall outside of [0..order-1], the encoding is
// invalid. R and S with value 0 are allowed in the encoding."
let r = Scalar::ZERO;
let s = Scalar::ZERO;
let mut bytes: [u8; 64] = [0; 64];
bytes[..32].copy_from_slice(&r.to_be_bytes());
bytes[32..].copy_from_slice(&s.to_be_bytes());
assert!(Signature::from_compact(&bytes).is_ok())
}
#[test]
fn test_from_compact_max_r_and_max_s() {
let r = Scalar::MAX;
let s = Scalar::MAX;
let mut bytes: [u8; 64] = [0; 64];
bytes[..32].copy_from_slice(&r.to_be_bytes());
bytes[32..].copy_from_slice(&s.to_be_bytes());
assert!(Signature::from_compact(&bytes).is_ok())
}
#[test]
fn test_from_compact_invalid_r() {
let r = Scalar::MAX;
let s = Scalar::MAX;
let mut bytes: [u8; 64] = [0; 64];
bytes[..32].copy_from_slice(&r.to_be_bytes());
bytes[32..].copy_from_slice(&s.to_be_bytes());
bytes[31] += 1;
assert!(Signature::from_compact(&bytes).is_err())
}
#[test]
fn test_from_compact_invalid_s() {
let r = Scalar::MAX;
let s = Scalar::MAX;
let mut bytes: [u8; 64] = [0; 64];
bytes[..32].copy_from_slice(&r.to_be_bytes());
bytes[32..].copy_from_slice(&s.to_be_bytes());
bytes[63] += 1;
assert!(Signature::from_compact(&bytes).is_err())
}
}