Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The to_local output under VerifyScript: a witness spends it iff it is a signature, a selector and the script, and the selected branch holds

Proved
BitcoinToLocal.p2wsh_spend

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

bip112bitcoinbitcoin-mainnetbolt3csvformal-verificationlightningp2wshscript

For a context with the WITNESS flag, keys of at most 520 bytes, ∣delay∣<232|{\rm delay}| < 2^{32}∣delay∣<232 and any witness ws: verifyScript tap c [] (witnessBytes 0 (sha256 (toLocalBytes rev delay dpk))) (ws ++ [toLocalBytes rev delay dpk]) = true if and only if castToBool (sha256 (toLocalBytes rev delay dpk)) = true and there are bbb, sig with ws = [sig, b], both of at most 520 bytes, and SpendCondition for the flags of ccc and the signature mission's legacyOracle of ccc's transaction, input, amount and SigVersion::WITNESS_V0 on the script. So the output is spent only by the revocation key, or by the delayed key after the sequence check; the first conjunct is VerifyScript's requirement that the program be a true value.

Preamble
import Definitions.Def_BitcoinToLocal
import Definitions.Def_BitcoinVerify
import Definitions.Def_BitcoinScript
import Definitions.Def_BitcoinSer
import Definitions.Def_BitcoinSig
open BitcoinToLocal BitcoinSig BitcoinVerify
open BitcoinScript
Formal statement
theorem BitcoinToLocal.p2wsh_spend (tap : BitcoinVerify.TaprootHandler) (c : BitcoinVerify.Ctx)
    (hf : c.flags.witness = true) (rev dpk : Bytes) (delay : Int) (ws : List Bytes)
    (hrev : rev.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE) (hdpk : dpk.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE)
    (hd : delay.natAbs < 2 ^ 32) :
    BitcoinVerify.verifyScript tap c [] (BitcoinVerify.witnessBytes 0 (BitcoinSer.sha256 (BitcoinToLocal.toLocalBytes rev delay dpk)))
        (ws ++ [BitcoinToLocal.toLocalBytes rev delay dpk]) = true ↔
      BitcoinScript.castToBool (BitcoinSer.sha256 (BitcoinToLocal.toLocalBytes rev delay dpk)) = true ∧
      ∃ b sig, ws = [sig, b] ∧ sig.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE ∧ b.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE ∧
        BitcoinToLocal.SpendCondition (BitcoinVerify.scriptFlagsOf c.flags)
          (BitcoinSig.legacyOracle (BitcoinVerify.sigFlagsOf c.flags) c.tx c.nIn c.amount .witnessV0 (BitcoinToLocal.toLocalBytes rev delay dpk))
          rev delay dpk b sig := by sorry
Source
BOLT #3, Bitcoin Transaction and Script Formats, 'to_local Output': https://github.com/lightning/bolts/blob/master/03-transactions.md#to_local-output; Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: EvalScript (OP_IF/OP_ELSE/OP_ENDIF, OP_CHECKSEQUENCEVERIFY, OP_CHECKSIG), GenericTransactionSignatureChecker::CheckSequence, ExecuteWitnessScript, VerifyWitnessProgram (src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp); CScript::operator<<(int64_t) (script/script.h); BIP68, BIP112, BIP141
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: BitcoinToLocal.p2wsh_spend

