Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

A commitment spend with nSequence = to_self_delay: the delayed path opens iff version ≥ 2 and nSequence ≥ delay

Proved
BitcoinToLocal.lightning_delay

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

bip112bitcoinbitcoin-mainnetbolt3csvformal-verificationlightningp2wshscript

For 0≤delay<2160 \le {\rm delay} < 2^{16}0≤delay<216 and an input whose nSequence is below 2162^{16}216 (no flags, as Lightning sets it): checkSequence tx nIn delay = true ↔ 2 ≤ tx.version ∧ delay ≤ inputSequence tx nIn. Together with p2wsh_spend and csv_concrete: the local node's delayed spend is accepted exactly when its transaction is version 2 or later, sets the input's nSequence to at least to_self_delay, and carries a valid signature for the delayed key.

Preamble
import Definitions.Def_BitcoinToLocal
import Definitions.Def_BitcoinSer
import Definitions.Def_BitcoinSig
open BitcoinToLocal BitcoinSig BitcoinVerify
open BitcoinScript
Formal statement
theorem BitcoinToLocal.lightning_delay (tx : BitcoinSer.Tx) (nIn : Nat) (delay : Int) (h0 : 0 ≤ delay) (h16 : delay < 2 ^ 16)
    (hseq : BitcoinToLocal.inputSequence tx nIn < 2 ^ 16) :
    BitcoinSig.checkSequence tx nIn delay = true ↔ 2 ≤ tx.version ∧ delay.toNat ≤ BitcoinToLocal.inputSequence tx nIn := by sorry
Source
BOLT #3, Bitcoin Transaction and Script Formats, 'to_local Output': https://github.com/lightning/bolts/blob/master/03-transactions.md#to_local-output; Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: EvalScript (OP_IF/OP_ELSE/OP_ENDIF, OP_CHECKSEQUENCEVERIFY, OP_CHECKSIG), GenericTransactionSignatureChecker::CheckSequence, ExecuteWitnessScript, VerifyWitnessProgram (src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp); CScript::operator<<(int64_t) (script/script.h); 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: BitcoinToLocal.lightning_delay

Statement. For every transaction tx\mathrm{tx}tx, every natural number ninn_{\mathrm{in}}nin​, and every integer δ\deltaδ (named delay), under the three hypotheses

0≤δ,δ<216,inputSequence(tx,nin)<216,0 \le \delta, \qquad \delta < 2^{16}, \qquad \mathrm{inputSequence}(\mathrm{tx}, n_{\mathrm{in}}) < 2^{16},0≤δ,δ<216,inputSequence(tx,nin​)<216,

the following equivalence holds:

checkSequence(tx,nin,δ)=true  ⟺  2≤tx.version  ∧  toNat⁡(δ)≤inputSequence(tx,nin).\mathrm{checkSequence}(\mathrm{tx}, n_{\mathrm{in}}, \delta) = \mathrm{true} \iff 2 \le \mathrm{tx}.\mathrm{version}\ \ \land\ \ \operatorname{toNat}(\delta) \le \mathrm{inputSequence}(\mathrm{tx}, n_{\mathrm{in}}).checkSequence(tx,nin​,δ)=true⟺2≤tx.version  ∧  toNat(δ)≤inputSequence(tx,nin​).

Since δ≥0\delta \ge 0δ≥0, toNat⁡(δ)\operatorname{toNat}(\delta)toNat(δ) (which sends negative integers to 000) is just δ\deltaδ viewed as a natural number.

The objects involved.

  • A transaction is a record (version∈N, vin, vout, nLockTime∈N)(\mathrm{version} \in \mathbb{N},\ \mathrm{vin},\ \mathrm{vout},\ \mathrm{nLockTime} \in \mathbb{N})(version∈N, vin, vout, nLockTime∈N); each element of the list vin\mathrm{vin}vin is an input record carrying, among other fields, a natural number nSequence\mathrm{nSequence}nSequence. No upper bound on version\mathrm{version}version or nSequence\mathrm{nSequence}nSequence comes from the types.
  • inputSequence(tx,nin)\mathrm{inputSequence}(\mathrm{tx}, n_{\mathrm{in}})inputSequence(tx,nin​) is the nSequence\mathrm{nSequence}nSequence of the ninn_{\mathrm{in}}nin​-th input of tx\mathrm{tx}tx (zero-indexed), or 000 when nin≥∣tx.vin∣n_{\mathrm{in}} \ge |\mathrm{tx}.\mathrm{vin}|nin​≥∣tx.vin∣ (a default input is substituted). Write σ\sigmaσ for this value; the third hypothesis says σ<65536\sigma < 65536σ<65536, which is automatically true in the out-of-range case.
  • checkSequence(tx,nin,s)\mathrm{checkSequence}(\mathrm{tx}, n_{\mathrm{in}}, s)checkSequence(tx,nin​,s), for an integer sss, is the boolean: if tx.version<2\mathrm{tx}.\mathrm{version} < 2tx.version<2 then false\mathrm{false}false; else if σ&231≠0\sigma \mathbin{\&} 2^{31} \ne 0σ&231=0 then false\mathrm{false}false; else, with M:=222∣0xffff=0x40ffffM := 2^{22} \mid \mathtt{0xffff} = \mathtt{0x40ffff}M:=222∣0xffff=0x40ffff, τ:=σ&M\tau := \sigma \mathbin{\&} Mτ:=σ&M and ς:=toNat⁡(s)&M\varsigma := \operatorname{toNat}(s) \mathbin{\&} Mς:=toNat(s)&M: if neither (τ<222\tau < 2^{22}τ<222 and ς<222\varsigma < 2^{22}ς<222) nor (222≤τ2^{22} \le \tau222≤τ and 222≤ς2^{22} \le \varsigma222≤ς) then false\mathrm{false}false; else if τ<ς\tau < \varsigmaτ<ς then false\mathrm{false}false; else true\mathrm{true}true. (Here &\mathbin{\&}& and ∣\mid∣ are bitwise AND and OR on natural numbers; the constants are the preamble's SEQUENCE_LOCKTIME_DISABLE_FLAG=231\mathrm{SEQUENCE\_LOCKTIME\_DISABLE\_FLAG} = 2^{31}SEQUENCE_LOCKTIME_DISABLE_FLAG=231, SEQUENCE_LOCKTIME_TYPE_FLAG=222\mathrm{SEQUENCE\_LOCKTIME\_TYPE\_FLAG} = 2^{22}SEQUENCE_LOCKTIME_TYPE_FLAG=222, SEQUENCE_LOCKTIME_MASK=0xffff\mathrm{SEQUENCE\_LOCKTIME\_MASK} = \mathtt{0xffff}SEQUENCE_LOCKTIME_MASK=0xffff.)

Edge cases made explicit. When ninn_{\mathrm{in}}nin​ is out of range, σ=0\sigma = 0σ=0 and the right-hand side becomes 2≤tx.version∧δ≤02 \le \mathrm{tx}.\mathrm{version} \land \delta \le 02≤tx.version∧δ≤0, i.e. δ=0\delta = 0δ=0. The hypotheses bound both δ\deltaδ and σ\sigmaσ strictly below 2162^{16}216, so each lies entirely within the low 161616 bits; neither bit 222222 nor bit 313131 can be set in either. Nothing constrains tx.version\mathrm{tx}.\mathrm{version}tx.version beyond what appears in the conclusion.

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