Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Native P2WSH characterized

Proved
BitcoinVerify.p2wsh_correct

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

bip141bitcoinbitcoin-mainnetconsensusformal-verificationp2shsegwitverifyscript

With the WITNESS flag, the output OP_0 <SHA256(script)> spent with an empty scriptSig and witness ws ++ [script] verifies iff the program is true as a script value and the witness script, run under witness v0 on the stack of the remaining elements (last on top), leaves exactly one true element.

Preamble
import Definitions.Def_BitcoinVerify
import Definitions.Def_BitcoinSer
import Definitions.Def_BitcoinScript
open BitcoinVerify BitcoinSer BitcoinSig
open BitcoinScript hiding Bytes Byte
Formal statement
theorem BitcoinVerify.p2wsh_correct (tap : TaprootHandler) (c : Ctx) (ws : List Bytes) (script : Bytes)
    (hf : c.flags.witness = true) :
    BitcoinVerify.verifyScript tap c [] (BitcoinVerify.witnessBytes 0 (BitcoinSer.sha256 script)) (ws ++ [script]) = true ↔
      BitcoinScript.castToBool (BitcoinSer.sha256 script) = true ∧
      BitcoinVerify.executeWitnessScriptV0 c ws.reverse script = 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.p2wsh_correct

Binders and hypothesis. Universally quantified: tap, a Taproot handler (any function from a context, a list of byte strings and a byte string to a Boolean); ccc, a verification context (consensus flags c.flags, transaction c.tx, input index c.nIn, integer amount c.amount); ws, a list of byte strings; script, a byte string. One hypothesis hf: c.flags.witness = true. There are no size hypotheses on ws, its elements, or script.

Conclusion. Let P=sha256(script)P = \mathrm{sha256}(\mathrm{script})P=sha256(script) (32 bytes) and π=witnessBytes(0,P)=0x00 0x20 P\pi = \mathrm{witnessBytes}(0, P) = \mathtt{0x00}\ \mathtt{0x20}\ Pπ=witnessBytes(0,P)=0x00 0x20 P (34 bytes). Then

verifyScript(tap,c,[],π,ws ∥ [script])=true  ⟺  castToBool(P)=true ∧ executeWitnessScriptV0(c,reverse(ws),script)=true.\mathrm{verifyScript}(\mathrm{tap}, c, [], \pi, \mathrm{ws} \,\|\, [\mathrm{script}]) = \mathrm{true} \iff \mathrm{castToBool}(P) = \mathrm{true} \ \wedge\ \mathrm{executeWitnessScriptV0}(c, \mathrm{reverse}(\mathrm{ws}), \mathrm{script}) = \mathrm{true}.verifyScript(tap,c,[],π,ws∥[script])=true⟺castToBool(P)=true ∧ executeWitnessScriptV0(c,reverse(ws),script)=true.

The witness is the list ws with script appended as its last element.

Unfolding the left-hand side. The empty scriptSig evaluates to the empty stack. π\piπ evaluates (version base) to the top-first stack [P,[]][P, []][P,[]] (the byte 0x00\mathtt{0x00}0x00 pushes the empty string, then the 32-byte push). topTrue is castToBool(P)\mathrm{castToBool}(P)castToBool(P): if the SHA-256 digest is all zeros or zeros followed by a final 0x80\mathtt{0x80}0x80, verifyScript returns false. Otherwise, with the witness flag on (hf), isWitnessProgram π =some(0,P)= \mathrm{some}(0, P)=some(0,P) (length 34∈[4,42]34 \in [4, 42]34∈[4,42], first byte 000, second byte 32=34−232 = 34 - 232=34−2), and the scriptSig is empty, so verifyScript returns exactly verifyWitnessProgram tap c (ws ++ [script]) 0 P false.

Unfolding verifyWitnessProgram. Version 000, ∣P∣=32|P| = 32∣P∣=32 (the script-hash size): the last element of the witness, getLast? (ws ++ [script]), is script (the witness is non-empty by construction); the test sha256(script)≠P\mathrm{sha256}(\mathrm{script}) \ne Psha256(script)=P fails to trigger since PPP is defined as that hash; so the result is executeWitnessScriptV0 c ((ws ++ [script]).dropLast.reverse) script === executeWitnessScriptV0 c (reverse ws) script. The witness elements other than the script are reversed, so the element of ws that was last in serialized order becomes the interpreter's top.

Unfolding executeWitnessScriptV0 (the right-hand side). executeWitnessScriptV0 c (reverse ws) script is: false if some element of ws is longer than 520 bytes; otherwise evalBytes c witnessV0 (reverse ws) script — false if ∣script∣>10000|\mathrm{script}| > 10000∣script∣>10000, false if script fails the byte-level parse, false if the interpreter (flags scriptFlagsOf c.flags; oracle with signature check checkSigPreTapscript (sigFlagsOf c.flags) c.tx c.nIn c.amount witnessV0 script, script code the whole script) fails; otherwise true iff the final stack has exactly one element and that element casts to true. The script element itself is not subject to the 520-byte test (it was removed by dropLast), only to the 100001000010000-byte test.

Logical content. For every handler, context with the witness flag on, witness prefix ws and script, the full verification of an empty scriptSig against the native version-0 32-byte program equals the conjunction of (i) the program bytes casting to true and (ii) the witness-script execution verdict on the reversed remaining witness. Both directions are asserted.

What is not said. Nothing when the witness flag is off; nothing for a non-empty scriptSig; nothing about witnesses whose last element does not hash to the program (the statement fixes the program as the hash of the last element); nothing about P2SH-wrapped P2WSH; no size constraints are asserted — they are absorbed into executeWitnessScriptV0 on the right; no clean-stack condition beyond the one-element rule inside executeWitnessScriptV0; the handler tap, the taproot and p2sh flags are not consulted on this path and the theorem is silent about them. It does not unfold what the interpreter does with script.

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