Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The Lightning offered and received HTLC witness scripts and their spending conditions

Definition
BitcoinHtlc

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

bip65bitcoinbitcoin-mainnetbolt3checkmultisigcltvformal-verificationhtlclightningp2wshscript

Names over the script, signature, verification and to_local missions; nothing of Core is re-transcribed.

offeredScript revHash remoteKey localKey payHash / offeredBytes … — the BOLT #3 offered-HTLC witness script OP_DUP OP_HASH160 <revHash> OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE <remoteKey> OP_SWAP OP_SIZE 32 OP_EQUAL OP_NOTIF OP_DROP 2 OP_SWAP <localKey> 2 OP_CHECKMULTISIG OP_ELSE OP_HASH160 <payHash> OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF OP_ENDIF, parsed and as CScript serializes it (2 as OP_2, 32 as the one-byte push 0x20).

receivedScript … expiry / receivedBytes … — the received-HTLC script, whose inner branches are OP_IF OP_HASH160 <payHash> OP_EQUALVERIFY 2 OP_SWAP <localKey> 2 OP_CHECKMULTISIG OP_ELSE OP_DROP <expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP OP_CHECKSIG OP_ENDIF, the expiry encoded by the to_local mission's numInstr/numBytes.

CltvPasses f o expiry — what OP_CHECKLOCKTIMEVERIFY lets through: the flag is off, or expiry is non-negative and the oracle's checkLockTime expiry holds.

OfferedSpend f o revHash remoteKey localKey payHash st — for a witness stack st (top first): the revocation path [pk, sig] with hash160 pk = revHash and sig valid for pk; the preimage path [preimage, sig] with hash160 preimage ≠ revHash, a 32-byte preimage hashing to payHash, and sig valid for remoteKey; or the timeout path [x, localSig, remoteSig, dummy] with hash160 x ≠ revHash, x not 32 bytes, the 2-of-2 multisigLoop o [localSig, remoteSig] [localKey, remoteKey] passing, and an empty dummy when NULLDUMMY is enforced.

ReceivedSpend … expiry st — the revocation path as above; the success path [preimage, localSig, remoteSig, dummy] (32-byte preimage hashing to payHash, not to revHash, the 2-of-2 passing, dummy as above); or the timeout path [x, sig] with hash160 x ≠ revHash, x not 32 bytes, CltvPasses, and sig valid for remoteKey.

Formalization Note. multisigLoop, Flags, Oracle, evalScript are the script mission's; legacyOracle, checkLockTime, parseScript the signature mission's; verifyScript, topTrue the verification mission's; numInstr, inputSequence the to_local mission's. The two 20-byte hashes are free parameters. Only Std is imported.

Definition code
import Definitions.Def_BitcoinToLocal

/-!
# The Lightning HTLC outputs under Bitcoin's script interpreter

BOLT #3 (Lightning Network specification, *Bitcoin Transaction and Script Formats*, non-anchor
form) gives the two HTLC outputs of a commitment transaction the witness scripts

Offered HTLC (the local node offered the HTLC):
```
OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationpubkey))> OP_EQUAL
OP_IF OP_CHECKSIG
OP_ELSE <remote_htlcpubkey> OP_SWAP OP_SIZE 32 OP_EQUAL
  OP_NOTIF OP_DROP 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG
  OP_ELSE OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY OP_CHECKSIG
  OP_ENDIF
OP_ENDIF
```
Received HTLC:
```
OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationpubkey))> OP_EQUAL
OP_IF OP_CHECKSIG
OP_ELSE <remote_htlcpubkey> OP_SWAP OP_SIZE 32 OP_EQUAL
  OP_IF OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG
  OP_ELSE OP_DROP <cltv_expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP OP_CHECKSIG
  OP_ENDIF
OP_ENDIF
```
paid to as pay-to-witness-script-hash. The 20-byte hashes are parameters `revHash` and `payHash`
(any bytes), the keys `remoteKey` and `localKey`. `2` is `CScript << 2` (`OP_2`), `32` is
`CScript << 32` (the one-byte script-number push `01 20`), and `<cltv_expiry>` is
`CScript << int64` (`BitcoinToLocal.numInstr` / `numBytes`).

Witness stacks are listed top first, as `evalScript` takes them. In BOLT #3's `CScriptWitness`
order (last element on top) the witnesses are: revocation `<revocation_sig> <revocationpubkey>`;
offered-timeout `0 <remotehtlcsig> <localhtlcsig> <>`; offered-preimage
`<remotehtlcsig> <payment_preimage>`; received-success `0 <remotehtlcsig> <localhtlcsig>
<payment_preimage>`; received-timeout `<remotehtlcsig> <>`.
-/

namespace BitcoinHtlc

open BitcoinScript BitcoinSig BitcoinVerify BitcoinToLocal

