Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Block headers round-trip

Proved
BitcoinSer.header_roundtrip

by pitimespi · Oct 26, 2026 · Mathlib 0df444a (Lean v4.33.1)

bitcoinbitcoin-mainnetconsensusformal-verificationgenesismerkleserializationsha256

For every well-formed header hhh and remainder rrr: readHeader(serHeader(h)++r)=some(h,r)\mathrm{readHeader}(\mathrm{serHeader}(h) ++ r) = \mathrm{some}(h, r)readHeader(serHeader(h)++r)=some(h,r), including the signed nVersion.

Preamble
import Definitions.Def_BitcoinSer
Formal statement
theorem BitcoinSer.header_roundtrip (h : Header) (rest : Bytes) (hw : h.wf) :
    BitcoinSer.readHeader (BitcoinSer.serHeader h ++ rest) = some (h, rest) := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: WriteCompactSize/ReadCompactSize and vector serialization, src/serialize.h: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/serialize.h; SerializeTransaction/UnserializeTransaction, COutPoint, CTxIn, CTxOut, ComputeHash, ComputeWitnessHash, src/primitives/transaction.h and transaction.cpp; CBlockHeader, CBlock, GetHash, src/primitives/block.h and block.cpp; Hash, Hash160, src/hash.h; CSHA256, src/crypto/sha256.cpp; CRIPEMD160, src/crypto/ripemd160.cpp; CSHA1, src/crypto/sha1.cpp; GetTransactionWeight/GetBlockWeight, src/consensus/validation.h; WITNESS_SCALE_FACTOR, MAX_BLOCK_WEIGHT, src/consensus/consensus.h; CreateGenesisBlock and hashGenesisBlock, src/kernel/chainparams.cpp
Read-back

What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)

Read-back: BitcoinSer.header_roundtrip

Binders and hypothesis. A header record hhh, a byte string rest\texttt{rest}rest (arbitrary), and hw:h.wfhw : h.\texttt{wf}hw:h.wf, which is the conjunction

−231≤h.version<231,∣h.prevBlock∣=32,∣h.merkleRoot∣=32,h.time<232,h.bits<232,h.nonce<232.-2^{31} \le h.\texttt{version} < 2^{31},\quad |h.\texttt{prevBlock}| = 32,\quad |h.\texttt{merkleRoot}| = 32,\quad h.\texttt{time} < 2^{32},\quad h.\texttt{bits} < 2^{32},\quad h.\texttt{nonce} < 2^{32}.−231≤h.version<231,∣h.prevBlock∣=32,∣h.merkleRoot∣=32,h.time<232,h.bits<232,h.nonce<232.

Conclusion. Parsing the serialized header followed by rest\texttt{rest}rest succeeds and returns exactly hhh with remainder rest\texttt{rest}rest:

readHeader(serHeader(h)+ ⁣ ⁣+rest)=some (h,rest).\texttt{readHeader}\big(\texttt{serHeader}(h) \mathbin{+\!\!+} \texttt{rest}\big) = \mathrm{some}\,(h, \texttt{rest}).readHeader(serHeader(h)++rest)=some(h,rest).

This is an equality of optional pairs; it asserts success, equality of every field of the parsed record with the corresponding field of hhh (record equality is field-wise), and that the unconsumed suffix is rest\texttt{rest}rest.

Unfolding. The serialization is serInt32(version)+ ⁣ ⁣+prevBlock+ ⁣ ⁣+merkleRoot+ ⁣ ⁣+ser32(time)+ ⁣ ⁣+ser32(bits)+ ⁣ ⁣+ser32(nonce)\texttt{serInt32}(\texttt{version}) \mathbin{+\!\!+} \texttt{prevBlock} \mathbin{+\!\!+} \texttt{merkleRoot} \mathbin{+\!\!+} \texttt{ser32}(\texttt{time}) \mathbin{+\!\!+} \texttt{ser32}(\texttt{bits}) \mathbin{+\!\!+} \texttt{ser32}(\texttt{nonce})serInt32(version)++prevBlock++merkleRoot++ser32(time)++ser32(bits)++ser32(nonce), where serInt32(v)\texttt{serInt32}(v)serInt32(v) is the 4 little-endian bytes of v mod 232v \bmod 2^{32}vmod232 taken in [0,232)[0, 2^{32})[0,232) (two's complement for negative vvv) and ser32\texttt{ser32}ser32 is 4 little-endian bytes of a natural number mod 2322^{32}232. The parser readHeader\texttt{readHeader}readHeader performs, in order: readInt32\texttt{readInt32}readInt32 (4 bytes little-endian uuu, returned as uuu if u<231u < 2^{31}u<231, else u−232u - 2^{32}u−232); readHash\texttt{readHash}readHash twice (each takes exactly 32 bytes, failing if fewer remain); then readLE  4\texttt{readLE}\;4readLE4 three times (unsigned). Each step fails on short input and the whole parse then fails.

The hypotheses are used as follows: the version range guarantees that the signed decode of the two's-complement bytes gives back exactly h.versionh.\texttt{version}h.version (for vvv outside [−231,231)[-2^{31}, 2^{31})[−231,231) the wrap-around would produce a different integer); the two length equalities guarantee that the 32 bytes taken by each readHash\texttt{readHash}readHash are exactly the field (if prevBlock\texttt{prevBlock}prevBlock were shorter or longer, the parser would still take 32 bytes but they would straddle fields); and the three <232< 2^{32}<232 bounds guarantee that the truncating ser32\texttt{ser32}ser32 loses nothing so readLE  4\texttt{readLE}\;4readLE4 recovers the value. The hypothesis is therefore exactly matched to what the round trip needs — every conjunct is used.

What is not said. Nothing for headers violating wf\texttt{wf}wf: e.g. with a 31-byte prevBlock\texttt{prevBlock}prevBlock the parse may still succeed but return a different record, and the theorem is silent. The converse direction ("every successful readHeader\texttt{readHeader}readHeader comes from serHeader\texttt{serHeader}serHeader of a well-formed header", or that parsed headers are well-formed) is not stated. Nothing about headerHash\texttt{headerHash}headerHash, about 80-byte length (that is serHeader_length), about blocks, or about failure on short input. It is the one-directional parse-after-serialize statement, under full wf\texttt{wf}wf, with an arbitrary trailing suffix.

Human review
  • Endorsed by Shuze Chen · Oct 26, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Oct 26, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me