Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Revocation spends and height-locked transactions

Definition
BitcoinHtlcLocks

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

bip65bitcoinbitcoin-mainnetbolt3consensusfinalityformal-verificationhtlclightninglocktime

Two names over the HTLC and block missions.

RevocationSpend c script revHash pk sig — in verification context ccc (transaction, input index, amount, flags), the witness's public key hashes (hash160) to the revocation hash and the signature verifies for it under the signature mission's checkSigPreTapscript with the witness-v0 sighash on script: the revocation path of an HTLC output.

HeightLocked tx — the transaction's nLockTime is nonzero and below LOCKTIME_THRESHOLD (500,000,000), i.e. a block height, as for a BOLT #3 HTLC-timeout transaction whose lock time is cltv_expiry.

Formalization Note. isFinalTx, LOCKTIME_THRESHOLD, SEQUENCE_FINAL are the block mission's (BitcoinBlockV2); receivedBytes, ReceivedSpend the HTLC mission's; verifyScript, Ctx the verification mission's. Only Std is imported.

Definition code
import Definitions.Def_BitcoinHtlc
import Definitions.Def_BitcoinBlockV2

/-!
# HTLC lock times at the block level

The HTLC mission proves which witnesses spend a received HTLC output under `VerifyScript`; the
timeout path carries `OP_CHECKLOCKTIMEVERIFY <cltv_expiry>`. The block mission's
`ContextualCheckBlock` requires every transaction of a block to be final (`IsFinalTx`) at the
block's height. This file composes the two: a transaction that spends a received HTLC with a
two-element witness and sits in a block at height `h` is either a revocation spend or has
`cltv_expiry < h`. The same bound holds for a pre-signed HTLC-timeout transaction whose
`nLockTime` is the expiry.
-/

namespace BitcoinHtlcLocks

open BitcoinSer BitcoinScript BitcoinSig BitcoinVerify BitcoinHtlc BitcoinBlockV2

/-- The revocation spend of an HTLC output in context `c`: the witness's public key hashes to the
revocation hash and the signature verifies for it under the context's checker on `script`. -/
def RevocationSpend (c : BitcoinVerify.Ctx) (script revHash pk sig : BitcoinSer.Bytes) : Prop :=
  BitcoinSer.hash160 pk = revHash ∧
    BitcoinSig.checkSigPreTapscript (BitcoinVerify.sigFlagsOf c.flags) c.tx c.nIn c.amount .witnessV0 script sig pk = some true

/-- A transaction whose lock time is a block height (nonzero and below the threshold), as a
second-stage HTLC-timeout transaction's is. -/
def HeightLocked (tx : BitcoinSer.Tx) : Prop := 0 < tx.nLockTime ∧ tx.nLockTime < BitcoinBlockV2.LOCKTIME_THRESHOLD

end BitcoinHtlcLocks
Source
BOLT #3, HTLC-Timeout and HTLC-Success Transactions (locktime = cltv_expiry), Received HTLC Outputs: https://github.com/lightning/bolts/blob/master/03-transactions.md#htlc-timeout-and-htlc-success-transactions; Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: IsFinalTx (src/consensus/tx_verify.cpp) as the block mission's `isFinalTx`, ContextualCheckBlock (src/validation.cpp), GenericTransactionSignatureChecker::CheckLockTime (src/script/interpreter.cpp); BIP65
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: definitions in Def_BitcoinHtlcLocks

This file introduces two definitions and no theorems.

BitcoinHtlcLocks.RevocationSpend

A proposition RevocationSpend(c,script,r,pk,sig)\mathrm{RevocationSpend}(c, \mathit{script}, r, \mathit{pk}, \mathit{sig})RevocationSpend(c,script,r,pk,sig) in five arguments: a verification context ccc and four byte strings (finite lists of 8-bit vectors) script\mathit{script}script, rrr (revHash), pk\mathit{pk}pk and sig\mathit{sig}sig. The context ccc is a record (c.flags,c.tx,c.nIn,c.amount)(c.\mathrm{flags}, c.\mathrm{tx}, c.\mathrm{nIn}, c.\mathrm{amount})(c.flags,c.tx,c.nIn,c.amount) where c.flagsc.\mathrm{flags}c.flags is a record of seven Booleans (p2sh,witness,taproot,dersig,cltv,csv,nulldummy)(\mathrm{p2sh}, \mathrm{witness}, \mathrm{taproot}, \mathrm{dersig}, \mathrm{cltv}, \mathrm{csv}, \mathrm{nulldummy})(p2sh,witness,taproot,dersig,cltv,csv,nulldummy), c.txc.\mathrm{tx}c.tx is a transaction (a record with a version ∈N\in \mathbb{N}∈N, a list of inputs, a list of outputs, and a lock time ∈N\in \mathbb{N}∈N; each input having a previous outpoint, a scriptSig byte string, a sequence number ∈N\in \mathbb{N}∈N and a witness stack), c.nIn∈Nc.\mathrm{nIn} \in \mathbb{N}c.nIn∈N is an input index and c.amount∈Zc.\mathrm{amount} \in \mathbb{Z}c.amount∈Z is an amount. The proposition is the conjunction of two conditions:

hash160(pk)=randcheckSigPreTapscript(sigFlagsOf(c.flags), c.tx, c.nIn, c.amount, witnessV0, script, sig, pk)=some true.\mathrm{hash160}(\mathit{pk}) = r \qquad\text{and}\qquad \mathrm{checkSigPreTapscript}\big(\mathrm{sigFlagsOf}(c.\mathrm{flags}),\ c.\mathrm{tx},\ c.\mathrm{nIn},\ c.\mathrm{amount},\ \mathrm{witnessV0},\ \mathit{script},\ \mathit{sig},\ \mathit{pk}\big) = \mathrm{some}\ \mathbf{true}.hash160(pk)=randcheckSigPreTapscript(sigFlagsOf(c.flags), c.tx, c.nIn, c.amount, witnessV0, script, sig, pk)=some true.