/-- The BOLT #3 offered-HTLC witness script, parsed. -/
def offeredScript (revHash remoteKey localKey payHash : Bytes) : Script :=
  [.op OP_DUP, .op OP_HASH160, .push revHash, .op OP_EQUAL,
   .op OP_IF, .op OP_CHECKSIG,
   .op OP_ELSE, .push remoteKey, .op OP_SWAP, .op OP_SIZE, numInstr 32, .op OP_EQUAL,
   .op OP_NOTIF, .op OP_DROP, numInstr 2, .op OP_SWAP, .push localKey, numInstr 2, .op OP_CHECKMULTISIG,
   .op OP_ELSE, .op OP_HASH160, .push payHash, .op OP_EQUALVERIFY, .op OP_CHECKSIG,
   .op OP_ENDIF,
   .op OP_ENDIF]

/-- The BOLT #3 received-HTLC witness script, parsed. -/
def receivedScript (revHash remoteKey localKey payHash : Bytes) (expiry : Int) : Script :=
  [.op OP_DUP, .op OP_HASH160, .push revHash, .op OP_EQUAL,
   .op OP_IF, .op OP_CHECKSIG,
   .op OP_ELSE, .push remoteKey, .op OP_SWAP, .op OP_SIZE, numInstr 32, .op OP_EQUAL,
   .op OP_IF, .op OP_HASH160, .push payHash, .op OP_EQUALVERIFY, numInstr 2, .op OP_SWAP, .push localKey,
     numInstr 2, .op OP_CHECKMULTISIG,
   .op OP_ELSE, .op OP_DROP, numInstr expiry, .op OP_CHECKLOCKTIMEVERIFY, .op OP_DROP, .op OP_CHECKSIG,
   .op OP_ENDIF,
   .op OP_ENDIF]

/-- The offered-HTLC witness script, as `CScript` serializes it. -/
def offeredBytes (revHash remoteKey localKey payHash : Bytes) : Bytes :=
  [OP_DUP, OP_HASH160] ++ pushBytes revHash ++ [OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE] ++
    pushBytes remoteKey ++ [OP_SWAP, OP_SIZE] ++ numBytes 32 ++ [OP_EQUAL, OP_NOTIF, OP_DROP] ++
    numBytes 2 ++ [OP_SWAP] ++ pushBytes localKey ++ numBytes 2 ++ [OP_CHECKMULTISIG, OP_ELSE, OP_HASH160] ++
    pushBytes payHash ++ [OP_EQUALVERIFY, OP_CHECKSIG, OP_ENDIF, OP_ENDIF]

/-- The received-HTLC witness script, as `CScript` serializes it. -/
def receivedBytes (revHash remoteKey localKey payHash : Bytes) (expiry : Int) : Bytes :=
  [OP_DUP, OP_HASH160] ++ pushBytes revHash ++ [OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE] ++
    pushBytes remoteKey ++ [OP_SWAP, OP_SIZE] ++ numBytes 32 ++ [OP_EQUAL, OP_IF, OP_HASH160] ++
    pushBytes payHash ++ [OP_EQUALVERIFY] ++ numBytes 2 ++ [OP_SWAP] ++ pushBytes localKey ++ numBytes 2 ++
    [OP_CHECKMULTISIG, OP_ELSE, OP_DROP] ++ numBytes expiry ++
    [OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_CHECKSIG, OP_ENDIF, OP_ENDIF]

/-- What `OP_CHECKLOCKTIMEVERIFY` lets through for the operand `expiry`: nothing is checked when the
`CHECKLOCKTIMEVERIFY` flag is off; otherwise the operand must be non-negative and satisfy the
checker's `CheckLockTime` (there is no disable bit for lock times). -/
def CltvPasses (f : Flags) (o : Oracle) (expiry : Int) : Prop :=
  f.cltv = false ∨ (0 ≤ expiry ∧ o.checkLockTime expiry = true)

instance (f : Flags) (o : Oracle) (expiry : Int) : Decidable (CltvPasses f o expiry) := by
  unfold CltvPasses; infer_instance

/-- The three ways of satisfying the offered-HTLC script, on a witness stack `st` (top first):
the revocation path `[pubkey, sig]` with the pubkey hashing to `revHash` and `sig` valid for it;
the preimage path `[preimage, sig]` with a 32-byte preimage hashing to `payHash` (and not to
`revHash`) and `sig` valid for the remote key; or the timeout path `[x, localSig, remoteSig, dummy]`
with `x` neither hashing to `revHash` nor 32 bytes long, the 2-of-2 check of the local then the
remote signature passing, and an empty dummy when `NULLDUMMY` is enforced. -/
def OfferedSpend (f : Flags) (o : Oracle) (revHash remoteKey localKey payHash : Bytes) (st : List Bytes) : Prop :=
  (∃ pk sig, st = [pk, sig] ∧ o.hash160 pk = revHash ∧ o.checkSig sig pk = some true) ∨
  (∃ preimage sig, st = [preimage, sig] ∧ o.hash160 preimage ≠ revHash ∧ preimage.length = 32 ∧
      o.hash160 preimage = payHash ∧ o.checkSig sig remoteKey = some true) ∨
  (∃ x localSig remoteSig dummy, st = [x, localSig, remoteSig, dummy] ∧ o.hash160 x ≠ revHash ∧ x.length ≠ 32 ∧
      multisigLoop o [localSig, remoteSig] [localKey, remoteKey] = some true ∧ (f.nulldummy = true → dummy = []))

