Transactions round-trip in both formats
ProvedBitcoinSer.tx_roundtripFor every well-formed transaction , witness mode with ( ⇒ vin non-empty), and remainder :
With witnesses allowed the extended format (dummy, flag byte, witness stacks) is parsed back exactly; without, the basic format returns the transaction with cleared witnesses. The non-empty-vin side condition is genuine: UnserializeTransaction reads an empty vin as the BIP144 dummy, so a witness-less, input-less transaction does not round-trip through the witness-aware reader.
import Definitions.Def_BitcoinSer
theorem BitcoinSer.tx_roundtrip (aw : Bool) (tx : Tx) (rest : Bytes) (hw : tx.wf)
(hv : aw = true → tx.vin ≠ []) :
BitcoinSer.readTx aw (BitcoinSer.serTx aw tx ++ rest) =
some (if aw then tx else tx.stripWitness, 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.tx_roundtrip
Binders and hypothesis. A Boolean ("allow witness", either value), a transaction record , a byte string (arbitrary), and two hypotheses:
- , which unfolds to: ; ; ; ; every input satisfies , , , , and every witness item has length ; every output satisfies and . Here and all bounds on lengths are non-strict.
- : if witness mode is on, the transaction has at least one input. When is false this hypothesis is vacuous and may have no inputs.
Conclusion.
That is: parsing, in mode , the mode- serialization of followed by arbitrary trailing bytes succeeds; the parsed record equals itself when is true and equals with all witness lists emptied when is false; and the remainder is exactly .
Unfolding the serializer. Let if and (some input has a non-empty witness), else . is the 4-byte little-endian version, then if , then the CompactSize count of inputs and each input (32 raw hash bytes, 4-byte index, CompactSize-prefixed script, 4-byte sequence), then the count of outputs and each output (8-byte two's-complement value, prefixed script), then, if , for each input a CompactSize count of witness items followed by each item length-prefixed, then the 4-byte lock time.
Unfolding the parser. reads the 4-byte version (unsigned), then a range-checked input list . If is empty and is true, it consumes one byte : if it re-reads an input list and reads an output list; if it sets the output list to without reading one. Otherwise it reads the output list directly with flags . Then, if the flags value is odd and is true, it reads one witness stack per input, attaches them in order, and fails unless at least one stack is non-empty, decrementing the flags. Then it fails if the flags are still non-zero, and finally reads the 4-byte lock time.
How the cases play out. (i) : , no marker; the parser never enters the flag branch; all fields are recovered and every parsed input has witness , which is why the conclusion names — this is an equality even if had witnesses. (ii) and : ; the parser reads the input count from the marker, consumes , re-reads inputs and outputs, reads witness stacks, the non-empty-witness test passes, flags become , lock time is read; the result is with witnesses attached, i.e. . (iii) , no witness: , the encoding is the legacy one; by the input list is non-empty, so the parser takes the direct branch; result . The hypothesis excludes exactly the case , , where the serialization begins with an input count of and the parser would read the output count byte as the flags byte (for outputs it would then try to re-parse inputs from output bytes; for outputs it would consume the count and still succeed with empty outputs); the theorem makes no claim there. The bounds ensure that the truncating encoders and the range-checked, canonical readers recover every value; the signed range on values ensures the two's-complement decode is exact; the 32-byte hash lengths ensure the raw hash bytes are cut at the right place.
What is not said. Nothing about with no inputs. Nothing about parsing a witness serialization in non-witness mode or vice versa. No converse (parsed transactions need not be well-formed, and nothing says a successful parse came from ). Nothing about of the result, about , or about lengths. It is the one-directional parse-after-serialize statement, per mode, under and .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.