An implementation of the Cairo VM in TypeScript, focusing on education
Built with π₯ by KKRT Labs
β οΈ This project is in its genesis, undergoing fast-paced development. It is not suitable for production yet. Expect frequent breaking changes.
Cairo stands for CPU AIR "o" (like insp"o", conv"o", π€).
It is a framework (a machine, an assembly and a language) which allows writing provable programs without having to understand the underneath ZK-technology.
A program written in Cairo (or Cairo Zero) is compiled to Cairo Assembly (CASM) and then executed on a Cairo VM which produces traces to be used for the STARK proof generation.
See resources for more information.
There are currently seven other Cairo VM implementations:
- Reference (original) implementation in Python by Starkware (prod)
- New implementation in Rust by Lambda Class (prod)
- A Go implementation, by Lambda Class (dev)
- Another Go implementation by Nethermind (dev)
- A Zig implementation, by Community (dev)
- A C++ implementation by Lambda Class (dev)
- A GoogleSheets (gs AppScript) implementation by ClΓ©ment Walter (dev)
The Lambda Class alt-implementations comes with a detailed guide (Go, C++) on how they built their Cairo VM. It gives insights into the overall Cairo VM but is incomplete and rather specific to language details.
Why would you have different implementations of the same program in multiple languages? For implementation diversity.
More implementations provide more:
- Resilience. It helps in finding bugs in the existing Β implementations.
- Documentation. The documentation over the Cairo VM is Β still scarce, and the latest version in prod (Rust) is not easy to read for Β the average dev.
- Architecture diversity. Different architectures can be Β implemented to achieve the same goal (e.g. memory model). However, most of the current implementations essentially are a rewrite of the Rust implementation, which is an (enhanced) rewrite of the Python implementation itself.
Implementation diversity also implies usage diversity. The primary goals of each implementation can differ.
For example, the EVM implementation in clients (e.g. geth and reth written in Go and Rust), whose primary goals are performance and safety, and the reference EVM implementation in Python, prioritizing readability and simplicity.
Analogous to the EVM implementations, the primary goals of the Rust Cairo VM are performance and safety. While the ones of our TypeScript implementation is education through readability and simplicity.
- TypeScript is easily readable and known by most devs if not all
- Deliberate design choices to further improve readability and simplicity
- Extensive documentation: JSDoc, diagrams, explainers, etc.
bun install # install all dependencies
bun test # run all tests
You can install the CLI cairo-vm-ts
by doing the following:
- Clone this repo:
git clone git@github.com:kkrt-labs/cairo-vm-ts.git
- Go to the cloned directory:
cd cairo-vm-ts
- Install the dependencies:
bun install
- Register the package as a linkable package:
bun link
Steps 3. and 4. can be replaced by make build
Example usage:
cairo run fibonacci.json --export-memory fib_mem.bin --print-memory --print-output
No package release has been done yet.
You can still add it as a dependency with a local copy:
- Clone this repo:
git clone git@github.com:kkrt-labs/cairo-vm-ts.git
- Go to the cloned directory:
cd cairo-vm-ts
- Install the dependencies:
bun install
- Build the project:
bun run build
- Go to your project
cd ~/my-project
- Add
cairo-vm-ts
to your project dependency:<bun | yarn | npm> add ~/path/to/cairo-vm-ts
Goals | Done? |
---|---|
Run basic Cairo Zero program | β |
Run basic Cairo program | β |
Add builtins | β |
Add hints | β |
Run StarkNet contracts | β |
Benchmark against other VMs | β |
Builtin | Done? |
---|---|
Output | β |
Pedersen | β |
Range Check | β |
ECDSA | β |
Bitwise | β |
EcOp | β |
Keccak | β |
Poseidon | β |
Range Check 96 | β |
Segment Arena | β |
AddMod | β |
MulMod | β |
Hints are currently being implemented.
Their development can be tracked here.
A how-to guide has been written here.
Pre-requisite: make
Compare the encoded memory and trace of execution between different Cairo VM implementations on a broad range of Cairo programs.
It is currently only done in execution mode (non-proof mode) on programs with no hints. It uses the CLI of each VM implementation.
Cairo VM Implementations | Added to diff-test |
---|---|
Cairo VM TS | β |
Cairo VM Rust | β |
Cairo VM Python | β |
Cairo VM Zig | β |
Cairo VM Go - ProofMode only | β |
To build the different projects CLI, you'll need the following dependencies:
To run the differential tests, simply use make diff-test
.
If you wanna compare an arbitrary amount of memory or trace files, you can
directly use the compare
command
compare memory fib.memory fib2.memory fib3.memory
compare trace fib.trace fib2.trace fib3.trace
For a quick benchmarking tool, one can run make bench
It uses hyperfine
to benchmark the CLI command of the different Cairo VM
implementations. These benchmarks might not be accurate, it should be done in a
proper environment.
The benchmark programs used currently come from the
cairo-vm/cairo_programs/benchmarks
. The workload of each program has been
reduced for slower implementations to finish () Only Cairo programs with no
hints are used at the moment:
Cairo Program | Description | Value |
---|---|---|
big_factorial.cairo |
Computes |
|
big_fibonacci.cairo |
Computes |
|
integration_builtins.cairo |
Computes |
|
pedersen.cairo |
Computes |
|
A benchmark using each VM implementation API is developped at
cairo-vm-bench
- All commits must be signed (PGP or SSH key, we recommend PGP keys)
- Issue assignment have a lifespan (e.g. 1d, 2d, 1w...), if no PR has been opened between assignment and expiry date, you'll be unassigned. Issues lifespan is introduced to avoid staling issues as the project moves quickly.