The Lightning anchor-output scripts (to_remote, anchor, CSV-1 HTLCs) and their spending conditions
DefinitionBitcoinAnchorsNames over the script, signature, verification, to_local and HTLC missions; nothing of Core is re-transcribed. These are the BOLT #3 scripts with option_anchors, the form every modern channel uses.
toRemoteScript remoteKey / toRemoteBytes — <remote_pubkey> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY. anchorScript fundingKey / anchorBytes — <funding_pubkey> OP_CHECKSIG OP_IFDUP OP_NOTIF 16 OP_CHECKSEQUENCEVERIFY OP_ENDIF. offeredScript …, receivedScript … expiry / their byte forms — the HTLC mission's scripts with 1 OP_CHECKSEQUENCEVERIFY OP_DROP inserted after the inner OP_ENDIF, inside the outer OP_ELSE.
ToRemoteSpend f o remoteKey st — st = [sig] with sig valid for remoteKey and CsvPasses f o 1. AnchorSpend f o fundingKey st — st = [sig] and either sig valid for fundingKey (the key holder, at any time) or the check returns a clean false (as for the empty signature) and CsvPasses f o 16 (anyone, after 16 blocks). OfferedSpend, ReceivedSpend — the HTLC mission's three paths each, with CsvPasses f o 1 added to the two non-revocation paths.
Formalization Note. CsvPasses, numInstr, inputSequence are the to_local mission's; CltvPasses, multisigLoop the HTLC and script missions'; legacyOracle, checkSequence, parseScript the signature mission's; verifyScript the verification mission's. The 20-byte hashes are parameters. Only Std is imported.
import Definitions.Def_BitcoinHtlc
/-!
# The Lightning anchor-output commitment scripts under Bitcoin's script interpreter
BOLT #3 (Lightning Network specification, *Bitcoin Transaction and Script Formats*) with
`option_anchors` changes three outputs of a commitment transaction and adds a fourth:
`to_remote` (the counterparty's balance, now delayed by one block so that the anchor can be used
for fee bumping first):
```
<remote_pubkey> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY
```
Anchor (one per side, keyed by that side's funding key; spendable by the key holder at once and by
anyone after 16 blocks):
```
<funding_pubkey> OP_CHECKSIG OP_IFDUP
OP_NOTIF
16 OP_CHECKSEQUENCEVERIFY
OP_ENDIF
```
Offered HTLC (the non-anchor script of `BitcoinHtlc.offeredScript` with `1 OP_CHECKSEQUENCEVERIFY
OP_DROP` at the end of the outer `OP_ELSE` branch):
```
OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationpubkey))> OP_EQUAL
OP_IF OP_CHECKSIG
OP_ELSE <remote_htlcpubkey> OP_SWAP OP_SIZE 32 OP_EQUAL
OP_NOTIF OP_DROP 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG
OP_ELSE OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
1 OP_CHECKSEQUENCEVERIFY OP_DROP
OP_ENDIF
```
Received HTLC (likewise, from `BitcoinHtlc.receivedScript`):
```
OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationpubkey))> OP_EQUAL
OP_IF OP_CHECKSIG
OP_ELSE <remote_htlcpubkey> OP_SWAP OP_SIZE 32 OP_EQUAL
OP_IF OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG
OP_ELSE OP_DROP <cltv_expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP OP_CHECKSIG
OP_ENDIF
1 OP_CHECKSEQUENCEVERIFY OP_DROP
OP_ENDIF
```
All four are paid to as pay-to-witness-script-hash. `1` and `16` are `CScript << 1` / `<< 16`,
i.e. `OP_1` (`0x51`) and `OP_16` (`0x60`); `2`, `32` and the expiry are as in `BitcoinHtlc`
(`BitcoinToLocal.numInstr` / `numBytes`). The hashes and keys are arbitrary byte strings.
Witness stacks are listed top first, as `evalScript` takes them (the reverse of BOLT #3's
`CScriptWitness` order): `to_remote` is spent with `<remote_sig>`; an anchor with `<sig>` by the key
holder, or with the empty signature `<>` by anyone once 16 blocks have passed (the interpreter turns
the empty signature into a clean `false`, which `OP_IFDUP` does not duplicate and `OP_NOTIF` consumes,
so the `16 OP_CHECKSEQUENCEVERIFY` branch runs and leaves `16` as the lone true value); the HTLCs
exactly as in `BitcoinHtlc`, now with `OP_CHECKSEQUENCEVERIFY 1` enforced on every non-revocation path.
-/
namespace BitcoinAnchors
open BitcoinScript BitcoinSig BitcoinVerify BitcoinToLocal
/-- The BOLT #3 `to_remote` witness script (`option_anchors`), parsed. -/
def toRemoteScript (remoteKey : Bytes) : Script :=
[.push remoteKey, .op OP_CHECKSIGVERIFY, numInstr 1, .op OP_CHECKSEQUENCEVERIFY]
/-- The `to_remote` witness script, as `CScript` serializes it. -/
def toRemoteBytes (remoteKey : Bytes) : Bytes :=
pushBytes remoteKey ++ [OP_CHECKSIGVERIFY] ++ numBytes 1 ++ [OP_CHECKSEQUENCEVERIFY]
/-- The BOLT #3 anchor witness script, parsed. -/
def anchorScript (fundingKey : Bytes) : Script :=
[.push fundingKey, .op OP_CHECKSIG, .op OP_IFDUP, .op OP_NOTIF, numInstr 16, .op OP_CHECKSEQUENCEVERIFY,
.op OP_ENDIF]
/-- The anchor witness script, as `CScript` serializes it. -/
def anchorBytes (fundingKey : Bytes) : Bytes :=
pushBytes fundingKey ++ [OP_CHECKSIG, OP_IFDUP, OP_NOTIF] ++ numBytes 16 ++ [OP_CHECKSEQUENCEVERIFY, OP_ENDIF]
/-- The BOLT #3 offered-HTLC witness script with `option_anchors`, parsed. -/
def offeredScript (revHash remoteKey localKey payHash : Bytes) : Script :=
[.op OP_DUP, .op OP_HASH160, .push revHash, .op OP_EQUAL,
.op OP_IF, .op OP_CHECKSIG,
.op OP_ELSE, .push remoteKey, .op OP_SWAP, .op OP_SIZE, numInstr 32, .op OP_EQUAL,
.op OP_NOTIF, .op OP_DROP, numInstr 2, .op OP_SWAP, .push localKey, numInstr 2, .op OP_CHECKMULTISIG,
.op OP_ELSE, .op OP_HASH160, .push payHash, .op OP_EQUALVERIFY, .op OP_CHECKSIG,
.op OP_ENDIF,
numInstr 1, .op OP_CHECKSEQUENCEVERIFY, .op OP_DROP,
.op OP_ENDIF]
/-- The BOLT #3 received-HTLC witness script with `option_anchors`, parsed. -/
def receivedScript (revHash remoteKey localKey payHash : Bytes) (expiry : Int) : Script :=
[.op OP_DUP, .op OP_HASH160, .push revHash, .op OP_EQUAL,
.op OP_IF, .op OP_CHECKSIG,
.op OP_ELSE, .push remoteKey, .op OP_SWAP, .op OP_SIZE, numInstr 32, .op OP_EQUAL,
.op OP_IF, .op OP_HASH160, .push payHash, .op OP_EQUALVERIFY, numInstr 2, .op OP_SWAP, .push localKey,
numInstr 2, .op OP_CHECKMULTISIG,
.op OP_ELSE, .op OP_DROP, numInstr expiry, .op OP_CHECKLOCKTIMEVERIFY, .op OP_DROP, .op OP_CHECKSIG,
.op OP_ENDIF,
numInstr 1, .op OP_CHECKSEQUENCEVERIFY, .op OP_DROP,
.op OP_ENDIF]
/-- The offered-HTLC witness script (`option_anchors`), as `CScript` serializes it. -/
def offeredBytes (revHash remoteKey localKey payHash : Bytes) : Bytes :=
[OP_DUP, OP_HASH160] ++ pushBytes revHash ++ [OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE] ++
pushBytes remoteKey ++ [OP_SWAP, OP_SIZE] ++ numBytes 32 ++ [OP_EQUAL, OP_NOTIF, OP_DROP] ++
numBytes 2 ++ [OP_SWAP] ++ pushBytes localKey ++ numBytes 2 ++ [OP_CHECKMULTISIG, OP_ELSE, OP_HASH160] ++
pushBytes payHash ++ [OP_EQUALVERIFY, OP_CHECKSIG, OP_ENDIF] ++ numBytes 1 ++
[OP_CHECKSEQUENCEVERIFY, OP_DROP, OP_ENDIF]
/-- The received-HTLC witness script (`option_anchors`), as `CScript` serializes it. -/
def receivedBytes (revHash remoteKey localKey payHash : Bytes) (expiry : Int) : Bytes :=
[OP_DUP, OP_HASH160] ++ pushBytes revHash ++ [OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE] ++
pushBytes remoteKey ++ [OP_SWAP, OP_SIZE] ++ numBytes 32 ++ [OP_EQUAL, OP_IF, OP_HASH160] ++
pushBytes payHash ++ [OP_EQUALVERIFY] ++ numBytes 2 ++ [OP_SWAP] ++ pushBytes localKey ++ numBytes 2 ++
[OP_CHECKMULTISIG, OP_ELSE, OP_DROP] ++ numBytes expiry ++
[OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_CHECKSIG, OP_ENDIF] ++ numBytes 1 ++
[OP_CHECKSEQUENCEVERIFY, OP_DROP, OP_ENDIF]
/-- The one way of satisfying the `to_remote` script, on a witness stack `st` (top first): a lone
signature valid for the remote key, and the one-block delay check (`OP_CHECKSEQUENCEVERIFY 1`)
passing. `OP_CHECKSIGVERIFY` fails the script outright on an invalid signature, so the only value
ever left is the pushed `1`. -/
def ToRemoteSpend (f : Flags) (o : Oracle) (remoteKey : Bytes) (st : List Bytes) : Prop :=
∃ sig, st = [sig] ∧ o.checkSig sig remoteKey = some true ∧ CsvPasses f o 1
/-- The two ways of satisfying the anchor script, on a witness stack `st` (top first): a lone element
`sig` that is either a valid signature for the funding key (the key holder, at any time: `OP_IFDUP`
keeps the `true` and `OP_NOTIF` skips the delay), or a *clean* `false` of the signature check (such as
the empty signature) together with the 16-block delay check passing (anyone, after the delay: the
`16` pushed for `OP_CHECKSEQUENCEVERIFY` is the value left). A signature the oracle rejects outright
(`none`, e.g. a malformed encoding) fails the script. -/
def AnchorSpend (f : Flags) (o : Oracle) (fundingKey : Bytes) (st : List Bytes) : Prop :=
∃ sig, st = [sig] ∧
(o.checkSig sig fundingKey = some true ∨ (o.checkSig sig fundingKey = some false ∧ CsvPasses f o 16))
/-- The three ways of satisfying the offered-HTLC script with `option_anchors`: exactly
`BitcoinHtlc.OfferedSpend`, with the one-block delay check `CsvPasses f o 1` added to the preimage
and timeout paths (the revocation path skips it). -/
def OfferedSpend (f : Flags) (o : Oracle) (revHash remoteKey localKey payHash : Bytes) (st : List Bytes) : Prop :=
(∃ pk sig, st = [pk, sig] ∧ o.hash160 pk = revHash ∧ o.checkSig sig pk = some true) ∨
(∃ preimage sig, st = [preimage, sig] ∧ o.hash160 preimage ≠ revHash ∧ preimage.length = 32 ∧
o.hash160 preimage = payHash ∧ o.checkSig sig remoteKey = some true ∧ CsvPasses f o 1) ∨
(∃ x localSig remoteSig dummy, st = [x, localSig, remoteSig, dummy] ∧ o.hash160 x ≠ revHash ∧ x.length ≠ 32 ∧
multisigLoop o [localSig, remoteSig] [localKey, remoteKey] = some true ∧ (f.nulldummy = true → dummy = []) ∧
CsvPasses f o 1)
/-- The three ways of satisfying the received-HTLC script with `option_anchors`: exactly
`BitcoinHtlc.ReceivedSpend`, with the one-block delay check `CsvPasses f o 1` added to the success
and timeout paths (the revocation path skips it). -/
def ReceivedSpend (f : Flags) (o : Oracle) (revHash remoteKey localKey payHash : Bytes) (expiry : Int)
(st : List Bytes) : Prop :=
(∃ pk sig, st = [pk, sig] ∧ o.hash160 pk = revHash ∧ o.checkSig sig pk = some true) ∨
(∃ preimage localSig remoteSig dummy, st = [preimage, localSig, remoteSig, dummy] ∧
o.hash160 preimage ≠ revHash ∧ preimage.length = 32 ∧ o.hash160 preimage = payHash ∧
multisigLoop o [localSig, remoteSig] [localKey, remoteKey] = some true ∧ (f.nulldummy = true → dummy = []) ∧
CsvPasses f o 1) ∨
(∃ x sig, st = [x, sig] ∧ o.hash160 x ≠ revHash ∧ x.length ≠ 32 ∧ BitcoinHtlc.CltvPasses f o expiry ∧
o.checkSig sig remoteKey = some true ∧ CsvPasses f o 1)
end BitcoinAnchors
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: the definitions of Def_BitcoinAnchors.lean
This file introduces twelve definitions in the namespace BitcoinAnchors; each is rendered below in order. Throughout, a byte is an 8-bit word, a byte string is a finite list of bytes, a stack is a finite list of byte strings written top first, and all the auxiliary notions (, , , , , , , flags, oracles) are unfolded in parts A and P below.
1. toRemoteScript , for a byte string : the four-instruction script
where . No constraint is placed on (it may be empty or longer than 520 bytes; it is not required to be a public key).
2. toRemoteBytes : the byte string , with , i.e. the canonical push of followed by the bytes .
3. anchorScript , for a byte string : the seven-instruction script
with . is unconstrained.
4. anchorBytes : the byte string , with .
5. offeredScript , for byte strings (revHash), (remoteKey), (localKey), (payHash): the 30-instruction script
OP_DUP, OP_HASH160, , OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE, , OP_SWAP, OP_SIZE, , OP_EQUAL, OP_NOTIF, OP_DROP, , OP_SWAP, , , OP_CHECKMULTISIG, OP_ELSE, OP_HASH160, , OP_EQUALVERIFY, OP_CHECKSIG, OP_ENDIF, , OP_CHECKSEQUENCEVERIFY, OP_DROP, OP_ENDIF;
here (a one-byte data push), and . None of is constrained (lengths are arbitrary, they may coincide).
6. receivedScript , for byte strings and an integer (expiry): the 32-instruction script
OP_DUP, OP_HASH160, , OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE, , OP_SWAP, OP_SIZE, , OP_EQUAL, OP_IF, OP_HASH160, , OP_EQUALVERIFY, , OP_SWAP, , , OP_CHECKMULTISIG, OP_ELSE, OP_DROP, , OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_CHECKSIG, OP_ENDIF, , OP_CHECKSEQUENCEVERIFY, OP_DROP, OP_ENDIF.
is when or and otherwise ( for ; for negative a push whose last byte has its top bit set). is an arbitrary integer with no range restriction in the definition.
7. offeredBytes : the byte string
,
with , , .
8. receivedBytes : the byte string
,
where is the single byte for or and otherwise.
9. ToRemoteSpend , a proposition on a flag record , an oracle , a byte string and a stack :
That is: the stack has exactly one element ; the oracle's check of against is the clean value true (not none, not false); and either or (the bit-31 disjunct of being false for the operand ). There is only one disjunct.
10. AnchorSpend :
Two disjuncts, both requiring a one-element stack : (a) , with no condition on flags or sequence; (b) together with ( or ). A for which the oracle returns satisfies neither disjunct. The predicate does not say what is (e.g. it does not require in disjunct (b)).
11. OfferedSpend : the disjunction of exactly three disjuncts.
- (i) Revocation: with (two elements, on top), , and (the signature is checked against the stack element , not against or ). No flag or sequence condition.
- (ii) Preimage: with , , , , , and (i.e. or ).
- (iii) Timeout: with (four elements, on top, at the bottom), , , (which, unfolded, is and ), the implication (so is unconstrained when the flag is off), and . The disjuncts are not stated to be exclusive; is an arbitrary function, so e.g. with makes (i) and (ii) compete only through the condition in (ii).
12. ReceivedSpend : the disjunction of exactly three disjuncts.
- (i) Revocation: with , , . No flag, sequence or lock-time condition.
- (ii) Success: with , , , , (i.e. and ), , and .
- (iii) Timeout: with , , , (i.e. , or and ), , and . In all three predicates (10)–(12) the oracle and flags are parameters with no constraints, so , , and are arbitrary functions, and the predicates are statements about those function values only.
Common unfolded definitions, part A: the script interpreter
Everything below is a literal unfolding of the definitions the statement depends on. Nothing here is a claim about the theorem; it only says what the symbols mean.
Bytes and stacks. A byte is an 8-bit word, i.e. a value in . A byte string is a finite list of bytes; is its length and is the empty byte string. A stack is a finite list of byte strings written top first: in the element is on top. The constants are , , , .
Truth value of a byte string, : is false; a one-byte string is true iff and ; a string of length at least two is true iff or . Equivalently, is true iff some byte of is nonzero, except that a string consisting of zero bytes followed by a final byte is false.
: (the single byte ) and (the empty byte string).
Stack literals used in the statements. is the stack with exactly one element, the one-byte string ; is the stack with exactly one element, the one-byte string (decimal ); is the stack with exactly one element, the empty byte string; is the empty stack. A stack written has exactly one element ; has exactly two, on top.
Flags : a record of three Booleans , , . No other verification flag exists in this model. Where is a parameter of a theorem, all three values are unconstrained.
Oracle : a record of arbitrary functions with no axioms relating them to each other or to anything else: five byte-string-to-byte-string functions , , , , ; a function from pairs of byte strings (first the signature, second the key) to the three values , , ; and two functions . In particular, in a theorem that takes as a parameter, is an arbitrary function (its outputs need not be 20 bytes long, it need not be injective, and it has no relation to ), and is an arbitrary three-valued function.
Script numbers. for : if ; otherwise the little-endian bytes of with no trailing zero byte, after which: if the last byte has its top bit () set, one extra byte is appended ( if , if ); otherwise, if , the top bit of the last byte is set. Thus , , , . The decoding is for , and otherwise the little-endian value of with the top bit of the last byte removed and read as a minus sign. is if and otherwise (no minimal-encoding check is made).
Instructions and scripts. An instruction is either a push of a byte string , or an opcode for a byte . A script is a finite list of instructions. Opcode bytes that occur: OP_1NEGATE , OP_1 , OP_2 , OP_16 , OP_IF , OP_NOTIF , OP_ELSE , OP_ENDIF , OP_IFDUP , OP_DROP , OP_DUP , OP_SWAP , OP_SIZE , OP_EQUAL , OP_EQUALVERIFY , OP_HASH160 , OP_CHECKSIG , OP_CHECKSIGVERIFY , OP_CHECKMULTISIG , OP_CHECKLOCKTIMEVERIFY , OP_CHECKSEQUENCEVERIFY .
for : if or , the opcode (so , , , ); otherwise the push (so and ).
The parsed scripts of the anchors file, for arbitrary byte strings (funding key), (remote key), (local key), (revocation hash), (payment hash) and an integer (expiry):
is the 30-instruction list: OP_DUP, OP_HASH160, , OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE, , OP_SWAP, OP_SIZE, , OP_EQUAL, OP_NOTIF, OP_DROP, OP_2, OP_SWAP, , OP_2, OP_CHECKMULTISIG, OP_ELSE, OP_HASH160, , OP_EQUALVERIFY, OP_CHECKSIG, OP_ENDIF, OP_1, OP_CHECKSEQUENCEVERIFY, OP_DROP, OP_ENDIF.
is the 32-instruction list: OP_DUP, OP_HASH160, , OP_EQUAL, OP_IF, OP_CHECKSIG, OP_ELSE, , OP_SWAP, OP_SIZE, , OP_EQUAL, OP_IF, OP_HASH160, , OP_EQUALVERIFY, OP_2, OP_SWAP, , OP_2, OP_CHECKMULTISIG, OP_ELSE, OP_DROP, , OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_CHECKSIG, OP_ENDIF, OP_1, OP_CHECKSEQUENCEVERIFY, OP_DROP, OP_ENDIF.
Execution state: a main stack, an alt stack, a condition stack (a list of Booleans) and an op counter. The initial state for a stack has main stack , empty alt stack, empty , counter . The machine is executing when every entry of is true (vacuously, when is empty).
: starting from the initial state of , apply to the instructions of in order; if any step returns the result is ; after the last instruction the result is if is non-empty and otherwise of the final main stack. So the value is either (script failure) or for a stack (the alt stack and the counter are discarded).
on : if ; otherwise, if executing, is placed on top of the main stack (if not executing, nothing changes); then if the main-stack size plus the alt-stack size exceeds , else the new state.
on : is counted when ; if counted and the counter plus one exceeds , ; otherwise the counter is incremented when counted; if is one of the fifteen disabled opcodes (none of the opcodes listed above is disabled); otherwise, if executing or (the conditionals), is applied and the result is if it is or if the stack-size sum then exceeds ; otherwise (not executing, not a conditional) the state is unchanged, again subject to the size check.
for the opcodes that occur ("" means script failure; "no change" means the state is returned unchanged):
- OP_1NEGATE, OP_1, …, OP_16: push ; so OP_1 pushes , OP_2 pushes , OP_16 pushes . These bytes are , so they are not counted.
- OP_CHECKLOCKTIMEVERIFY: if , no change. Otherwise on an empty stack; else with top (not popped): if ; with , if , if , else no change.
- OP_CHECKSEQUENCEVERIFY: if , no change. Otherwise on an empty stack; else with top (not popped): if ; with , if ; no change if bit of is set (precisely: ); else if , else no change.
- OP_IF / OP_NOTIF: if executing, on an empty stack; else the top is popped, (negated for OP_NOTIF) and is pushed onto . If not executing, is pushed onto and the stack is untouched.
- OP_ELSE: if is empty; else its top entry is negated.
- OP_ENDIF: if is empty; else its top entry is removed.
- OP_IFDUP: on an empty stack; else if of the top is true the top is duplicated, otherwise no change.
- OP_DROP: on an empty stack; else the top is removed. OP_DUP: on an empty stack; else the top is duplicated. OP_SWAP: with fewer than two elements; else the top two are exchanged. OP_SIZE: on an empty stack; else is pushed above the top , which stays.
- OP_EQUAL / OP_EQUALVERIFY: with fewer than two elements; else the top and the next are popped and is the proposition " and are the same byte string"; OP_EQUAL pushes ; OP_EQUALVERIFY pushes nothing and is when is false.
- OP_HASH160: on an empty stack; else the top is replaced by .
- OP_CHECKSIG / OP_CHECKSIGVERIFY: with fewer than two elements; else the top (the key) and the next (the signature) are popped and is consulted: gives ; : OP_CHECKSIG pushes , while OP_CHECKSIGVERIFY pushes nothing and is when .
- OP_CHECKMULTISIG: on an empty stack; the top is popped and decoded by ( if longer than 4 bytes); if or ; the counter becomes counter , if that exceeds ; the next elements are the keys ( if fewer remain); the next element is popped ( if none remains) and decoded likewise, if or ; the next elements are the signatures ( if fewer remain); is computed: gives ; : if no element remains, else one more element is popped, if and , else is pushed.
(both lists top first): if sigs is empty; if sigs is non-empty and keys is empty; otherwise, with the first signature and the first key, : gives ; gives if fewer keys than signatures remain after removing both, else the loop on the remaining signatures and remaining keys; gives if the remaining keys are fewer than all the current signatures, else the loop on the same signatures and the remaining keys. For the only instance that occurs, unfolds to: if ; if ; and, if , the value itself. Hence it equals iff and ; the signature is never checked against , nor against .
for :
For and the bit-31 disjunct is false ( and ), so and . When and are arbitrary parameters, is an arbitrary function of an integer.
for : .
: for the empty stack; of the top element otherwise.
The spend predicates of the anchors file (all stacks top first; range over byte strings):
: there is with , and .
: there is with and either , or both and .
is the disjunction of
- there are with , and ;
- there are with , , , , and ;
- there are with , , , , () and .
is the disjunction of
- there are with , and ;
- there are with , , , , , () and ;
- there are with , , , , and .
Common unfolded definitions, part P: byte-level scripts and parsing
Canonical push, for a byte string : if (= ), the single byte followed by ; else if , the bytes , , then ; else if , the byte , the two little-endian bytes of , then ; else the byte , the four little-endian bytes of , then .
for : if or , the single byte (so , , ); otherwise (so and , the empty push).
The serialized scripts of the anchors file (byte strings, is concatenation):
.
.
.
.
, one instruction off the front of a byte string: if is empty. Otherwise let be the first byte and the rest. If : the push size is itself when ; the next byte when ( if is empty); the little-endian value of the next two bytes when ( if fewer than two remain); the little-endian value of the next four bytes when ( if fewer than four remain); then, with the size bytes removed from , if fewer than size bytes remain, else the pair ( of the next size bytes, the remainder). If : the pair (, ).
: an auxiliary recursion with fuel : on the empty string, ; with fuel and a non-empty string, ; otherwise ( propagates) and the instruction is prepended to the parse of the remainder with one less fuel. The value is either or of a list of instructions.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.