What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)
Read-back: BitcoinPenalty.penalty_any_block
What the declaration literally asserts
For every Taproot handler tap, every verification context c with c.flags.witness=true, every commitment-key record K with WellFormed(K), every byte string sigR, every natural number height and every integer blockTime, under the hypotheses
- castToBool(sha256(StoLocal(K)))=true,
- ∣sigR∣≤520,
- otoLocal.checkSig(sigR,K.revPk)=some true with otoLocal:=oracleOf(c,StoLocal(K)) (concrete BIP143 ECDSA check),
- c.tx.nLockTime=0,
the conclusion is the conjunction of two boolean facts:
V(StoLocal(K), [sigR, [0x01], StoLocal(K)])=true∧isFinalTx(c.tx,height,blockTime)=true.
Notes on the binders and edge cases
- height and blockTime are arbitrary (any natural number, any integer, including 0 and negative times); they appear only in the second conjunct.
- Under the hypothesis c.tx.nLockTime=0, the first branch of isFinalTx applies, so the second conjunct unfolds to true regardless of height, blockTime and the inputs' sequence numbers.
- The first conjunct is exactly the conclusion of
to_local_penalty_sig for this sigR.
- All consensus flags other than witness, and c.nIn, c.amount, c.tx's inputs, outputs and version, are unconstrained except via the
checkSig hypothesis (which, being concrete, can only hold with c.amount≥0 and a header-consistent 33/65-byte K.revPk); note c.tx.nLockTime=0 also enters the BIP143 signature hash that the checkSig hypothesis is about.
- WellFormed(K): the eight byte-string fields have length ≤520 and 0≤K.toSelfDelay<216.
Shared setting and unfolded definitions
Everything below is the literal content of the definitions the statement uses, unfolded from the preamble files. It is identical across the read-backs of this bundle; nothing in it is specific to one theorem except where a theorem's own section says so.
Basic objects
- A byte is an 8-bit word; a byte string (Bytes) is a finite list of bytes. Byte strings are the only kind of value scripts push and keys, hashes, signatures, witness elements are all byte strings with no length or format constraint unless a hypothesis states one.
- A transaction tx has a version tx.version∈N, a list of inputs tx.vin (each input has a previous outpoint, a scriptSig, a sequence number nSequence∈N and a witness stack), a list of outputs tx.vout (each with an amount ∈Z and an output script) and a lock time tx.nLockTime∈N. In these statements none of these fields is range-restricted: version, sequence numbers and lock time are arbitrary natural numbers (not bounded by 232) unless a hypothesis says otherwise.
- A consensus flag set F consists of seven booleans: F.p2sh, F.witness, F.taproot, F.dersig, F.cltv, F.csv, F.nulldummy. Every flag not fixed by a hypothesis is arbitrary (the statement is asserted for both values).
- A verification context c is a quadruple: flags c.flags, a transaction c.tx, an input index c.nIn∈N (not required to be a valid index into c.tx.vin unless a hypothesis says so) and an amount c.amount∈Z (may be negative).
- A Taproot handler tap is any function from (context, witness stack, 32-byte program) to booleans. It is universally quantified; in the unfolding below the version-0 witness branch never calls it, so the asserted statement is the same for every tap.
- f:=scriptFlagsOf(c.flags) is the interpreter flag triple (f.cltv,f.csv,f.nulldummy):=(c.flags.cltv, c.flags.csv, c.flags.nulldummy).
- sigFlagsOf(c.flags):=(dersig=c.flags.dersig, lowS=false, strictenc=false, witnessPubkeyType=false).
- MAX_SCRIPT_ELEMENT_SIZE=520, MAX_SCRIPT_SIZE=10000, MAX_STACK_SIZE=1000, MAX_OPS_PER_SCRIPT=201.
- SEQUENCE_LOCKTIME_DISABLE_FLAG=231=2147483648 (the same value is defined both in the script file and in the signature file; the theorems name the signature file's copy). LOCKTIME_THRESHOLD=500000000. SEQUENCE_FINAL=232−1=0xffffffff. SEQUENCE_LOCKTIME_TYPE_FLAG=222, SEQUENCE_LOCKTIME_MASK=0xffff.
castToBool
For a byte string v, castToBool(v)=true iff some byte of v is nonzero, except that a string whose bytes are all zero apart from a last byte equal to 0x80 ("negative zero") is false. The empty string is false. Hence for a 32-byte hash H, "castToBool(H)=true" means H is neither the all-zero string nor 31 zero bytes followed by 0x80.
Commitment keys and the size hypotheses
A CommitmentKeys record K has nine fields: byte strings K.revPk, K.revHash, K.localDelayedPk, K.remoteKey, K.localHtlcKey, K.remoteHtlcKey, K.fundingKeyLocal, K.fundingKeyRemote, and an integer K.toSelfDelay∈Z.
WellFormed(K) is the conjunction of ten facts: each of the eight byte-string fields has length ≤520, and 0≤K.toSelfDelay<216. Nothing else is required: the "keys" need not be valid public keys, K.revHash need not be 20 bytes, and no relation between K.revHash and K.revPk is imposed here.
An Htlc record h has two fields: a byte string h.payHash and an integer h.expiry∈Z. HtlcWellFormed(h) is: ∣h.payHash∣≤520 and ∣h.expiry∣<232 (absolute value; h.expiry may be negative or zero).
Script serialization primitives
- pushBytes(d): if ∣d∣<76, the byte ∣d∣ followed by d; if ∣d∣≤255, 0x4c,∣d∣ then d; if ∣d∣≤65535, 0x4d, two little-endian length bytes, then d; otherwise 0x4e, four little-endian length bytes, then d. (For the empty string this is the single byte 0x00.)
- serialize(v) for v∈Z is the
CScriptNum encoding: empty for 0; otherwise the little-endian magnitude bytes with the sign carried in bit 7 of the last byte (an extra byte 0x00/0x80 appended when the top bit of the magnitude is set).
- numBytes(n) for n∈Z: if n=−1 or 1≤n≤16, the single opcode byte 0x50+n (so 0x4f for −1, 0x51 =
OP_1 for 1, 0x52 = OP_2 for 2, 0x60 = OP_16 for 16); otherwise pushBytes(serialize(n)) (so 0x00 for 0, and 0x010x20 for 32).
Opcode bytes used: OP_IF =0x63, OP_NOTIF =0x64, OP_ELSE =0x67, OP_ENDIF =0x68, OP_IFDUP =0x73, OP_DROP =0x75, OP_DUP =0x76, OP_SWAP =0x7c, OP_SIZE =0x82, OP_EQUAL =0x87, OP_EQUALVERIFY =0x88, OP_HASH160 =0xa9, OP_CHECKSIG =0xac, OP_CHECKSIGVERIFY =0xad, OP_CHECKMULTISIG =0xae, OP_CHECKLOCKTIMEVERIFY =0xb1, OP_CHECKSEQUENCEVERIFY =0xb2.
The witness scripts (byte strings)
Writing ⟨d⟩ for pushBytes(d) and [n] for numBytes(n):
- StoLocal(K):=
OP_IF ⟨K.revPk⟩ OP_ELSE [K.toSelfDelay] OP_CHECKSEQUENCEVERIFY OP_DROP ⟨K.localDelayedPk⟩ OP_ENDIF OP_CHECKSIG.
- Soff(K,h):=
OP_DUP OP_HASH160 ⟨K.revHash⟩ OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE ⟨K.remoteHtlcKey⟩ OP_SWAP OP_SIZE [32] OP_EQUAL OP_NOTIF OP_DROP [2] OP_SWAP ⟨K.localHtlcKey⟩ [2] OP_CHECKMULTISIG OP_ELSE OP_HASH160 ⟨h.payHash⟩ OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF OP_ENDIF.
- Srec(K,h):=
OP_DUP OP_HASH160 ⟨K.revHash⟩ OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE ⟨K.remoteHtlcKey⟩ OP_SWAP OP_SIZE [32] OP_EQUAL OP_IF OP_HASH160 ⟨h.payHash⟩ OP_EQUALVERIFY [2] OP_SWAP ⟨K.localHtlcKey⟩ [2] OP_CHECKMULTISIG OP_ELSE OP_DROP [h.expiry] OP_CHECKLOCKTIMEVERIFY OP_DROP OP_CHECKSIG OP_ENDIF OP_ENDIF.
- SoffA(K,h) (anchors): as Soff(K,h) but with [1]
OP_CHECKSEQUENCEVERIFY OP_DROP inserted between the inner OP_ENDIF and the outer OP_ENDIF.
- SrecA(K,h) (anchors): as Srec(K,h) with the same insertion.
- StoRemote(K):=⟨K.remoteKey⟩
OP_CHECKSIGVERIFY [1] OP_CHECKSEQUENCEVERIFY.
- Sanchor(fk):=⟨fk⟩
OP_CHECKSIG OP_IFDUP OP_NOTIF [16] OP_CHECKSEQUENCEVERIFY OP_ENDIF; SanchorLocal(K):=Sanchor(K.fundingKeyLocal), SanchorRemote(K):=Sanchor(K.fundingKeyRemote).
In the hash-and-key slots the scripts push whatever byte strings K and h hold; the file does not compute K.revHash or h.payHash from anything.
p2wshOf
p2wshOf(S):=witnessBytes(0,sha256(S))=0x00∥pushBytes(sha256(S))=0x000x20∥sha256(S), a 34-byte output script, where sha256 is the concrete SHA-256 of the serialization file (a transcription of crypto/sha256.cpp).
oracleOf — the concrete oracle
oS:=oracleOf(c,S):=legacyOracle(sigFlagsOf(c.flags), c.tx, c.nIn, c.amount, witnessV0, S). Its eight components are concrete:
- oS.sha256, oS.ripemd160, oS.sha1, oS.hash256=sha256∘sha256 and oS.hash160=ripemd160∘sha256 are the transcribed hash functions of the serialization file (so "oS.hash160(x)" is the concrete RIPEMD160(SHA256(x)), independent of S and c).
- oS.checkSig(sig,pk):=checkSigPreTapscript(sigFlagsOf(c.flags),c.tx,c.nIn,c.amount,witnessV0,S,sig,pk)∈{none,some true,some false}, which is:
- none (script failure) iff the encoding check fails. With the signature flags above (only dersig possibly on), the public-key encoding check always passes, and the signature encoding check fails exactly when c.flags.dersig=true, sig=[] and sig is not a strict-DER encoding (
IsValidSignatureEncoding: length between 9 and 73, the 30 len 02 |r| r 02 |s| s hashtype layout with the checks of that function). An empty sig always passes the encoding check.
- otherwise some(checkECDSASignature(c.tx,c.nIn,c.amount,witnessV0,sig,pk,S)), which is true iff: pk is non-empty and its first byte announces its length (first byte 2 or 3 and ∣pk∣=33, or first byte 4, 6 or 7 and ∣pk∣=65); sig is non-empty, its last byte ht being the hash type; c.amount≥0; and
CPubKey::Verify succeeds on pk, the BIP143 signature hash sigHashWitnessV0(S,c.tx,c.nIn,ht,c.amount) (a double-SHA256 of version, hashPrevouts, hashSequence, the outpoint of input c.nIn — the default empty input if c.nIn is out of range —, the length-prefixed S, the amount, the input's sequence, hashOutputs, lock time and hash type) and sig without its last byte: the key parses as a secp256k1 point, the signature parses under the lax DER parser, its s is normalized to low-s, and the ECDSA verification equation holds with the message reduced modulo the group order. (No FindAndDelete is applied in the witness-v0 case.)
- oS.checkLockTime(L):=checkLockTime(c.tx,c.nIn,L) for L∈Z: true iff c.tx.nLockTime and L are on the same side of 500000000 (both below, or both ≥), L≤c.tx.nLockTime, and the sequence number of input c.nIn (see σ below) is =0xffffffff.
- oS.checkSequence(S):=checkSequence(c.tx,c.nIn,S) for S∈Z: with σ the sequence number of input c.nIn, true iff c.tx.version≥2, σ&231=0, and, with m:=222∣0xffff, σm:=σ&m and Sm:=(max(S,0))&m: σm and Sm are either both <222 or both ≥222, and Sm≤σm.
inputSequence
σ:=inputSequence(c.tx,c.nIn) is the nSequence field of the input at index c.nIn of c.tx; if c.nIn≥∣c.tx.vin∣ it is the sequence of the default input, i.e. σ=0. The same getD default is used inside checkLockTime, checkSequence and the BIP143 hash.
CsvPasses and CltvPasses
CsvPasses(f,o,d)⟺f.csv=false ∨ (0≤d ∧ ((max(d,0))&231=0 ∨ o.checkSequence(d)=true)).
CltvPasses(f,o,e)⟺f.cltv=false ∨ (0≤e ∧ o.checkLockTime(e)=true).
With o=oS these are the concrete checks above on c.tx and c.nIn.
isFinalTx
isFinalTx(tx,height,blockTime) for height∈N, blockTime∈Z is true iff: tx.nLockTime=0; or tx.nLockTime<height when tx.nLockTime<500000000, resp. tx.nLockTime<blockTime when tx.nLockTime≥500000000; or every input of tx has nSequence=0xffffffff (vacuously true when tx has no inputs).
verifyScript on a P2WSH output with an empty scriptSig
For a witness script S and a witness list w (in CScriptWitness order: the last element is on top of the stack), write
V(S,w):⟺verifyScript(tap,c,[],p2wshOf(S),w)=true.
Unfolding verifyScript with the empty scriptSig and the 34-byte output script 0020∥sha256(S):
- The empty scriptSig evaluates to the empty stack; the output script evaluates (as a legacy script) to the one-element stack [sha256(S)]; the top must be true: castToBool(sha256(S))=true is required.
- If c.flags.witness=true (all theorems assume this), the output script is recognised as a witness program of version 0 with program sha256(S) (32 bytes); the scriptSig is empty as required; the P2SH flag and the P2SH branch are then irrelevant.
- Version 0 with a 32-byte program: w must be non-empty; its last element S′ must satisfy sha256(S′)=sha256(S); then
executeWitnessScriptV0 runs on the stack reverse(w without its last element) and the script S′. For w=ws++[S] this stack is reverse(ws) and the script is S itself.
executeWitnessScriptV0: every stack element has length ≤520; ∣S∣≤10000; S parses instruction by instruction (GetOp); the Script-mission interpreter evalScript runs with flags f=scriptFlagsOf(c.flags) and oracle oS=oracleOf(c,S) on that stack (with the usual limits: pushes ≤520 bytes, stack plus altstack ≤1000, at most 201 counted opcodes, balanced conditionals, disabled opcodes rejected); it must succeed, leaving exactly one element, and castToBool of that element must be true.
The Taproot handler tap is not consulted on this path. So V(S,ws++[S]) is a decidable boolean fact about c, K, h and ws only.
Interpreter facts used by the unfoldings
OP_CHECKSIG pops pk (top) and sig, calls o.checkSig(sig,pk): none fails the script; some b pushes [0x01] if b else the empty string. OP_CHECKSIGVERIFY fails instead of pushing false.
OP_CHECKSEQUENCEVERIFY with f.csv=false does nothing; otherwise it reads the top element as a script number of at most 5 bytes n (without popping), fails if n<0, passes if n&231=0, else fails unless o.checkSequence(n)=true. OP_CHECKLOCKTIMEVERIFY likewise with f.cltv, no disable bit, and o.checkLockTime.
OP_CHECKMULTISIG with [2] ⟨k1⟩ ⟨k2⟩ [2] uses the matching loop multisigLoop(o,[s1,s2],[k1,k2]) and then pops one extra "dummy" element, failing if f.nulldummy=true and the dummy is non-empty.
- multisigLoop(o,sigs,keys): with no signatures left, some true; with signatures but no keys, some false; otherwise it checks the first signature against the first key: none propagates as none; on true it drops both and continues (failing with some false if fewer keys than signatures remain); on false it drops only the key and continues (same count check). Instantiated on two signatures and two keys, multisigLoop(o,[s1,s2],[k1,k2])=some true iff o.checkSig(s1,k1)=some true and o.checkSig(s2,k2)=some true.
The two sides of the penalty (predicates on an oracle o and K)
HoldsRevocation(o,K):⟺(∃sigR, ∣sigR∣≤520 ∧ o.checkSig(sigR,K.revPk)=some true) ∧ o.hash160(K.revPk)=K.revHash.
NoRevocationSig(o,K):⟺∀sig, o.checkSig(sig,K.revPk)=some true
(for every byte string sig the oracle's answer is none or some false).
NoRevHashPreimage(o,K):⟺∀pk, o.hash160(pk)=K.revHash⇒pk=K.revPk.
When o=oS=oracleOf(c,S), o.hash160 is the concrete RIPEMD160∘SHA256 and o.checkSig is the concrete BIP143 ECDSA check on c.tx, c.nIn, c.amount with script code S described above. So NoRevHashPreimage(oS,K) asserts that K.revPk is the only byte string in the whole of Bytes whose concrete hash160 is K.revHash (it does not by itself assert that K.revPk hashes to K.revHash); and NoRevocationSig(oS,K) asserts that no byte string whatsoever passes the concrete signature check for K.revPk on this transaction, input, amount and script code.
The penalty witnesses (CScriptWitness order, script last)
- toLocalPenaltyWitness(K,sigR):=[sigR, [0x01], StoLocal(K)]; the executed stack (top first) is [[0x01],sigR].
- htlcPenaltyWitness(K,sigR,S):=[sigR, K.revPk, S]; the executed stack (top first) is [K.revPk,sigR].
The cheater-path predicates (on flags f, oracle o, keys K, HTLC h, and a top-first stack st)
DelayedSpend(f,o,K,b,sig):⟺castToBool(b)=false ∧ CsvPasses(f,o,K.toSelfDelay) ∧ o.checkSig(sig,K.localDelayedPk)=some true.
OfferedNonRevocation(f,o,K,h,st) is the disjunction of:
- (preimage path) ∃pre,sig: st=[pre,sig]∧o.hash160(pre)=K.revHash∧∣pre∣=32∧o.hash160(pre)=h.payHash∧o.checkSig(sig,K.remoteHtlcKey)=some true;
- (timeout path) ∃x,sigL,sigR′,dummy: st=[x,sigL,sigR′,dummy]∧o.hash160(x)=K.revHash∧∣x∣=32∧multisigLoop(o,[sigL,sigR′],[K.localHtlcKey,K.remoteHtlcKey])=some true∧(f.nulldummy=true⇒dummy=[]).
ReceivedNonRevocation(f,o,K,h,st) is the disjunction of:
- (success path) ∃pre,sigL,sigR′,dummy: st=[pre,sigL,sigR′,dummy]∧o.hash160(pre)=K.revHash∧∣pre∣=32∧o.hash160(pre)=h.payHash∧multisigLoop(o,[sigL,sigR′],[K.localHtlcKey,K.remoteHtlcKey])=some true∧(f.nulldummy=true⇒dummy=[]);
- (timeout path) ∃x,sig: st=[x,sig]∧o.hash160(x)=K.revHash∧∣x∣=32∧CltvPasses(f,o,h.expiry)∧o.checkSig(sig,K.remoteHtlcKey)=some true.
OfferedNonRevocationAnchors(f,o,K,h,st):⟺OfferedNonRevocation(f,o,K,h,st)∧CsvPasses(f,o,1), and ReceivedNonRevocationAnchors(f,o,K,h,st):⟺ReceivedNonRevocation(f,o,K,h,st)∧CsvPasses(f,o,1).
In these predicates the stack st is top-first; in the theorems it is instantiated with reverse(ws), i.e. the last element of the witness list ws is the first element of st.
Confirmed by the moderator at approval.