Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The Lightning funding output script and its spending condition

Definition
BitcoinFunding

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

bip147bitcoinbitcoin-mainnetbolt3checkmultisigformal-verificationfundinglightningmultisigp2wsh

Names over the script, verification and to_local missions.

fundingScript pubkey1 pubkey2 / fundingBytes — the BOLT #3 funding witness script 2 <pubkey1> <pubkey2> 2 OP_CHECKMULTISIG, parsed and as CScript serializes it (2 as OP_2).

FundingSpend f o pubkey1 pubkey2 st — for a witness stack st (top first): st = [sig2, sig1, dummy] with sig1 valid for pubkey1, sig2 valid for pubkey2 (the oracle's checkSig), and an empty dummy when NULLDUMMY is enforced. In CScriptWitness order this is BOLT #3's 0 <signature_for_pubkey1> <signature_for_pubkey2>.

Formalization Note. numInstr, numBytes are the to_local mission's; multisigLoop, Flags, Oracle, evalScript the script mission's; verifyScript, topTrue the verification mission's. Only Std is imported.

Definition code
import Definitions.Def_BitcoinHtlc

/-!
# The Lightning funding output

A channel's funds sit in the funding transaction's output, a pay-to-witness-script-hash of the
2-of-2 script `2 <pubkey1> <pubkey2> 2 OP_CHECKMULTISIG` (BOLT #3, with the keys in lexicographic
order). Every commitment transaction spends it with the witness `0 <sig1> <sig2>`. This file states
the script and its single spending condition as the script and verification missions model them.
-/

namespace BitcoinFunding

open BitcoinScript BitcoinSig BitcoinVerify BitcoinToLocal

/-- The BOLT #3 funding witness script, parsed: `2 <pubkey1> <pubkey2> 2 OP_CHECKMULTISIG`. -/
def fundingScript (pubkey1 pubkey2 : Bytes) : Script :=
  [numInstr 2, .push pubkey1, .push pubkey2, numInstr 2, .op OP_CHECKMULTISIG]

/-- The funding witness script as `CScript` serializes it. -/
def fundingBytes (pubkey1 pubkey2 : Bytes) : Bytes :=
  numBytes 2 ++ pushBytes pubkey1 ++ pushBytes pubkey2 ++ numBytes 2 ++ [OP_CHECKMULTISIG]

/-- The only way to satisfy the funding script with a witness stack `st` (top first): three
elements `[sig2, sig1, dummy]`, `sig1` valid for `pubkey1`, `sig2` valid for `pubkey2`, and an empty
dummy when `NULLDUMMY` is enforced. (In `CScriptWitness` order the witness is `dummy sig1 sig2`.) -/
def FundingSpend (f : Flags) (o : Oracle) (pubkey1 pubkey2 : Bytes) (st : List Bytes) : Prop :=
  ∃ sig1 sig2 dummy, st = [sig2, sig1, dummy] ∧
    o.checkSig sig1 pubkey1 = some true ∧ o.checkSig sig2 pubkey2 = some true ∧
    (f.nulldummy = true → dummy = [])

end BitcoinFunding
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: Def_BitcoinFunding.lean

Throughout, a byte is an 8-bit word and a byte string is a finite list of bytes (written Bytes\mathsf{Bytes}Bytes). A stack is a finite list of byte strings whose first entry is the top of the stack. The file contains three definitions, read back below in order.


Definition 1: fundingScript

For any two byte strings k1,k2k_1, k_2k1​,k2​ (called pubkey1, pubkey2; they are completely unconstrained byte strings — any length, including empty, any contents), fundingScript(k1,k2)\mathrm{fundingScript}(k_1,k_2)fundingScript(k1​,k2​) is the following list of five parsed script instructions:

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) ].

