Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Fixed-width integers round-trip

Proved
BitcoinSer.readLE_leBytes

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

bitcoinbitcoin-mainnetconsensusformal-verificationgenesismerkleserializationsha256

For all kkk, n<28kn < 2^{8k}n<28k and remainder rrr: readLE(k,leBytes(k,n)++r)=some(n,r)\mathrm{readLE}(k, \mathrm{leBytes}(k,n) ++ r) = \mathrm{some}(n, r)readLE(k,leBytes(k,n)++r)=some(n,r).

Preamble
import Definitions.Def_BitcoinSer
Formal statement
theorem BitcoinSer.readLE_leBytes (k n : Nat) (rest : Bytes) (h : n < 2 ^ (8 * k)) :
    BitcoinSer.readLE k (BitcoinSer.leBytes k n ++ rest) = some (n, 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.readLE_leBytes

Binders and hypothesis. Two natural numbers kkk and nnn, a byte string rest\texttt{rest}rest (a finite list of 8-bit values, possibly empty), and one hypothesis

h:n<28k.h : n < 2^{8k}.h:n<28k.

No other restriction: kkk may be 000 (then the hypothesis forces n=0n = 0n=0), and rest\texttt{rest}rest is arbitrary.

Conclusion. Decoding kkk little-endian bytes from the concatenation of the kkk-byte encoding of nnn with rest\texttt{rest}rest succeeds and returns nnn together with rest\texttt{rest}rest as the unconsumed remainder:

readLE  k  (leBytes  k  n+ ⁣ ⁣+rest)=some (n,rest).\texttt{readLE}\;k\;\big(\texttt{leBytes}\;k\;n \mathbin{+\!\!+} \texttt{rest}\big) = \mathrm{some}\,(n, \texttt{rest}).readLEk(leByteskn++rest)=some(n,rest).

Unfolding. Here leBytes  k  n\texttt{leBytes}\;k\;nleByteskn is the list of kkk bytes, least significant first, with byte iii equal to ⌊n/256i⌋ mod 256\lfloor n / 256^i \rfloor \bmod 256⌊n/256i⌋mod256. The decoder readLE  k  s\texttt{readLE}\;k\;sreadLEks returns "no result" when ∣s∣<k|s| < k∣s∣<k, and otherwise returns the pair (leVal(s[:k]), s[k:])(\texttt{leVal}(s[{:}k]),\, s[k{:}])(leVal(s[:k]),s[k:]), where leVal\texttt{leVal}leVal sums bi⋅256ib_i \cdot 256^ibi​⋅256i over the bytes of its argument (with leVal([ ])=0\texttt{leVal}([\,]) = 0leVal([])=0). Since the encoded prefix has length exactly kkk (whatever nnn is), the input has length ≥k\ge k≥k and the "short read" case cannot occur; the statement then amounts to two facts:

  • leVal  (leBytes  k  n)=n\texttt{leVal}\;(\texttt{leBytes}\;k\;n) = nleVal(leByteskn)=n whenever n<28kn < 2^{8k}n<28k (the little-endian value of the kkk-byte truncated encoding recovers nnn exactly, because no bits were lost);
  • dropping the first kkk bytes of leBytes  k  n+ ⁣ ⁣+rest\texttt{leBytes}\;k\;n \mathbin{+\!\!+} \texttt{rest}leByteskn++rest leaves exactly rest\texttt{rest}rest.

Degenerate instance: k=0k = 0k=0 gives n=0n = 0n=0, leBytes  0  0=[ ]\texttt{leBytes}\;0\;0 = [\,]leBytes00=[], and readLE  0  rest=some (0,rest)\texttt{readLE}\;0\;\texttt{rest} = \mathrm{some}\,(0, \texttt{rest})readLE0rest=some(0,rest) since leVal\texttt{leVal}leVal of the empty prefix is 000; the statement holds trivially there. The equality is an equality of optional pairs, so it asserts both success (not "no result") and the exact values of both components.

What is not said. Nothing is claimed when n≥28kn \ge 2^{8k}n≥28k: in that case leBytes\texttt{leBytes}leBytes truncates and the decoder would return n mod 28kn \bmod 2^{8k}nmod28k, which the statement does not mention. Nothing is claimed about decoding arbitrary byte strings (the converse direction "if readLE  k  s=some (n,r)\texttt{readLE}\;k\;s = \mathrm{some}\,(n, r)readLEks=some(n,r) then s=leBytes  k  n+ ⁣ ⁣+rs = \texttt{leBytes}\;k\;n \mathbin{+\!\!+} rs=leByteskn++r" is not stated, although for readLE\texttt{readLE}readLE it would be true). The statement does not cover the signed readers readInt32/readInt64\texttt{readInt32}/\texttt{readInt64}readInt32/readInt64 paired with serInt32/serInt64\texttt{serInt32}/\texttt{serInt64}serInt32/serInt64, nor the big-endian beBytes\texttt{beBytes}beBytes. It does not say the decoder fails on short input. It is a single-direction round-trip for the unsigned little-endian codec under the exact bound n<28kn < 2^{8k}n<28k.

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