/-- The three ways of satisfying the received-HTLC script, on a witness stack `st` (top first):
the revocation path `[pubkey, sig]`; the success path `[preimage, localSig, remoteSig, dummy]` with a
32-byte preimage hashing to `payHash` (and not to `revHash`), the 2-of-2 check passing and an empty
dummy when `NULLDUMMY` is enforced; or the timeout path `[x, sig]` with `x` neither hashing to
`revHash` nor 32 bytes long, the lock-time check passing and `sig` valid for the remote key. -/
def ReceivedSpend (f : Flags) (o : Oracle) (revHash remoteKey localKey payHash : Bytes) (expiry : Int)
    (st : List Bytes) : Prop :=
  (∃ pk sig, st = [pk, sig] ∧ o.hash160 pk = revHash ∧ o.checkSig sig pk = some true) ∨
  (∃ preimage localSig remoteSig dummy, st = [preimage, localSig, remoteSig, dummy] ∧
      o.hash160 preimage ≠ revHash ∧ preimage.length = 32 ∧ o.hash160 preimage = payHash ∧
      multisigLoop o [localSig, remoteSig] [localKey, remoteKey] = some true ∧ (f.nulldummy = true → dummy = [])) ∨
  (∃ x sig, st = [x, sig] ∧ o.hash160 x ≠ revHash ∧ x.length ≠ 32 ∧ CltvPasses f o expiry ∧
      o.checkSig sig remoteKey = some true)

end BitcoinHtlc
Source
BOLT #3, Bitcoin Transaction and Script Formats, 'Offered HTLC Outputs' and 'Received HTLC Outputs': https://github.com/lightning/bolts/blob/master/03-transactions.md#offered-htlc-outputs; Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: EvalScript (OP_DUP, OP_HASH160, OP_EQUAL, OP_IF/OP_NOTIF, OP_SWAP, OP_SIZE, OP_CHECKMULTISIG, OP_CHECKLOCKTIMEVERIFY, OP_CHECKSIG), GenericTransactionSignatureChecker::CheckLockTime (src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp); BIP65, 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_BitcoinHtlc.lean (definitions, in file order)

The file introduces eight declarations in the namespace BitcoinHtlc: offeredScript, receivedScript, offeredBytes, receivedBytes, CltvPasses, a decidability instance for CltvPasses, OfferedSpend and ReceivedSpend. The preamble notions they depend on are rendered first, then one paragraph per definition.

Basic notions used throughout. A byte is an 8-bit word and a byte string (written Bytes\mathrm{Bytes}Bytes) is a finite list of bytes; ∣b∣|b|∣b∣ denotes the length of a byte string bbb. The constant Lmax⁡=520L_{\max} = 520Lmax​=520 is the interpreter's maximum size of one stack element. A flag set fff consists of three Booleans f.cltvf.\mathrm{cltv}f.cltv, f.csvf.\mathrm{csv}f.csv, f.nulldummyf.\mathrm{nulldummy}f.nulldummy. An oracle ooo is an arbitrary structure supplying eight functions with no constraints whatsoever on their behaviour: 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} : \mathrm{Bytes}\to\mathrm{Bytes}o.ripemd160,o.sha1,o.sha256,o.hash160,o.hash256:Bytes→Bytes (so in particular o.hash160o.\mathrm{hash160}o.hash160 is any function from byte strings to byte strings, not necessarily RIPEMD160∘\circ∘SHA256, not necessarily 20-byte-valued), o.checkSig:Bytes→Bytes→Option Boolo.\mathrm{checkSig} : \mathrm{Bytes}\to\mathrm{Bytes}\to \mathrm{Option}\,\mathrm{Bool}o.checkSig:Bytes→Bytes→OptionBool (first argument the signature, second the public key; the value none\mathrm{none}none represents a script-aborting failure, some b\mathrm{some}\,bsomeb a verdict bbb), o.checkLockTime:Z→Boolo.\mathrm{checkLockTime} : \mathbb{Z}\to\mathrm{Bool}o.checkLockTime:Z→Bool and o.checkSequence:Z→Boolo.\mathrm{checkSequence} : \mathbb{Z}\to\mathrm{Bool}o.checkSequence:Z→Bool. Stacks are lists written top element first. castToBool(b)\mathrm{castToBool}(b)castToBool(b) is: false for the empty string; for a one-byte string [x][x][x], true iff x≠0x\neq 0x=0 and x≠0x80x \neq 0\mathrm{x}80x=0x80; for x::rx :: rx::r with rrr non-empty, true iff x≠0x\neq 0x=0 or castToBool(r)\mathrm{castToBool}(r)castToBool(r) (so a string of zero bytes whose last byte is 0x800\mathrm{x}800x80 is false). boolBytes(true)=[0x01]\mathrm{boolBytes}(\mathrm{true}) = [0\mathrm{x}01]boolBytes(true)=[0x01] and boolBytes(false)=[]\mathrm{boolBytes}(\mathrm{false}) = []boolBytes(false)=[] (the empty string). The script-number encoding ser(v)\mathrm{ser}(v)ser(v) of an integer vvv is: [][][] if v=0v = 0v=0; otherwise the little-endian bytes of ∣v∣|v|∣v∣ with no trailing zero byte, then, if the last of those bytes has its top bit (0x800\mathrm{x}800x80) set, an extra byte 0x800\mathrm{x}800x80 (for v<0v<0v<0) or 0x000\mathrm{x}000x00 (for v>0v>0v>0) is appended, and otherwise, for v<0v<0v<0, the top bit of the last byte is set. For example ser(2)=[0x02]\mathrm{ser}(2) = [0\mathrm{x}02]ser(2)=[0x02] and ser(32)=[0x20]\mathrm{ser}(32) = [0\mathrm{x}20]ser(32)=[0x20]. The decoding numk(b)\mathrm{num}_k(b)numk​(b) ("script number of at most kkk bytes, non-minimal encodings allowed") is none\mathrm{none}none if ∣b∣>k|b| > k∣b∣>k, and otherwise some\mathrm{some}some of the little-endian value of bbb with the top bit of the last byte read as a sign (the empty string decodes to 000).

