ReadCompactSize accepts only canonical encodings
ProvedBitcoinSer.compactSize_canonicalFor every mode, byte list , value and remainder : if then . The reader is injective on encodings: a value has exactly one accepted encoding, so a CompactSize can never be padded to a longer form.
import Definitions.Def_BitcoinSer
theorem BitcoinSer.compactSize_canonical (rc : Bool) (b : Bytes) (n : Nat) (rest : Bytes)
(h : BitcoinSer.readCompactSize rc b = some (n, rest)) : b = BitcoinSer.writeCompactSize 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_canonical
Binders and hypothesis. A Boolean (the range-check flag, either value), a byte string , a natural number , a byte string , and the single hypothesis
i.e. the CompactSize reader with flag , applied to , succeeds and returns the value with unconsumed remainder .
Conclusion. The input is exactly the canonical encoding of followed by the remainder:
Unfolding. The reader, on a non-empty with first byte , returns itself when ; when it decodes the next 2 bytes little-endian as and succeeds only if ; when it decodes 4 bytes and succeeds only if ; when it decodes 8 bytes and succeeds only if . If is true it additionally fails when . Any short read fails. So the hypothesis tells us that has one of four shapes with the value in the right band for its prefix. The encoder chooses: one byte when ; prefix plus 2 bytes when ; prefix plus 4 bytes when ; prefix plus 8 bytes otherwise. The conclusion asserts that the band the reader accepted is exactly the band the encoder would choose for (this is where the reader's strict lower-threshold rejections matter: a value under prefix , for example, is never accepted, so the equation never has to hold for such a ), and that re-encoding the decoded value as little-endian bytes reproduces the very bytes that were read (which holds because the decoded value is below for a -byte payload). Together with being the tail after those bytes, is reconstructed exactly.
Both values of are covered: when is true the hypothesis is stronger (it also gives ) and the conclusion is the same; when is false, can be any value below that the reader accepts. The statement is uniform in .
What is not said. The theorem does not say that the reader succeeds on any input; it is conditional on success. It does not say what is bounded by (that is compactSize_range), and it does not say that the encoding of is readable (that is compactSize_roundtrip). It does not say the reader fails on non-canonical encodings — that is a consequence of the definition that this statement uses but does not assert. It says nothing about the length of . Finally, it concerns only itself, not or , which call it with the flag fixed to true.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.