Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

A revoked commitment transaction's outputs, the holder of the revocation key, and the cheater

Definition
BitcoinPenalty

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

bitcoinbitcoin-mainnetbolt3bolt5channel-safetycsvformal-verificationlightningpenaltyrevocation

Names over the to_local, HTLC, anchor, funding and lock-time missions.

CommitmentKeys — the keys of one commitment transaction: revPk and its hash revHash, localDelayedPk, remoteKey, localHtlcKey, remoteHtlcKey, the two funding keys, and toSelfDelay; Htlc — a payment hash and an expiry; WellFormed K, HtlcWellFormed h — the 520-byte bounds, 0≤0 \le0≤ toSelfDelay <216< 2^{16}<216, ∣expiry∣<232|{\rm expiry}| < 2^{32}∣expiry∣<232.

Output scripts — toLocalBytes K, offeredBytes K h, receivedBytes K h, their anchors forms, toRemoteBytes K, anchorLocalBytes K, anchorRemoteBytes K, all through the published constructors; p2wshOf sc — the pay-to-witness-script-hash program of a script; oracleOf c sc — the signature mission's legacyOracle for context ccc with witness-v0 sighash on sc.

HoldsRevocation o K — the oracle accepts some signature (at most 520 bytes) for revPk, and hash160 revPk = revHash: the honest party after a revocation. NoRevocationSig o K — the oracle accepts no signature for revPk: the cheater, who never learned the revocation secret. NoRevHashPreimage o K — revPk is the only hash160 preimage of revHash the oracle knows (second-preimage resistance, needed because the HTLC scripts test the hash of the pushed key).

Witnesses — toLocalPenaltyWitness K sigR = [sigR, [1], script], htlcPenaltyWitness K sigR sc = [sigR, revPk, sc]. Cheater paths — DelayedSpend (the to_local delayed path), OfferedNonRevocation, ReceivedNonRevocation and their anchors forms (the HTLC paths other than revocation, with the one-block delay in the anchors form).

Formalization Note. Everything else is the published missions'. Unforgeability is a hypothesis on one oracle and one key; there is no key derivation, signing oracle, ECDSA or network model. Only Std is imported.

Definition code
import Definitions.Def_BitcoinToLocal
import Definitions.Def_BitcoinHtlc
import Definitions.Def_BitcoinAnchors
import Definitions.Def_BitcoinFunding
import Definitions.Def_BitcoinHtlcLocks

/-!
# The Lightning penalty: a revoked commitment transaction under Bitcoin's script interpreter

BOLT #3 (*Bitcoin Transaction and Script Formats*) and BOLT #5 (*Recommendations for On-chain
Transaction Handling*) rest on one mechanism: when a party broadcasts a commitment transaction it
has already *revoked*, the counterparty, who learned the revocation secret, can spend every
output of that transaction at once with the revocation key, while the broadcaster itself is held
back by `to_self_delay` on its own balance. This file names the outputs of such a commitment
transaction, built from the script constructors of the `BitcoinToLocal`, `BitcoinHtlc` and
`BitcoinAnchors` missions, and the two hypotheses on the interpreter's signature oracle under
which the penalty is stated:

* `HoldsRevocation o K`: the oracle accepts some (witness-sized) signature for the revocation
  public key `revPk`, and `hash160 revPk` is the hash `revHash` the HTLC scripts carry — the
  honest party's side.
* `NoRevocationSig o K`: the oracle accepts *no* signature for `revPk` — the cheater's side, an
  unforgeability hypothesis on the oracle for this one key. `NoRevHashPreimage o K` adds that
  `revPk` is the only byte string hashing to `revHash` (second-preimage resistance of `hash160`
  at `revPk`), which the HTLC scripts need since they check the hash of the pushed key.

The oracle is abstract (`BitcoinScript.Oracle`); the `*_p2wsh_spend` theorems of the sibling
missions instantiate it with `BitcoinSig.legacyOracle` on the spending transaction, the input
index, the amount and the witness script (`oracleOf`). Witnesses are listed in `CScriptWitness`
order (last element on top of the stack, the script last), as `BitcoinVerify.verifyScript` takes
them.
-/

namespace BitcoinPenalty

open BitcoinScript BitcoinSig BitcoinVerify BitcoinToLocal