Statement. Let

  • tap\mathrm{tap}tap be a Taproot handler: an arbitrary function taking a verification context, a list of byte strings and a byte string to a boolean (nothing is assumed about it);
  • ccc be a verification context: a record (c.flags,c.tx,c.nin,c.amount)(c.\mathrm{flags}, c.\mathrm{tx}, c.n_{\mathrm{in}}, c.\mathrm{amount})(c.flags,c.tx,c.nin​,c.amount) where c.flagsc.\mathrm{flags}c.flags is a consensus flag record of seven booleans (p2sh,witness,taproot,dersig,cltv,csv,nulldummy)(\mathrm{p2sh}, \mathrm{witness}, \mathrm{taproot}, \mathrm{dersig}, \mathrm{cltv}, \mathrm{csv}, \mathrm{nulldummy})(p2sh,witness,taproot,dersig,cltv,csv,nulldummy), c.txc.\mathrm{tx}c.tx a transaction (record of version∈N\mathrm{version} \in \mathbb{N}version∈N, a list vin\mathrm{vin}vin of inputs each with prevout\mathrm{prevout}prevout, scriptSig\mathrm{scriptSig}scriptSig, nSequence∈N\mathrm{nSequence} \in \mathbb{N}nSequence∈N, witness\mathrm{witness}witness; a list vout\mathrm{vout}vout of outputs each with nValue∈Z\mathrm{nValue} \in \mathbb{Z}nValue∈Z and scriptPubKey\mathrm{scriptPubKey}scriptPubKey; and nLockTime∈N\mathrm{nLockTime} \in \mathbb{N}nLockTime∈N), c.nin∈Nc.n_{\mathrm{in}} \in \mathbb{N}c.nin​∈N and c.amount∈Zc.\mathrm{amount} \in \mathbb{Z}c.amount∈Z;
  • the hypothesis c.flags.witness=truec.\mathrm{flags}.\mathrm{witness} = \mathrm{true}c.flags.witness=true hold;
  • RRR (rev), DDD (dpk) be byte strings, δ∈Z\delta \in \mathbb{Z}δ∈Z (delay), and ws\mathrm{ws}ws a list of byte strings;
  • ∣R∣≤520|R| \le 520∣R∣≤520, ∣D∣≤520|D| \le 520∣D∣≤520 and ∣δ∣<232|\delta| < 2^{32}∣δ∣<232 (∣δ∣|\delta|∣δ∣ the absolute value as a natural; 520520520 is MAX_SCRIPT_ELEMENT_SIZE).

Write W:=toLocalBytes(R,δ,D)W := \mathrm{toLocalBytes}(R, \delta, D)W:=toLocalBytes(R,δ,D) for the serialized witness script and H:=sha256(W)H := \mathrm{sha256}(W)H:=sha256(W) for its digest under the preamble's SHA-256 implementation (a 32-byte string: eight big-endian 32-bit words). Then

verifyScript(tap, c, [ ], witnessBytes(0,H), ws+ ⁣ ⁣+[W])=true  ⟺  castToBool(H)=true ∧ ∃ b sig. (ws=[sig,b] ∧ ∣sig∣≤520 ∧ ∣b∣≤520 ∧ SpendCondition(φ, ω, R,δ,D,b,sig)),\mathrm{verifyScript}\big(\mathrm{tap},\ c,\ [\,],\ \mathrm{witnessBytes}(0, H),\ \mathrm{ws} \mathbin{+\!\!+} [W]\big) = \mathrm{true} \iff \mathrm{castToBool}(H) = \mathrm{true}\ \land\ \exists\, b\ \mathrm{sig}.\ \Big( \mathrm{ws} = [\mathrm{sig}, b]\ \land\ |\mathrm{sig}| \le 520\ \land\ |b| \le 520\ \land\ \mathrm{SpendCondition}\big(\varphi,\ \omega,\ R, \delta, D, b, \mathrm{sig}\big) \Big),verifyScript(tap, c, [], witnessBytes(0,H), ws++[W])=true⟺castToBool(H)=true ∧ ∃b sig. (ws=[sig,b] ∧ ∣sig∣≤520 ∧ ∣b∣≤520 ∧ SpendCondition(φ, ω, R,δ,D,b,sig)),

where

φ:=scriptFlagsOf(c.flags)=(c.flags.cltv, c.flags.csv, c.flags.nulldummy),ω:=legacyOracle(sigFlagsOf(c.flags), c.tx, c.nin, c.amount, witnessV0, W),\varphi := \mathrm{scriptFlagsOf}(c.\mathrm{flags}) = (c.\mathrm{flags}.\mathrm{cltv},\ c.\mathrm{flags}.\mathrm{csv},\ c.\mathrm{flags}.\mathrm{nulldummy}), \qquad \omega := \mathrm{legacyOracle}\big(\mathrm{sigFlagsOf}(c.\mathrm{flags}),\ c.\mathrm{tx},\ c.n_{\mathrm{in}},\ c.\mathrm{amount},\ \mathrm{witnessV0},\ W\big),φ:=scriptFlagsOf(c.flags)=(c.flags.cltv, c.flags.csv, c.flags.nulldummy),ω:=legacyOracle(sigFlagsOf(c.flags), c.tx, c.nin​, c.amount, witnessV0, W),

