AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Moderate 59 Bitcoin

Return 0 on Target::from_compact overflow

Public commit record

What the developer wrote

Authored by Mitchell Bagot

73/100 · Adequate
Return 0 on Target::from_compact overflow

Currently, the Target::from_compact function treats a negative mantissa
as a failure mode and returns a Target value of zero. This lines up
with Core's SetCompact, which sets a pfNegative flag in the same case.
In Core, a pfOverflow flag is similarly set in overflow cases, and in
every case where a negative flag is treated as a failure, the overflow
is too. As such, our implementation should also return Target::ZERO in
the overflow case as it does for the negative case.

Return Target::ZERO for mantissa/exponent values which overflow in
Target::from_compact.
✓ Descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Names security-relevant behavior explicitly
The short version

What changed, and why it matters

This commit fixes a bug in how rust-bitcoin converts Bitcoin 'compact' difficulty targets into full numeric Target values. Previously, if the compact value encoded a number too large to fit in a valid Target, the code would silently produce a truncated/wrapped result instead of treating it as invalid. The fix makes overflow behave like an invalid negative mantissa: return zero. This aligns the library with Bitcoin Core's consensus behavior, reducing the risk of consensus divergence or incorrect difficulty calculations.

Recommended action

Review callers of Target::from_compact to ensure they handle Target::ZERO as an error/invalid case. Backport this fix to maintained release branches. Consider adding unit tests covering the new overflow branches and comparing outputs against Bitcoin Core's SetCompact for known edge-case nBits values. No immediate emergency response is indicated unless this function is used directly in consensus validation without additional checks.

Security signals we found

01

Consensus-critical code path modified

02

Overflow/wraparound in cryptographic/numeric conversion

03

Alignment with Bitcoin Core behavior (SetCompact overflow flag)

04

Silent truncation could lead to incorrect target/difficulty values

05

No explicit CVE or vendor security advisory in commit

Risk score

Why this scored 59/100

Our methodology →
Potential impact 18/30
Exploitability 12/25
Stealth signal 8/15
Affected reach 10/15
Confidence 7/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.