Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

P2SH is correct

Proved
BitcoinVerify.p2sh_correct

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

bip141bitcoinbitcoin-mainnetconsensusformal-verificationp2shsegwitverifyscript

With the P2SH flag, the output OP_HASH160 <h> OP_EQUAL spent with the push-only scriptSig ⟨a1⟩⋯⟨ak⟩⟨redeem⟩\langle a_1\rangle \cdots \langle a_k\rangle \langle redeem\rangle⟨a1​⟩⋯⟨ak​⟩⟨redeem⟩ and no witness, where redeemredeemredeem is not a witness program, verifies iff Hash160(redeem)=h\mathrm{Hash160}(redeem) = hHash160(redeem)=h and redeemredeemredeem, run on the stack [ak,…,a1][a_k, \dots, a_1][ak​,…,a1​], 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).

Preamble
import Definitions.Def_BitcoinVerify
import Definitions.Def_BitcoinScript
import Definitions.Def_BitcoinSer
open BitcoinVerify BitcoinSer BitcoinSig
open BitcoinScript hiding Bytes Byte
Formal statement
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 sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: VerifyScript, VerifyWitnessProgram, ExecuteWitnessScript, src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp; CScript::IsPayToScriptHash, IsWitnessProgram, IsPushOnly, IsPayToAnchor, DecodeOP_N, src/script/script.cpp and script.h; MAX_SCRIPT_SIZE, WITNESS_V0_SCRIPTHASH_SIZE, WITNESS_V0_KEYHASH_SIZE, WITNESS_V1_TAPROOT_SIZE; BIP16, BIP141, BIP143 at bips revision 100ba872c808946155b1eeb8c4568058f2eea0a7
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: 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); ccc, 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 hhh, byte strings. Seven hypotheses:

  • hp: c.flags.p2sh = true;
  • hh: ∣h∣=20|h| = 20∣h∣=20;
  • hargs: every element aaa of args has ∣a∣≤520|a| \le 520∣a∣≤520 (MAX_SCRIPT_ELEMENT_SIZE);
  • hred: ∣redeem∣≤520|\mathrm{redeem}| \le 520∣redeem∣≤520;
  • hn: ∣args∣+2≤1000|\mathrm{args}| + 2 \le 1000∣args∣+2≤1000 (MAX_STACK_SIZE), where ∣args∣|\mathrm{args}|∣args∣ is the number of elements;
  • hsz: the byte length of pushesBytes (args ++ [redeem]) is at most 100001000010000 (MAX_SCRIPT_SIZE);
  • hnw: isWitnessProgram redeem is "nothing" (the redeem script is not of the form version-byte, length byte 2..402..402..40, program, with total length 4..424..424..42).

Note that c.flags.witness is not constrained by any hypothesis; the statement is claimed for both values.

Conclusion. A biconditional. Let

σ=pushesBytes(args ∥ [redeem]),π=p2shBytes(h)=0xa9 0x14 h 0x87.\sigma = \mathrm{pushesBytes}(\mathrm{args} \,\|\, [\mathrm{redeem}]), \qquad \pi = \mathrm{p2shBytes}(h) = \mathtt{0xa9}\ \mathtt{0x14}\ h\ \mathtt{0x87}.σ=pushesBytes(args∥[redeem]),π=p2shBytes(h)=0xa9 0x14 h 0x87.

Then

verifyScript(tap,c,σ,π,[])=true  ⟺  hash160(redeem)=h ∧ ∃ st, evalBytes(c,base,reverse(args),redeem)=some(st) ∧ topTrue(st)=true.\mathrm{verifyScript}(\mathrm{tap}, c, \sigma, \pi, []) = \mathrm{true} \iff \mathrm{hash160}(\mathrm{redeem}) = h \ \wedge\ \exists\, st,\ \mathrm{evalBytes}(c, \mathrm{base}, \mathrm{reverse}(\mathrm{args}), \mathrm{redeem}) = \mathrm{some}(st) \ \wedge\ \mathrm{topTrue}(st) = \mathrm{true}.verifyScript(tap,c,σ,π,[])=true⟺hash160(redeem)=h ∧ ∃st, evalBytes(c,base,reverse(args),redeem)=some(st) ∧ topTrue(st)=true.

The witness passed to verifyScript is the empty list.

Unfolding the left-hand side. σ\sigmaσ is the concatenation of the canonical pushes of a1,…,aka_1, \ldots, a_ka1​,…,ak​ (the elements of args in order) followed by the canonical push of redeem. verifyScript evaluates σ\sigmaσ 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 k+1≤998k + 1 \le 998k+1≤998). The resulting top-first stack is stackSig =[redeem,ak,…,a1]= [\mathrm{redeem}, a_k, \ldots, a_1]=[redeem,ak​,…,a1​] (redeem on top). It then evaluates π\piπ on that stack: OP_HASH160 replaces the top by hash160(redeem)\mathrm{hash160}(\mathrm{redeem})hash160(redeem), the push adds hhh, and OP_EQUAL replaces the two top elements by [0x01][\mathtt{0x01}][0x01] if hash160(redeem)=h\mathrm{hash160}(\mathrm{redeem}) = hhash160(redeem)=h and by the empty string otherwise; the top therefore casts to true iff the hashes agree, else verifyScript returns false. Next, the native branch: π\piπ starts with 0xa9\mathtt{0xa9}0xa9, which is neither 000 nor in [0x51,0x60][\mathtt{0x51}, \mathtt{0x60}][0x51,0x60], so isWitnessProgram π is nothing and native is none whatever the witness flag. With hp, and isPayToScriptHash π true (23 bytes, 0xa9\mathtt{0xa9}0xa9, 0x14\mathtt{0x14}0x14, 0x87\mathtt{0x87}0x87 at the right indices, using hh), the P2SH branch runs: isPushOnly σ must hold (it does, since σ\sigmaσ is a concatenation of canonical pushes); stackSig is non-empty with top redeem and remainder [ak,…,a1]=reverse(args)[a_k, \ldots, a_1] = \mathrm{reverse}(\mathrm{args})[ak​,…,a1​]=reverse(args); 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 hhh 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 100001000010000-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 ∣h∣≠20|h| \ne 20∣h∣=20, 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.

Human review
  • Endorsed by Shuze Chen · Oct 27, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Oct 27, 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