A received HTLC cannot be timed out before its expiry at the consensus level
ProvedBitcoinHtlcLocks.received_spend_heightFor a context with the WITNESS and CHECKLOCKTIMEVERIFY flags, keys and hashes of at most 520 bytes, , and 's input index in range: if verifyScript accepts the two-element witness [sig, pk] for the pay-to-witness-script-hash output of receivedBytes revHash remoteKey localKey payHash expiry, and 's transaction is final at height (isFinalTx c.tx height blockTime = true, which ContextualCheckBlock requires of every transaction in a block at that height), then either RevocationSpend c (receivedBytes …) revHash pk sig, or expiry < height. The HTLC mission's script conditions and the block mission's finality rule compose: besides the revocation key, the only two-element witness is the timeout path, whose OP_CHECKLOCKTIMEVERIFY forces nLockTime ≥ expiry and a non-final input, and finality then forces nLockTime < height.
import Definitions.Def_BitcoinHtlcLocks import Definitions.Def_BitcoinVerify import Definitions.Def_BitcoinSer import Definitions.Def_BitcoinScript import Definitions.Def_BitcoinHtlc open BitcoinHtlcLocks BitcoinVerify BitcoinHtlc open BitcoinScript hiding Bytes Byte
theorem BitcoinHtlcLocks.received_spend_height (tap : BitcoinVerify.TaprootHandler) (c : BitcoinVerify.Ctx)
(hf : c.flags.witness = true) (hcltv : c.flags.cltv = true)
(revHash remoteKey localKey payHash : BitcoinSer.Bytes) (expiry : Int) (sig pk : BitcoinSer.Bytes)
(hrev : revHash.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE) (hrem : remoteKey.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE)
(hloc : localKey.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE) (hpay : payHash.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE)
(hexp : 0 < expiry ∧ expiry < BitcoinBlockV2.LOCKTIME_THRESHOLD) (hnIn : c.nIn < c.tx.vin.length)
(hv : BitcoinVerify.verifyScript tap c []
(BitcoinVerify.witnessBytes 0 (BitcoinSer.sha256 (BitcoinHtlc.receivedBytes revHash remoteKey localKey payHash expiry)))
([sig, pk] ++ [BitcoinHtlc.receivedBytes revHash remoteKey localKey payHash expiry]) = true)
(height : Nat) (blockTime : Int) (hfinal : BitcoinBlockV2.isFinalTx c.tx height blockTime = true) :
BitcoinHtlcLocks.RevocationSpend c (BitcoinHtlc.receivedBytes revHash remoteKey localKey payHash expiry) revHash pk sig ∨
expiry < height := by sorryRead-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: BitcoinHtlcLocks.received_spend_height
Declaration shape. A theorem universally quantified over a Taproot handler, a verification context, four byte strings, an integer expiry, two further byte strings, a height and a block time, with eleven hypotheses; the conclusion is a disjunction of a "revocation spend" proposition and a strict integer inequality.
Objects involved
Byte strings. A byte is an 8-bit vector; a byte string is a finite list of bytes. Lengths are natural numbers.
Transactions. A transaction has a version , a list of inputs , a list of outputs and a lock time . An input has a previous outpoint (byte string, index), a scriptSig byte string, a sequence number and a witness stack (list of byte strings). "The -th input of with default" is the zero-based -th input when , else the default input with outpoint (empty, ), empty scriptSig, , empty witness.
Consensus flag set. A record of seven Booleans: . Two projections:
- interpreter flags ;
- signature flags .
Verification context. is a record : a flag set, a transaction, an input index and an amount (the statement places no sign constraint on the amount).
Taproot handler. is an arbitrary function from (context, list of byte strings, byte string) to Booleans. It is unconstrained by any hypothesis.
Constants. ; ; ; .
Hashes. is a concrete SHA-256 implementation on byte strings (32-byte output); with a concrete RIPEMD-160.
The received-HTLC script bytes
Write for the canonical push of byte string : if it is the single length byte followed by ; if it is , one length byte, ; if it is , two little-endian length bytes, ; otherwise , four little-endian length bytes, .
Write for : if or , the single byte (so ); otherwise where is the script-number encoding (empty for ; otherwise the minimal little-endian magnitude bytes, with an extra sign byte / appended if the top bit of the last magnitude byte is set, else the sign folded into that top bit). In particular .
Then, for byte strings (revHash), (remoteKey), (localKey), (payHash) and integer (expiry),
(The opcode bytes are: 0x76 DUP, 0xa9 HASH160, 0x87 EQUAL, 0x63 IF, 0xac CHECKSIG, 0x67 ELSE, 0x7c SWAP, 0x82 SIZE, 0x88 EQUALVERIFY, 0xae CHECKMULTISIG, 0x75 DROP, 0xb1 CHECKLOCKTIMEVERIFY, 0x68 ENDIF.) The parsed-instruction form receivedScript is not used by this statement; only the byte form is.
The output script and the witness
. With (32 bytes) this is the 34-byte string .
The witness list is , in "last element on top" order.
The verification predicate
With scriptSig , scriptPubKey and witness list , the Boolean is computed as follows. Let be: failure if ; failure if does not parse into instructions; otherwise the result of running the parsed script on top-first stack under interpreter flags and the legacy oracle for signature version and script code , which supplies the concrete hashes, , and . A stack's top is "true" () when the stack is non-empty and its top element is non-zero in the script sense: a byte string is true unless it is empty or every byte is except possibly a final .
- Run (base version) on the empty stack; failure gives false; call the result .
- Run (base version) on ; failure gives false; if the top is not true, false.
- If holds and is a witness program (length between 4 and 42, first byte either or in .., and second byte equal to the length; the version is for first byte else first byte , the program is the bytes after the second): if the answer is false; otherwise the answer is .
- Otherwise (witness flag off, or not a witness program) a pay-to-script-hash branch is tried when holds and is the 23-byte pattern ; failing that, the answer is false if the witness flag is on and is non-empty, else true.
Here has length 34, first byte , second byte , so step 3 applies with version and program , and , so the hypothesis reduces to provided steps 1–2 pass (running the empty script, then running "push 32 bytes" and testing the pushed for truth).
with version and a 32-byte program: take the last witness element (); if of it differs from the program, false (here the program is itself); otherwise , i.e. with top-first stack ( on top). That is: false if some stack element exceeds bytes; else must succeed with a stack of exactly one element whose value is true. (So the 10 000-byte bound and the parsability of , and the 520-byte bounds on and , are enforced inside the hypothesis, not by separate hypotheses.) The Taproot handler would only be consulted for version 1 programs and is not reached on this path.
The lock-time checker used by CHECKLOCKTIMEVERIFY. for is false unless either ( and ) or ( and ); then false if (integer comparison with the lock time cast); then false if the -th input of (with default) has ; else true. Whether the interpreter calls it at all is governed by the interpreter flag, which hypothesis (hcltv) sets to true.
The revocation-spend proposition
is the conjunction
with version uses the script code unchanged (no find-and-delete). It returns no value (hence not ) if the signature-encoding check fails or the public-key-encoding check fails, and otherwise of the ECDSA verdict. With signature flags : the signature-encoding check passes for an empty , fails when holds and is not a valid DER encoding, and passes otherwise; the public-key-encoding check always passes (both relevant flags are off). The ECDSA verdict is false if is not a valid public key; false if is empty; otherwise, with the last byte of as hash type and the rest as the raw signature, false if , else the outcome of verifying the raw signature with against the witness-v0 signature hash of .
Statement
For every Taproot handler , every context , every byte strings , every integer , every byte strings , every (height) and (blockTime), assuming
- (hf) ;
- (hcltv) ;
- (hrev) ; (hrem) ; (hloc) ; (hpay) ;
- (hexp) and (integer comparisons; the constant is cast from );
- (hnIn) ;
- (hv) with ;
- (hfinal) , where is: true if ; else true if (cast to ) is strictly less than (cast to ) when , or strictly less than otherwise; else true exactly when every input of has ;
the conclusion is
where the inequality compares the integer with the natural number cast to .
Edge cases made explicit
- No hypothesis fixes or to 20 bytes, or to 33 bytes; they are bounded only above by 520. No hypothesis bounds or directly; the 520-byte bound on them is enforced only inside (hv).
- , , , , are unconstrained; may be negative (in which case the ECDSA verdict is false, so the first disjunct cannot hold through a verified signature).
- is unconstrained by the hypotheses: it may be (then (hfinal) holds automatically), below the threshold (compared with ), or at/above it (compared with , in which case is not consulted by (hfinal)). is otherwise unused.
- (hnIn) guarantees the -th input exists, so the default input never arises in or ; the sequence number of that input is not constrained by any hypothesis.
- may be . The disjunction is inclusive: both disjuncts may hold.
- (hexp) is on the integer ; the conclusion's is also an integer comparison. in is a one-byte opcode when and a script-number push otherwise.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.