Merge rust-bitcoin/rust-bitcoin#7051: pow: Fix overflow in min difficulty time check
What changed, and why it matters
This commit fixes an arithmetic overflow bug in the code that calculates the next mining difficulty target for Bitcoin's testnet. When a block's timestamp was very close to the maximum value a 32-bit unsigned integer can hold, adding 20 minutes to it would either crash the program in debug builds or silently wrap around to a small number in release builds, causing the wrong difficulty target to be returned. The fix converts the values to 64-bit integers before adding them, matching how Bitcoin Core handles the same calculation.
Review and merge the fix; ensure downstream users update to a release containing this commit. The bug is primarily relevant to testnet, but any code relying on `next_target_after` with crafted or boundary timestamps should be considered. No immediate emergency response is warranted for mainnet consensus.
Security signals we found
Integer overflow in difficulty-target calculation
Debug-build panic / release-build silent wraparound
Consensus-relevant code path (testnet minimum-difficulty rule)
Fix aligns implementation with Bitcoin Core behavior
Regression test added for boundary condition
Evidence from the diff
In next_target_after in bitcoin/src/pow.rs, the testnet minimum-difficulty check compared new_block_timestamp > current_header.time.to_u32() + params.pow_target_spacing * 2. Because all operands were u32, the addition could overflow when current_header.time was near u32::MAX. In Rust, this panics in debug builds and wraps in release builds, leading to an incorrect target calculation. The patch changes the comparison to use u64 arithmetic, consistent with Bitcoin Core’s use of int64_t. A regression test is added with time = 0xFFFF_FF00 and new_block_timestamp = Some(u32::MAX) to verify the overflow no longer occurs.
Changed components
bitcoin/src/pow.rsnext_target_after functionTestnet3 minimum-difficulty target computationInspect captured patch +33 / −1
### bitcoin/src/pow.rs
@@ -244,7 +244,9 @@ where
let pow_limit = params.max_attainable_target.to_compact_lossy();
// if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
- if new_block_timestamp > current_header.time.to_u32() + params.pow_target_spacing * 2 {
+ if u64::from(new_block_timestamp)
+ > u64::from(current_header.time.to_u32()) + u64::from(params.pow_target_spacing) * 2
+ {
Ok(pow_limit)
} else {
let mut header = current_header;
@@ -811,4 +813,34 @@ mod tests {
// Should return the real_target from the walked-back header
assert_eq!(got, want);
}
+
+ // The min difficulty check must not overflow when the current header's time is close to
+ // `u32::MAX`.
+ #[test]
+ fn next_target_testnet_min_difficulty_time_near_max() {
+ let params = Params::TESTNET3;
+ let want = CompactTarget::from_consensus(0x1c00_ffff);
+
+ let header = Header {
+ version: crate::block::Version::from_consensus(0x2000_0000),
+ prev_blockhash: BlockHash::from_byte_array([1u8; 32]),
+ merkle_root: crate::TxMerkleNode::from_byte_array([2u8; 32]),
+ time: 0xFFFF_FF00.into(),
+ bits: want,
+ nonce: 0,
+ };
+
+ fn fetch_header(_height: BlockHeight) -> Result<Header, core::convert::Infallible> {
+ unreachable!("fetcher should not be called when bits are not pow_limit");
+ }
+
+ let height = BlockHeight::from_u32(100); // non-retarget height
+
+ // New block timestamp is less than 20 minutes after the current header.
+ let new_block_timestamp = Some(u32::MAX);
+
+ let got = next_target_after(header, height, ¶ms, new_block_timestamp, fetch_header)
+ .expect("failed to calculate next target");
+ assert_eq!(got, want);
+ }
}Why this scored 45/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.