bitcoin: add test for `push_int` with largest and smallest valid numbers
What changed, and why it matters
This commit only adds new test cases for two existing functions, `push_int` and `push_int_unchecked`, checking that they correctly encode the largest positive and negative 32-bit integers allowed by Bitcoin's script number rules. No production code was changed, so there is no security issue here.
No action required; this is a benign test-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds four assertions in each of two existing unit tests (script and script_buf_push_int) in bitcoin/src/blockdata/script/tests.rs. It verifies that push_int(0x7fffffff), push_int_unchecked(0x7fffffff), push_int(-0x7fffffff), and push_int_unchecked(-0x7fffffff) produce the expected 4-byte little-endian script-number encodings ([4, 0xFF, 0xFF, 0xFF, 0x7F] and [4, 0xFF, 0xFF, 0xFF, 0xFF]). No library code is modified.
Changed components
Inspect captured patch +12 / −0
diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs
index f621603e..16ae1729 100644
--- a/bitcoin/src/blockdata/script/tests.rs
+++ b/bitcoin/src/blockdata/script/tests.rs
@@ -57,6 +57,12 @@ fn script() {
script = script.push_int(-1).unwrap(); comp.extend([0x4f].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
script = script.push_int_non_minimal(-1); comp.extend([1, 0x81].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+ script = script.push_int(0x7fffffff).unwrap(); comp.extend([4u8, 0xFF, 0xFF, 0xFF, 0x7F].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+ script = script.push_int_unchecked(0x7fffffff); comp.extend([4u8, 0xFF, 0xFF, 0xFF, 0x7F].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+
+ script = script.push_int(-0x7fffffff).unwrap(); comp.extend([4u8, 0xFF, 0xFF, 0xFF, 0xFF].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+ script = script.push_int_unchecked(-0x7fffffff); comp.extend([4u8, 0xFF, 0xFF, 0xFF, 0xFF].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+
// keys
const KEYSTR1: &str = "21032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af";
let key = KEYSTR1[2..].parse::<PublicKey>().unwrap();
@@ -101,6 +107,12 @@ fn script_buf_push_int() {
script.push_int(-1).unwrap(); comp.extend([0x4f].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
script.push_int_non_minimal(-1); comp.extend([1, 0x81].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+
+ script.push_int(0x7fffffff).unwrap(); comp.extend([4u8, 0xFF, 0xFF, 0xFF, 0x7F].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+ script.push_int_unchecked(0x7fffffff); comp.extend([4u8, 0xFF, 0xFF, 0xFF, 0x7F].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+
+ script.push_int(-0x7fffffff).unwrap(); comp.extend([4u8, 0xFF, 0xFF, 0xFF, 0xFF].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
+ script.push_int_unchecked(-0x7fffffff); comp.extend([4u8, 0xFF, 0xFF, 0xFF, 0xFF].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);
}
#[test]
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.