UpdateCoins respects ledger equivalence
ProvedBitcoinReorg.updateCoins_equivIf two ledgers hold the same coin at every outpoint, so do the ledgers UpdateCoins produces from them for the same transaction and height.
import Definitions.Def_BitcoinReorg import Definitions.Def_BitcoinSer open BitcoinReorg BitcoinBlockV2
theorem BitcoinReorg.updateCoins_equiv (u v : Ledger) (h : BitcoinBlockV2.Equiv u v) (tx : BitcoinSer.Tx) (H : Nat) :
BitcoinBlockV2.Equiv (BitcoinBlockV2.updateCoins u tx H) (BitcoinBlockV2.updateCoins v tx H) := 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: BitcoinReorg.updateCoins_equiv
Notions used
Ledgers and coin lookup. A ledger is a finite list of pairs , where is an outpoint (a byte string and a natural number ) and is a coin (an output consisting of an integer value and a byte-string script ; a natural-number creation height ; and a Boolean coinbase flag ). The lookup scans from the front and returns the coin of the first pair whose outpoint equals (as a value "some "), or "none" if no pair has outpoint ; any later pair with the same outpoint is shadowed and never consulted. Two ledgers are equivalent, written , when
the quantifier ranging over every outpoint (every byte string and every natural number). Equivalence says nothing about the order of the lists, about duplicate or shadowed entries, or about list length.
Spending and adding coins. is with every pair whose outpoint equals removed. A script byte string is unspendable when it is non-empty and its first byte is the opcode , or when its length exceeds the constant . is unchanged if is unspendable, and otherwise is the pair prepended to .
Transactions. A transaction has a natural-number version, a list of inputs (each input has an outpoint , a script , a natural number , and a witness stack, a list of byte strings), a list of outputs (each an integer value and a script), and a natural number . is a coinbase exactly when has exactly one input and that input's outpoint is the null outpoint (hash equal to 32 zero bytes and index ). is the double SHA-256 of the serialization of with all witness data stripped.
The ledger update , for a ledger , a transaction and a natural number : let if is a coinbase, and otherwise let be with applied successively for the outpoint of every input of in order (no check is made that those outpoints are present). Then, for the outputs of in order, apply successively
and the result is . Outputs with unspendable scripts are therefore not stored, and each stored output replaces (after removal of) any earlier entry with the same outpoint.
Statement
Binders. Two ledgers and ; a hypothesis , i.e. ; a transaction (arbitrary: it need not be well-formed, need not have any inputs or outputs, and its inputs' outpoints need not be present in or ); and a natural number (the height stamped on the created coins; any natural number, including ).
Conclusion.
that is, for every outpoint , the first-match lookup of in the ledger obtained from by (if is not a coinbase) removing all entries at each input's outpoint and then adding, for each output index , the coin at outpoint unless 's script is unspendable, equals the same lookup in the ledger obtained from in the same way.
Edge cases the statement includes. with no inputs is not a coinbase (a coinbase has exactly one, null-outpoint input), so its (empty) input spend is a no-op; with no outputs adds nothing; spending an outpoint absent from both ledgers is a no-op on both; if and differ in shadowed duplicate entries, removes all copies and prepends, so the conclusion is still only about first-match lookups. The theorem places no condition on , having distinct keys.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.