Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Lean transcription of the pinned Core sequence-lock helpers

Definition
BIP68_Core

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

bip68bitcoinbitcoin-mainnetconsensusformal-verification

The implementation reads unsigned 32-bit words using the masks 2312^{31}231, 2222^{22}222, and 655356553565535, and converts the masked delay to seconds with a nine-bit left shift. Enforcement checks unsigned version at least two and flags bit zero.

An accumulator consists of integer thresholds a,ba,ba,b and a reversed height list. It starts at (−1,−1,[])(-1,-1,[])(−1,−1,[]). Processing a disabled input prepends zero and leaves both thresholds unchanged. For an enabled time input it updates bbb to max⁡(b,M(max⁡(c−1,0))+512d−1)\max(b,M(\max(c-1,0))+512d-1)max(b,M(max(c−1,0))+512d−1); for an enabled height input it updates aaa to max⁡(a,c+d−1)\max(a,c+d-1)max(a,c+d−1). Both enabled branches prepend the original coin height. The masked delay and seconds are computed with bit-vector operations. The final subtraction takes place in the integers, preserving the −1-1−1 sentinel.

If transaction-level enforcement is off, calculation preserves all original heights and returns thresholds (−1,−1)(-1,-1)(−1,−1). The output height list is obtained by reversing the accumulator's list. Evaluation accepts precisely when

a<H∧b<M(max⁡(H−1,0)).a<H\quad\land\quad b<M(\max(H-1,0)).a<H∧b<M(max(H−1,0)).

The sequence-lock function composes calculation with evaluation. These are total functions over the model's records; their definitions do not assume valid histories or bounded arithmetic. The main theorem states the domain appropriate for comparison with Core. This is a manual transcription of the named routines, not a formal C++ semantics or a proof about compiled machine code.

Definition code
import Definitions.Def_BIP68_Model

namespace BitcoinBIP68

def coreEnabled (version flags : BitVec 32) : Bool :=
  decide (2 ≤ version.toNat) && ((flags &&& 1) != 0)

def coreDisabled (sequence : BitVec 32) : Bool :=
  (sequence &&& 0x80000000) != 0

def coreTimeBased (sequence : BitVec 32) : Bool :=
  (sequence &&& 0x00400000) != 0

def coreDelay (sequence : BitVec 32) : Nat :=
  (sequence &&& 0x0000ffff).toNat

def coreSeconds (sequence : BitVec 32) : Nat :=
  ((sequence &&& 0x0000ffff) <<< 9).toNat

structure Accumulator where
  minHeight : Int
  minTime : Int
  heightsRev : List Nat
  deriving Repr, DecidableEq

def initial : Accumulator := ⟨-1, -1, []⟩

def coreStep (ctx : Context) (acc : Accumulator) (input : Input) : Accumulator :=
  if coreDisabled input.sequence then
    { acc with heightsRev := 0 :: acc.heightsRev }
  else if coreTimeBased input.sequence then
    { acc with
      minTime := max acc.minTime
        ((ctx.mtp (input.coinHeight - 1) : Int) + (coreSeconds input.sequence : Int) - 1)
      heightsRev := input.coinHeight :: acc.heightsRev }
  else
    { acc with
      minHeight := max acc.minHeight
        ((input.coinHeight : Int) + (coreDelay input.sequence : Int) - 1)
      heightsRev := input.coinHeight :: acc.heightsRev }

def calculate (version flags : BitVec 32) (ctx : Context)
    (inputs : List Input) : Accumulator :=
  if coreEnabled version flags then inputs.foldl (coreStep ctx) initial
  else { initial with heightsRev := (inputs.map Input.coinHeight).reverse }

def returnedHeights (acc : Accumulator) : List Nat := acc.heightsRev.reverse

def evaluate (ctx : Context) (acc : Accumulator) : Bool :=
  decide (acc.minHeight < (ctx.height : Int) ∧
    acc.minTime < (ctx.mtp (ctx.height - 1) : Int))

def sequenceLocks (version flags : BitVec 32) (ctx : Context)
    (inputs : List Input) : Bool :=
  evaluate ctx (calculate version flags ctx inputs)