The number push numInstr(n)\mathrm{numInstr}(n)numInstr(n) / numBytes(n)\mathrm{numBytes}(n)numBytes(n). For an integer nnn, numInstr(n)\mathrm{numInstr}(n)numInstr(n) is the single opcode with byte value n+0x50n + 0\mathrm{x}50n+0x50 when n=−1n = -1n=−1 or 1≤n≤161 \le n \le 161≤n≤16 (so numInstr(−1)\mathrm{numInstr}(-1)numInstr(−1) is the opcode 0x4f0\mathrm{x}4\mathrm{f}0x4f, numInstr(2)\mathrm{numInstr}(2)numInstr(2) is the opcode 0x520\mathrm{x}520x52, i.e. OP_2), and otherwise it is the data push of ser(n)\mathrm{ser}(n)ser(n) (so numInstr(32)\mathrm{numInstr}(32)numInstr(32) is the push of the one-byte string [0x20][0\mathrm{x}20][0x20], and numInstr(0)\mathrm{numInstr}(0)numInstr(0) is the push of the empty string). numBytes(n)\mathrm{numBytes}(n)numBytes(n) is the byte-level form: the one opcode byte n+0x50n + 0\mathrm{x}50n+0x50 in the first case, and pushBytes(ser(n))\mathrm{pushBytes}(\mathrm{ser}(n))pushBytes(ser(n)) otherwise, where pushBytes(d)\mathrm{pushBytes}(d)pushBytes(d) is the canonical push encoding of a byte string ddd: [ ∣d∣ ]+ ⁣ ⁣+d[\,|d|\,] \mathbin{+\!\!+} d[∣d∣]++d if ∣d∣<0x4c|d| < 0\mathrm{x}4\mathrm{c}∣d∣<0x4c; [0x4c,∣d∣]+ ⁣ ⁣+d[0\mathrm{x}4\mathrm{c}, |d|] \mathbin{+\!\!+} d[0x4c,∣d∣]++d if ∣d∣≤0xff|d| \le 0\mathrm{xff}∣d∣≤0xff; [0x4d]+ ⁣ ⁣+(2 little-endian bytes of ∣d∣)+ ⁣ ⁣+d[0\mathrm{x}4\mathrm{d}] \mathbin{+\!\!+} (\text{2 little-endian bytes of } |d|) \mathbin{+\!\!+} d[0x4d]++(2 little-endian bytes of ∣d∣)++d if ∣d∣≤0xffff|d| \le 0\mathrm{xffff}∣d∣≤0xffff; and [0x4e]+ ⁣ ⁣+(4 little-endian bytes of ∣d∣)+ ⁣ ⁣+d[0\mathrm{x}4\mathrm{e}] \mathbin{+\!\!+} (\text{4 little-endian bytes of } |d|) \mathbin{+\!\!+} d[0x4e]++(4 little-endian bytes of ∣d∣)++d otherwise.

