Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

cairo-vm is a Rust implementation of the Cairo VM. Cairo (CPU Algebraic Intermediate Representation) is a programming language for writing provable programs, where one party can prove to another that a certain computation was executed correctly without the need for this party to re-execute the same program.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

lambdaclass/cairo-vm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ Cairo-vm ⚡

A faster and safer implementation of the Cairo VM in Rust

Report Bug ·Request Feature

rustcodecovlicensepr-welcomeTelegram Chat

Table of Contents

📖 About

Cairo VM is the virtual machine for theCairo language.

Previously, there was a version ofCairo VM written in Python, whichwas 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 generatingSTARK proofs for general computation.

It's Turing-complete and it was created byStarkware as part of theStarknet ecosystem.

🌅 Getting Started

Dependencies

Required

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

Optional

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

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

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

Installation script

You can install all of the required and optional dependencies by running the scriptinstall.sh while in the repository root.

Installing project dependencies

In order to compile programs you need to install the cairo-lang package.

Running themake deps (or themake 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

. cairo-vm-env/bin/activate

🚀 Usage

Adding cairo-vm as a dependency

You can add the following to your rust project'sCargo.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:

cd cairo-vm-cli; cargo build --release;cd ..

Once the binary is built, it can be found intarget/release/ under the namecairo-vm-cli.

In order to compile Cairo programs you need to activate the environment created while installing dependencies. To start it, run:

. cairo-vm-env/bin/activate

To compile a program, usecairo-compile [path_to_the_.cairo_file] --output [desired_path_of_the_compiled_.json_file]. For example:

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:

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

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

git clone https://github.com/lambdaclass/cairo-vm.gitcd cairo-vmcargo build --release. cairo-vm-env/bin/activatecairo-compile cairo_programs/abs_value_array.cairo --output cairo_programs/abs_value_array_compiled.jsontarget/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 <TRACE_FILE>: Receives the name of a file and outputs the relocated trace into it

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

    # First, compile a Cairo 0 program and run it to generate a Cairo PIE filecairo-compile cairo_programs/print.cairo --output cairo_programs/print_compiled.jsontarget/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 filetarget/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 :

  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 thePython 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 interfaceHintProcessor.
  • Usecairo-vm-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-vm repository you would first need to prepare a couple of things.

  1. Specify the Cairo program you want to run
let program =Program::from_file(Path::new(&file_path),None);
  1. Instantiate the VM, the cairo_runner, the hint processor, and the entrypoint
letmut cairo_runner =CairoRunner::new(&program,LayoutName::all_cairo,false,false);letmut hint_processor =BuiltinHintProcessor::new_empty();let entrypoint = program.identifiers.get(&format!("__main__.{}",&func_name))?.pc;
  1. Lastly, initialize the builtins and segments.
cairo_runner.initialize_builtins(false)?;cairo_runner.initialize_segments(None);

When using cairo-vm with the Starknet devnet there are additional parameters that are part of the OS context passed on to therun_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(            entrypoint,vec![&MaybeRelocatable::from(2).into(),//this is the entry point selector&MaybeRelocatable::from((2,0)).into()//this would be the output_ptr for example if our cairo function uses it],false,&mut hint_processor,);

Running cairo 1 programs

To run a cairo 1 program enter in the foldercd cairo1-run and follow thecairo1-run documentation

WebAssembly Demo

A demo on how to usecairo-vm with WebAssembly can be found inexamples/wasm-demo

Testing

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

make deps

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

maketest

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:

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 ithere

📊 Benchmarks

Running aCairo program that gets the 1.5 millionth Fibonacci number we got the following benchmarks:

Note before running the benchmark suite: the benchmark namediai_benchmark depends on Valgrind. Please make sure it is installed prior to running theiai_benchmark benchmark.

Run the complete benchmark suite with cargo:

cargo bench

Run only thecriterion_benchmark benchmark suite with cargo:

cargo bench --bench criterion_benchmark

Run only theiai_benchmark benchmark suite with cargo:

cargo bench --bench iai_benchmark

Benchmark thecairo-vm in a hyper-threaded environment with theexamples/hyper_threading/ crate

make hyper-threading-benchmarks

📜 Changelog

Keeps track of the latest changeshere.

🛠 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 aregreatly appreciated.

If you have any suggestions for how to improve the project, please feel free to fork the repo and create a pull request, oropen 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 theCONTRIBUTING.md document.

🌞 Related Projects

  • starknet_in_rust: implementation of Starknet in Rust, powered by the cairo-vm.
  • cairo-vm-py: Bindings for using cairo-vm from Python code.

📚 Documentation

Cairo

Original Cairo VM Internals

We wrote a document explaining how the Cairo VM works. It can be foundhere.

Compilers and Interpreters

This is a list of recommended books to learn how to implement a compiler or an interpreter.

StarkNet

Computational Integrity and Zero Knowledge Proofs

Basics

ZK SNARKs

STARKs

Introduction:

Vitalik Buterin's blog series on zk-STARKs:

Alan Szepieniec's STARK tutorial:

StarkWare's STARK Math blog series:

⚖️ License

This project is dual-licensed under Apache 2.0 and MIT. You may choose either license.

SeeApache 2.0 License orMIT License for more information.

About

cairo-vm is a Rust implementation of the Cairo VM. Cairo (CPU Algebraic Intermediate Representation) is a programming language for writing provable programs, where one party can prove to another that a certain computation was executed correctly without the need for this party to re-execute the same program.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE
MIT
LICENSE-MIT

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp