larzvm
A deterministic, gas-metered stack virtual machine in pure Python. Zero dependencies.
A sandboxed bytecode VM you can drop into anything that needs to run untrusted, resource-bounded logic: a smart-contract engine for a blockchain (it's the companion VM for larzchain), a rules/scripting layer, a safe expression evaluator, or a hands-on way to learn how virtual machines work.
from larzvm import execute
r = execute("""
PUSH 2
PUSH 3
ADD
RETURN
""", gas_limit=1000)
r.returned # 5
r.gas_used # gas spent
Why it's safe to run untrusted code
- Deterministic. No floats, no randomness, no wall clock — the current time is passed in through the execution context, so it's identical for everyone. The same program and inputs always produce the same result and the same gas cost on every machine. That's exactly the property a blockchain needs to reach consensus on contract execution.
- Bounded. Every opcode costs gas from a fixed table; when the budget runs
out, execution stops with
OutOfGas. There are no infinite loops — a loop just burns gas until the limit halts it. - Isolated. A program can only touch its own stack, scratch memory, and a
storagedict you hand it. No file system, no network, no host calls.
Install
pip install larzvm
The machine
A stack of arbitrary-precision integers, plus scratch memory and persistent storage. Instructions:
| group | opcodes |
|---|---|
| stack | PUSH n POP DUP d SWAP OVER |
| arithmetic | ADD SUB MUL DIV MOD NEG ABS |
| comparison | EQ NE LT GT LE GE → push 1/0 |
| logic / bitwise | AND OR NOT BAND BOR BXOR |
| control flow | JMP label JMPIF label JMPZ label |
| memory / storage | MLOAD MSTORE SLOAD SSTORE |
| crypto / env | HASH (SHA-256) CALLER VALUE TIMESTAMP HEIGHT |
| output / halt | EMIT RETURN STOP ASSERT REVERT |
Assembly with labels
You write readable text; the assembler resolves labels to jump targets so you
never count offsets. Comments start with ; or #.
from larzvm import execute
# store a value, guard it, and return it
program = """
PUSH 1 ; slot
PUSH 500 ; amount
SSTORE ; storage[1] = 500
PUSH 1
SLOAD ; read it back
DUP 0
PUSH 0
GT
ASSERT ; require amount > 0, else revert
RETURN
"""
r = execute(program)
r.returned # 500
r.storage[1] # 500 (commit this if r.ok, discard if r.reverted)
Contracts on a host
A host (like larzchain) runs a program with a gas budget, an execution context, and the contract's current storage, then commits the results only if it succeeded:
from larzvm import VM, ExecutionContext
ctx = ExecutionContext(caller=0xABCD, value=10, timestamp=1690000000, height=42)
result = VM(gas_limit=50_000, context=ctx).run(program, storage=contract_state)
if result.ok:
commit(result.storage) # persist state changes
for event in result.logs: # EMITted values
record(event)
else:
discard() # reverted or out of gas -> no state change
Revert and faults (division by zero, failed ASSERT) are reported on the result
(result.reverted, result.error) so the host can roll back cleanly;
OutOfGas and genuinely malformed programs raise.
Scope
larzvm is intentionally a small integer stack machine — the core needed for deterministic, metered execution. It is not (yet) a full EVM with 256-bit words, contract-to-contract calls, or a gas market. It's the solid, auditable core you'd build those on, and a genuinely useful sandbox on its own.
Tests
python -m unittest discover -s tests -v # 41 tests, zero deps
The Larz stack
Pure-Python, zero-dependency building blocks:
- larz — money-native web framework
- larzchain — from-scratch PoW blockchain
- larzmoney — exact, penny-perfect money
- larzcrypt — pure-Python cryptography toolkit
- larzdb — crash-safe embedded database
- larzagent — zero-dep AI agent framework
- larzchart — data to inline SVG charts
- larzmark — Markdown + SEO static sites
- larztask — durable background job queue
- larzvault — encrypted secrets manager
- larzvm — this VM
License
MIT © larz-scripter
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file larzvm-0.1.0.tar.gz.
File metadata
- Download URL: larzvm-0.1.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c262e43a3fae773d2ca977c60313a347be144c6d4ce9198e8de21ccf238c6287
|
|
| MD5 |
c35eb7074bd0751db609eaec04f27e8d
|
|
| BLAKE2b-256 |
ffcf27b3f13bdbef9f566e8a474ba6a740557ad503bb7ba3f7da08f2d6495a9f
|
File details
Details for the file larzvm-0.1.0-py3-none-any.whl.
File metadata
- Download URL: larzvm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
095f87efb7372a9c1c9b3c12ef8ea56b9d33db9030d491369b9b31b5eee6de77
|
|
| MD5 |
7a86aa71a7a9e47de5838161db1e92a9
|
|
| BLAKE2b-256 |
573458bb25d2181f674e63c24a02ed6c35c48cf0678689ae906f72a7bcdfc991
|