The multisignature matching loop. msLoopo(sigs,keys)\mathrm{msLoop}_o(\mathit{sigs}, \mathit{keys})msLoopo​(sigs,keys), for lists of signatures and keys (top first), is defined by recursion: it is some true\mathrm{some}\,\mathrm{true}sometrue when sigs\mathit{sigs}sigs is empty; some false\mathrm{some}\,\mathrm{false}somefalse when sigs\mathit{sigs}sigs is non-empty and keys\mathit{keys}keys is empty; and for sigs=s::S\mathit{sigs} = s :: Ssigs=s::S, keys=k::K\mathit{keys} = k :: Kkeys=k::K it evaluates o.checkSig(s,k)o.\mathrm{checkSig}(s, k)o.checkSig(s,k): if that is none\mathrm{none}none the loop is none\mathrm{none}none; if it is some true\mathrm{some}\,\mathrm{true}sometrue the loop is some false\mathrm{some}\,\mathrm{false}somefalse when ∣K∣<∣S∣|K| < |S|∣K∣<∣S∣ and otherwise msLoopo(S,K)\mathrm{msLoop}_o(S, K)msLoopo​(S,K); if it is some false\mathrm{some}\,\mathrm{false}somefalse the loop is some false\mathrm{some}\,\mathrm{false}somefalse when ∣K∣<∣S∣+1|K| < |S| + 1∣K∣<∣S∣+1 and otherwise msLoopo(s::S,K)\mathrm{msLoop}_o(s :: S, K)msLoopo​(s::S,K). Unfolded on exactly two signatures and two keys, msLoopo([s1,s2],[k1,k2])\mathrm{msLoop}_o([s_1, s_2], [k_1, k_2])msLoopo​([s1​,s2​],[k1​,k2​]) is: none\mathrm{none}none if o.checkSig(s1,k1)=noneo.\mathrm{checkSig}(s_1, k_1) = \mathrm{none}o.checkSig(s1​,k1​)=none; some false\mathrm{some}\,\mathrm{false}somefalse if o.checkSig(s1,k1)=some falseo.\mathrm{checkSig}(s_1, k_1) = \mathrm{some}\,\mathrm{false}o.checkSig(s1​,k1​)=somefalse (since 1<21 < 21<2; the second signature is then never checked); and if o.checkSig(s1,k1)=some trueo.\mathrm{checkSig}(s_1, k_1) = \mathrm{some}\,\mathrm{true}o.checkSig(s1​,k1​)=sometrue it is none\mathrm{none}none, some true\mathrm{some}\,\mathrm{true}sometrue or some false\mathrm{some}\,\mathrm{false}somefalse according as o.checkSig(s2,k2)o.\mathrm{checkSig}(s_2, k_2)o.checkSig(s2​,k2​) is none\mathrm{none}none, some true\mathrm{some}\,\mathrm{true}sometrue or some false\mathrm{some}\,\mathrm{false}somefalse.


1. offeredScript =Off(r,Kr,Kl,h)= \mathrm{Off}(r, K_r, K_l, h)=Off(r,Kr​,Kl​,h)

For four arbitrary byte strings rrr (revHash), KrK_rKr​ (remoteKey), KlK_lKl​ (localKey), hhh (payHash), with no constraint on their lengths or contents (in particular rrr and hhh need not be 20 bytes long nor be hash values of anything), Off(r,Kr,Kl,h)\mathrm{Off}(r, K_r, K_l, h)Off(r,Kr​,Kl​,h) is the following list of 26 parsed instructions, where ⟨d⟩\langle d\rangle⟨d⟩ is the data push of ddd: OP_DUP, OP_HASH160, ⟨r⟩\langle r\rangle⟨r⟩, OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE, ⟨Kr⟩\langle K_r\rangle⟨Kr​⟩, OP_SWAP, OP_SIZE, numInstr(32)\mathrm{numInstr}(32)numInstr(32) =⟨[0x20]⟩= \langle[0\mathrm{x}20]\rangle=⟨[0x20]⟩, OP_EQUAL, OP_NOTIF, OP_DROP, numInstr(2)\mathrm{numInstr}(2)numInstr(2) === the opcode 0x520\mathrm{x}520x52 (OP_2), OP_SWAP, ⟨Kl⟩\langle K_l\rangle⟨Kl​⟩, numInstr(2)\mathrm{numInstr}(2)numInstr(2) =0x52= 0\mathrm{x}52=0x52, OP_CHECKMULTISIG, OP_ELSE, OP_HASH160, ⟨h⟩\langle h\rangle⟨h⟩, OP_EQUALVERIFY, OP_CHECKSIG, OP_ENDIF, OP_ENDIF. The opcode bytes are OP_DUP =0x76= 0\mathrm{x}76=0x76, OP_HASH160 =0xa9= 0\mathrm{xa}9=0xa9, OP_EQUAL =0x87= 0\mathrm{x}87=0x87, OP_IF =0x63= 0\mathrm{x}63=0x63, OP_CHECKSIG =0xac= 0\mathrm{xac}=0xac, OP_ELSE =0x67= 0\mathrm{x}67=0x67, OP_SWAP =0x7c= 0\mathrm{x}7\mathrm{c}=0x7c, OP_SIZE =0x82= 0\mathrm{x}82=0x82, OP_NOTIF =0x64= 0\mathrm{x}64=0x64, OP_DROP =0x75= 0\mathrm{x}75=0x75, OP_CHECKMULTISIG =0xae= 0\mathrm{xae}=0xae, OP_EQUALVERIFY =0x88= 0\mathrm{x}88=0x88, OP_ENDIF =0x68= 0\mathrm{x}68=0x68.

2. receivedScript =Rec(r,Kr,Kl,h,e)= \mathrm{Rec}(r, K_r, K_l, h, e)=Rec(r,Kr​,Kl​,h,e)

