百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

cairo-vm

> 编程语言
开源

cairo-vm是开罗甚高频的Rust执行. 开罗(CPU代数中级代表)是用于编写可证明程序的一种编程语言,w

587 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

cairo-vm是开罗甚高频的Rust执行. 开罗(CPU代数中级代表)是用于编写可证明程序的一种编程语言,w

Table of Contents

  • Table of Contents
  • About
    • The Cairo language
  • Getting Started
    • Dependencies
      • Required
      • Optional
      • Installation script
  • Usage
    • Adding cairo-vm as a dependency
    • Running cairo-vm from CLI
    • Using hints
    • Running a function in a Cairo program with arguments
    • Testing
    • Tracer
  • Benchmarks
  • Changelog
  • Contributing
  • Related Projects
  • Documentation
    • Cairo
    • Original Cairo VM Internals
    • Compilers and Interpreters
    • StarkNet
    • Computational Integrity and Zero Knowledge Proofs
      • Basics
      • ZK SNARKs
      • STARKs
  • ⚖️ License

About

Cairo VM is the virtual machine for the Cairo language.

Previously, there was a version of Cairo VM 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 proofs for general computation.

It's Turing-complete and it was created by Starkware as part of the Starknet ecosystem.

Getting Started

Dependencies

Required

These are needed in order to compile and use the project.

  • Rust 1.85.0 or newer
  • Cargo

Optional

These dependencies are only necessary in order to run the original VM, compile Cairo programs, and run tests.

  • make
  • uv

You can install uv, a modern python package and env manager made in Rust with the following command:

bash
curl -LsSf https://astral.sh/uv/install.sh | sh

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 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.

To sum up, the following code will get you from zero to running a Cairo program:

bash
git clone https://github.com/starkware-libs/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 : Receives the name of a file and outputs the relocated trace into it

  • --memory_file : Receives the name of a file and outputs the relocated memory into it

  • --print_output : Prints the program output

  • --proof_mode: Runs the program in proof_mode

  • --secure_run: Runs security checks after execution. Enabled by default when not in proof_mode.

  • --air_public_input : Receives the name of a file and outputs the AIR public inputs into it. Can only be used if proof_mode is also enabled.

  • --air_private_input : Receives the name of a file and outputs the AIR private inputs into it. Can only be used if proof_mode, trace_file & memory_file are also enabled.

  • --cairo_pie_output : Receives the name of a file and outputs the Cairo PIE into it. Can only be used if proof_mode is not enabled.

  • --allow_missing_builtins: Disables the check that all builtins used by the program need to be included in the selected layout. Enabled by default when in proof_mode.

  • --run_from_cairo_pie: Runs a Cairo PIE instead of a compiled json file. The name of the file will be the first argument received by the CLI (as if it were to run a normal compiled program). Can only be used if proof_mode is not enabled.

    bash
    # First, compile a Cairo 0 program and run it to generate a Cairo PIE file
    cairo-compile cairo_programs/print.cairo --output cairo_programs/print_compiled.json
    target/release/cairo-vm-cli cairo_programs/print_compiled.json --layout all_cairo --cairo_pie_output print.pie
    
    # Then, run the program from the Cairo PIE file
    target/release/cairo-vm-cli print.pie --run_from_cairo_pie --layout all_cairo --print_output

    Note: When using the --run_from_cairo_pie flag, the layout specified must be the same as the one used to create the PIE file, otherwise you may encounter compatibility issues.

  • cairo_layout_params_file: Only used with dynamic layout. Receives the name of a json file with the dynamic layout parameters.

For example, to obtain the air public inputs from a fibonacci program run, we can run :

bash
  target/release/cairo-vm-cli cairo_programs/proof_programs/fibonacci.json --layout all_cairo --proof_mode --air_public_input fibonacci_public_input.json

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-vm code and build your own binary using the interface HintProcessor.

Running a function in a Cairo program with arguments

When running a Cairo program directly using the Cairo-vm repository you would first need to prepare a couple of things.

  1. Specify the Cairo program you want to run
rust
let program =
        Program::from_file(Path::new(&file_path), None);
  1. Instantiate the VM, the cairo_runner, the hint processor, and the entrypoint
rust
let mut cairo_runner =
    CairoRunner::new(&program, CairoLayout::new(LayoutName::all_cairo, None)?, false, false);

let mut hint_processor = BuiltinHintProcessor::new_empty();

let entrypoint = program
        .identifiers
        .get(&format!("__main__.{}", &func_name))?
        .pc;
  1. Lastly, initialize the builtins and segments.
rust
cairo_runner.initialize_builtins(false)?;
cairo_runner.initialize_segments(None);

Running cairo 1 programs

To run a cairo 1 program enter in the folder cd cairo1-run and follow the cairo1-run documentation

Testing

To run the test suite you'll need cargo-llvm-cov dependency so make sure to run this command beforehand:

bash
make deps

Now that you have the dependencies necessary to run the test suite you can run:

bash
make test

Using a Dynamic Layout

A dynamic layout must be specified with a dynamic params file. You can find an example in: vm/src/tests/cairo_layout_params_file.json.

To run cairo 0 or 1 programs with a dynamic layout, you must use --layout dynamic and the --cairo_layout_params_file flag pointing a dynamic params file. For example, run:

bash
cargo run --bin cairo-vm-cli cairo_programs/fibonacci.json --layout dynamic --cairo_layout_params_file vm/src/tests/cairo_layout_params_file.json

Tracer

Cairo-vm offers a tracer which gives you a visualization of how your memory and registers change line after line as the VM executes the code. You can read more about it here

Benchmarks

Running a Cairo program that gets the 1.5 millionth 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:

bash
cargo bench

Run only the criterion_benchmark benchmark suite with cargo:

bash
cargo bench --bench criterion_benchmark

Run only the iai_benchmark benchmark suite with cargo:

bash
cargo bench --bench iai_benchmark

Benchmark the cairo-vm in a hyper-threaded environment with the examples/hyper_threading/ crate

bash
make hyper-threading-benchmarks

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'.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feat/AmazingFeature)
  3. Commit your Changes (git commit -m 'feat: add some AmazingFeature')
  4. Push to the Branch (git push origin feat/AmazingFeature)
  5. Open a Pull Request

And don't forget to give the project a star! ⭐ Thank you again for your support.

You can find more detailed instructions in the CONTRIBUTING.md document.

Related Projects

  • starknet_in_rust: implementation of Starknet in Rust, powered by the cairo-vm.

Documentation

Cairo

  • From Cairo Documentation: How Cairo Works
  • [Cairo – a Turing-complete STARK-friendly CPU architecture](https://eprint.ia

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustblockchaincompilercryptographyprogramming-language

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言