Add tests to verify 0 return for overflow
What changed, and why it matters
This commit only adds new unit tests to verify that a previously changed function, Target::from_compact, returns zero when a Bitcoin 'compact' difficulty value overflows a 256-bit target. It does not change any production code. The tests confirm the overflow behavior and check values right at the overflow boundary.
No action needed; this is a test-only commit. Review the prior change to Target::from_compact that introduced the overflow-to-zero behavior if you want to assess its security implications.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff in units/src/pow.rs adds test cases to the existing target_from_compact test and introduces a new target_from_compact_overflow_boundaries test. The new cases verify that compact-encoded targets with exponent/mantissa combinations that would exceed 256 bits (e.g., 0x2101_0000, 0x2200_0100, 0x2200_0101, 0x2300_0001) produce Target::ZERO. Boundary cases (0x2100_FFFF and 0x2200_00FF) are checked to ensure they still produce valid non-zero targets just below the overflow threshold. No library logic is modified.
Changed components
units/src/pow.rs (tests only)Inspect captured patch +17 / −0
diff --git a/units/src/pow.rs b/units/src/pow.rs
index f5d79c31..f6073328 100644
--- a/units/src/pow.rs
+++ b/units/src/pow.rs
@@ -1331,6 +1331,10 @@ mod tests {
(0x0500_9234_u32, 0x9234_0000_u64),
(0x0492_3456_u32, 0x00_u64), // High bit set (0x80 in 0x92).
(0x0412_3456_u32, 0x1234_5600_u64), // Inverse of above; no high bit.
+ (0x2101_0000_u32, 0x00_u64), // Overflows 256 bits.
+ (0x2200_0100_u32, 0x00_u64), // Overflows 256 bits.
+ (0x2200_0101_u32, 0x00_u64), // Overflows 256 bits.
+ (0x2300_0001_u32, 0x00_u64), // Overflows 256 bits.
];
for (n_bits, target) in tests {
@@ -1340,6 +1344,19 @@ mod tests {
}
}
+ #[test]
+ fn target_from_compact_overflow_boundaries() {
+ let tests = [
+ (0x2100_FFFF_u32, Target(U256::from(0xFFFF_u32) << 240)),
+ (0x2200_00FF_u32, Target(U256::from(0xFF_u32) << 248)),
+ ];
+
+ for (n_bits, want) in tests {
+ let got = Target::from_compact(CompactTarget::from_consensus(n_bits));
+ assert_eq!(got, want);
+ }
+ }
+
macro_rules! check_from_str {
($ty:ident, $err_ty:ident, $mod_name:ident) => {
#[cfg(feature = "alloc")]
Why this scored 12/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.