and sigFlagsOf(c.flags)=(dersig=c.flags.dersig, lowS=false, strictenc=false, witnessPubkeyType=false)\mathrm{sigFlagsOf}(c.\mathrm{flags}) = (\mathrm{dersig} = c.\mathrm{flags}.\mathrm{dersig},\ \mathrm{lowS} = \mathrm{false},\ \mathrm{strictenc} = \mathrm{false},\ \mathrm{witnessPubkeyType} = \mathrm{false})sigFlagsOf(c.flags)=(dersig=c.flags.dersig, lowS=false, strictenc=false, witnessPubkeyType=false). On the right, bbb and sig\mathrm{sig}sig range over byte strings, and ws=[sig,b]\mathrm{ws} = [\mathrm{sig}, b]ws=[sig,b] says the list ws\mathrm{ws}ws has exactly two elements, sig\mathrm{sig}sig first and bbb second (so the full witness list passed on the left is [sig,b,W][\mathrm{sig}, b, W][sig,b,W]).

The inputs on the left. The script-signature argument is the empty byte string [ ][\,][]. The output script is witnessBytes(0,H)=0x00::pushBytes(H)\mathrm{witnessBytes}(0, H) = \mathtt{0x00} :: \mathrm{pushBytes}(H)witnessBytes(0,H)=0x00::pushBytes(H), where pushBytes(d)\mathrm{pushBytes}(d)pushBytes(d) for ∣d∣<76|d| < 76∣d∣<76 is the byte ∣d∣|d|∣d∣ followed by ddd; for ∣H∣=32|H| = 32∣H∣=32 this is the 34-byte string [0x00,0x20]+ ⁣ ⁣+H[\mathtt{0x00}, \mathtt{0x20}] \mathbin{+\!\!+} H[0x00,0x20]++H. The witness argument is ws\mathrm{ws}ws followed by the single extra element WWW, and

W=[0x63]+ ⁣ ⁣+pushBytes(R)+ ⁣ ⁣+[0x67]+ ⁣ ⁣+numBytes(δ)+ ⁣ ⁣+[0xb2,0x75]+ ⁣ ⁣+pushBytes(D)+ ⁣ ⁣+[0x68,0xac],W = [\mathtt{0x63}] \mathbin{+\!\!+} \mathrm{pushBytes}(R) \mathbin{+\!\!+} [\mathtt{0x67}] \mathbin{+\!\!+} \mathrm{numBytes}(\delta) \mathbin{+\!\!+} [\mathtt{0xb2}, \mathtt{0x75}] \mathbin{+\!\!+} \mathrm{pushBytes}(D) \mathbin{+\!\!+} [\mathtt{0x68}, \mathtt{0xac}],W=[0x63]++pushBytes(R)++[0x67]++numBytes(δ)++[0xb2,0x75]++pushBytes(D)++[0x68,0xac],

with pushBytes(d)\mathrm{pushBytes}(d)pushBytes(d) using the prefixes: byte ∣d∣|d|∣d∣ if ∣d∣<76|d| < 76∣d∣<76; 0x4c,∣d∣\mathtt{0x4c}, |d|0x4c,∣d∣ if ∣d∣≤255|d| \le 255∣d∣≤255; 0x4d\mathtt{0x4d}0x4d and 2-byte little-endian ∣d∣|d|∣d∣ if ∣d∣≤65535|d| \le 65535∣d∣≤65535; 0x4e\mathtt{0x4e}0x4e and 4-byte little-endian ∣d∣|d|∣d∣ otherwise; and numBytes(δ)\mathrm{numBytes}(\delta)numBytes(δ) the single byte δ+80\delta + 80δ+80 when δ=−1\delta = -1δ=−1 or 1≤δ≤161 \le \delta \le 161≤δ≤16, else pushBytes(serialize(δ))\mathrm{pushBytes}(\mathrm{serialize}(\delta))pushBytes(serialize(δ)) (serialize(0)=[ ]\mathrm{serialize}(0) = [\,]serialize(0)=[], so numBytes(0)=[0x00]\mathrm{numBytes}(0) = [\mathtt{0x00}]numBytes(0)=[0x00]; otherwise the little-endian magnitude of ∣δ∣|\delta|∣δ∣ without trailing zeros, with an appended 0x80\mathtt{0x80}0x80/0x00\mathtt{0x00}0x00 byte when the top bit of the last byte is set, else the top bit set on the last byte when δ<0\delta<0δ<0).

