What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)
Read-back: BitcoinBlock.connectBlock_iff
Binders and hypotheses
- S : a chain state ⟨headers,utxo⟩.
- b : a serialized block (header and transaction list b.vtx).
- Hypothesis hne : b.vtx=[].
- Hypothesis hpos : for every transaction tx in b.vtx, 0≤valueOut(tx), where valueOut(tx)=∑o∈tx.vouto.nValue over Z.
Both hypotheses are carried by the statement; neither appears in the conclusion.
Conclusion
(∃r, connectBlock(S,b)=somer)⟺LedgerSpec(S,b).
Left side, unfolded
With hash the double-SHA256 of the serialized header of b and height=∣S.headers∣, connectBlock(S,b) returns some pair (ledger, undo) exactly in these cases:
- hash equals the genesis hash constant (result some(S.utxo,[]) unconditionally); or
- hash is not the genesis hash, and not [bip30Checked(S,height,hash) and some output ⟨txid(tx),o⟩, tx∈b.vtx, o<∣tx.vout∣, is present in S.utxo], and the transaction loop connectLoop(flags,height,mtpNatS,ltf,⟨S.utxo,0,0,[]⟩,b.vtx) returns some final state st, and valueOut(head(b.vtx))≤st.fees+coreSubsidy(height).
Here bip30Checked is (¬isBIP30Repeat(height,hash)∧¬[header at index 227931 exists and hashes to the BIP34 constant])∨1983702≤height; flags=blockScriptFlags(height,scriptException(hash)) (exception table keyed on two fixed hashes, then DERSIG/CLTV/CSV/NULLDUMMY by height thresholds 363725/388381/419328/481824); mtpNatS(k)=max(0,median of the first 11 of the times of headers 0..k most recent first); ltf=1 if height≥419328 else 0; coreSubsidy(height)=0 if ⌊height/210000⌋≥64, else 5000000000 shifted right by ⌊height/210000⌋ bits.
The loop, per transaction tx from ⟨v,fees,sigOps,undo⟩: if isCoinBase(tx) the fee/undo step is trivial; else checkTxInputs(v,tx,height) must be somefee (all prevouts present; coinbase coins mature: height−c.height≥100 in natural subtraction; each coin value and running sum in [0,M], M=2.1⋅1015; inputs ≥ outputs; difference in [0,M]), fees+fee∈[0,M], and sequenceLocksOk(v,height,mtpNatS,ltf,tx) (the BIP68 core calculation on 32-bit truncations of the version, flags and sequences, with coin heights from accessCoin, dummy height 0 if missing); then sigOps+txSigOpCost(flags,v,tx)≤80000; then (non-coinbase) inputScriptsOk(flags,v,tx): for every input index n, the full script verification (legacy, P2SH, witness v0 and Taproot paths) of the input's scriptSig, the spent coin's scriptPubKey and the witness succeeds, in the context of tx, n, the spent amount and the list of all spent outputs; then continue with updateCoins(v,tx,height).
Right side, unfolded
LedgerSpec(S,b) is: hash=genesis hash, or the conjunction of
- bip30Checked(S,height,hash)=true⇒∀tx∈b.vtx, ∀o<∣tx.vout∣, getCoin(S.utxo,⟨txid(tx),o⟩)=none;
- TxsSpec(flags,height,mtpNatS,ltf,S.utxo,0,0,b.vtx): defined recursively — true for the empty list; for tx::rest from (v,fees,sigOps): [isCoinBase(tx)=false⇒TxInputsSpec(flags,v,height,mtpNatS,ltf,tx)∧fees+feeOf(v,tx)≤M] ∧ sigOps+txSigOpCost(flags,v,tx)≤80000 ∧ TxsSpec on rest from (updateCoins(v,tx,height), fees or fees+feeOf(v,tx) for non-coinbase, sigOps+txSigOpCost(flags,v,tx)). TxInputsSpec requires: every input's prevout has a coin c in v with (c coinbase ⇒c.height+100≤height) and 0≤c.out.nValue≤M; valueIn(v,tx.vin)≤M; valueOut(tx)≤valueIn(v,tx.vin); the BIP68 specification predicate satisfiesBIP68 on the same 32-bit truncated inputs (either BIP68 is not enabled — version below 2 or flag bit 0 clear — or every input is disabled (bit 31 set) or satisfies its height/time lock); and for every n<∣tx.vin∣ the full script verification succeeds as in inputScriptsOk;
- for every cb among the first one element of b.vtx: valueOut(cb)≤totalFees(height,S.utxo,b.vtx)+specSubsidy(height), where totalFees sums feeOf of each non-coinbase against the running ledger (coinbases contribute 0) and specSubsidy(height)=⌊5000000000/2⌊height/210000⌋⌋.
Edge cases silently included
The hypotheses exclude an empty block and negative total outputs but nothing else (no checkBlock: coinbase-shaped transactions in later positions, duplicate txids, missing coins defaulting to value −1 in feeOf/valueIn on the right); genesis-hashed blocks satisfy both sides trivially; coreSubsidy versus specSubsidy (shift versus floor division, with a hard cut at 64 halvings on the left); 32-bit truncation of version/sequence values in BIP68; mtpNat clamps negative medians to 0; the undo component r.2 is unconstrained.
Confirmed by the moderator at approval.