Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The funding script's byte form parses to its instruction form

Proved
BitcoinFunding.parseScript_funding

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

bip147bitcoinbitcoin-mainnetbolt3checkmultisigformal-verificationfundinglightningmultisigp2wsh

For keys of at most 520 bytes: parseScript (fundingBytes pubkey1 pubkey2) = some (fundingScript pubkey1 pubkey2).

Preamble
import Definitions.Def_BitcoinFunding
import Definitions.Def_BitcoinScript
import Definitions.Def_BitcoinSig
open BitcoinFunding BitcoinHtlc
open BitcoinToLocal BitcoinSig BitcoinVerify
open BitcoinScript
Formal statement
theorem BitcoinFunding.parseScript_funding (pubkey1 pubkey2 : Bytes)
    (h1 : pubkey1.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE) (h2 : pubkey2.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE) :
    BitcoinSig.parseScript (BitcoinFunding.fundingBytes pubkey1 pubkey2) = some (BitcoinFunding.fundingScript pubkey1 pubkey2) := by sorry
Source
BOLT #3, Funding Transaction Output: https://github.com/lightning/bolts/blob/master/03-transactions.md#funding-transaction-output; Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: EvalScript (OP_CHECKMULTISIG), ExecuteWitnessScript, VerifyWitnessProgram (src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp); BIP141, BIP147
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: BitcoinFunding.parseScript_funding

A byte is an 8-bit word; a byte string is a finite list of bytes.

Statement

For all byte strings k1,k2k_1, k_2k1​,k2​ (named pubkey1, pubkey2; otherwise unconstrained in content), under the two hypotheses

∣k1∣≤520and∣k2∣≤520|k_1| \le 520 \qquad\text{and}\qquad |k_2| \le 520∣k1​∣≤520and∣k2​∣≤520

(520520520 is the constant MAX_SCRIPT_ELEMENT_SIZE; ∣⋅∣|\cdot|∣⋅∣ is the length in bytes), the theorem asserts the equality

parseScript(fundingBytes(k1,k2))  =  some(fundingScript(k1,k2)),\mathrm{parseScript}\big(\mathrm{fundingBytes}(k_1,k_2)\big) \;=\; \mathrm{some}\big(\mathrm{fundingScript}(k_1,k_2)\big),parseScript(fundingBytes(k1​,k2​))=some(fundingScript(k1​,k2​)),

an equality of values in Option(Script)\mathrm{Option}(\mathsf{Script})Option(Script), where a script is a list of parsed instructions. In words: the byte-level parser, applied to the serialized funding script, does not fail and returns exactly the five-instruction parsed funding script. Both sides are unfolded below.

The right-hand side: fundingScript

An instruction is either push(d)\mathrm{push}(d)push(d), a data push carrying a byte string ddd, or op(c)\mathrm{op}(c)op(c), a non-push opcode with byte ccc. By definition

fundingScript(k1,k2)=[ numInstr(2), push(k1), push(k2), numInstr(2), op(0xae) ],\mathrm{fundingScript}(k_1,k_2) = \big[\ \mathrm{numInstr}(2),\ \mathrm{push}(k_1),\ \mathrm{push}(k_2),\ \mathrm{numInstr}(2),\ \mathrm{op}(\mathtt{0xae})\ \big],fundingScript(k1​,k2​)=[ numInstr(2), push(k1​), push(k2​), numInstr(2), op(0xae) ],

where numInstr(n)\mathrm{numInstr}(n)numInstr(n) is op(n+0x50)\mathrm{op}(n+\mathtt{0x50})op(n+0x50) when n=−1n=-1n=−1 or 1≤n≤161\le n\le 161≤n≤16, and otherwise the push of the script-number encoding of nnn. For n=2n=2n=2 this gives op(0x52)\mathrm{op}(\mathtt{0x52})op(0x52). So the right-hand side is

some [ op(0x52), push(k1), push(k2), op(0x52), op(0xae) ].\mathrm{some}\ \big[\ \mathrm{op}(\mathtt{0x52}),\ \mathrm{push}(k_1),\ \mathrm{push}(k_2),\ \mathrm{op}(\mathtt{0x52}),\ \mathrm{op}(\mathtt{0xae})\ \big].some [ op(0x52), push(k1​), push(k2​), op(0x52), op(0xae) ].

