Lean transcription of the pinned Core sequence-lock helpers
DefinitionBIP68_CoreThe implementation reads unsigned 32-bit words using the masks , , and , 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 and a reversed height list. It starts at . Processing a disabled input prepends zero and leaves both thresholds unchanged. For an enabled time input it updates to ; for an enabled height input it updates to . 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 sentinel.
If transaction-level enforcement is off, calculation preserves all original heights and returns thresholds . The output height list is obtained by reversing the accumulator's list. Evaluation accepts precisely when
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.
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
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 and the bitwise AND of flags with is nonzero. Thus version ranges over all unsigned values from through , and only the least significant bit of flags affects this result; versions and 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 , namely , is nonzero. Equivalently, bit , counting the least significant bit as bit , 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 , namely , is nonzero. Equivalently, bit , counting the least significant bit as bit , is set; all other bits, including bit , 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 , namely . It is the unsigned value of the lowest sixteen bits, lies between and 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 , shifting the resulting 32-bit bit vector left by positions, and taking its unsigned value. If is the unsigned value of the lowest sixteen bits of sequence, the result is ; because , this shift loses no nonzero bit and its result ranges from to inclusive in multiples of . 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 , integer minTime equal to , and heightsRev equal to the empty list.
coreStep. Given a context consisting of a natural-number height and an arbitrary function from natural numbers to natural numbers, an accumulator with integer fields and natural-number list , and an input consisting of a 32-bit sequence and a natural-number coin height , this function returns an accumulator as follows. If sequence bit is set, the result is , where denotes prepending to . Otherwise, let be the unsigned value of the lowest sixteen sequence bits: if sequence bit is set, the result is ; if bit is unset, it is . The expressions added to or are computed in the integers after converting the natural-number quantities, so their final subtraction of may produce ; the subtraction in the argument to is natural-number subtraction, so uses . The context height is not used by this function. Inputs with both bits and 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 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 and flags bit is set, it starts with integer fields and list and processes the inputs from left to right: an input with coin height and sequence bit set prepends to and leaves unchanged; otherwise it prepends and, writing for the unsigned value of the lowest sixteen sequence bits, replaces with when bit is set, or replaces with when bit is unset, leaving the other integer field unchanged. Those updates use integer arithmetic, while the index expresses natural-number subtraction. If the enabling condition is false, the returned integer fields are both 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 in either branch. The context height is unused, and there are no restrictions on input heights or on .
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 and an arbitrary function from natural numbers to natural numbers, and any accumulator with integer fields and a finite natural-number list, this Boolean is true exactly when both strict inequalities and hold, with the natural-number right sides viewed as integers. The list field is ignored. The index uses natural-number subtraction, so height compares against ; negative integer fields are allowed, and in particular fields satisfy both inequalities for every context. No monotonicity or bound on , or restriction on the accumulator's origin, is assumed.
sequenceLocks. Given arbitrary 32-bit version and flags, a context with natural-number height and an arbitrary function 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 and for its integer fields. When the unsigned version is at least and flags bit is set, construction starts with and an empty list and processes inputs from left to right: sequence bit being set leaves both integer fields unchanged and prepends to the list; otherwise, for coin height and lowest-sixteen-bit unsigned value , it prepends and updates to if sequence bit is set, or updates to if bit is unset, leaving the other field unchanged. If the enabling condition fails, construction leaves both integer fields equal to 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 use natural-number subtraction with a floor of . Consequently the result is true for a failed enabling condition or an empty input list, including at context height . No bounds on heights, restrictions on , or conditions comparing input coin heights with the context height are required.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.