Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

A verifying script-path spend has a valid commitment

Proved
BitcoinTaproot.scriptpath_commitment

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

bip341bip342bitcoinbitcoin-mainnetconsensusformal-verificationschnorrtaproottapscript

If a witness stack ++ [script, control] without an annex verifies under the Taproot handler, then verifyTaprootCommitment control q leafHash = true, where leafHash is the tapleaf hash of the revealed script under the control byte's masked leaf version: the merkle path from the leaf, tweaked into the internal key, yields the output key with the control byte's parity.

Preamble
import Definitions.Def_BitcoinTaproot
import Definitions.Def_BitcoinVerify
open BitcoinTaproot BitcoinSer BitcoinSig BitcoinVerify
open BitcoinScript hiding Bytes Byte
Formal statement
theorem BitcoinTaproot.scriptpath_commitment (spent : List TxOut) (c : BitcoinVerify.Ctx) (stack : List Bytes)
    (script control q : Bytes) (hna : BitcoinTaproot.hasAnnex (stack ++ [script, control]) = false)
    (h : BitcoinTaproot.taprootHandler spent c (stack ++ [script, control]) q = true) :
    BitcoinTaproot.verifyTaprootCommitment control q
      (BitcoinTaproot.tapleafHash (BitVec.ofNat 8 ((control.getD 0 0).toNat &&& BitcoinTaproot.TAPROOT_LEAF_MASK)) script) = true := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: VerifyWitnessProgram (version 1), ExecuteWitnessScript, EvalScript under SigVersion::TAPSCRIPT, EvalChecksigTapscript, CheckSchnorrSignature, SignatureHashSchnorr, ComputeTapleafHash, ComputeTapbranchHash, ComputeTaprootMerkleRoot, VerifyTaprootCommitment, src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp; XOnlyPubKey::CheckTapTweak, ComputeTapTweakHash, src/pubkey.cpp; secp256k1_xonly_pubkey_tweak_add_check, src/secp256k1/src/modules/extrakeys/main_impl.h; IsOpSuccess, src/script/script.cpp; BIP340, BIP341, BIP342 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: BitcoinTaproot.scriptpath_commitment

Binders. spent\mathrm{spent}spent : a list of transaction outputs; ccc : a verification context BitcoinVerify.Ctx (fields flags\mathrm{flags}flags — Booleans p2sh, witness, taproot, dersig, cltv, csv, nulldummy —, tx\mathrm{tx}tx, nIn∈N\mathrm{nIn} \in \mathbb{N}nIn∈N, amount∈Z\mathrm{amount} \in \mathbb{Z}amount∈Z); stack\mathrm{stack}stack : a list of byte strings; script,control,q\mathrm{script}, \mathrm{control}, qscript,control,q : byte strings.

Throughout, W:=stack ∥ [script,control]W := \mathrm{stack} \,\Vert\, [\mathrm{script}, \mathrm{control}]W:=stack∥[script,control] denotes the witness list consisting of the elements of stack\mathrm{stack}stack followed by script\mathrm{script}script and then control\mathrm{control}control (so ∣W∣≥2|W| \ge 2∣W∣≥2 and control\mathrm{control}control is last).

Hypotheses.

  1. hasAnnex(W)=false\mathrm{hasAnnex}(W) = \mathrm{false}hasAnnex(W)=false. Unfolded: hasAnnex(W)\mathrm{hasAnnex}(W)hasAnnex(W) is "∣W∣≥2|W| \ge 2∣W∣≥2 and the last element of WWW is non-empty with first byte equal to 0x50\texttt{0x50}0x50"; since ∣W∣≥2|W| \ge 2∣W∣≥2 holds, the hypothesis says exactly that control\mathrm{control}control is empty or its first byte is not 0x50\texttt{0x50}0x50.
  2. taprootHandler(spent)(c,W,q)=true\mathrm{taprootHandler}(\mathrm{spent})(c, W, q) = \mathrm{true}taprootHandler(spent)(c,W,q)=true.

Conclusion.

verifyTaprootCommitment(control,  q,  tapleafHash(⟨control0 & 0xfe⟩8, script))=true,\mathrm{verifyTaprootCommitment}\big(\mathrm{control},\; q,\; \mathrm{tapleafHash}(\langle \mathrm{control}_0 \,\&\, \texttt{0xfe}\rangle_8,\ \mathrm{script})\big) = \mathrm{true},verifyTaprootCommitment(control,q,tapleafHash(⟨control0​&0xfe⟩8​, script))=true,