end BitcoinBIP68
Source
BIP68, Specification and Implementation sections: https://github.com/bitcoin/bips/blob/100ba872c808946155b1eeb8c4568058f2eea0a7/bip-0068.mediawiki; Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48, CalculateSequenceLocks/EvaluateSequenceLocks/SequenceLocks, lines 45–116: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/consensus/tx_verify.cpp#L45-L116; constants: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/primitives/transaction.h#L86-L115.
Read-back

What the Lean code literally says, in plain math · GPT-6 (Codex independent sub-agent)

Def_BIP68_Core read-back

coreEnabled. For any two 32-bit bit vectors called version and flags, this Boolean is true exactly when the unsigned natural-number value of version is at least 222 and the bitwise AND of flags with 111 is nonzero. Thus version ranges over all unsigned values from 000 through 232−12^{32}-1232−1, and only the least significant bit of flags affects this result; versions 000 and 111 give false regardless of flags.

coreDisabled. For any 32-bit bit vector called sequence, this Boolean is true exactly when its bitwise AND with hexadecimal 800000008000000080000000, namely 2312^{31}231, is nonzero. Equivalently, bit 313131, counting the least significant bit as bit 000, is set; all other bits are ignored.

coreTimeBased. For any 32-bit bit vector called sequence, this Boolean is true exactly when its bitwise AND with hexadecimal 004000000040000000400000, namely 2222^{22}222, is nonzero. Equivalently, bit 222222, counting the least significant bit as bit 000, is set; all other bits, including bit 313131, are ignored.

coreDelay. For any 32-bit bit vector called sequence, this natural number is the unsigned value of the bitwise AND of sequence with hexadecimal 0000ffff0000ffff0000ffff, namely 655356553565535. It is the unsigned value of the lowest sixteen bits, lies between 000 and 655356553565535 inclusive, and is unaffected by every higher bit.

coreSeconds. For any 32-bit bit vector called sequence, this natural number is obtained by masking sequence with hexadecimal 0000ffff0000ffff0000ffff, shifting the resulting 32-bit bit vector left by 999 positions, and taking its unsigned value. If ddd is the unsigned value of the lowest sixteen bits of sequence, the result is 512d512d512d; because 0≤d≤655350 ≤ d ≤ 655350≤d≤65535, this shift loses no nonzero bit and its result ranges from 000 to 335539203355392033553920 inclusive in multiples of 512512512. Every higher bit of the original sequence is ignored.

Accumulator. An accumulator consists of two unrestricted integers, named minHeight and minTime, together with a finite list of natural numbers named heightsRev. There are no conditions relating these fields, no bounds or sign restrictions on the integer fields, and no restriction on the list, which may be empty. A printable representation and a decision procedure for equality of accumulators are generated from these fields.

initial. This particular accumulator has integer minHeight equal to −1-1−1, integer minTime equal to −1-1−1, and heightsRev equal to the empty list.

coreStep. Given a context consisting of a natural-number height and an arbitrary function MMM from natural numbers to natural numbers, an accumulator with integer fields H,TH,TH,T and natural-number list RRR, and an input consisting of a 32-bit sequence and a natural-number coin height ccc, this function returns an accumulator as follows. If sequence bit 313131 is set, the result is (H,T,0::R)(H,T,0::R)(H,T,0::R), where x::Rx::Rx::R denotes prepending xxx to RRR. Otherwise, let ddd be the unsigned value of the lowest sixteen sequence bits: if sequence bit 222222 is set, the result is (H,max⁡(T,M(max⁡(c−1,0))+512d−1),c::R)(H,\max(T,M(\max(c-1,0))+512d-1),c::R)(H,max(T,M(max(c−1,0))+512d−1),c::R); if bit 222222 is unset, it is (max⁡(H,c+d−1),T,c::R)(\max(H,c+d-1),T,c::R)(max(H,c+d−1),T,c::R). The expressions added to HHH or TTT are computed in the integers after converting the natural-number quantities, so their final subtraction of 111 may produce −1-1−1; the subtraction in the argument to MMM is natural-number subtraction, so c=0c=0c=0 uses M(0)M(0)M(0). The context height is not used by this function. Inputs with both bits 313131 and 222222 set take the first branch, and no bounds, monotonicity, or consistency assumptions are imposed on the context, input, or accumulator.