For the same four arbitrary byte strings and an arbitrary integer eee (expiry; any integer, possibly zero or negative), Rec(r,Kr,Kl,h,e)\mathrm{Rec}(r, K_r, K_l, h, e)Rec(r,Kr​,Kl​,h,e) is the following list of 28 parsed instructions: OP_DUP, OP_HASH160, ⟨r⟩\langle r\rangle⟨r⟩, OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE, ⟨Kr⟩\langle K_r\rangle⟨Kr​⟩, OP_SWAP, OP_SIZE, ⟨[0x20]⟩\langle[0\mathrm{x}20]\rangle⟨[0x20]⟩, OP_EQUAL, OP_IF, OP_HASH160, ⟨h⟩\langle h\rangle⟨h⟩, OP_EQUALVERIFY, 0x520\mathrm{x}520x52 (OP_2), OP_SWAP, ⟨Kl⟩\langle K_l\rangle⟨Kl​⟩, 0x520\mathrm{x}520x52 (OP_2), OP_CHECKMULTISIG, OP_ELSE, OP_DROP, numInstr(e)\mathrm{numInstr}(e)numInstr(e), OP_CHECKLOCKTIMEVERIFY (=0xb1= 0\mathrm{xb}1=0xb1), OP_DROP, OP_CHECKSIG, OP_ENDIF, OP_ENDIF. Here numInstr(e)\mathrm{numInstr}(e)numInstr(e) is the opcode e+0x50e + 0\mathrm{x}50e+0x50 when e=−1e = -1e=−1 or 1≤e≤161 \le e \le 161≤e≤16, and otherwise the data push of ser(e)\mathrm{ser}(e)ser(e) (the empty push for e=0e = 0e=0; for e≤−2e \le -2e≤−2 a push whose last byte carries the sign bit; for large eee a push of as many bytes as the little-endian magnitude needs, possibly more than 5).

3. offeredBytes =OffB(r,Kr,Kl,h)= \mathrm{OffB}(r, K_r, K_l, h)=OffB(r,Kr​,Kl​,h)

For the same four arbitrary byte strings, OffB(r,Kr,Kl,h)\mathrm{OffB}(r, K_r, K_l, h)OffB(r,Kr​,Kl​,h) is the byte string obtained by concatenating, in order: [0x76,0xa9][0\mathrm{x}76, 0\mathrm{xa}9][0x76,0xa9]; pushBytes(r)\mathrm{pushBytes}(r)pushBytes(r); [0x87,0x63,0xac,0x67][0\mathrm{x}87, 0\mathrm{x}63, 0\mathrm{xac}, 0\mathrm{x}67][0x87,0x63,0xac,0x67]; pushBytes(Kr)\mathrm{pushBytes}(K_r)pushBytes(Kr​); [0x7c,0x82][0\mathrm{x}7\mathrm{c}, 0\mathrm{x}82][0x7c,0x82]; numBytes(32)=[0x01,0x20]\mathrm{numBytes}(32) = [0\mathrm{x}01, 0\mathrm{x}20]numBytes(32)=[0x01,0x20]; [0x87,0x64,0x75][0\mathrm{x}87, 0\mathrm{x}64, 0\mathrm{x}75][0x87,0x64,0x75]; numBytes(2)=[0x52]\mathrm{numBytes}(2) = [0\mathrm{x}52]numBytes(2)=[0x52]; [0x7c][0\mathrm{x}7\mathrm{c}][0x7c]; pushBytes(Kl)\mathrm{pushBytes}(K_l)pushBytes(Kl​); numBytes(2)=[0x52]\mathrm{numBytes}(2) = [0\mathrm{x}52]numBytes(2)=[0x52]; [0xae,0x67,0xa9][0\mathrm{xae}, 0\mathrm{x}67, 0\mathrm{xa}9][0xae,0x67,0xa9]; pushBytes(h)\mathrm{pushBytes}(h)pushBytes(h); [0x88,0xac,0x68,0x68][0\mathrm{x}88, 0\mathrm{xac}, 0\mathrm{x}68, 0\mathrm{x}68][0x88,0xac,0x68,0x68]. Each pushBytes(d)\mathrm{pushBytes}(d)pushBytes(d) is the canonical push encoding defined above (a one-byte length prefix when ∣d∣<76|d| < 76∣d∣<76, otherwise a PUSHDATA1/2/4 prefix according to ∣d∣|d|∣d∣).

4. receivedBytes =RecB(r,Kr,Kl,h,e)= \mathrm{RecB}(r, K_r, K_l, h, e)=RecB(r,Kr​,Kl​,h,e)

For the same parameters and an arbitrary integer eee, RecB(r,Kr,Kl,h,e)\mathrm{RecB}(r, K_r, K_l, h, e)RecB(r,Kr​,Kl​,h,e) is the concatenation, in order, of: [0x76,0xa9][0\mathrm{x}76, 0\mathrm{xa}9][0x76,0xa9]; pushBytes(r)\mathrm{pushBytes}(r)pushBytes(r); [0x87,0x63,0xac,0x67][0\mathrm{x}87, 0\mathrm{x}63, 0\mathrm{xac}, 0\mathrm{x}67][0x87,0x63,0xac,0x67]; pushBytes(Kr)\mathrm{pushBytes}(K_r)pushBytes(Kr​); [0x7c,0x82][0\mathrm{x}7\mathrm{c}, 0\mathrm{x}82][0x7c,0x82]; numBytes(32)=[0x01,0x20]\mathrm{numBytes}(32) = [0\mathrm{x}01, 0\mathrm{x}20]numBytes(32)=[0x01,0x20]; [0x87,0x63,0xa9][0\mathrm{x}87, 0\mathrm{x}63, 0\mathrm{xa}9][0x87,0x63,0xa9]; pushBytes(h)\mathrm{pushBytes}(h)pushBytes(h); [0x88][0\mathrm{x}88][0x88]; numBytes(2)=[0x52]\mathrm{numBytes}(2) = [0\mathrm{x}52]numBytes(2)=[0x52]; [0x7c][0\mathrm{x}7\mathrm{c}][0x7c]; pushBytes(Kl)\mathrm{pushBytes}(K_l)pushBytes(Kl​); numBytes(2)=[0x52]\mathrm{numBytes}(2) = [0\mathrm{x}52]numBytes(2)=[0x52]; [0xae,0x67,0x75][0\mathrm{xae}, 0\mathrm{x}67, 0\mathrm{x}75][0xae,0x67,0x75]; numBytes(e)\mathrm{numBytes}(e)numBytes(e); [0xb1,0x75,0xac,0x68,0x68][0\mathrm{xb}1, 0\mathrm{x}75, 0\mathrm{xac}, 0\mathrm{x}68, 0\mathrm{x}68][0xb1,0x75,0xac,0x68,0x68], where numBytes(e)\mathrm{numBytes}(e)numBytes(e) is the single byte e+0x50e + 0\mathrm{x}50e+0x50 when e=−1e = -1e=−1 or 1≤e≤161 \le e \le 161≤e≤16 and pushBytes(ser(e))\mathrm{pushBytes}(\mathrm{ser}(e))pushBytes(ser(e)) otherwise ([0x00][0\mathrm{x}00][0x00], the empty push, for e=0e = 0e=0).

5. CltvPasses

For a flag set fff, an oracle ooo and an integer eee, CltvPasses(f,o,e)\mathrm{CltvPasses}(f, o, e)CltvPasses(f,o,e) is the proposition

f.cltv=false ∨ (0≤e ∧ o.checkLockTime(e)=true).f.\mathrm{cltv} = \mathrm{false}\ \lor\ \bigl(0 \le e\ \land\ o.\mathrm{checkLockTime}(e) = \mathrm{true}\bigr).f.cltv=false ∨ (0≤e ∧ o.checkLockTime(e)=true).

It holds for every eee when the cltv flag is off; when the flag is on it requires e≥0e \ge 0e≥0 and acceptance by the oracle's (arbitrary) lock-time function. No disable bit, size bound, or relation between eee and 2322^{32}232 or 500 000 000500\,000\,000500000000 is part of this definition.

6. Decidability instance for CltvPasses

The file declares that, for every flag set fff, oracle ooo and integer eee, the proposition CltvPasses(f,o,e)\mathrm{CltvPasses}(f, o, e)CltvPasses(f,o,e) is decidable, by unfolding it to the disjunction/conjunction of decidable Boolean equalities and the integer comparison above. This is what allows the theorem statements to write "if CltvPasses(f,o,e)\mathrm{CltvPasses}(f, o, e)CltvPasses(f,o,e) then … else …". It asserts nothing beyond decidability.

7. OfferedSpend

For a flag set fff, an oracle ooo, byte strings r,Kr,Kl,hr, K_r, K_l, hr,Kr​,Kl​,h and a list of byte strings st\mathit{st}st (a stack, top first), OfferedSpend(f,o,r,Kr,Kl,h,st)\mathrm{OfferedSpend}(f, o, r, K_r, K_l, h, \mathit{st})OfferedSpend(f,o,r,Kr​,Kl​,h,st) is the disjunction of exactly these three propositions. (i) Revocation disjunct: there exist byte strings pk\mathit{pk}pk and sig\mathit{sig}sig such that st=[pk,sig]\mathit{st} = [\mathit{pk}, \mathit{sig}]st=[pk,sig] (a two-element stack, pk\mathit{pk}pk on top), o.hash160(pk)=ro.\mathrm{hash160}(\mathit{pk}) = ro.hash160(pk)=r, and o.checkSig(sig,pk)=some trueo.\mathrm{checkSig}(\mathit{sig}, \mathit{pk}) = \mathrm{some}\,\mathrm{true}o.checkSig(sig,pk)=sometrue (the signature is checked against the stack element pk\mathit{pk}pk). (ii) Preimage disjunct: there exist byte strings pre\mathit{pre}pre and sig\mathit{sig}sig such that st=[pre,sig]\mathit{st} = [\mathit{pre}, \mathit{sig}]st=[pre,sig], o.hash160(pre)≠ro.\mathrm{hash160}(\mathit{pre}) \ne ro.hash160(pre)=r, ∣pre∣=32|\mathit{pre}| = 32∣pre∣=32, o.hash160(pre)=ho.\mathrm{hash160}(\mathit{pre}) = ho.hash160(pre)=h, and o.checkSig(sig,Kr)=some trueo.\mathrm{checkSig}(\mathit{sig}, K_r) = \mathrm{some}\,\mathrm{true}o.checkSig(sig,Kr​)=sometrue. (iii) Timeout disjunct: there exist byte strings x,σl,σr,dx, \sigma_l, \sigma_r, dx,σl​,σr​,d such that st=[x,σl,σr,d]\mathit{st} = [x, \sigma_l, \sigma_r, d]st=[x,σl​,σr​,d] (a four-element stack, xxx on top, ddd at the bottom), o.hash160(x)≠ro.\mathrm{hash160}(x) \ne ro.hash160(x)=r, ∣x∣≠32|x| \ne 32∣x∣=32, msLoopo([σl,σr],[Kl,Kr])=some true\mathrm{msLoop}_o([\sigma_l, \sigma_r], [K_l, K_r]) = \mathrm{some}\,\mathrm{true}msLoopo​([σl​,σr​],[Kl​,Kr​])=sometrue (unfolded above: both o.checkSig(σl,Kl)o.\mathrm{checkSig}(\sigma_l, K_l)o.checkSig(σl​,Kl​) and o.checkSig(σr,Kr)o.\mathrm{checkSig}(\sigma_r, K_r)o.checkSig(σr​,Kr​) must be some true\mathrm{some}\,\mathrm{true}sometrue), and f.nulldummy=true→d=[]f.\mathrm{nulldummy} = \mathrm{true} \to d = []f.nulldummy=true→d=[] (when the nulldummy flag is off, ddd may be anything). Consequently the predicate is false for every stack whose length is not 222 or 444, including the empty stack; disjuncts (i) and (ii) can both hold for the same two-element stack only if o.hash160o.\mathrm{hash160}o.hash160 of its top equals rrr and differs from rrr, which is impossible, so they are mutually exclusive, while (iii) concerns four-element stacks only; no disjunct bounds the sizes of the stack elements; and o.hash160o.\mathrm{hash160}o.hash160, o.checkSigo.\mathrm{checkSig}o.checkSig are the arbitrary oracle functions.