where control0\mathrm{control}_0control0​ is the first byte of control\mathrm{control}control as a natural (default 000 if control\mathrm{control}control is empty), &\&& is bitwise AND, ⟨⋅⟩8\langle\cdot\rangle_8⟨⋅⟩8​ converts a natural to an 8-bit value (mod 256256256), and tapleafHash(v,s)=tagged("TapLeaf",[v] ∥ CompactSize(∣s∣) ∥ s)\mathrm{tapleafHash}(v, s) = \mathrm{tagged}(\texttt{"TapLeaf"}, [v] \,\Vert\, \mathrm{CompactSize}(|s|) \,\Vert\, s)tapleafHash(v,s)=tagged("TapLeaf",[v]∥CompactSize(∣s∣)∥s).

verifyTaprootCommitment\mathrm{verifyTaprootCommitment}verifyTaprootCommitment, unfolded. verifyTaprootCommitment(control,q,ℓ)=checkTapTweak(q, control[1:33], taprootMerkleRoot(control,ℓ), [control0 is odd])\mathrm{verifyTaprootCommitment}(\mathrm{control}, q, \ell) = \mathrm{checkTapTweak}\big(q,\ \mathrm{control}[1{:}33],\ \mathrm{taprootMerkleRoot}(\mathrm{control}, \ell),\ [\mathrm{control}_0 \text{ is odd}]\big)verifyTaprootCommitment(control,q,ℓ)=checkTapTweak(q, control[1:33], taprootMerkleRoot(control,ℓ), [control0​ is odd]), where control[1:33]\mathrm{control}[1{:}33]control[1:33] is "drop the first byte, then take up to 323232 bytes" (shorter if control\mathrm{control}control is shorter than 333333 bytes), and the parity bit is the low bit of the first control byte (default 000). taprootMerkleRoot(control,ℓ)\mathrm{taprootMerkleRoot}(\mathrm{control}, \ell)taprootMerkleRoot(control,ℓ) is the left fold h0=ℓh_0 = \ellh0​=ℓ, hj+1=tapbranchHash(hj,Nj)h_{j+1} = \mathrm{tapbranchHash}(h_j, N_j)hj+1​=tapbranchHash(hj​,Nj​) over the k=⌊(∣control∣−˙33)/32⌋k = \lfloor(|\mathrm{control}| \dot- 33)/32\rfloork=⌊(∣control∣−˙​33)/32⌋ consecutive 323232-byte chunks NjN_jNj​ of control\mathrm{control}control after its first 333333 bytes; tapbranchHash(x,y)=tagged("TapBranch",min⁡lex(x,y)∥max⁡lex(x,y))\mathrm{tapbranchHash}(x,y) = \mathrm{tagged}(\texttt{"TapBranch"}, \min_{\mathrm{lex}}(x,y) \Vert \max_{\mathrm{lex}}(x,y))tapbranchHash(x,y)=tagged("TapBranch",minlex​(x,y)∥maxlex​(x,y)) with the file's lexicographic order (ties concatenate y∥xy \Vert xy∥x, which equals x∥yx \Vert yx∥y). checkTapTweak(q,int,m,π)\mathrm{checkTapTweak}(q, \mathrm{int}, m, \pi)checkTapTweak(q,int,m,π) is: let P:=liftX(BE(int))P := \mathrm{liftX}(\mathrm{BE}(\mathrm{int}))P:=liftX(BE(int)) — none\mathrm{none}none if BE(int)≥p\mathrm{BE}(\mathrm{int}) \ge pBE(int)≥p or if x3+7x^3+7x3+7 is not a square mod ppp, else the point with abscissa x=BE(int)x = \mathrm{BE}(\mathrm{int})x=BE(int) and even ordinate; false\mathrm{false}false if PPP is none\mathrm{none}none; otherwise xonlyTweakAddCheck(q,π,P,tapTweakHash(int,m))\mathrm{xonlyTweakAddCheck}(q, \pi, P, \mathrm{tapTweakHash}(\mathrm{int}, m))xonlyTweakAddCheck(q,π,P,tapTweakHash(int,m)), where tapTweakHash(int,m)=tagged("TapTweak",int∥m)\mathrm{tapTweakHash}(\mathrm{int}, m) = \mathrm{tagged}(\texttt{"TapTweak"}, \mathrm{int} \Vert m)tapTweakHash(int,m)=tagged("TapTweak",int∥m), and xonlyTweakAddCheck(q,π,P,T)\mathrm{xonlyTweakAddCheck}(q, \pi, P, T)xonlyTweakAddCheck(q,π,P,T) is: let t:=BE(T)t := \mathrm{BE}(T)t:=BE(T); false\mathrm{false}false if n≤tn \le tn≤t; let Q:=P+t⋅GQ := P + t\cdot GQ:=P+t⋅G (bundle's affine formulas, t⋅Gt \cdot Gt⋅G being none\mathrm{none}none when t=0t = 0t=0 so that Q=PQ = PQ=P); false\mathrm{false}false if QQQ is the point at infinity; else true\mathrm{true}true iff the 32-byte big-endian encoding of xQ mod 2256x_Q \bmod 2^{256}xQ​mod2256 equals qqq and (yQy_QyQ​ is odd) =π= \pi=π. tagged(T,m)=SHA256(SHA256(T)∥SHA256(T)∥m)\mathrm{tagged}(T, m) = \mathrm{SHA256}(\mathrm{SHA256}(T)\Vert\mathrm{SHA256}(T)\Vert m)tagged(T,m)=SHA256(SHA256(T)∥SHA256(T)∥m), TTT the UTF-8 bytes of the tag.

