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

Merge rust-bitcoin/rust-bitcoin#6989: units: drop PartialOrd and Ord from CompactTarget

Public commit record

What the developer wrote

Authored by Andrew Poelstra

91/100 · Strong
Merge rust-bitcoin/rust-bitcoin#6989: units: drop PartialOrd and Ord from CompactTarget

78c5fce5a3c46509157637681c0b333143e119bf units: drop PartialOrd and Ord from CompactTarget (satsfy (Renato Britto))
9162fe34bfe5816ac893ef8d691e0aaf83b06d05 primitives: implement lexicographic Header Ord (satsfy (Renato Britto))

Pull request description:

Close https://github.com/rust-bitcoin/rust-bitcoin/issues/2758
See discussion on https://github.com/rust-bitcoin/rust-bitcoin/pull/2754#issuecomment-2102736580

Conversion between `CompactTarget` and `Target` is lossy both ways. A derived order on the underlying `u32` does not correspond to which target is actually harder. The policy doc says to consider carefully before deriving `PartialOrd` or `Ord` on types where the comparison has no semantic meaning, and this is one instance of. `block::Header` and p2p `HeaderAndShortIds` contain a `CompactTarget`, they get derive removed.

About equality, it stays on the consensus encoded `u32` and that is ok. Simplified the docs for it.

The issue also suggests `ArbitraryOrd` but we removed that some time ago, so nothing takes its place and its up to the user to convert their `CompactTargets` to compare.

Note that the issue and discussion is somewhat old (2024), but it still seems just as relevant, so I jumped directly to PR.


ACKs for top commit:
tcharding:
ACK 78c5fce5a3c46509157637681c0b333143e119bf
apoelstra:
ACK 78c5fce5a3c46509157637681c0b333143e119bf; successfully ran local tests


Tree-SHA512: f7fac55887e1f4ad4c98f6ce12c099b0997b73bace9504a63ad7661ca7e38ee8916444e8488d7be6e8b0b44e1f414259bd5c9e80b058b49d8da00e7777369bd9
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Mentions testing or verification✓ Links an issue, advisory, or supporting reference
The short version

What changed, and why it matters

This change removes automatic numeric ordering from a Bitcoin type called CompactTarget because its underlying u32 value does not reliably reflect which mining target is actually harder. It also replaces the derived ordering of block headers with a clear byte-by-byte (lexicographic) ordering based on how headers are serialized. The patch is a correctness and API-safety improvement rather than a fix for an active exploit, but it prevents future bugs where code might accidentally sort or compare CompactTarget values as if the raw number meant 'harder' or 'easier'.

Recommended action

Review downstream code that sorts collections keyed by CompactTarget or block::Header. Update any code relying on the old derived ordering to explicitly convert CompactTarget to Target before comparing, or use the new lexicographic Header ordering if appropriate. No urgent patch is required, but treat this as a breaking API correctness change.

Security signals we found

01

Removal of semantically incorrect derived ordering on CompactTarget

02

Explicit lexicographic ordering for block::Header based on consensus serialization

03

Documentation update warning that CompactTarget equality is on the u32 and ordering requires conversion

04

Test added to enforce that Header ordering matches consensus serialization

05

API test updated to reflect CompactTarget no longer implements Ord/PartialOrd

Risk score

Why this scored 28/100

Our methodology →
Potential impact 5/30
Exploitability 2/25
Stealth signal 3/15
Affected reach 6/15
Confidence 8/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.