CScriptNum serialization round-trips
ProvedBitcoinScript.serialize_roundtripFor every integer , : reading back the bytes CScriptNum::serialize produces gives the original value, including (the empty encoding), negative values (sign bit or sign byte), and magnitudes whose top byte already has bit 7 set (sign byte appended).
import Definitions.Def_BitcoinScript
theorem BitcoinScript.serialize_roundtrip (n : Int) : BitcoinScript.setVch (BitcoinScript.serialize n) = n := by sorry
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: BitcoinScript.serialize_roundtrip
Binders and hypothesis. One universally quantified integer (any sign, any magnitude, included). No hypotheses.
Conclusion. The integer equation
Decoding the byte list produced by serialize from , using setVch, returns exactly .
Unfolding serialize. . For , with (little-endian bytes of , no trailing zero bytes, hence non-empty with non-zero last byte): if the last byte of has bit set, the result is followed by (if ) or (if ); otherwise the result is with bit set on its last byte (if ) or unchanged (if ).
Unfolding setVch. For a byte list : if the top bit () of the last byte of is set, , where clearTop clears that one bit; otherwise . Here and , so the first byte is least significant. For the value is .
The claim in detail. For each integer :
- if : ;
- if : the last byte of has its top bit clear (either the last magnitude byte with clear top bit, or an appended ), and the little-endian value of the whole list is claimed to be ;
- if : the last byte has its top bit set (either by
setTopor because an appended ), and the negated little-endian value after clearing that bit is claimed to be . The theorem states the equation uniformly for all ; the case split above is only the shape the definitions impose.
Edge cases included by the quantifier. ; ( decodes to ); (two-byte outputs and ); , and beyond — there is no size restriction, so the equation is claimed for integers whose encodings exceed 4 or 5 bytes, outside anything toNum would accept.
What is not said. The theorem asserts only the composite is the identity on integers. It does not assert the other composite: nothing is claimed about for a byte list , and indeed the statement is silent on whether serialize is surjective onto byte lists (it is not claimed that every byte list decodes and re-encodes to itself). It does not say serialize is injective (though injectivity is a logical consequence of having a left inverse, the theorem does not state it). It does not mention minimality, nonMinimal, toNum, size limits, or the length of . It does not relate to castToBool. It makes no statement about the interpreter, pushNum, flags, or the oracle.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.