calculate. Given arbitrary 32-bit version and flags, a context consisting of natural-number height and an arbitrary function MMM from natural numbers to natural numbers, and any finite list of inputs, each input having a 32-bit sequence and a natural-number coin height, this function returns an accumulator. If the unsigned version is at least 222 and flags bit 000 is set, it starts with integer fields H=T=−1H=T=-1H=T=−1 and list R=[]R=[]R=[] and processes the inputs from left to right: an input with coin height ccc and sequence bit 313131 set prepends 000 to RRR and leaves H,TH,TH,T unchanged; otherwise it prepends ccc and, writing ddd for the unsigned value of the lowest sixteen sequence bits, replaces TTT with max⁡(T,M(max⁡(c−1,0))+512d−1)\max(T,M(\max(c-1,0))+512d-1)max(T,M(max(c−1,0))+512d−1) when bit 222222 is set, or replaces HHH with max⁡(H,c+d−1)\max(H,c+d-1)max(H,c+d−1) when bit 222222 is unset, leaving the other integer field unchanged. Those updates use integer arithmetic, while the index max⁡(c−1,0)\max(c-1,0)max(c−1,0) expresses natural-number subtraction. If the enabling condition is false, the returned integer fields are both −1-1−1 and the list field is the reverse of the input coin heights in their original order, irrespective of sequence bits. An empty input list returns (−1,−1,[])(-1,-1,[])(−1,−1,[]) in either branch. The context height is unused, and there are no restrictions on input heights or on MMM.

returnedHeights. For any accumulator consisting of two unrestricted integer fields and a finite list of natural numbers named heightsRev, this function returns that list in reverse order and ignores the integer fields. The accumulator need not have been produced by any other function, and an empty list returns an empty list.

evaluate. Given any context consisting of a natural-number height hhh and an arbitrary function MMM from natural numbers to natural numbers, and any accumulator with integer fields H,TH,TH,T and a finite natural-number list, this Boolean is true exactly when both strict inequalities H<hH<hH<h and T<M(max⁡(h−1,0))T<M(\max(h-1,0))T<M(max(h−1,0)) hold, with the natural-number right sides viewed as integers. The list field is ignored. The index uses natural-number subtraction, so height 000 compares against M(0)M(0)M(0); negative integer fields are allowed, and in particular fields H=T=−1H=T=-1H=T=−1 satisfy both inequalities for every context. No monotonicity or bound on MMM, or restriction on the accumulator's origin, is assumed.

sequenceLocks. Given arbitrary 32-bit version and flags, a context with natural-number height hhh and an arbitrary function MMM from natural numbers to natural numbers, and any finite list of inputs, each consisting of a 32-bit sequence and natural-number coin height, this Boolean first constructs an accumulator and then tests both strict inequalities H<hH<hH<h and T<M(max⁡(h−1,0))T<M(\max(h-1,0))T<M(max(h−1,0)) for its integer fields. When the unsigned version is at least 222 and flags bit 000 is set, construction starts with H=T=−1H=T=-1H=T=−1 and an empty list and processes inputs from left to right: sequence bit 313131 being set leaves both integer fields unchanged and prepends 000 to the list; otherwise, for coin height ccc and lowest-sixteen-bit unsigned value ddd, it prepends ccc and updates TTT to max⁡(T,M(max⁡(c−1,0))+512d−1)\max(T,M(\max(c-1,0))+512d-1)max(T,M(max(c−1,0))+512d−1) if sequence bit 222222 is set, or updates HHH to max⁡(H,c+d−1)\max(H,c+d-1)max(H,c+d−1) if bit 222222 is unset, leaving the other field unchanged. If the enabling condition fails, construction leaves both integer fields equal to −1-1−1 and uses the reversed list of input coin heights. The constructed list does not affect the Boolean test. Arithmetic in the integer-field updates and comparisons is integer arithmetic, and the indices supplied to MMM use natural-number subtraction with a floor of 000. Consequently the result is true for a failed enabling condition or an empty input list, including at context height 000. No bounds on heights, restrictions on MMM, or conditions comparing input coin heights with the context height are required.

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

    Confirmed by the moderator at approval.

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