P2SH is correct
ProvedBitcoinVerify.p2sh_correctWith the P2SH flag, the output OP_HASH160 <h> OP_EQUAL spent with the push-only scriptSig and no witness, where is not a witness program, verifies iff and , run on the stack , leaves a true top. The side conditions are the push and stack limits (elements within 520 bytes, at most 1000 elements, scriptSig within 10,000 bytes).
import Definitions.Def_BitcoinVerify import Definitions.Def_BitcoinScript import Definitions.Def_BitcoinSer open BitcoinVerify BitcoinSer BitcoinSig open BitcoinScript hiding Bytes Byte
theorem BitcoinVerify.p2sh_correct (tap : TaprootHandler) (c : Ctx) (args : List Bytes) (redeem h : Bytes)
(hp : c.flags.p2sh = true) (hh : h.length = 20)
(hargs : ∀ a ∈ args, a.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE)
(hred : redeem.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE)
(hn : args.length + 2 ≤ BitcoinScript.MAX_STACK_SIZE)
(hsz : (BitcoinVerify.pushesBytes (args ++ [redeem])).length ≤ BitcoinVerify.MAX_SCRIPT_SIZE)
(hnw : BitcoinVerify.isWitnessProgram redeem = none) :
BitcoinVerify.verifyScript tap c (BitcoinVerify.pushesBytes (args ++ [redeem])) (BitcoinVerify.p2shBytes h) [] = true ↔
BitcoinSer.hash160 redeem = h ∧
∃ st, BitcoinVerify.evalBytes c .base args.reverse redeem = some st ∧ BitcoinVerify.topTrue st = true := by sorryRead-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: BitcoinVerify.p2sh_correct
Binders and hypothesis. Universally quantified: tap, a Taproot handler (an arbitrary function from a context, a list of byte strings and a byte string to a Boolean); , a verification context (flags c.flags, transaction c.tx, input index c.nIn, integer amount c.amount); args, a list of byte strings; redeem and , byte strings. Seven hypotheses:
hp:c.flags.p2sh = true;hh: ;hargs: every element ofargshas (MAX_SCRIPT_ELEMENT_SIZE);hred: ;hn: (MAX_STACK_SIZE), where is the number of elements;hsz: the byte length ofpushesBytes (args ++ [redeem])is at most (MAX_SCRIPT_SIZE);hnw:isWitnessProgram redeemis "nothing" (the redeem script is not of the form version-byte, length byte , program, with total length ).
Note that c.flags.witness is not constrained by any hypothesis; the statement is claimed for both values.
Conclusion. A biconditional. Let
Then
The witness passed to verifyScript is the empty list.
Unfolding the left-hand side. is the concatenation of the canonical pushes of (the elements of args in order) followed by the canonical push of redeem. verifyScript evaluates on the empty stack with version base: the size test uses hsz; each push is executed; a push fails if its data exceeds 520 bytes (excluded by hargs, hred) or if the stack would exceed 1000 elements (hn gives ). The resulting top-first stack is stackSig (redeem on top). It then evaluates on that stack: OP_HASH160 replaces the top by , the push adds , and OP_EQUAL replaces the two top elements by if and by the empty string otherwise; the top therefore casts to true iff the hashes agree, else verifyScript returns false. Next, the native branch: starts with , which is neither nor in , so isWitnessProgram π is nothing and native is none whatever the witness flag. With hp, and isPayToScriptHash π true (23 bytes, , , at the right indices, using hh), the P2SH branch runs: isPushOnly σ must hold (it does, since is a concatenation of canonical pushes); stackSig is non-empty with top redeem and remainder ; evalBytes c base (reverse args) redeem is run — failure gives false — and its result stack2 must have a true top. Then, if c.flags.witness is true, isWitnessProgram redeem is nothing by hnw, so the result is false iff the witness is non-empty, which it is not (the witness is []), hence true; if c.flags.witness is false the result is true directly.
Logical content. The theorem says that, under the listed hypotheses and with an empty witness, verifyScript accepts the push-only scriptSig and the P2SH output exactly when the redeem script's hash160 equals and the redeem script, run as a legacy (base) script on the stack consisting of the arguments with the last-pushed argument on top, succeeds with a stack whose top casts to true. evalBytes on the redeem script includes its own -byte test, byte-level parse, and the interpreter with flags scriptFlagsOf c.flags and the oracle built from sigFlagsOf c.flags, c.tx, c.nIn, c.amount, version base, and script code redeem.
What is not said. Nothing is claimed for a non-empty witness, for redeem that is a witness program, for c.flags.p2sh = false, for , for arguments or redeem scripts above 520 bytes, for more than 998 arguments, or for a scriptSig over 10000 bytes. The theorem does not say the redeem evaluation leaves exactly one element (no clean-stack condition). It does not say anything about the handler tap (it is never invoked on this path). It does not say that stackSig has a particular length beyond what the pushes produce. It does not assert the final stack of the whole verification, only the Boolean verdict.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.