### โก Cairo-vm โก
A faster and safer implementation of the Cairo VM in Rust
[Report Bug](https://github.com/lambdaclass/cairo-vm/issues/new?assignees=&labels=bug&projects=&template=bug_report.md&title=bug%3A+) ยท [Request Feature](https://github.com/lambdaclass/cairo-vm/issues/new?labels=enhancement&title=feat%3A+)
[![rust](https://github.com/lambdaclass/cairo-vm/actions/workflows/rust.yml/badge.svg)](https://github.com/lambdaclass/cairo-vm/actions/workflows/rust.yml)
[![codecov](https://img.shields.io/codecov/c/github/lambdaclass/cairo-vm)](https://codecov.io/gh/lambdaclass/cairo-vm)
[![license](https://img.shields.io/github/license/lambdaclass/cairo-vm)](/LICENSE)
[![pr-welcome]](#-contributing)
[![Telegram Chat][tg-badge]][tg-url]
[pr-welcome]: https://img.shields.io/static/v1?color=orange&label=PRs&style=flat&message=welcome
[tg-badge]: https://img.shields.io/endpoint?url=https%3A%2F%2Ftg.sumanjay.workers.dev%2FLambdaStarkNet%2F&logo=telegram&label=chat&color=neon
[tg-url]: https://t.me/LambdaStarkNet
## Table of Contents
- [Table of Contents](#table-of-contents)
- [๐ About](#-about)
- [The Cairo language](#the-cairo-language)
- [๐
Getting Started](#-getting-started)
- [Dependencies](#dependencies)
- [Required](#required)
- [Optional](#optional)
- [Installation script](#installation-script)
- [๐ Usage](#-usage)
- [Adding cairo-vm as a dependency](#adding-cairo-vm-as-a-dependency)
- [Running cairo-vm from CLI](#running-cairo-vm-from-cli)
- [Using hints](#using-hints)
- [Running a function in a Cairo program with arguments](#running-a-function-in-a-cairo-program-with-arguments)
- [WebAssembly Demo](#webassembly-demo)
- [Testing](#testing)
- [Tracer](#tracer)
- [๐ Benchmarks](#-benchmarks)
- [๐ Changelog](#-changelog)
- [๐ Contributing](#-contributing)
- [๐ Related Projects](#-related-projects)
- [๐ Documentation](#-documentation)
- [Cairo](#cairo)
- [Original Cairo VM Internals](#original-cairo-vm-internals)
- [Compilers and Interpreters](#compilers-and-interpreters)
- [StarkNet](#starknet)
- [Computational Integrity and Zero Knowledge Proofs](#computational-integrity-and-zero-knowledge-proofs)
- [Basics](#basics)
- [ZK SNARKs](#zk-snarks)
- [STARKs](#starks)
- [โ๏ธ License](#๏ธ-license)
## ๐ About
Cairo VM is the virtual machine for the [Cairo language](https://www.cairo-lang.org/).
Previously, there was a version of [Cairo VM](https://github.com/starkware-libs/cairo-lang) written in Python, which **was used in production**.
This repository contains the newer version, written in Rust. It's faster and has safer and more expressive typing. Now in production, it has replaced the older Python version to become the primary Cairo VM.
### The Cairo language
Cairo is the first production-grade platform for generating [STARK](https://vitalik.ca/general/2017/11/09/starks_part_1.html) proofs for general computation.
It's Turing-complete and it was created by [Starkware](https://starkware.co/) as part of the [Starknet](https://starkware.co/starknet/) ecosystem.
## ๐
Getting Started
### Dependencies
#### Required
These are needed in order to compile and use the project.
- [Rust 1.81.0 or newer](https://www.rust-lang.org/tools/install)
- Cargo
#### Optional
These dependencies are only necessary in order to run the original VM, compile Cairo programs, and run tests.
- make
- PyEnv
#### Installation script
You can install all of the required and optional dependencies by running the script `install.sh` while in the repository root.
### Installing project dependencies
In order to compile programs you need to install the cairo-lang package.
Running the `make deps` (or the `make deps-macos` if you are runnning in MacOS) command will create a virtual environment with all the required dependencies.
You can then activate this environment by running
```bash
. cairo-vm-env/bin/activate
```
## ๐ Usage
### Adding cairo-vm as a dependency
You can add the following to your rust project's `Cargo.toml`:
```toml
cairo-vm = { version = '1.0.1'}
```
### Running cairo-vm from CLI
To run programs from the command line, first compile the repository from the cairo-vm-cli folder:
```bash
cd cairo-vm-cli; cargo build --release; cd ..
```
Once the binary is built, it can be found in `target/release/` under the name `cairo-vm-cli`.
In order to compile Cairo programs you need to activate the environment created while installing dependencies. To start it, run:
```bash
. cairo-vm-env/bin/activate
```
To compile a program, use `cairo-compile [path_to_the_.cairo_file] --output [desired_path_of_the_compiled_.json_file]`. For example:
```bash
cairo-compile cairo_programs/abs_value_array.cairo --output cairo_programs/abs_value_array_compiled.json
```
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:
```bash
target/release/cairo-vm-cli cairo_programs/abs_value_array_compiled.json --layout all_cairo
```
The flag `--layout` determines which builtins can be used. More info about layouts [here](https://docs.cairo-lang.org/how_cairo_works/builtins.html#layouts).
To sum up, the following code will get you from zero to running a Cairo program:
```bash
git clone https://github.com/lambdaclass/cairo-vm.git
cd cairo-vm
cargo build --release
. cairo-vm-env/bin/activate
cairo-compile cairo_programs/abs_value_array.cairo --output cairo_programs/abs_value_array_compiled.json
target/release/cairo-vm-cli cairo_programs/abs_value_array_compiled.json --layout all_cairo
```
#### Other CLI arguments
The cairo-vm-cli supports the following optional arguments:
- `--trace_file