Hypothesis 2, unfolded. taprootHandler(spent)(c,W,q)\mathrm{taprootHandler}(\mathrm{spent})(c, W, q)taprootHandler(spent)(c,W,q): WWW is non-empty; by hypothesis 1 there is no annex, so the working stack is WWW itself; its length is ∣stack∣+2≠1|\mathrm{stack}| + 2 \ne 1∣stack∣+2=1, so the key path is not taken. The script path pops the last element as the control block (=control= \mathrm{control}=control), then the last remaining element as the script (=script= \mathrm{script}=script), leaving stack\mathrm{stack}stack. It returns false\mathrm{false}false if ∣control∣<33|\mathrm{control}| < 33∣control∣<33, or ∣control∣>4129|\mathrm{control}| > 4129∣control∣>4129 (=33+32⋅128= 33 + 32 \cdot 128=33+32⋅128), or (∣control∣−33) mod 32≠0(|\mathrm{control}| - 33) \bmod 32 \neq 0(∣control∣−33)mod32=0. Otherwise, with leaf version v:=control0 & 0xfev := \mathrm{control}_0 \,\&\, \texttt{0xfe}v:=control0​&0xfe and ℓ:=tapleafHash(⟨v⟩8,script)\ell := \mathrm{tapleafHash}(\langle v \rangle_8, \mathrm{script})ℓ:=tapleafHash(⟨v⟩8​,script), it returns false\mathrm{false}false if verifyTaprootCommitment(control,q,ℓ)\mathrm{verifyTaprootCommitment}(\mathrm{control}, q, \ell)verifyTaprootCommitment(control,q,ℓ) is false\mathrm{false}false; otherwise, if v=0xc0v = \texttt{0xc0}v=0xc0 it returns executeTapscript\mathrm{executeTapscript}executeTapscript of the Tapscript context (flags cltv, csv, nulldummy from c.flagsc.\mathrm{flags}c.flags, c.txc.\mathrm{tx}c.tx, spent\mathrm{spent}spent, c.nInc.\mathrm{nIn}c.nIn), the ExecData (false,[ ],ℓ,0xFFFFFFFF)(\mathrm{false}, [\,], \ell, \texttt{0xFFFFFFFF})(false,[],ℓ,0xFFFFFFFF), the reversed stack\mathrm{stack}stack, script\mathrm{script}script, and the initial weight (serialized size of the whole witness WWW as a CompactSize-prefixed list of CompactSize-prefixed elements) +50+ 50+50; and if v≠0xc0v \neq \texttt{0xc0}v=0xc0 it returns true\mathrm{true}true. Hence hypothesis 2 entails that the size checks pass and that the commitment check in the conclusion evaluates to true\mathrm{true}true (plus either successful Tapscript execution or an unknown leaf version), and the theorem extracts the commitment part.

Edge cases the quantifiers include. stack=[ ]\mathrm{stack} = [\,]stack=[] (witness of exactly two elements); control\mathrm{control}control empty or shorter than 333333 bytes — then hypothesis 2 is false and the statement is vacuous for those; control\mathrm{control}control with first byte 0x50\texttt{0x50}0x50 is excluded by hypothesis 1 (first byte 0x51\texttt{0x51}0x51 is allowed, giving the same leaf version 0x50\texttt{0x50}0x50); qqq of length ≠32\neq 32=32 makes the commitment false, hence hypothesis 2 false; leaf versions other than 0xc0\texttt{0xc0}0xc0, where hypothesis 2 reduces to the size and commitment checks alone; script\mathrm{script}script empty; c.flags.taprootc.\mathrm{flags}.\mathrm{taproot}c.flags.taproot and c.amountc.\mathrm{amount}c.amount unused.

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