8. ReceivedSpend

For a flag set fff, an oracle ooo, byte strings r,Kr,Kl,hr, K_r, K_l, hr,Kr​,Kl​,h, an integer eee and a stack st\mathit{st}st, ReceivedSpend(f,o,r,Kr,Kl,h,e,st)\mathrm{ReceivedSpend}(f, o, r, K_r, K_l, h, e, \mathit{st})ReceivedSpend(f,o,r,Kr​,Kl​,h,e,st) is the disjunction of exactly these three propositions. (i) Revocation disjunct: there exist byte strings pk\mathit{pk}pk and sig\mathit{sig}sig such that st=[pk,sig]\mathit{st} = [\mathit{pk}, \mathit{sig}]st=[pk,sig], o.hash160(pk)=ro.\mathrm{hash160}(\mathit{pk}) = ro.hash160(pk)=r, and o.checkSig(sig,pk)=some trueo.\mathrm{checkSig}(\mathit{sig}, \mathit{pk}) = \mathrm{some}\,\mathrm{true}o.checkSig(sig,pk)=sometrue. (ii) Success disjunct: there exist byte strings pre,σl,σr,d\mathit{pre}, \sigma_l, \sigma_r, dpre,σl​,σr​,d such that st=[pre,σl,σr,d]\mathit{st} = [\mathit{pre}, \sigma_l, \sigma_r, d]st=[pre,σl​,σr​,d] (pre\mathit{pre}pre on top, ddd at the bottom), o.hash160(pre)≠ro.\mathrm{hash160}(\mathit{pre}) \ne ro.hash160(pre)=r, ∣pre∣=32|\mathit{pre}| = 32∣pre∣=32, o.hash160(pre)=ho.\mathrm{hash160}(\mathit{pre}) = ho.hash160(pre)=h, msLoopo([σl,σr],[Kl,Kr])=some true\mathrm{msLoop}_o([\sigma_l, \sigma_r], [K_l, K_r]) = \mathrm{some}\,\mathrm{true}msLoopo​([σl​,σr​],[Kl​,Kr​])=sometrue, and f.nulldummy=true→d=[]f.\mathrm{nulldummy} = \mathrm{true} \to d = []f.nulldummy=true→d=[]. (iii) Timeout disjunct: there exist byte strings xxx and sig\mathit{sig}sig such that st=[x,sig]\mathit{st} = [x, \mathit{sig}]st=[x,sig], o.hash160(x)≠ro.\mathrm{hash160}(x) \ne ro.hash160(x)=r, ∣x∣≠32|x| \ne 32∣x∣=32, CltvPasses(f,o,e)\mathrm{CltvPasses}(f, o, e)CltvPasses(f,o,e) holds (i.e. f.cltv=falsef.\mathrm{cltv} = \mathrm{false}f.cltv=false, or 0≤e0 \le e0≤e and o.checkLockTime(e)=trueo.\mathrm{checkLockTime}(e) = \mathrm{true}o.checkLockTime(e)=true), and o.checkSig(sig,Kr)=some trueo.\mathrm{checkSig}(\mathit{sig}, K_r) = \mathrm{some}\,\mathrm{true}o.checkSig(sig,Kr​)=sometrue. The predicate is false for every stack whose length is not 222 or 444 (including the empty stack); disjuncts (i) and (iii) both concern two-element stacks and are mutually exclusive via the rrr condition on the top element; no size bound is placed on any stack element; the expiry eee enters only through CltvPasses\mathrm{CltvPasses}CltvPasses in disjunct (iii), and only through the oracle's arbitrary checkLockTime\mathrm{checkLockTime}checkLockTime function.

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