Here hash160(m)=ripemd160(sha256(m))\mathrm{hash160}(m) = \mathrm{ripemd160}(\mathrm{sha256}(m))hash160(m)=ripemd160(sha256(m)) with concrete SHA-256 and RIPEMD-160 implementations, so the first conjunct says the 20-byte RIPEMD-160-of-SHA-256 digest of pk\mathit{pk}pk equals rrr as byte strings (nothing requires rrr to have length 20; if it does not, the equality is simply false). sigFlagsOf(F)\mathrm{sigFlagsOf}(F)sigFlagsOf(F) is the signature-flag record (dersig=F.dersig,lowS=false,strictenc=false,witnessPubkeyType=false)(\mathrm{dersig} = F.\mathrm{dersig}, \mathrm{lowS} = \mathbf{false}, \mathrm{strictenc} = \mathbf{false}, \mathrm{witnessPubkeyType} = \mathbf{false})(dersig=F.dersig,lowS=false,strictenc=false,witnessPubkeyType=false). checkSigPreTapscript\mathrm{checkSigPreTapscript}checkSigPreTapscript returns an optional Boolean: with signature version witnessV0\mathrm{witnessV0}witnessV0 the script code is script\mathit{script}script unchanged (no find-and-delete of the signature push, which only happens for the base version); it returns no value when the signature-encoding check fails (with these flags: an empty sig\mathit{sig}sig passes; otherwise, if c.flags.dersigc.\mathrm{flags}.\mathrm{dersig}c.flags.dersig holds and sig\mathit{sig}sig is not a valid DER-encoded signature it fails; otherwise it passes) or the public-key-encoding check fails (with strictenc\mathrm{strictenc}strictenc and witnessPubkeyType\mathrm{witnessPubkeyType}witnessPubkeyType both off it never fails); otherwise it returns some\mathrm{some}some of checkECDSASignature(c.tx,c.nIn,c.amount,witnessV0,sig,pk,script)\mathrm{checkECDSASignature}(c.\mathrm{tx}, c.\mathrm{nIn}, c.\mathrm{amount}, \mathrm{witnessV0}, \mathit{sig}, \mathit{pk}, \mathit{script})checkECDSASignature(c.tx,c.nIn,c.amount,witnessV0,sig,pk,script), which is false if pk\mathit{pk}pk is not a valid public key, false if sig\mathit{sig}sig is empty, and otherwise splits sig\mathit{sig}sig into its last byte (the hash type) and the remaining raw signature, computes the witness-version-0 signature hash of (script,c.tx,c.nIn,hash type,c.amount)(\mathit{script}, c.\mathrm{tx}, c.\mathrm{nIn}, \text{hash type}, c.\mathrm{amount})(script,c.tx,c.nIn,hash type,c.amount), returns false if c.amount<0c.\mathrm{amount} < 0c.amount<0, and otherwise returns the outcome of verifying the raw signature against that hash under pk\mathit{pk}pk. Thus the second conjunct requires the encoding checks to pass and the ECDSA verdict to be true; a "no value" result or a false verdict both make the proposition false. Nothing in the definition refers to c.flags.witnessc.\mathrm{flags}.\mathrm{witness}c.flags.witness, cltv\mathrm{cltv}cltv, or the other flags, to the witness stacks of c.txc.\mathrm{tx}c.tx, or to any bound on c.nInc.\mathrm{nIn}c.nIn (an out-of-range c.nInc.\mathrm{nIn}c.nIn is passed through to the signature-hash computation as is). The byte string script\mathit{script}script is used only as the script code for the signature hash.

BitcoinHtlcLocks.HeightLocked

A proposition HeightLocked(tx)\mathrm{HeightLocked}(tx)HeightLocked(tx) in one argument, a transaction txtxtx (a record with a version ∈N\in \mathbb{N}∈N, a list of inputs, a list of outputs and a lock time nLockTime(tx)∈N\mathrm{nLockTime}(tx) \in \mathbb{N}nLockTime(tx)∈N). It is the conjunction

0<nLockTime(tx)  ∧  nLockTime(tx)<LOCKTIME_THRESHOLD,LOCKTIME_THRESHOLD=500 000 000,0 < \mathrm{nLockTime}(tx) \;\wedge\; \mathrm{nLockTime}(tx) < \mathrm{LOCKTIME\_THRESHOLD}, \qquad \mathrm{LOCKTIME\_THRESHOLD} = 500\,000\,000,0<nLockTime(tx)∧nLockTime(tx)<LOCKTIME_THRESHOLD,LOCKTIME_THRESHOLD=500000000,

both comparisons on natural numbers. Equivalently, 1≤nLockTime(tx)≤499 999 9991 \le \mathrm{nLockTime}(tx) \le 499\,999\,9991≤nLockTime(tx)≤499999999. The proposition is false when nLockTime(tx)=0\mathrm{nLockTime}(tx) = 0nLockTime(tx)=0 and false when nLockTime(tx)≥500 000 000\mathrm{nLockTime}(tx) \ge 500\,000\,000nLockTime(tx)≥500000000. It says nothing about the transaction's inputs, their sequence numbers, its outputs or its version, and places no upper bound such as 2322^{32}232 on the lock time beyond the threshold itself.

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