Coder Social home page Coder Social logo

lambdaclass / cairo-vm Goto Github PK

View Code? Open in Web Editor NEW
466.0 9.0 114.0 189.8 MB

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.

Home Page: https://lambdaclass.github.io/cairo-vm

License: Apache License 2.0

Rust 82.79% Makefile 0.64% Shell 0.41% Cairo 15.01% Python 0.71% HTML 0.06% CSS 0.04% JavaScript 0.34%
blockchain compiler programming-language cryptography rust zkp stark

cairo-vm's Introduction

⚡ Cairo-vm ⚡

A faster and safer implementation of the Cairo VM in Rust

Report Bug · Request Feature

rust codecov license pr-welcome Telegram Chat

Table of Contents

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

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

. cairo-vm-env/bin/activate

🚀 Usage

Adding cairo-vm as a dependency

You can add the following to your rust project's Cargo.toml:

cairo-vm = { version = '0.7.0'}

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

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

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

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

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

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 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.
  • Use cairo-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
let mut vm = VirtualMachine::new(false);

let mut cairo_runner = CairoRunner::new(&program, "all_cairo", 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.
cairo_runner.initialize_builtins(&mut vm)?;
cairo_runner.initialize_segments(&mut vm, None);

When using cairo-vm 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(
            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 vm,
            &mut hint_processor,
        );

Running cairo 1 programs

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

WebAssembly Demo

A demo on how to use cairo-vm with WebAssembly can be found in examples/wasm-demo

Testing

To run the test suite you'll need cargo-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:

make test

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:

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:

cargo bench

Run only the criterion_benchmark benchmark suite with cargo:

cargo bench --bench criterion_benchmark

Run only the iai_benchmark benchmark suite with cargo:

cargo bench --bench iai_benchmark

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

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.
  • 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 found here.

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 licensed under the Apache 2.0 license.

See LICENSE for more information.

cairo-vm's People

Contributors

alonh5 avatar azteca1998 avatar entropidelic avatar fmoletta avatar franciscoidalgo avatar greged93 avatar hermanobst avatar igamigo avatar igaray avatar jpcenteno avatar jrigada avatar juan-m-v avatar juanbono avatar klaus993 avatar marco-paulucci avatar martinacantaro avatar megaredhand avatar mfachal avatar mmsc2 avatar oboulant avatar odesenfans avatar oppen avatar partylich avatar pefontana avatar rcatalan98 avatar santiagopittella avatar tdelabro avatar unbalancedparentheses avatar xjonathanlei avatar zarboq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cairo-vm's Issues

Rustdoc documentation

  • The rustdoc comments should be polished and expanded in general.
  • cleopatra is yet green to publish to crate.io and thus docs.rs but perhaps work could be done on the CI to add the rendered docs to the repo's github pages.

Add a CLI option to specify the entrypoint

Currently the entrypoint to the cairo file being run is hardcoded to the main function.
Since we'd like at Onlydust to leverage your VM to build a fast Cairo test runner, we would need to be able to specify a different entrypoint, let's say my_function for example.

I plan to work on this, I open the issue to start the conversation and get your point of view on this.

Remove duplicated Memory in VirtualMachine

Currently, VirtualMachine contains a context (RunContext) with a Memory, and it (VirtualMachine) also contains a ValidatedMemoryDict which also contains a Memory. Both of this Memories contain the same information, in order to avoid double reference/ duplication, we could keep only one Memory in VirtualMachine (removing memory from the context and getting rid of ValidatedMemoryDict altogether), and move the validated_addresses to VirtualMachine.

Add --memory_file

We should be able to output the relocated memory to a file like cairo_run does.

Discuss and analyze webassembly security

Research using kani

Remove all program.json files

Remove all the Cairo compiled programs (program.json files) from the Repo.

Programs should be compiled using the python cairo compiler.

Document hints

  • Expected output: a good walkthrough on how existing hints work in cairo and oraic

Add fuzzing

  • Research which fuzzy testing Rust tool to use.
  • Find possible areas in cleopatra in which to apply fuzzing testing.
  • Expected output: a decision on which fuzzy testing library to use and which tests to add.

Refactor if clause in verify_auto_deductions function

   pub fn verify_auto_deductions(&mut self) -> Result<(), VirtualMachineError> {
        for (i, segment) in self.memory.data.iter().enumerate() {
            for (j, value) in segment.iter().enumerate() {
                for (name, builtin) in self.builtin_runners.iter_mut() {
                    match builtin.base() {
                        Some(builtin_base) => {
                            if builtin_base.segment_index == i {
                                match builtin.deduce_memory_cell(
                                    &MaybeRelocatable::from((i, j)),
                                    &self.memory,
                                ) {
                                    Ok(None) => None,
                                    Ok(Some(deduced_memory_cell)) => {
                                        if Some(&deduced_memory_cell) != value.as_ref()
                                            && value != &None
                                        {
                                            return Err(
                                                VirtualMachineError::InconsistentAutoDeduction(
                                                    name.to_owned(),
                                                    deduced_memory_cell,
                                                    value.to_owned(),
                                                ),
                                            );
                                        }
                                        Some(deduced_memory_cell)
                                    }
                                    _ => {
                                        return Err(VirtualMachineError::InvalidInstructionEncoding)
                                    }
                                };
                            }
                        }
                        _ => return Err(VirtualMachineError::InvalidInstructionEncoding),
                    }
                }
            }
        }
        Ok(())
    }

Improve is_zero

Perhaps the is_zero function in vm_core might be improved by using the is_zero method from the num_bigint library instead of constructing a zero value and comparing against it.

Research cross-platform tests

Eventually, we will want to have tests that ensure parity across Cairo VM implementations.
How exactly to achieve that is an open question, but it will likely involve a Python testing framework.

  • Research and document
    • how tests are run in the cairo-lang repositor,
    • what kind of tests they are,
    • what they test,
    • what dependencies they have
    • and if possible, how they could be made vm-agnostic.

Expected outcome: a through discussion on how to test results of different vm implementations.

cleopatra vm fail to run my cairo program

Here is my code:

%builtins output pedersen

from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.hash import hash2

func main{output_ptr, pedersen_ptr: HashBuiltin*}():
    let (res) = hash2{hash_ptr=pedersen_ptr}(1, 2)

    res = [output_ptr]

    let output_ptr = output_ptr + 1
    return()
end

cairo-compile && cairo-run works

cairo-compile check_hash.cairo --output check_hash_compiled.json
cairo-run --program=check_hash_compiled.json --layout=small --print_output 
Program output:
  -1025514936890165471153863463586721648332140962090141185746964417035414175707

But cleaopatra doesn't

cargo run -- ~/Documents/rusty-crew/cairo_test/check_hash_compiled.json  
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/cleopatra-run /Users/tdelabro/Documents/rusty-crew/cairo_test/check_hash_compiled.json`
thread 'main' panicked at 'Couldn't compute or deduce op1', src/vm/vm_core.rs:441:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Why is that ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.