A serialized number of at most four bytes is accepted strictly and reads back
ProvedBitcoinScript.toNum_serializeFor every integer whose serialization has at most four bytes, : the strict (fRequireMinimal) four-byte constructor accepts the serializer's output and returns the original value.
import Definitions.Def_BitcoinScript
theorem BitcoinScript.toNum_serialize (n : Int) (h : (BitcoinScript.serialize n).length ≤ 4) :
BitcoinScript.toNum true 4 (BitcoinScript.serialize n) = some n := 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: BitcoinScript.toNum_serialize
Binders and hypothesis. One universally quantified integer . One hypothesis, named :
i.e. the byte list produced by serialize from has length at most (lengths are all allowed).
Conclusion. An equation between optional integers:
That is, parsing the encoding of with the minimal-encoding requirement switched on and a size limit of bytes succeeds (does not return "no value") and yields exactly .
Unfolding toNum. returns:
- "no value" if ;
- otherwise "no value" if is true, is non-empty, and is true;
- otherwise .
With , and , the conclusion therefore unfolds to the conjunction of three facts: (i) , which is exactly the hypothesis ; (ii) it is not the case that is non-empty and is true; and (iii) . Facts (ii) and (iii) are, respectively, the content of the separate theorems serialize_minimal and serialize_roundtrip in this bundle, but the present statement does not refer to them; it asserts the combined equation directly.
Unfolding serialize, nonMinimal, setVch. ; for it is the little-endian magnitude bytes of without trailing zeros, with the sign carried in bit of the last byte, adding an extra byte ( for negative, for positive) when the last magnitude byte already has that bit set. is true when is non-empty, its last byte is or , and either or the second-to-last byte has its top bit clear. is if the last byte's top bit is set and otherwise, with the little-endian unsigned value.
What the hypothesis amounts to. By the definition of serialize, the output length is at most exactly when : the magnitude encodes as (4 bytes), while needs plus a fifth padding byte. So the theorem is about the integers with , including (whose encoding is empty and is accepted by toNum because the minimality test is skipped for the empty list, and whose value is ).
Edge cases. (empty encoding, length ). , the largest magnitudes allowed. , , — values whose magnitude bytes end in a byte with the top bit set and so receive a padding byte; for these the length constraint bites one power of 256 earlier than one might expect from the magnitude alone.
What is not said. Nothing is claimed for integers whose encoding exceeds 4 bytes (e.g. or ): the theorem is silent, not negative, about them. Nothing is claimed about toNum with other size limits (such as the 5-byte limit used by CHECKLOCKTIMEVERIFY/CHECKSEQUENCEVERIFY in execOp) or with requireMinimal = false (the setting every arithmetic opcode in execOp actually uses). Nothing is claimed about toNum applied to byte lists not produced by serialize, in particular about whether non-minimal lists such as or are rejected. The theorem does not say that is the only 4-byte-or-shorter list that toNum maps to . It does not mention the interpreter, flags, or the oracle.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.