Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The BIP30 scan condition, freshness of a block's outputs, and the coinbase scriptSig

Definition
BitcoinUniqueness

by pitimespi · Oct 29, 2026 · Mathlib 0df444a (Lean v4.33.1)

bip30bip34bitcoinbitcoin-mainnetcoinbaseconsensusformal-verificationuniqueness

Three 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.

Definition code
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
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: ConnectBlock (fEnforceBIP30, BIP34_IMPLIES_BIP30_LIMIT), ContextualCheckBlock (the BIP34 height push), src/validation.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/validation.cpp; CScript::push_int64, CScriptNum::serialize (script/script.h); BIP30, BIP34
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 tx\mathrm{tx}tx is a record (version, list of inputs vin\mathrm{vin}vin, list of outputs vout\mathrm{vout}vout, lock time); an input is a record (previous outpoint, byte string scriptSig\mathrm{scriptSig}scriptSig, sequence number, witness stack); an outpoint is a pair (byte string hash\mathrm{hash}hash, natural number nnn); a block bbb is a record (header, list of transactions b.vtxb.\mathrm{vtx}b.vtx); a chain state SSS is a record (list of headers S.headersS.\mathrm{headers}S.headers, ledger S.utxoS.\mathrm{utxo}S.utxo), where a ledger is an association list of (outpoint, coin) pairs. The next height of SSS is S.nextHeight:=∣S.headers∣S.\mathrm{nextHeight} := |S.\mathrm{headers}|S.nextHeight:=∣S.headers∣, the length of its header list.

coinbaseScriptSig (a byte string attached to a block)

For a block bbb, coinbaseScriptSig(b)\mathrm{coinbaseScriptSig}(b)coinbaseScriptSig(b) is the byte string obtained as follows. Let tx0\mathrm{tx}_0tx0​ be the first transaction of b.vtxb.\mathrm{vtx}b.vtx if that list is non-empty, and otherwise the fixed genesis coinbase transaction (version 1, one input with previous outpoint (32 zero bytes, 0xffffffff)(32\text{ zero bytes},\ \mathtt{0xffffffff})(32 zero bytes, 0xffffffff), the fixed 77-byte genesis scriptSig beginning 04 ff ff 00 1d 01 04 45\mathtt{04\,ff\,ff\,00\,1d\,01\,04\,45}04ffff001d010445 "The Times 03/Jan/2009 …", sequence 0xffffffff\mathtt{0xffffffff}0xffffffff, empty witness; one output of value 5,000,000,0005{,}000{,}000{,}0005,000,000,000). Let i0i_0i0​ be the first input of tx0.vin\mathrm{tx}_0.\mathrm{vin}tx0​.vin if that list is non-empty, and otherwise the default input (previous outpoint ([ ],0)([\,],0)([],0), empty scriptSig, sequence 000, empty witness). Then

coinbaseScriptSig(b):=i0.scriptSig.\mathrm{coinbaseScriptSig}(b) := i_0.\mathrm{scriptSig}.coinbaseScriptSig(b):=i0​.scriptSig.

Degenerate cases built into the definition: if bbb 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 tx0\mathrm{tx}_0tx0​ to be a coinbase transaction, or bbb 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 SSS and a block bbb, write H:=∣S.headers∣H := |S.\mathrm{headers}|H:=∣S.headers∣ and let η:=headerHash(b.header)\eta := \mathrm{headerHash}(b.\mathrm{header})η:=headerHash(b.header), the double-SHA-256 of the 80-byte serialization of bbb's header (a 32-byte string in internal byte order). Then ScanRuns(S,b)\mathrm{ScanRuns}(S,b)ScanRuns(S,b) is the proposition that the Boolean bip30Checked(S,H,η)\mathrm{bip30Checked}(S, H, \eta)bip30Checked(S,H,η) equals true\mathtt{true}true, which unfolds to

