CompactSize round-trips
ProvedBitcoinSer.compactSize_roundtripFor all and : — the unchecked reader accepts every encoding the writer produces, in all four width classes.
import Definitions.Def_BitcoinSer
theorem BitcoinSer.compactSize_roundtrip (n : Nat) (rest : Bytes) (h : n < 2 ^ 64) :
BitcoinSer.readCompactSize false (BitcoinSer.writeCompactSize n ++ rest) = some (n, rest) := by sorryRead-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.compactSize_roundtrip
Binders and hypothesis. A natural number , a byte string (arbitrary, possibly empty), and the hypothesis
Conclusion. Reading a CompactSize without range check from the encoding of followed by succeeds and yields and :
The first argument of the reader is the Boolean false, i.e. the bound is not enforced in this statement.
Unfolding the encoder. is: the single byte if ; the byte followed by 2 little-endian bytes of if ; the byte followed by 4 little-endian bytes if ; and the byte followed by 8 little-endian bytes otherwise. Under the 8-byte case does not truncate.
Unfolding the decoder. fails on empty ; otherwise it inspects the first byte : if the value is ; if it reads 2 bytes as and fails if ; if it reads 4 bytes and fails if ; if it reads 8 bytes and fails if ; a short read in any of these fails. With the Boolean false the final range test is skipped, so the provisional result is returned as is. The theorem thus asserts, for every , that the prefix byte chosen by the encoder leads the decoder into the matching branch, that the fixed-width payload decodes back to , that passes the corresponding canonicality threshold (which holds because the encoder chose the shortest form), and that the remaining bytes are exactly .
Boundary cases included by the quantifier: (one zero byte), (one byte), (three bytes, value exactly at the lower canonical threshold), and (switch from 2 to 4 bytes), and (switch to 8 bytes), and . Values above are included, since no range check is applied.
What is not said. Nothing is asserted for the range-checked reader (): for that reader returns no result, and the theorem is silent on it, while for the range-checked result would agree but this is not stated. Nothing is asserted for , where the encoder truncates to and the decoder may either return the truncated value or fail the canonicality check. The converse ("every successful read comes from the canonical encoding") is a separate statement, compactSize_canonical. The theorem does not state the length of the encoding, nor that the decoder fails on malformed input. It is the one-directional round trip, for the unchecked reader only, under .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.