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 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);
- c be a verification context: a record (c.flags,c.tx,c.nin,c.amount) where c.flags is a consensus flag record of seven booleans (p2sh,witness,taproot,dersig,cltv,csv,nulldummy), c.tx a transaction (record of version∈N, a list vin of inputs each with prevout, scriptSig, nSequence∈N, witness; a list vout of outputs each with nValue∈Z and scriptPubKey; and nLockTime∈N), c.nin∈N and c.amount∈Z;
- the hypothesis c.flags.witness=true hold;
- R (
rev), D (dpk) be byte strings, δ∈Z (delay), and ws a list of byte strings;
- ∣R∣≤520, ∣D∣≤520 and ∣δ∣<232 (∣δ∣ the absolute value as a natural; 520 is
MAX_SCRIPT_ELEMENT_SIZE).
Write W:=toLocalBytes(R,δ,D) for the serialized witness script and 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)),
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),
and sigFlagsOf(c.flags)=(dersig=c.flags.dersig, lowS=false, strictenc=false, witnessPubkeyType=false). On the right, b and sig range over byte strings, and ws=[sig,b] says the list ws has exactly two elements, sig first and b second (so the full witness list passed on the left is [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), where pushBytes(d) for ∣d∣<76 is the byte ∣d∣ followed by d; for ∣H∣=32 this is the 34-byte string [0x00,0x20]++H. The witness argument is ws followed by the single extra element W, and
W=[0x63]++pushBytes(R)++[0x67]++numBytes(δ)++[0xb2,0x75]++pushBytes(D)++[0x68,0xac],
with pushBytes(d) using the prefixes: byte ∣d∣ if ∣d∣<76; 0x4c,∣d∣ if ∣d∣≤255; 0x4d and 2-byte little-endian ∣d∣ if ∣d∣≤65535; 0x4e and 4-byte little-endian ∣d∣ otherwise; and numBytes(δ) the single byte δ+80 when δ=−1 or 1≤δ≤16, else pushBytes(serialize(δ)) (serialize(0)=[], so numBytes(0)=[0x00]; otherwise the little-endian magnitude of ∣δ∣ without trailing zeros, with an appended 0x80/0x00 byte when the top bit of the last byte is set, else the top bit set on the last byte when δ<0).
Unfolding verifyScript. verifyScript(tap,c,scriptSig,scriptPubKey,witness) is the boolean computed as follows (all stacks top-first).
- Evaluate scriptSig from the empty stack with signature version base via evalBytes (below); if it fails, return false; call the resulting stack S1.
- Evaluate scriptPubKey from S1 with version base; if it fails, return false; call the result S2.
- If topTrue(S2) is false (topTrue: false on the empty stack, else castToBool of the top), return false.
- Since c.flags.witness=true, test whether scriptPubKey is a witness program: its length is between 4 and 42; its first byte is 0 or lies in 0x51–0x60; and its second byte plus 2 equals its length. If so, with version =0 when the first byte is 0 and (first byte −0x50) otherwise, and program = the bytes after the first two: if scriptSig=[], return false; otherwise return 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 with version 0: if the program has length 32, take the last element of witness (return false if witness is empty) as the script E; if sha256(E)= program, return false; else return executeWitnessScriptV0(c,reverse of witness without its last element,E). (Length 20 gives the pay-to-witness-public-key-hash branch; any other length gives false.) Here, with witness=ws++[W], the last element is W and the remaining stack is the reverse of ws — so for ws=[sig,b] the execution stack is [b,sig] with b on top.
executeWitnessScriptV0(c,stack,E): if any element of stack has length >520, return false; otherwise evaluate E from stack with version witnessV0 via evalBytes; if that fails return false; else return (resulting stack has length exactly 1) ∧ topTrue of it.
evalBytes(c,v,stack,script): if the script is longer than 10000 bytes, fail; parse it into instructions (fail on a malformed push); then run the script interpreter evalScript with flags φ=scriptFlagsOf(c.flags) and oracle legacyOracle(sigFlagsOf(c.flags),c.tx,c.nin,c.amount,v,script) — for v=witnessV0 and script W this is exactly ω.
The interpreter evalScript. From the state (main stack, alt-stack [], condition stack vfExec=[], opCount=0), one step per instruction; some of the final main stack if all steps succeed and vfExec is empty at the end, else none. Push of x: fail if ∣x∣>520; push onto the main stack if all of vfExec is true (else unchanged); fail if ∣stack∣+∣alt∣>1000. Opcode c′: let e = all of vfExec true; if c′>0x60, fail if opCount+1>201 else increment; fail on the fifteen disabled opcodes; if e or 0x63≤c′≤0x68, apply execOp with flag e then the size check; else unchanged apart from the size check. execOp for the opcodes of W: 0x4f/0x51–0x60 push serialize(c′−80); OP_IF 0x63 with e pops the top x (fail if empty) and pushes castToBool(x) onto vfExec, without e pushes false onto vfExec; OP_ELSE 0x67 negates the head of vfExec (fail if empty); OP_ENDIF 0x68 pops it (fail if empty); OP_CHECKSEQUENCEVERIFY 0xb2: unchanged if φ.csv=false, else fail on empty stack, let x be the top (kept), fail if ∣x∣>5, let n be its signed little-endian value, fail if n<0, unchanged if (toNat(n)&231)=0, else fail if ω.checkSequence(n)=false, else unchanged; OP_DROP 0x75 pops (fail if empty); OP_CHECKSIG 0xac needs pk::sg::rest, then ω.checkSig(sg,pk): none fails, someok gives boolBytes(ok)::rest ([0x01] for true, [] for false). Parsing of a byte script: byte ≤0x4e is a push whose length is the byte itself (<0x4c), the next byte (0x4c), the next two little-endian bytes (0x4d) or the next four (0x4e), failing if too few bytes follow; any byte >0x4e is an opcode.
The concrete oracle ω. Its five hash components are the preamble's hash implementations; its lock-time component is the transaction lock-time check; and:
- ω.checkSequence(s), for an integer s: let σ be the nSequence of input c.nin of c.tx (or 0 if out of range). If c.tx.version<2, false; else if σ&231=0, false; else with M=222∣0xffff, τ=σ&M, ς=toNat(s)&M: false unless (τ<222∧ς<222) or (222≤τ∧222≤ς); then false if τ<ς; else true.
- ω.checkSig(sg,pk), with signature flags (dersig=c.flags.dersig,false,false,false) and version witnessV0 (so the script code is W itself, with no signature deletion): the result is none exactly when c.flags.dersig=true, sg=[] and sg fails the strict-DER-with-hash-type encoding test (length in [9,73], tag 0x30, consistent length bytes, two 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 of the ECDSA verdict: false if pk is empty or its first byte does not announce its length (0x02/0x03↦33, 0x04/0x06/0x07↦65, anything else ↦0); false if sg=[]; otherwise, with hash type = the last byte of sg and sg′ = sg without that byte, compute the BIP143-style digest of (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 W, 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 if c.amount<0; else return the preamble's secp256k1 ECDSA verification of sg′ (lax-DER parsed, S normalized) against that digest reduced modulo the group order under the key pk (parsed as a compressed, uncompressed or hybrid point).
The right-hand side's SpendCondition. SpendCondition(φ,ω,R,δ,D,b,sig) is
(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)), and castToBool(x): false for []; for [y] true iff y=0∧y=0x80; for y::rest with rest=[], true iff y=0∨castToBool(rest).
What is quantified and what is not. The handler tap is arbitrary and the equivalence must hold for every one. The context c is arbitrary except that its witness flag is on; the p2sh, taproot, dersig, cltv, csv and nulldummy flags are unconstrained, as are c.tx (which may have no inputs), c.nin (possibly out of range) and c.amount (possibly negative). The witness prefix ws is an arbitrary list on the left; the right-hand side forces it to have exactly two elements, each of length at most 520. The first conjunct on the right, castToBool(H)=true, is a condition on the digest of the witness script itself.
Confirmed by the moderator at approval.