Here an instruction is either push(d)\mathrm{push}(d)push(d) (a data push carrying the byte string ddd) or op(c)\mathrm{op}(c)op(c) (a non-push opcode given by its byte ccc); 0xae\mathtt{0xae}0xae is the byte named OP_CHECKMULTISIG. The preamble's numInstr(n)\mathrm{numInstr}(n)numInstr(n), for an integer nnn, is defined as: if n=−1n = -1n=−1 or 1≤n≤161 \le n \le 161≤n≤16, then the opcode instruction op(n+0x50)\mathrm{op}\big(n + \mathtt{0x50}\big)op(n+0x50) (as an 8-bit byte), otherwise the push push(serialize(n))\mathrm{push}(\mathrm{serialize}(n))push(serialize(n)) of the script-number encoding of nnn. For n=2n = 2n=2 the first case applies, so numInstr(2)=op(0x52)\mathrm{numInstr}(2) = \mathrm{op}(\mathtt{0x52})numInstr(2)=op(0x52) (the byte usually called OP_2). Hence, concretely,

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

Note that two scripts are equal exactly when their instruction lists are equal entrywise, where push(d)=push(d′)\mathrm{push}(d) = \mathrm{push}(d')push(d)=push(d′) iff d=d′d = d'd=d′ as byte strings; a push of a byte string of length 000 is a legitimate instruction.


Definition 2: fundingBytes

For any two byte strings k1,k2k_1, k_2k1​,k2​ (again unconstrained), fundingBytes(k1,k2)\mathrm{fundingBytes}(k_1,k_2)fundingBytes(k1​,k2​) is the byte string obtained by concatenation:

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].

The preamble's numBytes(n)\mathrm{numBytes}(n)numBytes(n) is: if n=−1n=-1n=−1 or 1≤n≤161\le n\le 161≤n≤16 then the single byte [ n+0x50 ][\,n+\mathtt{0x50}\,][n+0x50], otherwise pushBytes(serialize(n))\mathrm{pushBytes}(\mathrm{serialize}(n))pushBytes(serialize(n)). So numBytes(2)=[0x52]\mathrm{numBytes}(2) = [\mathtt{0x52}]numBytes(2)=[0x52].

The preamble's pushBytes(d)\mathrm{pushBytes}(d)pushBytes(d), for a byte string ddd of length ℓ=∣d∣\ell = |d|ℓ=∣d∣, is the canonical push encoding:

  • if ℓ<76\ell < 76ℓ<76 (=0x4c=\mathtt{0x4c}=0x4c): the single byte ℓ\ellℓ followed by ddd;
  • else if ℓ≤255\ell \le 255ℓ≤255: the two bytes 0x4c, ℓ\mathtt{0x4c},\ \ell0x4c, ℓ followed by ddd;
  • else if ℓ≤65535\ell \le 65535ℓ≤65535: the byte 0x4d\mathtt{0x4d}0x4d, then ℓ\ellℓ as 2 little-endian bytes, then ddd;
  • else: the byte 0x4e\mathtt{0x4e}0x4e, then ℓ\ellℓ as 4 little-endian bytes (reduced modulo 2322^{32}232), then ddd.

(In the first two cases the length byte is ℓ\ellℓ reduced modulo 256256256, which is ℓ\ellℓ itself there.) In particular, for an empty ddd, pushBytes([ ])=[0x00]\mathrm{pushBytes}([\,]) = [\mathtt{0x00}]pushBytes([])=[0x00].

Concretely,

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

Nothing in this definition bounds ∣k1∣|k_1|∣k1​∣ or ∣k2∣|k_2|∣k2​∣.


Definition 3: FundingSpend

