The BIP30 scan condition, freshness of a block's outputs, and the coinbase scriptSig
DefinitionBitcoinUniquenessThree names over the block mission's BitcoinBlockV2 and the reorganization mission's BitcoinReorg; nothing is re-transcribed.
coinbaseScriptSig b — the scriptSig of the first input of the first transaction of a block (defaults: the genesis coinbase, an empty input).
ScanRuns S b — ConnectBlock performs the BIP30 scan when connecting b to S: fEnforceBIP30 || nHeight >= BIP34_IMPLIES_BIP30_LIMIT, i.e. the block mission's bip30Checked at the next height and the block's hash.
FreshOutputs u b — for every transaction of b and every output index below its output count, the ledger u holds no coin at that outpoint.
Formalization Note. validate, Reachable, heightScript (CScript() << nHeight), bip30Checked, isBIP30Repeat are the block mission's; connectChain is the reorganization mission's; pushBytes is the signature mission's push encoder and serialize the Script mission's CScriptNum::serialize. Only Std is imported.
import Definitions.Def_BitcoinBlockV2 import Definitions.Def_BitcoinReorg /-! # Transaction uniqueness: BIP30 and BIP34 Two transactions with the same identifier would let a block overwrite coins that already exist, which is what the undo data cannot restore. BIP30 forbids a block from creating a coin at an outpoint that already has one; its scan is skipped once the BIP34 activation block is an ancestor (until height 1,983,702), because BIP34 makes every coinbase start with a push of its height, and a height push is never a prefix of another. This file names the quantities those two rules speak about, so that the theorems can say exactly what validation guarantees where the scan runs and what the height rule guarantees where it does not. -/ namespace BitcoinUniqueness open BitcoinSer BitcoinBlockV2 /-- The `scriptSig` of a block's coinbase (its first transaction's first input). -/ def coinbaseScriptSig (b : Block) : Bytes := ((b.vtx.headD genesisCoinbase).vin.headD ⟨⟨[], 0⟩, [], 0, []⟩).scriptSig /-- The BIP30 scan runs when `ConnectBlock` connects `b` to `S` (`fEnforceBIP30 || nHeight >= BIP34_IMPLIES_BIP30_LIMIT`). -/ def ScanRuns (S : ChainState) (b : Block) : Prop := bip30Checked S S.nextHeight (headerHash b.header) = true /-- Freshness of a block against a ledger, over the output indices the block creates: no outpoint of any of its transactions already holds a coin. -/ def FreshOutputs (u : Ledger) (b : Block) : Prop := ∀ tx ∈ b.vtx, ∀ o, o < tx.vout.length → getCoin u ⟨txid tx, o⟩ = none end BitcoinUniqueness
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: Def_BitcoinUniqueness.lean
Three definitions, in file order. Throughout, a byte is an 8-bit word and a byte string is a finite list of bytes. A transaction is a record (version, list of inputs , list of outputs , lock time); an input is a record (previous outpoint, byte string , sequence number, witness stack); an outpoint is a pair (byte string , natural number ); a block is a record (header, list of transactions ); a chain state is a record (list of headers , ledger ), where a ledger is an association list of (outpoint, coin) pairs. The next height of is , the length of its header list.
coinbaseScriptSig (a byte string attached to a block)
For a block , is the byte string obtained as follows. Let be the first transaction of if that list is non-empty, and otherwise the fixed genesis coinbase transaction (version 1, one input with previous outpoint , the fixed 77-byte genesis scriptSig beginning "The Times 03/Jan/2009 …", sequence , empty witness; one output of value ). Let be the first input of if that list is non-empty, and otherwise the default input (previous outpoint , empty scriptSig, sequence , empty witness). Then
Degenerate cases built into the definition: if has no transactions at all, the value is the genesis coinbase's 77-byte scriptSig (not the empty string); if the first transaction has no inputs, the value is the empty byte string. Nothing in the definition requires to be a coinbase transaction, or to be valid in any sense; the value is defined for every block.
ScanRuns (a proposition about a chain state and a block)
For a chain state and a block , write and let , the double-SHA-256 of the 80-byte serialization of 's header (a 32-byte string in internal byte order). Then is the proposition that the Boolean equals , which unfolds to
where
- (the "BIP30 repeat" test) holds iff or , with and two fixed 32-byte constants (beginning and respectively);
- (the "BIP34 ancestor" test) holds iff the list has an element at position (zero-based; i.e. ) and the header hash of that element equals a fixed 32-byte constant (beginning ). If the position does not exist and is false.
Observations about the definition as written: the only feature of it reads is the hash of its header; the height it uses is , not any field of ; the second disjunct makes true for every whenever , regardless of and ; and no validity or reachability of is assumed.
FreshOutputs (a proposition about a ledger and a block)
For a ledger (an association list of outpoint–coin pairs) and a block , is the proposition
where is the double-SHA-256 of the witness-stripped serialization of , and scans the list from the front and returns the coin of the first pair whose outpoint equals (or if no pair matches). So the statement says: for every transaction of the block and every output index below that transaction's output count, no entry of is keyed by that (txid, index) outpoint.
Degenerate cases: if is empty, or if a transaction has no outputs, the corresponding quantification is vacuous. Membership "" is list membership, so two positions holding the same transaction impose the same condition once. The definition constrains only the relationship between the block and the ledger : it says nothing about two transactions within sharing a txid, nothing about the coinbase, and nothing about outpoints of that the block does not create. It is also indifferent to whether the outputs are spendable (the ledger-insertion function elsewhere skips unspendable outputs, but this predicate quantifies over all output indices).
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.