/-- The keys and parameters of one commitment transaction, as seen from the party that
broadcasts it ("local"): the revocation key and its `hash160`, the delayed key of `to_local`, the
`to_remote` key, the two HTLC keys, the two funding keys (the anchors' keys) and `to_self_delay`. -/
structure CommitmentKeys where
  revPk : Bytes
  revHash : Bytes
  localDelayedPk : Bytes
  remoteKey : Bytes
  localHtlcKey : Bytes
  remoteHtlcKey : Bytes
  fundingKeyLocal : Bytes
  fundingKeyRemote : Bytes
  toSelfDelay : Int

/-- One HTLC output's parameters: the (`hash160`) payment hash and the `cltv_expiry`. -/
structure Htlc where
  payHash : Bytes
  expiry : Int

/-- The size and range hypotheses of the sibling missions' theorems: every pushed string fits a
stack element (`MAX_SCRIPT_ELEMENT_SIZE = 520`) and `to_self_delay` is a plain block count below
`2 ^ 16` (no sequence-lock flag bits). -/
structure WellFormed (K : CommitmentKeys) : Prop where
  revPk : K.revPk.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  revHash : K.revHash.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  localDelayedPk : K.localDelayedPk.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  remoteKey : K.remoteKey.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  localHtlcKey : K.localHtlcKey.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  remoteHtlcKey : K.remoteHtlcKey.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  fundingKeyLocal : K.fundingKeyLocal.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  fundingKeyRemote : K.fundingKeyRemote.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  delayNonneg : 0 ≤ K.toSelfDelay
  delayLt : K.toSelfDelay < 2 ^ 16

/-- The size and range hypotheses on one HTLC: the payment hash fits a stack element and the
expiry is a 5-byte script number. -/
structure HtlcWellFormed (h : Htlc) : Prop where
  payHash : h.payHash.length ≤ MAX_SCRIPT_ELEMENT_SIZE
  expiry : h.expiry.natAbs < 2 ^ 32

/-! ### The outputs of the commitment transaction (witness scripts as `CScript` bytes) -/

/-- `to_local`: `BitcoinToLocal.toLocalBytes revPk to_self_delay localDelayedPk`. -/
def toLocalBytes (K : CommitmentKeys) : Bytes :=
  BitcoinToLocal.toLocalBytes K.revPk K.toSelfDelay K.localDelayedPk

/-- An offered HTLC (non-anchor form). -/
def offeredBytes (K : CommitmentKeys) (h : Htlc) : Bytes :=
  BitcoinHtlc.offeredBytes K.revHash K.remoteHtlcKey K.localHtlcKey h.payHash

/-- A received HTLC (non-anchor form). -/
def receivedBytes (K : CommitmentKeys) (h : Htlc) : Bytes :=
  BitcoinHtlc.receivedBytes K.revHash K.remoteHtlcKey K.localHtlcKey h.payHash h.expiry

/-- An offered HTLC with `option_anchors`. -/
def offeredBytesAnchors (K : CommitmentKeys) (h : Htlc) : Bytes :=
  BitcoinAnchors.offeredBytes K.revHash K.remoteHtlcKey K.localHtlcKey h.payHash

/-- A received HTLC with `option_anchors`. -/
def receivedBytesAnchors (K : CommitmentKeys) (h : Htlc) : Bytes :=
  BitcoinAnchors.receivedBytes K.revHash K.remoteHtlcKey K.localHtlcKey h.payHash h.expiry

/-- `to_remote` with `option_anchors` (`<remote_pubkey> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY`). -/
def toRemoteBytes (K : CommitmentKeys) : Bytes := BitcoinAnchors.toRemoteBytes K.remoteKey

/-- The broadcaster's anchor (keyed by its funding key). -/
def anchorLocalBytes (K : CommitmentKeys) : Bytes := BitcoinAnchors.anchorBytes K.fundingKeyLocal

/-- The counterparty's anchor (keyed by its funding key). -/
def anchorRemoteBytes (K : CommitmentKeys) : Bytes := BitcoinAnchors.anchorBytes K.fundingKeyRemote

/-- The P2WSH output script paying to a witness script: `OP_0 <SHA256(script)>`. -/
def p2wshOf (script : Bytes) : Bytes := witnessBytes 0 (BitcoinSer.sha256 script)

/-- The concrete oracle under which `verifyScript` runs a version-0 witness script `script` in
the verification context `c` (what the `*_p2wsh_spend` theorems instantiate). -/
def oracleOf (c : Ctx) (script : Bytes) : Oracle :=
  legacyOracle (sigFlagsOf c.flags) c.tx c.nIn c.amount .witnessV0 script

/-! ### The two sides of the penalty -/

/-- The honest party holds the revocation key: the oracle accepts some signature `sigR` (of
witness-element size) for `revPk`, and `revPk` hashes to the `revHash` of the HTLC scripts. -/
def HoldsRevocation (o : Oracle) (K : CommitmentKeys) : Prop :=
  (∃ sigR, sigR.length ≤ MAX_SCRIPT_ELEMENT_SIZE ∧ o.checkSig sigR K.revPk = some true) ∧
  o.hash160 K.revPk = K.revHash

/-- The cheater has no signature for the revocation key: the oracle accepts none. -/
def NoRevocationSig (o : Oracle) (K : CommitmentKeys) : Prop :=
  ∀ sig, o.checkSig sig K.revPk ≠ some true

/-- `revPk` is the only byte string the oracle hashes to `revHash`. -/
def NoRevHashPreimage (o : Oracle) (K : CommitmentKeys) : Prop :=
  ∀ pk, o.hash160 pk = K.revHash → pk = K.revPk

/-! ### The penalty witnesses (`CScriptWitness` order, script last) -/

/-- The `to_local` penalty witness `<revocation_sig> 1 <witness_script>`. -/
def toLocalPenaltyWitness (K : CommitmentKeys) (sigR : Bytes) : List Bytes :=
  [sigR, [1], toLocalBytes K]

/-- An HTLC penalty witness `<revocation_sig> <revocationpubkey> <witness_script>`. -/
def htlcPenaltyWitness (K : CommitmentKeys) (sigR script : Bytes) : List Bytes :=
  [sigR, K.revPk, script]

/-! ### The paths left to the cheater -/

/-- `BitcoinToLocal.SpendCondition` without its revocation path: `b` false, the delay check
passing and `sig` valid for the delayed key. -/
def DelayedSpend (f : Flags) (o : Oracle) (K : CommitmentKeys) (b sig : Bytes) : Prop :=
  castToBool b = false ∧ CsvPasses f o K.toSelfDelay ∧ o.checkSig sig K.localDelayedPk = some true

/-- `BitcoinHtlc.OfferedSpend` without its revocation path: the preimage path or the timeout path. -/
def OfferedNonRevocation (f : Flags) (o : Oracle) (K : CommitmentKeys) (h : Htlc) (st : List Bytes) : Prop :=
  (∃ preimage sig, st = [preimage, sig] ∧ o.hash160 preimage ≠ K.revHash ∧ preimage.length = 32 ∧
      o.hash160 preimage = h.payHash ∧ o.checkSig sig K.remoteHtlcKey = some true) ∨
  (∃ x localSig remoteSig dummy, st = [x, localSig, remoteSig, dummy] ∧ o.hash160 x ≠ K.revHash ∧ x.length ≠ 32 ∧
      multisigLoop o [localSig, remoteSig] [K.localHtlcKey, K.remoteHtlcKey] = some true ∧
      (f.nulldummy = true → dummy = []))

/-- `BitcoinHtlc.ReceivedSpend` without its revocation path: the success path or the timeout path. -/
def ReceivedNonRevocation (f : Flags) (o : Oracle) (K : CommitmentKeys) (h : Htlc) (st : List Bytes) : Prop :=
  (∃ preimage localSig remoteSig dummy, st = [preimage, localSig, remoteSig, dummy] ∧
      o.hash160 preimage ≠ K.revHash ∧ preimage.length = 32 ∧ o.hash160 preimage = h.payHash ∧
      multisigLoop o [localSig, remoteSig] [K.localHtlcKey, K.remoteHtlcKey] = some true ∧
      (f.nulldummy = true → dummy = [])) ∨
  (∃ x sig, st = [x, sig] ∧ o.hash160 x ≠ K.revHash ∧ x.length ≠ 32 ∧ BitcoinHtlc.CltvPasses f o h.expiry ∧
      o.checkSig sig K.remoteHtlcKey = some true)

/-- `BitcoinAnchors.OfferedSpend` without its revocation path: `OfferedNonRevocation` with the
one-block delay check on both paths. -/
def OfferedNonRevocationAnchors (f : Flags) (o : Oracle) (K : CommitmentKeys) (h : Htlc) (st : List Bytes) : Prop :=
  OfferedNonRevocation f o K h st ∧ CsvPasses f o 1

/-- `BitcoinAnchors.ReceivedSpend` without its revocation path: `ReceivedNonRevocation` with the
one-block delay check on both paths. -/
def ReceivedNonRevocationAnchors (f : Flags) (o : Oracle) (K : CommitmentKeys) (h : Htlc) (st : List Bytes) : Prop :=
  ReceivedNonRevocation f o K h st ∧ CsvPasses f o 1

end BitcoinPenalty
Source
BOLT #3 (commitment transaction outputs, revocation) and BOLT #5 (penalty transactions): https://github.com/lightning/bolts/blob/master/03-transactions.md, https://github.com/lightning/bolts/blob/master/05-onchain.md#revoked-transaction-close-handling; Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: VerifyScript, EvalScript, CheckSequence, IsFinalTx (src/script/interpreter.cpp, src/consensus/tx_verify.cpp); BIP68, BIP112, BIP141
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_BitcoinPenalty.lean (namespace BitcoinPenalty)

One paragraph per definition, in file order. Throughout, a byte string is a finite list of 8-bit words; ⟨d⟩\langle d\rangle⟨d⟩ denotes pushBytes(d)\mathrm{pushBytes}(d)pushBytes(d) (the canonical data push: length byte then ddd for ∣d∣<76|d| < 76∣d∣<76, OP_PUSHDATA1/2/4 forms otherwise) and [n][n][n] denotes numBytes(n)\mathrm{numBytes}(n)numBytes(n) (the single opcode byte 0x50+n\mathtt{0x50}+n0x50+n for n=−1n = -1n=−1 or 1≤n≤161 \le n \le 161≤n≤16, otherwise pushBytes\mathrm{pushBytes}pushBytes of the CScriptNum encoding of nnn, which is the single byte 0x00\mathtt{0x00}0x00 for n=0n = 0n=0). The constant MAX_SCRIPT_ELEMENT_SIZE\mathrm{MAX\_SCRIPT\_ELEMENT\_SIZE}MAX_SCRIPT_ELEMENT_SIZE is 520520520. An oracle ooo is a record of eight functions: o.ripemd160o.\mathrm{ripemd160}o.ripemd160, o.sha1o.\mathrm{sha1}o.sha1, o.sha256o.\mathrm{sha256}o.sha256, o.hash160o.\mathrm{hash160}o.hash160, o.hash256o.\mathrm{hash256}o.hash256 (byte strings to byte strings), o.checkSigo.\mathrm{checkSig}o.checkSig (two byte strings to {none,some true,some false}\{\mathrm{none}, \mathrm{some\ true}, \mathrm{some\ false}\}{none,some true,some false}), o.checkLockTimeo.\mathrm{checkLockTime}o.checkLockTime and o.checkSequenceo.\mathrm{checkSequence}o.checkSequence (integers to booleans). A flag triple fff has booleans f.cltvf.\mathrm{cltv}f.cltv, f.csvf.\mathrm{csv}f.csv, f.nulldummyf.\mathrm{nulldummy}f.nulldummy.

CommitmentKeys is a structure with nine fields, no constraints: eight byte strings revPk\mathrm{revPk}revPk, revHash\mathrm{revHash}revHash, localDelayedPk\mathrm{localDelayedPk}localDelayedPk, remoteKey\mathrm{remoteKey}remoteKey, localHtlcKey\mathrm{localHtlcKey}localHtlcKey, remoteHtlcKey\mathrm{remoteHtlcKey}remoteHtlcKey, fundingKeyLocal\mathrm{fundingKeyLocal}fundingKeyLocal, fundingKeyRemote\mathrm{fundingKeyRemote}fundingKeyRemote, and one integer toSelfDelay∈Z\mathrm{toSelfDelay} \in \mathbb ZtoSelfDelay∈Z. Any byte strings and any integer (negative, zero, huge) form a valid record; nothing relates revHash\mathrm{revHash}revHash to revPk\mathrm{revPk}revPk.

Htlc is a structure with two fields, no constraints: a byte string payHash\mathrm{payHash}payHash and an integer expiry∈Z\mathrm{expiry} \in \mathbb Zexpiry∈Z.

WellFormed(K)(K)(K), for a CommitmentKeys KKK, is a proposition that is the conjunction of exactly ten facts: ∣K.revPk∣≤520|K.\mathrm{revPk}| \le 520∣K.revPk∣≤520, ∣K.revHash∣≤520|K.\mathrm{revHash}| \le 520∣K.revHash∣≤520, ∣K.localDelayedPk∣≤520|K.\mathrm{localDelayedPk}| \le 520∣K.localDelayedPk∣≤520, ∣K.remoteKey∣≤520|K.\mathrm{remoteKey}| \le 520∣K.remoteKey∣≤520, ∣K.localHtlcKey∣≤520|K.\mathrm{localHtlcKey}| \le 520∣K.localHtlcKey∣≤520, ∣K.remoteHtlcKey∣≤520|K.\mathrm{remoteHtlcKey}| \le 520∣K.remoteHtlcKey∣≤520, ∣K.fundingKeyLocal∣≤520|K.\mathrm{fundingKeyLocal}| \le 520∣K.fundingKeyLocal∣≤520, ∣K.fundingKeyRemote∣≤520|K.\mathrm{fundingKeyRemote}| \le 520∣K.fundingKeyRemote∣≤520, 0≤K.toSelfDelay0 \le K.\mathrm{toSelfDelay}0≤K.toSelfDelay, and K.toSelfDelay<216K.\mathrm{toSelfDelay} < 2^{16}K.toSelfDelay<216. It imposes nothing else (no key validity, no 20-byte hash length, no hash relation).

HtlcWellFormed(h)(h)(h), for an Htlc hhh, is the conjunction of two facts: ∣h.payHash∣≤520|h.\mathrm{payHash}| \le 520∣h.payHash∣≤520 and ∣h.expiry∣<232|h.\mathrm{expiry}| < 2^{32}∣h.expiry∣<232 (the absolute value of the integer; h.expiryh.\mathrm{expiry}h.expiry may be zero or negative).

toLocalBytes(K)(K)(K) is the byte string BitcoinToLocal.toLocalBytes(K.revPk,K.toSelfDelay,K.localDelayedPk)\mathrm{BitcoinToLocal.toLocalBytes}(K.\mathrm{revPk}, K.\mathrm{toSelfDelay}, K.\mathrm{localDelayedPk})BitcoinToLocal.toLocalBytes(K.revPk,K.toSelfDelay,K.localDelayedPk), which unfolds to OP_IF (0x63)(\mathtt{0x63})(0x63) ∥\Vert∥ ⟨K.revPk⟩\langle K.\mathrm{revPk}\rangle⟨K.revPk⟩ ∥\Vert∥ OP_ELSE (0x67)(\mathtt{0x67})(0x67) ∥\Vert∥ [K.toSelfDelay][K.\mathrm{toSelfDelay}][K.toSelfDelay] ∥\Vert∥ OP_CHECKSEQUENCEVERIFY OP_DROP (0xb2 0x75)(\mathtt{0xb2}\,\mathtt{0x75})(0xb20x75) ∥\Vert∥ ⟨K.localDelayedPk⟩\langle K.\mathrm{localDelayedPk}\rangle⟨K.localDelayedPk⟩ ∥\Vert∥ OP_ENDIF OP_CHECKSIG (0x68 0xac)(\mathtt{0x68}\,\mathtt{0xac})(0x680xac).

offeredBytes(K,h)(K, h)(K,h) is BitcoinHtlc.offeredBytes(K.revHash,K.remoteHtlcKey,K.localHtlcKey,h.payHash)\mathrm{BitcoinHtlc.offeredBytes}(K.\mathrm{revHash}, K.\mathrm{remoteHtlcKey}, K.\mathrm{localHtlcKey}, h.\mathrm{payHash})BitcoinHtlc.offeredBytes(K.revHash,K.remoteHtlcKey,K.localHtlcKey,h.payHash): OP_DUP OP_HASH160 ⟨K.revHash⟩\langle K.\mathrm{revHash}\rangle⟨K.revHash⟩ OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE ⟨K.remoteHtlcKey⟩\langle K.\mathrm{remoteHtlcKey}\rangle⟨K.remoteHtlcKey⟩ OP_SWAP OP_SIZE [32][32][32] OP_EQUAL OP_NOTIF OP_DROP [2][2][2] OP_SWAP ⟨K.localHtlcKey⟩\langle K.\mathrm{localHtlcKey}\rangle⟨K.localHtlcKey⟩ [2][2][2] OP_CHECKMULTISIG OP_ELSE OP_HASH160 ⟨h.payHash⟩\langle h.\mathrm{payHash}\rangle⟨h.payHash⟩ OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF OP_ENDIF. (Argument order: the second key argument of the HTLC constructor, "remoteKey", receives K.remoteHtlcKeyK.\mathrm{remoteHtlcKey}K.remoteHtlcKey and the third, "localKey", receives K.localHtlcKeyK.\mathrm{localHtlcKey}K.localHtlcKey.)

receivedBytes(K,h)(K, h)(K,h) is BitcoinHtlc.receivedBytes(K.revHash,K.remoteHtlcKey,K.localHtlcKey,h.payHash,h.expiry)\mathrm{BitcoinHtlc.receivedBytes}(K.\mathrm{revHash}, K.\mathrm{remoteHtlcKey}, K.\mathrm{localHtlcKey}, h.\mathrm{payHash}, h.\mathrm{expiry})BitcoinHtlc.receivedBytes(K.revHash,K.remoteHtlcKey,K.localHtlcKey,h.payHash,h.expiry): OP_DUP OP_HASH160 ⟨K.revHash⟩\langle K.\mathrm{revHash}\rangle⟨K.revHash⟩ OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE ⟨K.remoteHtlcKey⟩\langle K.\mathrm{remoteHtlcKey}\rangle⟨K.remoteHtlcKey⟩ OP_SWAP OP_SIZE [32][32][32] OP_EQUAL OP_IF OP_HASH160 ⟨h.payHash⟩\langle h.\mathrm{payHash}\rangle⟨h.payHash⟩ OP_EQUALVERIFY [2][2][2] OP_SWAP ⟨K.localHtlcKey⟩\langle K.\mathrm{localHtlcKey}\rangle⟨K.localHtlcKey⟩ [2][2][2] OP_CHECKMULTISIG OP_ELSE OP_DROP [h.expiry][h.\mathrm{expiry}][h.expiry] OP_CHECKLOCKTIMEVERIFY OP_DROP OP_CHECKSIG OP_ENDIF OP_ENDIF.

offeredBytesAnchors(K,h)(K, h)(K,h) is BitcoinAnchors.offeredBytes(K.revHash,K.remoteHtlcKey,K.localHtlcKey,h.payHash)\mathrm{BitcoinAnchors.offeredBytes}(K.\mathrm{revHash}, K.\mathrm{remoteHtlcKey}, K.\mathrm{localHtlcKey}, h.\mathrm{payHash})BitcoinAnchors.offeredBytes(K.revHash,K.remoteHtlcKey,K.localHtlcKey,h.payHash): the same byte string as offeredBytes(K,h)(K,h)(K,h) except that between the inner OP_ENDIF and the outer OP_ENDIF the bytes [1][1][1] OP_CHECKSEQUENCEVERIFY OP_DROP (0x51 0xb2 0x75\mathtt{0x51}\,\mathtt{0xb2}\,\mathtt{0x75}0x510xb20x75) are inserted.

receivedBytesAnchors(K,h)(K, h)(K,h) is BitcoinAnchors.receivedBytes(K.revHash,K.remoteHtlcKey,K.localHtlcKey,h.payHash,h.expiry)\mathrm{BitcoinAnchors.receivedBytes}(K.\mathrm{revHash}, K.\mathrm{remoteHtlcKey}, K.\mathrm{localHtlcKey}, h.\mathrm{payHash}, h.\mathrm{expiry})BitcoinAnchors.receivedBytes(K.revHash,K.remoteHtlcKey,K.localHtlcKey,h.payHash,h.expiry): the same byte string as receivedBytes(K,h)(K,h)(K,h) with [1][1][1] OP_CHECKSEQUENCEVERIFY OP_DROP inserted between the inner and outer OP_ENDIF.

toRemoteBytes(K)(K)(K) is BitcoinAnchors.toRemoteBytes(K.remoteKey)=⟨K.remoteKey⟩\mathrm{BitcoinAnchors.toRemoteBytes}(K.\mathrm{remoteKey}) = \langle K.\mathrm{remoteKey}\rangleBitcoinAnchors.toRemoteBytes(K.remoteKey)=⟨K.remoteKey⟩ ∥\Vert∥ OP_CHECKSIGVERIFY (0xad)(\mathtt{0xad})(0xad) ∥\Vert∥ [1][1][1] (0x51)(\mathtt{0x51})(0x51) ∥\Vert∥ OP_CHECKSEQUENCEVERIFY (0xb2)(\mathtt{0xb2})(0xb2).

anchorLocalBytes(K)(K)(K) is BitcoinAnchors.anchorBytes(K.fundingKeyLocal)=⟨K.fundingKeyLocal⟩\mathrm{BitcoinAnchors.anchorBytes}(K.\mathrm{fundingKeyLocal}) = \langle K.\mathrm{fundingKeyLocal}\rangleBitcoinAnchors.anchorBytes(K.fundingKeyLocal)=⟨K.fundingKeyLocal⟩ ∥\Vert∥ OP_CHECKSIG OP_IFDUP OP_NOTIF (0xac 0x73 0x64)(\mathtt{0xac}\,\mathtt{0x73}\,\mathtt{0x64})(0xac0x730x64) ∥\Vert∥ [16][16][16] (0x60)(\mathtt{0x60})(0x60) ∥\Vert∥ OP_CHECKSEQUENCEVERIFY OP_ENDIF (0xb2 0x68)(\mathtt{0xb2}\,\mathtt{0x68})(0xb20x68).

anchorRemoteBytes(K)(K)(K) is the same construction applied to K.fundingKeyRemoteK.\mathrm{fundingKeyRemote}K.fundingKeyRemote: ⟨K.fundingKeyRemote⟩\langle K.\mathrm{fundingKeyRemote}\rangle⟨K.fundingKeyRemote⟩ OP_CHECKSIG OP_IFDUP OP_NOTIF [16][16][16] OP_CHECKSEQUENCEVERIFY OP_ENDIF.

p2wshOf(S)(\mathcal S)(S), for any byte string S\mathcal SS, is witnessBytes(0,sha256(S))\mathrm{witnessBytes}(0, \mathrm{sha256}(\mathcal S))witnessBytes(0,sha256(S)), i.e. the byte 0x00\mathtt{0x00}0x00 followed by pushBytes(sha256(S))\mathrm{pushBytes}(\mathrm{sha256}(\mathcal S))pushBytes(sha256(S)), which is 0x00 0x20\mathtt{0x00}\,\mathtt{0x20}0x000x20 followed by the 32 bytes of BitcoinSer.sha256(S)\mathrm{BitcoinSer.sha256}(\mathcal S)BitcoinSer.sha256(S) — the concrete SHA-256 transcribed in the serialization file. No constraint on S\mathcal SS (it need not be a parseable script).

oracleOf(c,S)(c, \mathcal S)(c,S), for a verification context ccc (flags c.flagsc.\mathrm{flags}c.flags, transaction c.txc.txc.tx, input index c.nIn∈Nc.\mathrm{nIn} \in \mathbb Nc.nIn∈N, amount c.amount∈Zc.\mathrm{amount} \in \mathbb Zc.amount∈Z) and a byte string S\mathcal SS, is the oracle legacyOracle(sigFlagsOf(c.flags),c.tx,c.nIn,c.amount,witnessV0,S)\mathrm{legacyOracle}(\mathrm{sigFlagsOf}(c.\mathrm{flags}), c.tx, c.\mathrm{nIn}, c.\mathrm{amount}, \mathrm{witnessV0}, \mathcal S)legacyOracle(sigFlagsOf(c.flags),c.tx,c.nIn,c.amount,witnessV0,S), whose components are: the concrete ripemd160\mathrm{ripemd160}ripemd160, sha1\mathrm{sha1}sha1, sha256\mathrm{sha256}sha256, hash160=ripemd160∘sha256\mathrm{hash160} = \mathrm{ripemd160}\circ\mathrm{sha256}hash160=ripemd160∘sha256, hash256=sha256∘sha256\mathrm{hash256} = \mathrm{sha256}\circ\mathrm{sha256}hash256=sha256∘sha256 of the serialization file; checkSig(sig,pk)=checkSigPreTapscript((dersig=c.flags.dersig,lowS=false,strictenc=false,witnessPubkeyType=false),c.tx,c.nIn,c.amount,witnessV0,S,sig,pk)\mathrm{checkSig}(sig, pk) = \mathrm{checkSigPreTapscript}((\mathrm{dersig} = c.\mathrm{flags}.\mathrm{dersig}, \mathrm{lowS} = \mathrm{false}, \mathrm{strictenc} = \mathrm{false}, \mathrm{witnessPubkeyType} = \mathrm{false}), c.tx, c.\mathrm{nIn}, c.\mathrm{amount}, \mathrm{witnessV0}, \mathcal S, sig, pk)checkSig(sig,pk)=checkSigPreTapscript((dersig=c.flags.dersig,lowS=false,strictenc=false,witnessPubkeyType=false),c.tx,c.nIn,c.amount,witnessV0,S,sig,pk), which is none\mathrm{none}none iff c.flags.dersig=truec.\mathrm{flags}.\mathrm{dersig} = \mathrm{true}c.flags.dersig=true and sigsigsig is non-empty and fails IsValidSignatureEncoding, and otherwise some\mathrm{some}some of the BIP143 ECDSA verdict (public key header/length check, non-empty signature whose last byte is the hash type, c.amount≥0c.\mathrm{amount} \ge 0c.amount≥0, lax-DER parse, low-sss normalization, secp256k1 verification against the signature hash of c.txc.txc.tx at input c.nInc.\mathrm{nIn}c.nIn — the default empty input if out of range — with script code S\mathcal SS and amount c.amountc.\mathrm{amount}c.amount); checkLockTime(L)=checkLockTime(c.tx,c.nIn,L)\mathrm{checkLockTime}(L) = \mathrm{checkLockTime}(c.tx, c.\mathrm{nIn}, L)checkLockTime(L)=checkLockTime(c.tx,c.nIn,L) (c.tx.nLockTimec.tx.\mathrm{nLockTime}c.tx.nLockTime and LLL on the same side of 500000000500000000500000000, L≤c.tx.nLockTimeL \le c.tx.\mathrm{nLockTime}L≤c.tx.nLockTime, input c.nInc.\mathrm{nIn}c.nIn's sequence ≠0xffffffff\ne \mathtt{0xffffffff}=0xffffffff); and checkSequence(S)=checkSequence(c.tx,c.nIn,S)\mathrm{checkSequence}(S) = \mathrm{checkSequence}(c.tx, c.\mathrm{nIn}, S)checkSequence(S)=checkSequence(c.tx,c.nIn,S) (c.tx.version≥2c.tx.\mathrm{version} \ge 2c.tx.version≥2, input c.nInc.\mathrm{nIn}c.nIn's sequence σ\sigmaσ has bit 31 clear, σ&(222∣0xffff)\sigma \mathbin{\&} (2^{22} \mathbin{|} \mathtt{0xffff})σ&(222∣0xffff) and max⁡(S,0)&(222∣0xffff)\max(S,0) \mathbin{\&} (2^{22} \mathbin{|} \mathtt{0xffff})max(S,0)&(222∣0xffff) on the same side of 2222^{22}222, and the latter ≤\le≤ the former).

HoldsRevocation(o,K)(o, K)(o,K), for an oracle ooo and keys KKK, is the conjunction of (i) there exists a byte string sigRsig_RsigR​ with ∣sigR∣≤520|sig_R| \le 520∣sigR​∣≤520 and o.checkSig(sigR,K.revPk)=some trueo.\mathrm{checkSig}(sig_R, K.\mathrm{revPk}) = \mathrm{some\ true}o.checkSig(sigR​,K.revPk)=some true, and (ii) o.hash160(K.revPk)=K.revHasho.\mathrm{hash160}(K.\mathrm{revPk}) = K.\mathrm{revHash}o.hash160(K.revPk)=K.revHash. It is stated for an abstract oracle; nothing ties ooo to any transaction unless ooo is instantiated (e.g. by oracleOf).

NoRevocationSig(o,K)(o, K)(o,K) is: for every byte string sigsigsig, o.checkSig(sig,K.revPk)≠some trueo.\mathrm{checkSig}(sig, K.\mathrm{revPk}) \ne \mathrm{some\ true}o.checkSig(sig,K.revPk)=some true (each answer is none\mathrm{none}none or some false\mathrm{some\ false}some false). It says nothing about other keys and nothing about K.revHashK.\mathrm{revHash}K.revHash.

NoRevHashPreimage(o,K)(o, K)(o,K) is: for every byte string pkpkpk, if o.hash160(pk)=K.revHasho.\mathrm{hash160}(pk) = K.\mathrm{revHash}o.hash160(pk)=K.revHash then pk=K.revPkpk = K.\mathrm{revPk}pk=K.revPk. It does not assert that K.revPkK.\mathrm{revPk}K.revPk itself hashes to K.revHashK.\mathrm{revHash}K.revHash; it holds vacuously when no byte string hashes to K.revHashK.\mathrm{revHash}K.revHash.

toLocalPenaltyWitness(K,sigR)(K, sig_R)(K,sigR​) is the three-element list of byte strings [ sigR, [0x01], toLocalBytes(K) ][\,sig_R,\ [\mathtt{0x01}],\ \mathrm{toLocalBytes}(K)\,][sigR​, [0x01], toLocalBytes(K)], in CScriptWitness order (last element on top of the stack, the script last): when executed, the stack top-first is [[0x01],sigR][[\mathtt{0x01}], sig_R][[0x01],sigR​].

htlcPenaltyWitness(K,sigR,S)(K, sig_R, \mathcal S)(K,sigR​,S) is the three-element list [ sigR, K.revPk, S ][\,sig_R,\ K.\mathrm{revPk},\ \mathcal S\,][sigR​, K.revPk, S] for an arbitrary byte string S\mathcal SS (not required to be one of the HTLC scripts); when executed, the stack top-first is [K.revPk,sigR][K.\mathrm{revPk}, sig_R][K.revPk,sigR​].

DelayedSpend(f,o,K,b,sig)(f, o, K, b, sig)(f,o,K,b,sig), for a flag triple fff, oracle ooo, keys KKK and byte strings bbb, sigsigsig, is the conjunction of three facts: castToBool(b)=false\mathrm{castToBool}(b) = \mathrm{false}castToBool(b)=false (bbb is empty, all zero bytes, or zero bytes ending in 0x80\mathtt{0x80}0x80); CsvPasses(f,o,K.toSelfDelay)\mathrm{CsvPasses}(f, o, K.\mathrm{toSelfDelay})CsvPasses(f,o,K.toSelfDelay), i.e. f.csv=falsef.\mathrm{csv} = \mathrm{false}f.csv=false or (0≤K.toSelfDelay0 \le K.\mathrm{toSelfDelay}0≤K.toSelfDelay and (max⁡(K.toSelfDelay,0)&231≠0\max(K.\mathrm{toSelfDelay},0) \mathbin{\&} 2^{31} \ne 0max(K.toSelfDelay,0)&231=0 or o.checkSequence(K.toSelfDelay)=trueo.\mathrm{checkSequence}(K.\mathrm{toSelfDelay}) = \mathrm{true}o.checkSequence(K.toSelfDelay)=true)); and o.checkSig(sig,K.localDelayedPk)=some trueo.\mathrm{checkSig}(sig, K.\mathrm{localDelayedPk}) = \mathrm{some\ true}o.checkSig(sig,K.localDelayedPk)=some true.

OfferedNonRevocation(f,o,K,h,st)(f, o, K, h, st)(f,o,K,h,st), for a top-first stack ststst (list of byte strings), is the disjunction of two existential statements. First disjunct: there exist byte strings pre\mathrm{pre}pre, sigsigsig with st=[pre,sig]st = [\mathrm{pre}, sig]st=[pre,sig], o.hash160(pre)≠K.revHasho.\mathrm{hash160}(\mathrm{pre}) \ne K.\mathrm{revHash}o.hash160(pre)=K.revHash, ∣pre∣=32|\mathrm{pre}| = 32∣pre∣=32, o.hash160(pre)=h.payHasho.\mathrm{hash160}(\mathrm{pre}) = h.\mathrm{payHash}o.hash160(pre)=h.payHash, and o.checkSig(sig,K.remoteHtlcKey)=some trueo.\mathrm{checkSig}(sig, K.\mathrm{remoteHtlcKey}) = \mathrm{some\ true}o.checkSig(sig,K.remoteHtlcKey)=some true. Second disjunct: there exist byte strings xxx, sigLsig_LsigL​, sigR′sig_R'sigR′​, dummy\mathrm{dummy}dummy with st=[x,sigL,sigR′,dummy]st = [x, sig_L, sig_R', \mathrm{dummy}]st=[x,sigL​,sigR′​,dummy], o.hash160(x)≠K.revHasho.\mathrm{hash160}(x) \ne K.\mathrm{revHash}o.hash160(x)=K.revHash, ∣x∣≠32|x| \ne 32∣x∣=32, multisigLoop(o,[sigL,sigR′],[K.localHtlcKey,K.remoteHtlcKey])=some true\mathrm{multisigLoop}(o, [sig_L, sig_R'], [K.\mathrm{localHtlcKey}, K.\mathrm{remoteHtlcKey}]) = \mathrm{some\ true}multisigLoop(o,[sigL​,sigR′​],[K.localHtlcKey,K.remoteHtlcKey])=some true (which, by the loop's definition on two signatures and two keys, holds iff o.checkSig(sigL,K.localHtlcKey)=some trueo.\mathrm{checkSig}(sig_L, K.\mathrm{localHtlcKey}) = \mathrm{some\ true}o.checkSig(sigL​,K.localHtlcKey)=some true and o.checkSig(sigR′,K.remoteHtlcKey)=some trueo.\mathrm{checkSig}(sig_R', K.\mathrm{remoteHtlcKey}) = \mathrm{some\ true}o.checkSig(sigR′​,K.remoteHtlcKey)=some true), and f.nulldummy=true⇒dummy=[ ]f.\mathrm{nulldummy} = \mathrm{true} \Rightarrow \mathrm{dummy} = [\,]f.nulldummy=true⇒dummy=[]. There is no revocation disjunct.

ReceivedNonRevocation(f,o,K,h,st)(f, o, K, h, st)(f,o,K,h,st) is the disjunction of two existential statements. First disjunct: there exist pre\mathrm{pre}pre, sigLsig_LsigL​, sigR′sig_R'sigR′​, dummy\mathrm{dummy}dummy with st=[pre,sigL,sigR′,dummy]st = [\mathrm{pre}, sig_L, sig_R', \mathrm{dummy}]st=[pre,sigL​,sigR′​,dummy], o.hash160(pre)≠K.revHasho.\mathrm{hash160}(\mathrm{pre}) \ne K.\mathrm{revHash}o.hash160(pre)=K.revHash, ∣pre∣=32|\mathrm{pre}| = 32∣pre∣=32, o.hash160(pre)=h.payHasho.\mathrm{hash160}(\mathrm{pre}) = h.\mathrm{payHash}o.hash160(pre)=h.payHash, multisigLoop(o,[sigL,sigR′],[K.localHtlcKey,K.remoteHtlcKey])=some true\mathrm{multisigLoop}(o, [sig_L, sig_R'], [K.\mathrm{localHtlcKey}, K.\mathrm{remoteHtlcKey}]) = \mathrm{some\ true}multisigLoop(o,[sigL​,sigR′​],[K.localHtlcKey,K.remoteHtlcKey])=some true (iff both signatures verify for their respective keys with answer some true\mathrm{some\ true}some true), and f.nulldummy=true⇒dummy=[ ]f.\mathrm{nulldummy} = \mathrm{true} \Rightarrow \mathrm{dummy} = [\,]f.nulldummy=true⇒dummy=[]. Second disjunct: there exist xxx, sigsigsig with st=[x,sig]st = [x, sig]st=[x,sig], o.hash160(x)≠K.revHasho.\mathrm{hash160}(x) \ne K.\mathrm{revHash}o.hash160(x)=K.revHash, ∣x∣≠32|x| \ne 32∣x∣=32, BitcoinHtlc.CltvPasses(f,o,h.expiry)\mathrm{BitcoinHtlc.CltvPasses}(f, o, h.\mathrm{expiry})BitcoinHtlc.CltvPasses(f,o,h.expiry) — i.e. f.cltv=falsef.\mathrm{cltv} = \mathrm{false}f.cltv=false or (0≤h.expiry0 \le h.\mathrm{expiry}0≤h.expiry and o.checkLockTime(h.expiry)=trueo.\mathrm{checkLockTime}(h.\mathrm{expiry}) = \mathrm{true}o.checkLockTime(h.expiry)=true) — and o.checkSig(sig,K.remoteHtlcKey)=some trueo.\mathrm{checkSig}(sig, K.\mathrm{remoteHtlcKey}) = \mathrm{some\ true}o.checkSig(sig,K.remoteHtlcKey)=some true. There is no revocation disjunct.

OfferedNonRevocationAnchors(f,o,K,h,st)(f, o, K, h, st)(f,o,K,h,st) is the conjunction OfferedNonRevocation(f,o,K,h,st)∧CsvPasses(f,o,1)\mathrm{OfferedNonRevocation}(f, o, K, h, st) \land \mathrm{CsvPasses}(f, o, 1)OfferedNonRevocation(f,o,K,h,st)∧CsvPasses(f,o,1), where CsvPasses(f,o,1)\mathrm{CsvPasses}(f, o, 1)CsvPasses(f,o,1) unfolds to f.csv=false∨(0≤1∧(1&231≠0∨o.checkSequence(1)=true))f.\mathrm{csv} = \mathrm{false} \lor (0 \le 1 \land (1 \mathbin{\&} 2^{31} \ne 0 \lor o.\mathrm{checkSequence}(1) = \mathrm{true}))f.csv=false∨(0≤1∧(1&231=0∨o.checkSequence(1)=true)), i.e. f.csv=false∨o.checkSequence(1)=truef.\mathrm{csv} = \mathrm{false} \lor o.\mathrm{checkSequence}(1) = \mathrm{true}f.csv=false∨o.checkSequence(1)=true. The CSV conjunct is outside the disjunction, so it is required on both paths.

ReceivedNonRevocationAnchors(f,o,K,h,st)(f, o, K, h, st)(f,o,K,h,st) is the conjunction ReceivedNonRevocation(f,o,K,h,st)∧CsvPasses(f,o,1)\mathrm{ReceivedNonRevocation}(f, o, K, h, st) \land \mathrm{CsvPasses}(f, o, 1)ReceivedNonRevocation(f,o,K,h,st)∧CsvPasses(f,o,1), with CsvPasses(f,o,1)\mathrm{CsvPasses}(f, o, 1)CsvPasses(f,o,1) as just unfolded; again required on both paths.

Human review
  • Endorsed by Shuze Chen · Oct 30, 2026

    Confirmed by the moderator at approval.

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