Unfolding verifyScript. verifyScript(tap,c,scriptSig,scriptPubKey,witness)\mathrm{verifyScript}(\mathrm{tap}, c, \mathrm{scriptSig}, \mathrm{scriptPubKey}, \mathrm{witness})verifyScript(tap,c,scriptSig,scriptPubKey,witness) is the boolean computed as follows (all stacks top-first).

  1. Evaluate scriptSig\mathrm{scriptSig}scriptSig from the empty stack with signature version base\mathrm{base}base via evalBytes\mathrm{evalBytes}evalBytes (below); if it fails, return false\mathrm{false}false; call the resulting stack S1S_1S1​.
  2. Evaluate scriptPubKey\mathrm{scriptPubKey}scriptPubKey from S1S_1S1​ with version base\mathrm{base}base; if it fails, return false\mathrm{false}false; call the result S2S_2S2​.
  3. If topTrue(S2)\mathrm{topTrue}(S_2)topTrue(S2​) is false (topTrue\mathrm{topTrue}topTrue: false on the empty stack, else castToBool\mathrm{castToBool}castToBool of the top), return false\mathrm{false}false.
  4. Since c.flags.witness=truec.\mathrm{flags}.\mathrm{witness} = \mathrm{true}c.flags.witness=true, test whether scriptPubKey\mathrm{scriptPubKey}scriptPubKey is a witness program: its length is between 444 and 424242; its first byte is 000 or lies in 0x51\mathtt{0x51}0x51–0x60\mathtt{0x60}0x60; and its second byte plus 222 equals its length. If so, with version =0= 0=0 when the first byte is 000 and (first byte −0x50- \mathtt{0x50}−0x50) otherwise, and program === the bytes after the first two: if scriptSig≠[ ]\mathrm{scriptSig} \ne [\,]scriptSig=[], return false\mathrm{false}false; otherwise return verifyWitnessProgram(tap,c,witness,version,program,false)\mathrm{verifyWitnessProgram}(\mathrm{tap}, c, \mathrm{witness}, \text{version}, \text{program}, \mathrm{false})verifyWitnessProgram(tap,c,witness,version,program,false). (If it is not a witness program, the pay-to-script-hash and "no witness allowed" branches follow; they are not reached when the test succeeds.)

verifyWitnessProgram\mathrm{verifyWitnessProgram}verifyWitnessProgram with version 000: if the program has length 323232, take the last element of witness\mathrm{witness}witness (return false\mathrm{false}false if witness\mathrm{witness}witness is empty) as the script EEE; if sha256(E)≠\mathrm{sha256}(E) \nesha256(E)= program, return false\mathrm{false}false; else return executeWitnessScriptV0(c,reverse of witness without its last element,E)\mathrm{executeWitnessScriptV0}(c, \text{reverse of } \mathrm{witness} \text{ without its last element}, E)executeWitnessScriptV0(c,reverse of witness without its last element,E). (Length 202020 gives the pay-to-witness-public-key-hash branch; any other length gives false\mathrm{false}false.) Here, with witness=ws+ ⁣ ⁣+[W]\mathrm{witness} = \mathrm{ws} \mathbin{+\!\!+} [W]witness=ws++[W], the last element is WWW and the remaining stack is the reverse of ws\mathrm{ws}ws — so for ws=[sig,b]\mathrm{ws} = [\mathrm{sig}, b]ws=[sig,b] the execution stack is [b,sig][b, \mathrm{sig}][b,sig] with bbb on top.

executeWitnessScriptV0(c,stack,E)\mathrm{executeWitnessScriptV0}(c, \mathrm{stack}, E)executeWitnessScriptV0(c,stack,E): if any element of stack\mathrm{stack}stack has length >520> 520>520, return false\mathrm{false}false; otherwise evaluate EEE from stack\mathrm{stack}stack with version witnessV0\mathrm{witnessV0}witnessV0 via evalBytes\mathrm{evalBytes}evalBytes; if that fails return false\mathrm{false}false; else return (resulting stack has length exactly 111) ∧\land∧ topTrue\mathrm{topTrue}topTrue of it.

