Giter VIP home page Giter VIP logo

cairo-vm-py's Introduction

drawing

🐍 Cairo-vm-py 🐍

FFI Python bindings for cairo-vm

Report Bug Β· Request Feature

rust codecov license Telegram Chat

Table of Contents

πŸ“– About

cairo-vm-py adds Python bindings to the Cairo VM.

πŸŒ… Getting Started

Dependencies

  • Rust and Cargo
  • Pyenv and Python 3.9
  • GMP
  • make

Installation

To set up the Python environment, and install necessary Python libraries, run make deps. This command builds two virtual environments, one intended for the Rust VM and the other one for the Original Python VM. It also initializes the submodules of some of the projects we integrated with.

After setting up the environments, you can install the python binary using maturin develop --release.

Finally, install into the Python environment with maturin develop --release. For mac installation, these flags might be necessary: maturin develop --release -m cairo-vm-py/Cargo.toml --no-default-features --features extension.

πŸš€ Usage

After installation, you can access the Cairo VM from Python code. As an example, after compiling the program array_sum into cairo_programs/array_sum.json, you can run it with the VM using:

import cairo_vm_py

with open(f"cairo_programs/array_sum.json") as file:
    runner = cairo_vm_py.CairoRunner(file.read(), "main", "all", False)
    runner.cairo_run(True)

Testing

To run the test suite:

make full-test

Demo

The build_envs.sh script will build two Python virtual environments:

  • cairo-lang containing a pristine installation of cairo-lang==0.10.3;
  • cairo-vm-py containing a patched installation of cairo-lang==0.10.3 that uses cairo-vm-py as dependency. It will also install the required dependencies automatically in Debian-based distributions, CentOs, Fedora and OSX. If you use another OS, you can check how to install them manually below.

To run the script:

./scripts/build_envs.sh

Both virtual environment will be created under the /scripts directory.

To actually use any of the implementations, you would have to activate the environment you want. For example, to use the cairo-vm-py integration you need to run:

source scripts/cairo-vm-py/bin/activate

After activating the cairo-vm-py virtualenv you can try out any Cairo project and it will use cairo-vm. In some cases some projects are coupled to cairo-run or need some extra patching to be able to use the cairo-vm runner (e.g. Protostar, Zerosync).

Note that the script assumes you have a Rust toolchain, Python 3.9 and the venv program installed.

How to manually install the script dependencies

cairo-lang requires the gmp library to build. You can install it on Debian-based GNU/Linux distributions with:

sudo apt install -y libgmp3-dev

In Mac you can use Homebrew:

brew install gmp

In Mac you'll also need to tell the script where to find the gmp lib:

export CFLAGS=-I/opt/homebrew/opt/gmp/include LDFLAGS=-L/opt/homebrew/opt/gmp/lib
sh build_envs.sh

πŸ“Š Benchmarking

To run the benchmarks of the projects we integrated with, first you need to set up the dependencies:

make benchmark-deps

Lastly, run make + the project you desire to try:

benchmark-devnet

🌞 Related Projects

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

βš–οΈ License

This project is licensed under the Apache 2.0 license.

See LICENSE for more information.

cairo-vm-py's People

Contributors

azteca1998 avatar entropidelic avatar fmoletta avatar hermanobst avatar jrigada avatar juan-m-v avatar juanbono avatar marco-paulucci avatar martinacantaro avatar megaredhand avatar mmsc2 avatar oppen avatar pefontana avatar santiagopittella avatar unbalancedparentheses 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

Watchers

 avatar  avatar  avatar  avatar

cairo-vm-py's Issues

Add a workflow that checks the rev of cairo-rs used points to main

This should be a separate workflow running on PRs that blocks merge.
It's needed because once a PR in cairo-rs is merged the branch is deleted and the ref may become stale and break main.
It needs to be separate so temporarily pointing to a dev branch is allowed and doesn't give false negatives when it comes to just tests.

Implement extended write arg function

Cairo-rs possesses a segment manager that is tasked with generating new memory segments as needed.

This manager has a method called write_arg, which can take a pointer and a vector of bigints to write said vector into memory, at the position indicated by the pointer.

For the cairo-rs-py crate, we want to overwrite this method to be able to take any iterable argument. Each element should also be able to be an iterable or a bigint as well.

The memory should have bigints and pointers to segments whenever an iterable element was encountered. The segments are assigned as needed each time we find an iterable and the parent of that iterable will have a pointer to that new segment.

As an example, if write_arg is called with a mixed list args at the position (0, 0), the segments should look like this:

As we start stepping through each element of the input array, we check if that element is an iterable or not. If it is, we'll generate a new segment, we point to its base from the segment we started form and dump all the values into the new segment.

A few references from the cairo python VM and the rust VM:

Add requirements.txt for pip

While for the most part we should have no Python deps, at the very least we use them for testing. The proper place to keep them and to specify the versions used is in such file.

Fix all exec scopes

In cairo-rs, ExecScopesProxy will be removed and this should be modified in cairo-rs-py.

[Bug/Security] cairo-rs-py does not throw an InvalidStopPointer error

cairo-rs-py does not throw an InvalidStopPointer error

We (@FuzzingLabs) found that cairo-rs-py does not throw an InvalidStopPointer error while processing the following file.

Result using cairo-rs

Invalid stop pointer for output 
Error: Runner(InvalidStopPointer("output"))

Result using cairo-lang

Error: Invalid stop pointer for output. Expected: 2:0, found: 4:0

Result using cairo-rs-py

Program Output: 

Expected Behavior

cairo-rs-py should throw an error as cairo-rs and cairo-lang

Your Environment

  • 8dba86dbec935fa04a255e2edf3d5d184950fa22
  • cargo 1.67.0-nightly (ba607b23d 2022-11-22)
  • Ubuntu 22.04

Steps to reproduce

Download:
crash.zip

Using cairo-rs

./target/release/cairo-rs-run cairo_programs/crash2.json --layout all

Using cairo-lang

cairo-run --program crash2.json --layout all

Using cairo-rs-py

import cairo_rs_py
with open("crash2.json") as file:
        return cairo_rs_py.CairoRunner(file.read(), "main", "all", False)

Add unit tests for typed variables

#58 implements support for custom types in references for hints. It's currently only using integration tests to validate the functionality. It would be desirable to also unit test it.

Add functionality to support setting a struct as another struct's field

Inside a hint, it may be necessary to assign a struct inside another struct. Consider the example:

struct Foo {
    a: Bar
}

struct Bar {
    b: felt
}

A hint may want to assign field a from Foo like so:

ids.foo.a = ids.bar

Where foo and bar are instances of the structs Foo and Bar respectively.

This is not supported yet, and if necessary, support for this should be added.

Move cairo_run_py to PyVM

Keep in mind that we are going to pass the path to the contract but only temporarily. In the future, the bytecode would be retrieved from the contract address.

Create struct for cairo_run_py input

As the number of arguments to cairo_run_py is getting larger, we should consider creating a struct that will hold these arguments and pass that to cairo_run_py.

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.