The Lightning funding output script and its spending condition
DefinitionBitcoinFundingNames 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.
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
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 ). 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 (called pubkey1, pubkey2; they are completely unconstrained byte strings — any length, including empty, any contents), is the following list of five parsed script instructions:
Here an instruction is either (a data push carrying the byte string ) or (a non-push opcode given by its byte ); is the byte named OP_CHECKMULTISIG. The preamble's , for an integer , is defined as: if or , then the opcode instruction (as an 8-bit byte), otherwise the push of the script-number encoding of . For the first case applies, so (the byte usually called OP_2). Hence, concretely,
Note that two scripts are equal exactly when their instruction lists are equal entrywise, where iff as byte strings; a push of a byte string of length is a legitimate instruction.
Definition 2: fundingBytes
For any two byte strings (again unconstrained), is the byte string obtained by concatenation:
The preamble's is: if or then the single byte , otherwise . So .
The preamble's , for a byte string of length , is the canonical push encoding:
- if (): the single byte followed by ;
- else if : the two bytes followed by ;
- else if : the byte , then as 2 little-endian bytes, then ;
- else: the byte , then as 4 little-endian bytes (reduced modulo ), then .
(In the first two cases the length byte is reduced modulo , which is itself there.) In particular, for an empty , .
Concretely,
Nothing in this definition bounds or .
Definition 3: FundingSpend
This is a proposition (a truth value, not a computation) depending on:
- a flag record of type , which consists of exactly three booleans , , (only the last is consulted here);
- an oracle : a record of abstract functions , a function (first argument a "signature" byte string, second a "public key" byte string; its value is one of , , ), and . The oracle is a free parameter: nothing in this definition says what any of its fields compute;
- two byte strings (
pubkey1,pubkey2), unconstrained; - a stack (a list of byte strings, top first), unconstrained.
holds if and only if
Spelled out:
- must have exactly three entries; the top entry is , the second is , the bottom is . A stack of any other length (including the empty stack) never satisfies .
- is paired with and with ; both oracle calls must return precisely . A return value of or for either pair makes the proposition false.
- If is , must be the empty byte string; if is , is arbitrary.
- No length bound is imposed on , , , or by this definition; and play no role; and no hash function of the oracle is consulted.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.