evalBytes(c,v,stack,script)\mathrm{evalBytes}(c, v, \mathrm{stack}, \text{script})evalBytes(c,v,stack,script): if the script is longer than 100001000010000 bytes, fail; parse it into instructions (fail on a malformed push); then run the script interpreter evalScript\mathrm{evalScript}evalScript with flags φ=scriptFlagsOf(c.flags)\varphi = \mathrm{scriptFlagsOf}(c.\mathrm{flags})φ=scriptFlagsOf(c.flags) and oracle legacyOracle(sigFlagsOf(c.flags),c.tx,c.nin,c.amount,v,script)\mathrm{legacyOracle}(\mathrm{sigFlagsOf}(c.\mathrm{flags}), c.\mathrm{tx}, c.n_{\mathrm{in}}, c.\mathrm{amount}, v, \text{script})legacyOracle(sigFlagsOf(c.flags),c.tx,c.nin​,c.amount,v,script) — for v=witnessV0v = \mathrm{witnessV0}v=witnessV0 and script WWW this is exactly ω\omegaω.

The interpreter evalScript. From the state (main stack, alt-stack [ ][\,][], condition stack vfExec=[ ]\mathrm{vfExec} = [\,]vfExec=[], opCount=0\mathrm{opCount} = 0opCount=0), one step per instruction; some\mathrm{some}some of the final main stack if all steps succeed and vfExec\mathrm{vfExec}vfExec is empty at the end, else none\mathrm{none}none. Push of xxx: fail if ∣x∣>520|x| > 520∣x∣>520; push onto the main stack if all of vfExec\mathrm{vfExec}vfExec is true (else unchanged); fail if ∣stack∣+∣alt∣>1000|\text{stack}| + |\text{alt}| > 1000∣stack∣+∣alt∣>1000. Opcode c′c'c′: let eee = all of vfExec\mathrm{vfExec}vfExec true; if c′>0x60c' > \mathtt{0x60}c′>0x60, fail if opCount+1>201\mathrm{opCount} + 1 > 201opCount+1>201 else increment; fail on the fifteen disabled opcodes; if eee or 0x63≤c′≤0x68\mathtt{0x63} \le c' \le \mathtt{0x68}0x63≤c′≤0x68, apply execOp\mathrm{execOp}execOp with flag eee then the size check; else unchanged apart from the size check. execOp\mathrm{execOp}execOp for the opcodes of WWW: 0x4f\mathtt{0x4f}0x4f/0x51\mathtt{0x51}0x51–0x60\mathtt{0x60}0x60 push serialize(c′−80)\mathrm{serialize}(c' - 80)serialize(c′−80); OP_IF 0x63\mathtt{0x63}0x63 with eee pops the top xxx (fail if empty) and pushes castToBool(x)\mathrm{castToBool}(x)castToBool(x) onto vfExec\mathrm{vfExec}vfExec, without eee pushes false\mathrm{false}false onto vfExec\mathrm{vfExec}vfExec; OP_ELSE 0x67\mathtt{0x67}0x67 negates the head of vfExec\mathrm{vfExec}vfExec (fail if empty); OP_ENDIF 0x68\mathtt{0x68}0x68 pops it (fail if empty); OP_CHECKSEQUENCEVERIFY 0xb2\mathtt{0xb2}0xb2: unchanged if φ.csv=false\varphi.\mathrm{csv} = \mathrm{false}φ.csv=false, else fail on empty stack, let xxx be the top (kept), fail if ∣x∣>5|x| > 5∣x∣>5, let nnn be its signed little-endian value, fail if n<0n < 0n<0, unchanged if (toNat⁡(n)&231)≠0(\operatorname{toNat}(n) \mathbin{\&} 2^{31}) \ne 0(toNat(n)&231)=0, else fail if ω.checkSequence(n)=false\omega.\mathrm{checkSequence}(n) = \mathrm{false}ω.checkSequence(n)=false, else unchanged; OP_DROP 0x75\mathtt{0x75}0x75 pops (fail if empty); OP_CHECKSIG 0xac\mathtt{0xac}0xac needs pk::sg::rest\mathrm{pk} :: \mathrm{sg} :: \mathrm{rest}pk::sg::rest, then ω.checkSig(sg,pk)\omega.\mathrm{checkSig}(\mathrm{sg}, \mathrm{pk})ω.checkSig(sg,pk): none\mathrm{none}none fails, some ok\mathrm{some}\,\mathrm{ok}someok gives boolBytes(ok)::rest\mathrm{boolBytes}(\mathrm{ok}) :: \mathrm{rest}boolBytes(ok)::rest ([0x01][\mathtt{0x01}][0x01] for true, [ ][\,][] for false). Parsing of a byte script: byte ≤0x4e\le \mathtt{0x4e}≤0x4e is a push whose length is the byte itself (<0x4c< \mathtt{0x4c}<0x4c), the next byte (0x4c\mathtt{0x4c}0x4c), the next two little-endian bytes (0x4d\mathtt{0x4d}0x4d) or the next four (0x4e\mathtt{0x4e}0x4e), failing if too few bytes follow; any byte >0x4e> \mathtt{0x4e}>0x4e is an opcode.