This is a proposition (a truth value, not a computation) depending on:

  • a flag record fff of type Flags\mathsf{Flags}Flags, which consists of exactly three booleans f.cltvf.\mathrm{cltv}f.cltv, f.csvf.\mathrm{csv}f.csv, f.nulldummyf.\mathrm{nulldummy}f.nulldummy (only the last is consulted here);
  • an oracle ooo: a record of abstract functions o.ripemd160,o.sha1,o.sha256,o.hash160,o.hash256:Bytes→Byteso.\mathrm{ripemd160}, o.\mathrm{sha1}, o.\mathrm{sha256}, o.\mathrm{hash160}, o.\mathrm{hash256} : \mathsf{Bytes}\to\mathsf{Bytes}o.ripemd160,o.sha1,o.sha256,o.hash160,o.hash256:Bytes→Bytes, a function o.checkSig:Bytes×Bytes→Option(Bool)o.\mathrm{checkSig} : \mathsf{Bytes}\times\mathsf{Bytes} \to \mathrm{Option}(\mathrm{Bool})o.checkSig:Bytes×Bytes→Option(Bool) (first argument a "signature" byte string, second a "public key" byte string; its value is one of none\mathrm{none}none, some true\mathrm{some}\,\mathrm{true}sometrue, some false\mathrm{some}\,\mathrm{false}somefalse), and o.checkLockTime,o.checkSequence:Z→Boolo.\mathrm{checkLockTime}, o.\mathrm{checkSequence} : \mathbb{Z}\to\mathrm{Bool}o.checkLockTime,o.checkSequence:Z→Bool. The oracle is a free parameter: nothing in this definition says what any of its fields compute;
  • two byte strings k1,k2k_1, k_2k1​,k2​ (pubkey1, pubkey2), unconstrained;
  • a stack ststst (a list of byte strings, top first), unconstrained.

FundingSpend(f,o,k1,k2,st)\mathrm{FundingSpend}(f, o, k_1, k_2, st)FundingSpend(f,o,k1​,k2​,st) holds if and only if

∃ s1,s2,d∈Bytes:st=[ s2, s1, d ] ∧ o.checkSig(s1,k1)=some true ∧ o.checkSig(s2,k2)=some true ∧ (f.nulldummy=true  ⇒  d=[ ]).\exists\, s_1, s_2, d \in \mathsf{Bytes}:\quad st = [\,s_2,\ s_1,\ d\,] \ \wedge\ o.\mathrm{checkSig}(s_1, k_1) = \mathrm{some}\,\mathrm{true} \ \wedge\ o.\mathrm{checkSig}(s_2, k_2) = \mathrm{some}\,\mathrm{true} \ \wedge\ \big(f.\mathrm{nulldummy} = \mathrm{true} \;\Rightarrow\; d = [\,]\big).∃s1​,s2​,d∈Bytes:st=[s2​, s1​, d] ∧ o.checkSig(s1​,k1​)=sometrue ∧ o.checkSig(s2​,k2​)=sometrue ∧ (f.nulldummy=true⇒d=[]).

Spelled out:

  • ststst must have exactly three entries; the top entry is s2s_2s2​, the second is s1s_1s1​, the bottom is ddd. A stack of any other length (including the empty stack) never satisfies FundingSpend\mathrm{FundingSpend}FundingSpend.
  • s1s_1s1​ is paired with k1k_1k1​ and s2s_2s2​ with k2k_2k2​; both oracle calls must return precisely some true\mathrm{some}\,\mathrm{true}sometrue. A return value of none\mathrm{none}none or some false\mathrm{some}\,\mathrm{false}somefalse for either pair makes the proposition false.
  • If f.nulldummyf.\mathrm{nulldummy}f.nulldummy is true\mathrm{true}true, ddd must be the empty byte string; if f.nulldummyf.\mathrm{nulldummy}f.nulldummy is false\mathrm{false}false, ddd is arbitrary.
  • No length bound is imposed on s1s_1s1​, s2s_2s2​, ddd, k1k_1k1​ or k2k_2k2​ by this definition; f.cltvf.\mathrm{cltv}f.cltv and f.csvf.\mathrm{csv}f.csv play no role; and no hash function of the oracle is consulted.
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