⚡ Cairo-rs ⚡
A faster and safer implementation of the Cairo VM in Rust
Table of Contents
- Disclaimer
- About
- Getting Started
- Usage
- Benchmarks
- Changelog
- Contributing
- Related Projects
- Documentation
- License
⚠️ Disclaimer
🚧 cairo-rs
is still being built therefore breaking changes might happen often so use it at your own risk. 🚧
Cargo doesn't comply with semver, so we advise to pin the version to 0.1.0. This can be done adding cairo-vm = "0.1.0"
to your Cargo.toml
📖 About
Cairo VM is the virtual machine for the Cairo language.
There's an older version of Cairo VM written in Python, which is currently in production.
This repository contains the newer version, written in Rust. It's faster and has safer and more expressive typing. Once completed, it will replace the older one as the sole Cairo VM.
The Cairo language
Cairo is the first production-grade platform for generating STARK proofs for general computation.
It's Turing-complete and it was created by Starkware as part of the Starknet ecosystem.
🌅 Getting Started
Dependencies
Required
- Rust 1.66.1
- Cargo
Optional
These dependencies are only necessary in order to run the original VM and compile Cairo programs.
- PyEnv with Python 3.9
- cairo-lang
🚀 Usage
Running cairo-rs
To compile the repository, run:
Once the binary is built, it can be found in target/release/
under the name cairo-rs-run
.
To compile a program, use cairo-compile [path_to_the_.cairo_file] --output [desired_path_of_the_compiled_.json_file]
. For example:
To run a compiled .json program through the VM, call the executable giving it the path and name of the file to be executed. For example:
The flag --layout
determines which builtins can be used. More info about layouts here.
To sum up, the following code will get you from zero to running a Cairo program:
Using hints
Currently, as this VM is under construction, it's missing some of the features of the original VM. Notably, this VM only implements a limited number of Python hints at the moment, while the Python Cairo VM allows users to run any Python code.
There are two ways to use non-standard hints in this VM:
- Extend the cairo-rs code and build your own binary using the interface hint processor
- Use cairo-rs-py which supports running any hint in a Python interpreter.
Running a function in a Cairo program with arguments
When running a Cairo program directly using the Cairo-rs repository you would first need to prepare a couple of things.
- Specify the Cairo program you want to run
let program =
from_file;
- Instantiate the VM, the cairo_runner, the hint processor, and the entrypoint
let mut vm = new;
let mut cairo_runner = new;
let mut hint_processor = new_empty;
let entrypoint = program
.identifiers
.get?
.pc;
- Lastly, initialize the builtins and segments.
cairo_runner.initialize_builtins?;
cairo_runner.initialize_segments;
When using cairo-rs with the Starknet devnet there are additional parameters that are part of the OS context passed on to the run_from_entrypoint
method that we do not have here when using it directly. These parameters are, for example, initial stacks of the builtins, which are the base of each of them and are needed as they are the implicit arguments of the function.
let _var = cairo_runner.run_from_entrypoint;
WebAssembly Demo
A demo on how to use cairo-rs
with WebAssembly can be found
here.
Testing
To run the test suite you'll need cargo-llvm-cov
dependency so make sure to run this command beforehand:
Now that you have the dependencies necessary to run the test suite you can run:
📊 Benchmarks
Running a Cairo program that gets the 1000th Fibonacci number we got the following benchmarks:
- Execution time with Criterion
- Flamegraph
- Github action results
Note before running the benchmark suite: the benchmark named iai_benchmark depends on Valgrind. Please make sure it is installed prior to running the iai_benchmark
benchmark.
Run the complete benchmark suite with cargo:
Run only the criterion_benchmark
benchmark suite with cargo:
Run only the iai_benchmark
benchmark suite with cargo:
📜 Changelog
Keeps track of the latest changes here.
🛠 Contributing
The open-source community is a fantastic place for learning, inspiration, and creation, and this is all thanks to contributions from people like you. Your contributions are greatly appreciated.
If you have any suggestions for how to improve the project, please feel free to fork the repo and create a pull request, or open an issue with the tag 'enhancement'.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
And don't forget to give the project a star! ⭐ Thank you again for your support.
🌞 Related Projects
- starknet_in_rust: implementation of Starknet in Rust, powered by the cairo-rs VM.
- cairo-rs-py: Bindings for using cairo-rs from Python code.
📚 Documentation
Cairo
- From Cairo Documentation: How Cairo Works
- Cairo – a Turing-complete STARK-friendly CPU architecture
- A Verified Algebraic Representation of Cairo Program Execution
- Cairo Verifier in Rust
Original Cairo VM Internals
We wrote a document explaining how the Cairo VM works. It can be found here.
Compilers and Interpreters
This is a list of recommended books to learn how to implement a compiler or an interpreter.
- How I wrote my own "proper" programming language - Mukul Rathi
- Introduction to Compilers and Language Design - Douglas Thain
- Beautiful Racket - Matthew Flatt
- Crafting interpreters - Robert Nystrom
- Engineering a Compiler - Keith D. Cooper, Linda Torczon
StarkNet
Computational Integrity and Zero Knowledge Proofs
Basics
- Intro to zero-knowledge proofs
- Security and Privacy for Crypto with Zero-Knowledge Proofs
- A Hands-On Tutorial for Zero-Knowledge Proofs Series
ZK SNARKs
- What are zk-SNARKs?
- Vitalik's introduction to how zk-SNARKs are possible
- Vitalik's post on quadratic arithmetic programs
- Why and How zk-SNARK Works - Maksym Petkus
- Comparing General Purpose zk-SNARKs
- Dark forest's intro + circuits PART 1
- Dark forest's intro + circuits PART 2
STARKs
Introduction:
- Cryptography Stack Exchange Answer
- Hasu gets STARK-pilled - with Eli Ben-Sasson
- Cairo for Blockchain Developers
- Why STARKs are the key to unlocking blockchain scalability
- STARKs whitepaper: Scalable, transparent, and post-quantum secure computational integrity
- STARKs vs. SNARKs: A Cambrian Explosion of Crypto Proofs
Vitalik Buterin's blog series on zk-STARKs:
- STARKs, part 1: Proofs with Polynomials
- STARKs, part 2: Thank Goodness it's FRI-day
- STARKs, part 3: Into the Weeds
Alan Szepieniec's STARK tutorial:
StarkWare's STARK Math blog series:
- STARK Math: The Journey Begins
- Arithmetization I
- Arithmetization II
- Low Degree Testing
- A Framework for Efficient STARKs
⚖️ License
This project is licensed under the Apache 2.0 license.
See LICENSE for more information.