The concrete oracle ω\omegaω. Its five hash components are the preamble's hash implementations; its lock-time component is the transaction lock-time check; and:

  • ω.checkSequence(s)\omega.\mathrm{checkSequence}(s)ω.checkSequence(s), for an integer sss: let σ\sigmaσ be the nSequence\mathrm{nSequence}nSequence of input c.ninc.n_{\mathrm{in}}c.nin​ of c.txc.\mathrm{tx}c.tx (or 000 if out of range). If c.tx.version<2c.\mathrm{tx}.\mathrm{version} < 2c.tx.version<2, false\mathrm{false}false; else if σ&231≠0\sigma \mathbin{\&} 2^{31} \ne 0σ&231=0, false\mathrm{false}false; else with M=222∣0xffffM = 2^{22} \mid \mathtt{0xffff}M=222∣0xffff, τ=σ&M\tau = \sigma \mathbin{\&} Mτ=σ&M, ς=toNat⁡(s)&M\varsigma = \operatorname{toNat}(s) \mathbin{\&} Mς=toNat(s)&M: false\mathrm{false}false unless (τ<222∧ς<222\tau < 2^{22} \land \varsigma < 2^{22}τ<222∧ς<222) or (222≤τ∧222≤ς2^{22} \le \tau \land 2^{22} \le \varsigma222≤τ∧222≤ς); then false\mathrm{false}false if τ<ς\tau < \varsigmaτ<ς; else true\mathrm{true}true.
  • ω.checkSig(sg,pk)\omega.\mathrm{checkSig}(\mathrm{sg}, \mathrm{pk})ω.checkSig(sg,pk), with signature flags (dersig=c.flags.dersig,false,false,false)(\mathrm{dersig} = c.\mathrm{flags}.\mathrm{dersig}, \mathrm{false}, \mathrm{false}, \mathrm{false})(dersig=c.flags.dersig,false,false,false) and version witnessV0\mathrm{witnessV0}witnessV0 (so the script code is WWW itself, with no signature deletion): the result is none\mathrm{none}none exactly when c.flags.dersig=truec.\mathrm{flags}.\mathrm{dersig} = \mathrm{true}c.flags.dersig=true, sg≠[ ]\mathrm{sg} \ne [\,]sg=[] and sg\mathrm{sg}sg fails the strict-DER-with-hash-type encoding test (length in [9,73][9, 73][9,73], tag 0x30\mathtt{0x30}0x30, consistent length bytes, two 0x02\mathtt{0x02}0x02 integers each non-empty, non-negative and minimally padded, hash-type byte last); the public-key encoding test passes unconditionally since the strict-encoding and witness-pubkey-type flags are false. Otherwise it is some\mathrm{some}some of the ECDSA verdict: false\mathrm{false}false if pk\mathrm{pk}pk is empty or its first byte does not announce its length (0x02/0x03↦33\mathtt{0x02}/\mathtt{0x03} \mapsto 330x02/0x03↦33, 0x04/0x06/0x07↦65\mathtt{0x04}/\mathtt{0x06}/\mathtt{0x07} \mapsto 650x04/0x06/0x07↦65, anything else ↦0\mapsto 0↦0); false\mathrm{false}false if sg=[ ]\mathrm{sg} = [\,]sg=[]; otherwise, with hash type === the last byte of sg\mathrm{sg}sg and sg′\mathrm{sg}'sg′ === sg\mathrm{sg}sg without that byte, compute the BIP143-style digest of (c.tx,c.nin,W,hash type,c.amount)(c.\mathrm{tx}, c.n_{\mathrm{in}}, W, \text{hash type}, c.\mathrm{amount})(c.tx,c.nin​,W,hash type,c.amount) as defined in the preamble (double SHA-256 over version, hashed prevouts, hashed sequences, this input's outpoint, the length-prefixed script code WWW, the 8-byte amount, this input's sequence, hashed outputs, lock time and 4-byte hash type, with the usual ANYONECANPAY/SINGLE/NONE variations); return false\mathrm{false}false if c.amount<0c.\mathrm{amount} < 0c.amount<0; else return the preamble's secp256k1 ECDSA verification of sg′\mathrm{sg}'sg′ (lax-DER parsed, SSS normalized) against that digest reduced modulo the group order under the key pk\mathrm{pk}pk (parsed as a compressed, uncompressed or hybrid point).