Equality of scripts is entrywise equality of instruction lists; push(d)=push(d′)\mathrm{push}(d)=\mathrm{push}(d')push(d)=push(d′) iff d=d′d=d'd=d′.

The argument of the left-hand side: fundingBytes

fundingBytes(k1,k2)=numBytes(2) ∥ pushBytes(k1) ∥ pushBytes(k2) ∥ numBytes(2) ∥ [0xae],\mathrm{fundingBytes}(k_1,k_2) = \mathrm{numBytes}(2)\ \|\ \mathrm{pushBytes}(k_1)\ \|\ \mathrm{pushBytes}(k_2)\ \|\ \mathrm{numBytes}(2)\ \|\ [\mathtt{0xae}],fundingBytes(k1​,k2​)=numBytes(2) ∥ pushBytes(k1​) ∥ pushBytes(k2​) ∥ numBytes(2) ∥ [0xae],

with numBytes(n)=[ n+0x50 ]\mathrm{numBytes}(n) = [\,n+\mathtt{0x50}\,]numBytes(n)=[n+0x50] when n=−1n=-1n=−1 or 1≤n≤161\le n\le161≤n≤16 (so numBytes(2)=[0x52]\mathrm{numBytes}(2)=[\mathtt{0x52}]numBytes(2)=[0x52]), otherwise pushBytes\mathrm{pushBytes}pushBytes of the script-number encoding of nnn; and pushBytes(d)\mathrm{pushBytes}(d)pushBytes(d), for ℓ=∣d∣\ell=|d|ℓ=∣d∣:

  • ℓ<76\ell<76ℓ<76: the single byte ℓ\ellℓ, then ddd;
  • 76≤ℓ≤25576\le\ell\le25576≤ℓ≤255: bytes 0x4c, ℓ\mathtt{0x4c},\ \ell0x4c, ℓ, then ddd;
  • 256≤ℓ≤65535256\le\ell\le65535256≤ℓ≤65535: byte 0x4d\mathtt{0x4d}0x4d, then ℓ\ellℓ as 2 little-endian bytes, then ddd;
  • otherwise: byte 0x4e\mathtt{0x4e}0x4e, then ℓ\ellℓ as 4 little-endian bytes (modulo 2322^{32}232), then ddd.

Under the hypotheses, each of ∣k1∣,∣k2∣|k_1|,|k_2|∣k1​∣,∣k2​∣ is at most 520520520, so only the first three cases can arise for the two pushes (in particular pushBytes([ ])=[0x00]\mathrm{pushBytes}([\,])=[\mathtt{0x00}]pushBytes([])=[0x00] when a key is empty).

The parser: parseScript

parseScript(s)\mathrm{parseScript}(s)parseScript(s) runs an auxiliary parser with a fuel equal to ∣s∣|s|∣s∣:

  • on the empty byte string, it returns some [ ]\mathrm{some}\,[\,]some[] (regardless of fuel);
  • on a non-empty string with fuel 000, it returns none\mathrm{none}none;
  • otherwise it calls getOp(s)\mathrm{getOp}(s)getOp(s); if that is none\mathrm{none}none the result is none\mathrm{none}none; if it is some(i,r)\mathrm{some}(i, r)some(i,r) (one instruction and the remaining bytes), the remainder rrr is parsed recursively with one unit less fuel, and none\mathrm{none}none propagates, otherwise iii is prepended to the recursively parsed list.

getOp(s)\mathrm{getOp}(s)getOp(s) (one instruction) is:

  • s=[ ]s=[\,]s=[]: none\mathrm{none}none.
  • s=c::rs = c :: rs=c::r with ooo the numeric value of ccc:
    • if o≤0x4eo \le \mathtt{0x4e}o≤0x4e (=78=78=78), a push: the size and remainder are

      • o<0x4co<\mathtt{0x4c}o<0x4c: size ooo, remainder rrr (so the byte 0x00\mathtt{0x00}0x00 is a push of the empty string);
      • o=0x4co=\mathtt{0x4c}o=0x4c: size = the next byte, remainder after it; none\mathrm{none}none if rrr is empty;
      • o=0x4do=\mathtt{0x4d}o=0x4d: size = little-endian value of the next 2 bytes; none\mathrm{none}none if fewer than 2 remain;
      • o=0x4eo=\mathtt{0x4e}o=0x4e: size = little-endian value of the next 4 bytes; none\mathrm{none}none if fewer than 4 remain;

      then none\mathrm{none}none if fewer than size bytes remain, else some(push(the next size bytes), the rest)\mathrm{some}\big(\mathrm{push}(\text{the next } \mathit{size}\text{ bytes}),\ \text{the rest}\big)some(push(the next size bytes), the rest);

    • if o>0x4eo > \mathtt{0x4e}o>0x4e: some(op(c),r)\mathrm{some}(\mathrm{op}(c), r)some(op(c),r) — every byte from 0x4f\mathtt{0x4f}0x4f to 0xff\mathtt{0xff}0xff, including 0x52\mathtt{0x52}0x52 and 0xae\mathtt{0xae}0xae, is returned as a non-push opcode with no further check.

What is and is not constrained

  • The only hypotheses are the two length bounds on k1k_1k1​ and k2k_2k2​; the theorem makes no claim when either key exceeds 520520520 bytes.
  • k1k_1k1​ and k2k_2k2​ are arbitrary byte strings within that bound (they may be empty, equal to each other, or of any content; no public-key format is required).
  • The conclusion is a statement about parsing only: it asserts that parsing succeeds (is not none\mathrm{none}none) and that the resulting instruction list is exactly the five-entry list above, with the pushed data equal to k1k_1k1​ and k2k_2k2​ respectively.
Human review
  • Endorsed by Shuze Chen · Oct 29, 2026

    Confirmed by the moderator at approval.

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