(¬ Rep(H,η) ∧ ¬ Anc(S))  ∨  1,983,702≤H,\Big(\neg\,\mathrm{Rep}(H,\eta)\ \wedge\ \neg\,\mathrm{Anc}(S)\Big)\ \ \vee\ \ 1{,}983{,}702 \le H,(¬Rep(H,η) ∧ ¬Anc(S))  ∨  1,983,702≤H,

where

  • Rep(H,η)\mathrm{Rep}(H,\eta)Rep(H,η) (the "BIP30 repeat" test) holds iff (H=91842∧η=C91842)\big(H = 91842 \wedge \eta = C_{91842}\big)(H=91842∧η=C91842​) or (H=91880∧η=C91880)\big(H = 91880 \wedge \eta = C_{91880}\big)(H=91880∧η=C91880​), with C91842C_{91842}C91842​ and C91880C_{91880}C91880​ two fixed 32-byte constants (beginning ec ca e0 00\mathtt{ec\,ca\,e0\,00}eccae000 and 21 d7 7c cb\mathtt{21\,d7\,7c\,cb}21d77ccb respectively);
  • Anc(S)\mathrm{Anc}(S)Anc(S) (the "BIP34 ancestor" test) holds iff the list S.headersS.\mathrm{headers}S.headers has an element at position 227,931227{,}931227,931 (zero-based; i.e. H≥227,932H \ge 227{,}932H≥227,932) and the header hash of that element equals a fixed 32-byte constant C34C_{34}C34​ (beginning b8 08 08 9c\mathtt{b8\,08\,08\,9c}b808089c). If H≤227,931H \le 227{,}931H≤227,931 the position does not exist and Anc(S)\mathrm{Anc}(S)Anc(S) is false.

Observations about the definition as written: the only feature of bbb it reads is the hash of its header; the height it uses is ∣S.headers∣|S.\mathrm{headers}|∣S.headers∣, not any field of bbb; the second disjunct makes ScanRuns(S,b)\mathrm{ScanRuns}(S,b)ScanRuns(S,b) true for every bbb whenever ∣S.headers∣≥1,983,702|S.\mathrm{headers}| \ge 1{,}983{,}702∣S.headers∣≥1,983,702, regardless of Rep\mathrm{Rep}Rep and Anc\mathrm{Anc}Anc; and no validity or reachability of SSS is assumed.

FreshOutputs (a proposition about a ledger and a block)

For a ledger uuu (an association list of outpoint–coin pairs) and a block bbb, FreshOutputs(u,b)\mathrm{FreshOutputs}(u,b)FreshOutputs(u,b) is the proposition

∀ tx∈b.vtx,  ∀ o∈N,  o<∣tx.vout∣ ⟹ getCoin(u,(txid(tx),o))=none,\forall\, \mathrm{tx} \in b.\mathrm{vtx},\ \ \forall\, o \in \mathbb{N},\ \ o < |\mathrm{tx}.\mathrm{vout}| \ \Longrightarrow\ \mathrm{getCoin}\big(u, (\mathrm{txid}(\mathrm{tx}), o)\big) = \mathrm{none},∀tx∈b.vtx,  ∀o∈N,  o<∣tx.vout∣ ⟹ getCoin(u,(txid(tx),o))=none,

where txid(tx)\mathrm{txid}(\mathrm{tx})txid(tx) is the double-SHA-256 of the witness-stripped serialization of tx\mathrm{tx}tx, and getCoin(u,p)\mathrm{getCoin}(u, p)getCoin(u,p) scans the list uuu from the front and returns the coin of the first pair whose outpoint equals ppp (or none\mathrm{none}none 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 uuu is keyed by that (txid, index) outpoint.

Degenerate cases: if b.vtxb.\mathrm{vtx}b.vtx is empty, or if a transaction has no outputs, the corresponding quantification is vacuous. Membership "tx∈b.vtx\mathrm{tx} \in b.\mathrm{vtx}tx∈b.vtx" 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 uuu: it says nothing about two transactions within bbb sharing a txid, nothing about the coinbase, and nothing about outpoints of uuu 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).

Human review
  • Endorsed by Shuze Chen · Oct 29, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Oct 29, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me