A revoked commitment transaction's outputs, the holder of the revocation key, and the cheater
DefinitionBitcoinPenaltyNames 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, toSelfDelay , .
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 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.
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
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; denotes (the canonical data push: length byte then for , OP_PUSHDATA1/2/4 forms otherwise) and denotes (the single opcode byte for or , otherwise of the CScriptNum encoding of , which is the single byte for ). The constant is . An oracle is a record of eight functions: , , , , (byte strings to byte strings), (two byte strings to ), and (integers to booleans). A flag triple has booleans , , .
CommitmentKeys is a structure with nine fields, no constraints: eight byte strings , , , , , , , , and one integer . Any byte strings and any integer (negative, zero, huge) form a valid record; nothing relates to .
Htlc is a structure with two fields, no constraints: a byte string and an integer .
WellFormed, for a CommitmentKeys , is a proposition that is the conjunction of exactly ten facts: , , , , , , , , , and . It imposes nothing else (no key validity, no 20-byte hash length, no hash relation).
HtlcWellFormed, for an Htlc , is the conjunction of two facts: and (the absolute value of the integer; may be zero or negative).
toLocalBytes is the byte string , which unfolds to OP_IF OP_ELSE OP_CHECKSEQUENCEVERIFY OP_DROP OP_ENDIF OP_CHECKSIG .
offeredBytes is : OP_DUP OP_HASH160 OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE OP_SWAP OP_SIZE OP_EQUAL OP_NOTIF OP_DROP OP_SWAP OP_CHECKMULTISIG OP_ELSE OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF OP_ENDIF. (Argument order: the second key argument of the HTLC constructor, "remoteKey", receives and the third, "localKey", receives .)
receivedBytes is : OP_DUP OP_HASH160 OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE OP_SWAP OP_SIZE OP_EQUAL OP_IF OP_HASH160 OP_EQUALVERIFY OP_SWAP OP_CHECKMULTISIG OP_ELSE OP_DROP OP_CHECKLOCKTIMEVERIFY OP_DROP OP_CHECKSIG OP_ENDIF OP_ENDIF.
offeredBytesAnchors is : the same byte string as offeredBytes except that between the inner OP_ENDIF and the outer OP_ENDIF the bytes OP_CHECKSEQUENCEVERIFY OP_DROP () are inserted.
receivedBytesAnchors is : the same byte string as receivedBytes with OP_CHECKSEQUENCEVERIFY OP_DROP inserted between the inner and outer OP_ENDIF.
toRemoteBytes is OP_CHECKSIGVERIFY OP_CHECKSEQUENCEVERIFY .
anchorLocalBytes is OP_CHECKSIG OP_IFDUP OP_NOTIF OP_CHECKSEQUENCEVERIFY OP_ENDIF .
anchorRemoteBytes is the same construction applied to : OP_CHECKSIG OP_IFDUP OP_NOTIF OP_CHECKSEQUENCEVERIFY OP_ENDIF.
p2wshOf, for any byte string , is , i.e. the byte followed by , which is followed by the 32 bytes of — the concrete SHA-256 transcribed in the serialization file. No constraint on (it need not be a parseable script).
oracleOf, for a verification context (flags , transaction , input index , amount ) and a byte string , is the oracle , whose components are: the concrete , , , , of the serialization file; , which is iff and is non-empty and fails IsValidSignatureEncoding, and otherwise of the BIP143 ECDSA verdict (public key header/length check, non-empty signature whose last byte is the hash type, , lax-DER parse, low- normalization, secp256k1 verification against the signature hash of at input — the default empty input if out of range — with script code and amount ); ( and on the same side of , , input 's sequence ); and (, input 's sequence has bit 31 clear, and on the same side of , and the latter the former).
HoldsRevocation, for an oracle and keys , is the conjunction of (i) there exists a byte string with and , and (ii) . It is stated for an abstract oracle; nothing ties to any transaction unless is instantiated (e.g. by oracleOf).
NoRevocationSig is: for every byte string , (each answer is or ). It says nothing about other keys and nothing about .
NoRevHashPreimage is: for every byte string , if then . It does not assert that itself hashes to ; it holds vacuously when no byte string hashes to .
toLocalPenaltyWitness is the three-element list of byte strings , in CScriptWitness order (last element on top of the stack, the script last): when executed, the stack top-first is .
htlcPenaltyWitness is the three-element list for an arbitrary byte string (not required to be one of the HTLC scripts); when executed, the stack top-first is .
DelayedSpend, for a flag triple , oracle , keys and byte strings , , is the conjunction of three facts: ( is empty, all zero bytes, or zero bytes ending in ); , i.e. or ( and ( or )); and .
OfferedNonRevocation, for a top-first stack (list of byte strings), is the disjunction of two existential statements. First disjunct: there exist byte strings , with , , , , and . Second disjunct: there exist byte strings , , , with , , , (which, by the loop's definition on two signatures and two keys, holds iff and ), and . There is no revocation disjunct.
ReceivedNonRevocation is the disjunction of two existential statements. First disjunct: there exist , , , with , , , , (iff both signatures verify for their respective keys with answer ), and . Second disjunct: there exist , with , , , — i.e. or ( and ) — and . There is no revocation disjunct.
OfferedNonRevocationAnchors is the conjunction , where unfolds to , i.e. . The CSV conjunct is outside the disjunction, so it is required on both paths.
ReceivedNonRevocationAnchors is the conjunction , with as just unfolded; again required on both paths.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.