The right-hand side's SpendCondition. SpendCondition(φ,ω,R,δ,D,b,sig)\mathrm{SpendCondition}(\varphi, \omega, R, \delta, D, b, \mathrm{sig})SpendCondition(φ,ω,R,δ,D,b,sig) is

(castToBool(b)=true∧ω.checkSig(sig,R)=some true) ∨ (castToBool(b)=false∧CsvPasses(φ,ω,δ)∧ω.checkSig(sig,D)=some true),\big(\mathrm{castToBool}(b) = \mathrm{true} \land \omega.\mathrm{checkSig}(\mathrm{sig}, R) = \mathrm{some}\,\mathrm{true}\big)\ \lor\ \big(\mathrm{castToBool}(b) = \mathrm{false} \land \mathrm{CsvPasses}(\varphi, \omega, \delta) \land \omega.\mathrm{checkSig}(\mathrm{sig}, D) = \mathrm{some}\,\mathrm{true}\big),(castToBool(b)=true∧ω.checkSig(sig,R)=sometrue) ∨ (castToBool(b)=false∧CsvPasses(φ,ω,δ)∧ω.checkSig(sig,D)=sometrue),

with CsvPasses(φ,ω,δ)≡φ.csv=false∨(0≤δ∧((toNat⁡(δ)&231)≠0∨ω.checkSequence(δ)=true))\mathrm{CsvPasses}(\varphi, \omega, \delta) \equiv \varphi.\mathrm{csv} = \mathrm{false} \lor \big(0 \le \delta \land ((\operatorname{toNat}(\delta) \mathbin{\&} 2^{31}) \ne 0 \lor \omega.\mathrm{checkSequence}(\delta) = \mathrm{true})\big)CsvPasses(φ,ω,δ)≡φ.csv=false∨(0≤δ∧((toNat(δ)&231)=0∨ω.checkSequence(δ)=true)), and castToBool(x)\mathrm{castToBool}(x)castToBool(x): false for [ ][\,][]; for [y][y][y] true iff y≠0∧y≠0x80y \ne 0 \land y \ne \mathtt{0x80}y=0∧y=0x80; for y::resty :: \mathrm{rest}y::rest with rest≠[ ]\mathrm{rest} \ne [\,]rest=[], true iff y≠0∨castToBool(rest)y \ne 0 \lor \mathrm{castToBool}(\mathrm{rest})y=0∨castToBool(rest).

What is quantified and what is not. The handler tap\mathrm{tap}tap is arbitrary and the equivalence must hold for every one. The context ccc is arbitrary except that its witness\mathrm{witness}witness flag is on; the p2sh\mathrm{p2sh}p2sh, taproot\mathrm{taproot}taproot, dersig\mathrm{dersig}dersig, cltv\mathrm{cltv}cltv, csv\mathrm{csv}csv and nulldummy\mathrm{nulldummy}nulldummy flags are unconstrained, as are c.txc.\mathrm{tx}c.tx (which may have no inputs), c.ninc.n_{\mathrm{in}}c.nin​ (possibly out of range) and c.amountc.\mathrm{amount}c.amount (possibly negative). The witness prefix ws\mathrm{ws}ws is an arbitrary list on the left; the right-hand side forces it to have exactly two elements, each of length at most 520520520. The first conjunct on the right, castToBool(H)=true\mathrm{castToBool}(H) = \mathrm{true}castToBool(H)=true, is a condition on the digest of the witness script itself.

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