Lotus VM specification
On this page
Status: active development draft, summarizing and indexing the Lotus virtual machine defined across P03: VM semantics, P05: Resources and parameters, Script and wallet ABI, and Deferred checks (C12a). For the high-level contract language and compiler targeting this VM, see Lotus language and compiler.
Execution model
The Lotus VM is a deterministic, statically bounded stack machine designed for high-throughput UTXO validation. It is a clean-slate V2 virtual machine—not Bitcoin Script with legacy quirks.
- Two stacks: Each invocation runs with a main stack and an alternate stack of bounded byte strings.
- Pre-execution scan and decode: Before running any instruction, the VM performs a bounded
OP_SUCCESS_Nscan. If an unassigned extension opcode is present, the entire invocation succeeds immediately without initializing stacks. Otherwise, the entire script (including unexecuted branches) is decoded into a structured AST of sequences, conditionals, and loops; malformed pushes or unmatched delimiters reject before execution starts. - Two invocation kinds:
- Spend invocation (
INVOCATIONKIND = 1): Authorizes one branch of the selected path in a spent coin (coin.paths[path_index][witness_index]). Initial main stack contains the ordered witness arguments;BRANCHROOTreturns the proven branch root. - Mint invocation (
INVOCATIONKIND = 3): Authorizes a mint input (input.flags = 0x03) for a token tree. Initial main stack contains the witness arguments with the node-pushed positive mint delta (Amount) on top;BRANCHROOTreturns the 32-bytetoken_id. Mint inputs resolve no spent coin. - Completion rule: Normal execution succeeds if and only if no error occurred and at termination
main = [N(1)]andalt = []. - Monotone validity (C22a): No opcode exposes block height, block timestamp, or ancestor block hashes. A script's outcome depends strictly on the transaction, its resolved input coins, and
CHAINID, so an accepted mempool transaction never becomes invalid as new blocks arrive. Time-based conditions useCHECKLOCKTIMEVERIFYandCHECKSEQUENCEVERIFYagainst transaction/input locks.
Values and numeric arithmetic
| Value kind | Representation | Rules |
|---|---|---|
| Byte strings | 0..MAX_ELEMENT_BYTES raw bytes |
General stack items (hashes, public keys, signatures, templates, serialized records). |
Signed integer N(x) |
16..1024 bytes, little-endian two's complement |
128-bit baseline (MIN_NUMERIC_BYTES = 16) up to 8,192-bit big-int (MAX_NUMERIC_BYTES = 1024). Constants, counts, indices, values, and LOG push 16-byte int128. |
Unsigned integer U(x) |
0..1024 bytes, little-endian unsigned |
Used by ADDCARRY, SUBCARRY, and MUL128_WIDE; shorter operands zero-pad to W = max(16, len(a), len(b)). |
| Canonical Boolean | Exact 16-byte N(0) or N(1) |
All Boolean consumers (IF, NOTIF, VERIFY, NOT, BOOLAND, BOOLOR, COND_BREAK) reject any other byte string. Comparisons and signature checks always produce canonical Booleans. |
Arithmetic is checked:
- Unary ops (
1ADD,1SUB,NEGATE,ABS,SQRT) preservelen(a)and reject on overflow. - Binary ops (
ADD,SUB,DIV,MOD,MIN,MAX) operate atW = max(len(a), len(b))and reject on overflow, divide-by-zero, orMIN_INT / -1. MULwidens automatically tomax(W, minimal_signed_width(a * b))up toMAX_MUL_BYTES = 2048bytes.BITWISEops (AND,OR,XOR) zero-pad the shorter operand tomax(len(a), len(b)).LOGreturns a deterministic fixed-algorithm 64-bit fractional $\log_2$ (N(e * 2^64 + f)), andSQRTreturnsfloor(sqrt(x))forx >= 0.
Bytecode framing and structured control flow
- Canonical pushes:
0x00(ZERO),0x4d(NEGATIVE_ONE),0x4e–0x5d(ONE–SIXTEEN) push canonical 16-byteN(...)constants.0x01–0x4b(PUSH_1–PUSH_75) push the nextnbytes directly.0x4c(PUSHDATA) pushes aCompactSize-prefixed byte string and is valid only for length0or length>= 76(no legacyPUSHDATA1/2/4).- Conditionals:
IF/NOTIF…[ELSE …]ENDIF. - Bounded loops:
ENUMERATE(0x69) pops a bound0 <= bound <= 256and executes its bodyboundtimes, pushingN(index)on each iteration, up toENDENUMERATE(0x6a). At most two nested loops are allowed on any syntax path (including unexecuted arms). Iterating inputs or outputs usesTXINPUTCOUNT ENUMERATEorTXOUTPUTCOUNT ENUMERATE. - Early exit:
BREAK(0x6b) popsN(depth)(1 <= depth <= active_loop_depth) and unwindsdepthenclosing loops cleanly across nested conditionals.COND_BREAK(0x6d) popsN(depth)thenBoolean(cond)and breaks whencond == N(1). - Off-chain simulation logging:
EMITLOG(0x6c) emits structured events in off-chain wallet simulation (item_1 ... item_k N(k) -> empty), but is permanentlyOP_INVALIDon-chain (neverOP_SUCCESS_N). Production compiler builds stripemitstatements completely.
Instruction set overview
The canonical byte allocation lives in tools/opcodes.json and is defined instruction-by-instruction in P03: VM semantics.
| Hex range | Group | Representative instructions |
|---|---|---|
00–5d |
Literals and pushes | ZERO, PUSH_1..PUSH_75, PUSHDATA, NEGATIVE_ONE, ONE..SIXTEEN |
60–6d |
Control flow | NOP, IF, NOTIF, ELSE, ENDIF, VERIFY, RETURN, ENUMERATE, ENDENUMERATE, BREAK, EMITLOG (sim-only), COND_BREAK |
70–81 |
Stack | TOALTSTACK, FROMALTSTACK, DEPTH, DROP, DUP, NIP, OVER, PICK, ROLL, ROT, SWAP, TUCK, 2DROP, 2DUP, 3DUP, 2OVER, 2ROT, 2SWAP |
88–94 |
Byte strings | CAT, SPLIT, NUM2BIN, BIN2NUM, SIZE, INVERT, AND, OR, XOR, EQUAL, EQUALVERIFY, LESSSWAP, RAWLEFTBITSHIFT |
98–b4 |
Arithmetic | 1ADD, 1SUB, NEGATE, ABS, NOT, 0NOTEQUAL, ADD, SUB, MUL, DIV, MOD, MULPOW2, BOOLAND, BOOLOR, NUMEQUAL, NUMEQUALVERIFY, NUMNOTEQUAL, LESSTHAN, GREATERTHAN, LESSTHANOREQUAL, GREATERTHANOREQUAL, MIN, MAX, WITHIN, ADDCARRY, SUBCARRY, MUL128_WIDE, LOG, SQRT |
b8–c3 |
Cryptography and commitments | SHA256, HASH256, BLAKE3, CHECKSIG, CHECKSIGVERIFY, CHECKDATASIG, LEAFHASH, NODEHASH, MERKLEROOT_SORTED, MERKLEROOT_INDEXED, SIGHASH, STATEHASH |
c8–cf |
Transaction | TXVERSION, TXINPUTCOUNT, TXOUTPUTCOUNT, TXMINTCOUNT, TXLOCKTIME, TXLOCKKIND, TXID, CHAINID |
d0–df |
Inputs and resolved coins | INPUTINDEX, INPUTFLAGS, OUTPOINTTXID, OUTPOINTINDEX, INPUTSEQUENCENUMBER, INPUTPATHINDEX, UTXOVALUE, UTXOTOKENID, UTXOBRANCHCOUNT, UTXOBRANCH, UTXOPATHCOUNT, UTXOPATH, UTXOOUTPUT, UTXOREQUIRESBRANCH, UTXOFLAGS, INPUTWITNESSARG |
e0–ef |
Outputs, assets, and state | OUTPUTFLAGS, OUTPUTVALUE, OUTPUTTOKENID, OUTPUTBRANCHCOUNT, OUTPUTBRANCH, OUTPUTPATHCOUNT, OUTPUTPATH, OUTPUTRECORD, OUTPUTREQUIRESBRANCH, OUTPUTEXCLUSIVEBRANCH, OUTPUTCONTINUESBRANCH, ENFORCEOUTPUT, TXTOKENBALANCE, STATEVERIFY, UTXOSTATEROOT, OUTPUTSTATEROOT |
f0–f6, ff |
Invocation, locks, and extensions | ACTIVEBYTECODE, INVOCATIONKIND, BRANCHROOT, CHECKLOCKTIMEVERIFY, CHECKSEQUENCEVERIFY, ACTIVEBRANCHINDEX, ACTIVEWITNESSINDEX, EXT2 |
Introspection, covenants, and contract state
Lotus provides first-class covenant and state primitives directly in the VM without requiring scripts to manually parse transaction bytes:
- Policy and branch continuation:
UTXOREQUIRESBRANCH(0xdd) andOUTPUTREQUIRESBRANCH(0xe8) check in $O(1)$ bitmask operations whether a 32-byte root is mandatory on every spend path of a coin or output.OUTPUTEXCLUSIVEBRANCH(0xe9) verifies that an output has a solo path consisting only ofrootand that every other path also requiresroot.OUTPUTCONTINUESBRANCH(0xea) verifies that an output preserves every other branch and path of the active spent coin while replacing the activeBRANCHROOTwithnew_root.- Native UTXO state (
HAS_STATEflag0x04): STATEHASH(0xc3) computes the canonical domain-separated tuple commitmentH_contractstate(...)over0..64stack items.STATEVERIFY(0xed) verifies in $O(1)$ that the spent coin hasHAS_STATEset and matchesexpected_state.UTXOSTATEROOT(0xee) andOUTPUTSTATEROOT(0xef) read the 32-bytestate_rootof any coin or output (or0x00..00whenHAS_STATEis unset).- Templated output enforcement and deferred accounting (C12a):
ENFORCEOUTPUT(0xeb) matches outputs against a compact binaryOutputTemplate, records value-budget claims, and authorizes state transitions checked transaction-wide after all invocations finish (see Deferred checks).TXTOKENBALANCE(0xec) pushes(total_in, total_out)for native XPI (0x00..00) or any 32-bytetoken_idin $O(1)$.INPUTWITNESSARG(0xdf) lets N-to-1 stateful merges inspect another input's witness argument directly without $O(N^2)$ witness duplication.- Signatures and custom schemes:
CHECKSIG/CHECKSIGVERIFYverify BIP340 Schnorr signatures over P01's O(1) BLAKE3 sighash in mode00(FULL, 64 bytes),01(OWN_INPUT_ALL_OUTPUTS, 65 bytes), or02(OWN_INPUT_ONE_OUTPUT, 65 bytes).SIGHASH(0xc2) exposes the same 32-byte P01 signing digest toCHECKDATASIG(0xbd) for alternative signature profiles.
Static resource bounds and cost units (P05)
Because loops are statically bounded (<= 256 iterations, nesting depth <= 2), the node computes an exact static Cost Unit (cu) upper bound in one pass over the decoded AST before execution. There is no runtime "out of gas" revert that forfeits fees.
- Core limits (candidate profile): script size
<= 8,192bytes,<= 256inputs/outputs,<= 16branches and<= 16paths per output,<= 64Merkle proof siblings, element and stack-byte caps enforced before every push or concatenation. - Cost accounting: Each transaction's linear cost $L(\text{tx})$ sums wire bytes (2 cu/byte) and each invocation's static VM charge (
static_size_cost+ 1 cu/script byte), feeding the parabolic fee-burn curve $C_{\text{tx}} = L + \lfloor L^2 / 2^{18} \rfloor$ defined in P05: Resources and parameters.
Extension points
- Unassigned single-byte opcodes: Every byte not assigned in
tools/opcodes.jsonisOP_SUCCESS_N. Future soft forks can assign new semantics cleanly. - Two-byte extension prefix (
EXT2,0xff): Consumes the following selector byte00–ffto form a 16-bit opcodeff00–ffff; unassigned pairs act asOP_SUCCESS_EXT_N. - Permanent reservations:
NOP(0x60) is permanently a no-op;EMITLOG(0x6c) is permanentlyOP_INVALIDon-chain.