Cleaopatra is a Rust implementation of the Cairo VM

Overview

Cleopatra Cairo VM

Cleaopatra is a Rust implementation of the Cairo VM. The VM is fully functional but we don't support builtins or hints yet. We are working on it.

Running Cleopatra

Compile with cargo build --release, once the binary is built, it can be found in target/release/ under the name cleopatra-run. To run a compiled json program through the VM, call the executable giving it the path and name to the file to be executed.

Full compilation and execution example:

git clone https://github.com/lambdaclass/cleopatra_cairo.git
cd cleopatra_cairo

cargo build --release
./target/release/cleopatra-run tests/support/fibonacci_compiled.json

Testing

Run the test suite with cargo:

cargo test

Code Coverage

Track of the project's code coverage: Codecov.

Cleopatra benchmarks:

Running a Cairo program that gets the 1000th Fibonacci number we got the following benchmarks:

Cairo

Original Cairo VM Internals

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

Compilers and interpreters

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

Computational integrity and Zero Knowledge Proofs

Basics

ZK SNARKs

ZK STARKs

Introduction:

Vitalik Buterin's blog series on zk-STARKs:

Alan Szepieniec's STARK tutorial:

StarkWare's STARK Math blog series:

Comments
  • Add CLI option to specify entrypoint

    Add CLI option to specify entrypoint

    Configurable entrypoint

    Description

    As described in #180, it might be useful to be able to run another function than the main function as an entrypoint.

    Checklist

    • [x] Linked to Github Issue
    • [x] Unit tests added
    • [x] Integration tests added.
    opened by Bernardstanislas 15
  • ci: add gh-pages docs workflow

    ci: add gh-pages docs workflow

    Description

    Partially addresses #75, providing rendered documentation on github pages.

    This pull adds a CI task and makefile target to build and deploy rustdocs.

    The current benchmark CI task should be unaffected, though its template is being updated in PR#201 to link to the generated html docs.

    Checklist

    • [x] Linked to Github Issue
    • [ ] Unit tests added
    • [ ] Integration tests added.
    • [ ] This change requires new documentation.
      • [ ] Documentation has been added/updated.
    opened by PartyLich 11
  • feat(CairoRunner): add a get_output function

    feat(CairoRunner): add a get_output function

    Expose the raw output

    Description

    Atm, you can only print the execution output. I need a way to get it directly from the lib, without printing it. write_output now wrap the new get_output

    opened by tdelabro 8
  • Chore: Separate each builtin into a different file

    Chore: Separate each builtin into a different file

    Separate each builtin into a different file

    Description

    Issue #195

    • Moves the implementation of each builtin runner into its own file.
    • Export paths remain unchanged.
    • No additional tests have been added. Functionality should not be affected, it's just a reorganization chore

    Checklist

    • [x] Linked to Github Issue
    • [ ] Unit tests added
    • [ ] Integration tests added.
    • [ ] This change requires new documentation.
      • [ ] Documentation has been added/updated.
    opened by PartyLich 6
  • Implement `get_memory_segment_addresses` for builtins

    Implement `get_memory_segment_addresses` for builtins

    Implement get_memory_segment_addresses for builtins

    Description

    Implementation of get_memory_segment_addresses for builtins.

    Checklist

    • [ ] Linked to Github Issue
    • [x] Unit tests added
    • [ ] Integration tests added.
    • [ ] This change requires new documentation.
      • [ ] Documentation has been added/updated.
    opened by azteca1998 5
  • Add implementation for hint on assert_not_zero (math.cairo)

    Add implementation for hint on assert_not_zero (math.cairo)

    Add implementation for hint on assert_not_zero (math.cairo)

    Add implementation of assert_not_zero cairo hint (math.cairo) https://github.com/starkware-libs/cairo-lang/blob/167b28bcd940fd25ea3816204fa882a0b0a49603/src/starkware/cairo/common/math.cairo#L4

    Checklist

    • [ ] Linked to Github Issue
    • [x] Unit tests added
    • [x] Integration tests added.
    • [ ] This change requires new documentation.
      • [ ] Documentation has been added/updated.
    opened by pefontana 5
  • Finish `CairoRunner::end_run()`'s `proof_mode` section.

    Finish `CairoRunner::end_run()`'s `proof_mode` section.

    Finish CairoRunner::end_run()'s proof_mode section

    Description

    Finish CairoRunner::end_run()'s proof_mode section.

    Checklist

    • [ ] Linked to Github Issue
    • [ ] Unit tests added
    • [ ] Integration tests added.
    • [ ] This change requires new documentation.
      • [ ] Documentation has been added/updated.
    opened by azteca1998 4
  • Extract constants from program and pass them to `HintProcessor::execute_hint`

    Extract constants from program and pass them to `HintProcessor::execute_hint`

    Extract constants from program and pass them to HintProcessor::execute_hint

    Description

    Allow hint processors to use constants defined in the program specification. Following issue #423.

    Checklist

    • [x] Linked to Github Issue
    • [x] Unit tests added
    • [ ] Integration tests added.
    • [x] This change requires new documentation.
      • [x] Documentation has been added/updated.
    opened by azteca1998 4
  • pow and math_cmp integration test

    pow and math_cmp integration test

    pow and math_cmp integration test

    Description

    • The cairo_programs/math_cmp_is_le_felt.cairo was really short, so it was replaced with cairo_programs/math_cmp.cairo that tests all the math_cmp functions.
    • Add integration test: cairo_programs/math_cmp_and_pow_integration_test.cairo that the all the pow and math_cmp functions

    Checklist

    • [ ] Linked to Github Issue
    • [ ] Unit tests added
    • [x] Integration tests added.
    • [ ] This change requires new documentation.
      • [ ] Documentation has been added/updated.
    opened by pefontana 4
  • Redesign hints processing

    Redesign hints processing

    Our current hint handling has a few issues:

    • It exposes the whole VM state, limiting how much of the inner workings of the VM we can change further down the road;
    • It forces all the processing to happen during program execution, without the ability to validate/preprocess beforehand;
    • Relies too much in &mut vm, which forces a lot of cloning to make the borrow checker happy;
    • It's, thus, too unfriendly towards custom hint processors.

    This ticket is not about directly solving the issues, as that will probably take several iterations that may be done independently. This is about discussing how to solve them. Any feedback is welcome.

    My current proposal is the following:

    1. Determine what hints actually need. At first sight, they seem to need the following:
      • Mutable access to AP;
      • Immutable access to FP and PC;
      • Mutable access to the VM memory (but in a proxy, as we shouldn't include accesses made by hints into the accessed addresses tracking, according to the Python VM);
      • Mutable access to the current hint execution scope;
      • The ability to add and remove one level from the scope;
      • A way to map referenced IDs to VM memory;
      • Access to AP tracking;
    2. Refactor the current (WIP) trait to only receive what's needed;
    3. Split the process into compilation (ran during program loading) and execution (ran during VM steps). The trait would now need to implement two or maybe three methods, as the executor is bound to the compiler.
      • The compiling step would need to store relevant data in a new custom structure, which represents the hint in some compiled form (maybe Python bytecode for Python executors, an enumerated value for our hardcoded hints or even the plain string as we use it now, lambdas, etc) and its auxiliary data, such as resolved references, PC (as it's already known), AP tracking information, etc.;
      • The processing step would receive the object previously created as well as the scope, the FP and AP, etc., and would then appropriately execute the compiled hint.
    enhancement help wanted question hints 
    opened by Oppen 4
  • Add offsets as primitive

    Add offsets as primitive

    Offsets in instructions are specified to be signed 16 bits integers. We were keeping them as BigInts and operating with those, heavily slowing down execution. By converting them to isize we save about 30% of execution time, up to 50% in some cases.

    opened by Oppen 3
  • Optimize accessed address tracking

    Optimize accessed address tracking

    This commit does a few things:

    • It uses chained iterators to avoid creating too many intermediary containers.
    • It specializes relocation for addresses for ad-hoc use when computing memory holes. This saves lot's of back and forth conversions and makes the code more readable, at the cost of a little bit of duplication.
    • Removes the accessed_addresses HashMap from CairoRunner. Filling it consumed almost half of the runtime in several programs.
    • Replaces CairoRunner::mark_as_accessed with VirtualMachine::mark_address_range_as_accessed to keep up with the previous change.

    TODO:

    • Maybe go back with relocate_address or completely exploit the difference somehow.
    • Update cairo-rs-py to use the new VirtualMachine method.
    • Fix the new function to actually figure out if the run ended.

    TITLE

    Description

    Description of the pull request changes and motivation.

    Checklist

    • [ ] Linked to Github Issue
    • [ ] Unit tests added
    • [ ] Integration tests added.
    • [ ] This change requires new documentation.
      • [ ] Documentation has been added/updated.
    opened by Oppen 1
  • Wrong builtins being passed to a test function

    Wrong builtins being passed to a test function

    This issue relates to the testing of the get_allocated_memory_units function.

    There's one test for each builtin runner, starting here.

    Inside each test, there's a call to the program macro, and a string is passed to the builtins parameter. In some of the tests, this string seems to be incorrect

    bitwise → “output”, “bitwise”

    • Problem: it calls two builtin runners, it should just call bitwise

    ec_op → “ec_op”

    hash → “pedersen”

    range_check → “pedersen”

    • problem: it should call range_check

    keccak → “keccak”

    opened by martinacantaro 0
  • Increase code coverage even more

    Increase code coverage even more

    Adding tests for Keccak

    Description

    Add test to get_allocated_memory_units() to check if it returns the keccak builtin's allocated memory units correctly. Rename fn get_allocated_memory_units_bitwise_with_values() to fn get_allocated_memory_units_bitwise_with_items() to match the other tests naming convention

    Checklist

    • [x] Linked to Github Issue https://github.com/lambdaclass/cairo-rs/issues/667
    • [x] Unit tests added
    • [-] Integration tests added.
    • [-] This change requires new documentation.
      • [-] Documentation has been added/updated.
    opened by martinacantaro 1
Owner
Lambdaclass
A software company from another dimension
Lambdaclass
STARK Cairo prover using lambdaworks

STARK Cairo prover using lambdaworks. 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. Cairo and similar proof systems can be used to provide scalability to blockchains.

Lambdaclass 18 Jun 13, 2023
Starknet Stack let's you easily create new Cairo Starknet chains with their own sequencers, provers and verifiers

Starknet Stack flowchart LR A("Client") ==>|"Starknet Transactions"| subGraph0["Sequencer"] subGraph0 -.->|"Blocks with txs"| 300319["Watcher prover

Lambdaclass 7 Jul 11, 2023
IBC modules and relayer - Formal specifications and Rust implementation

ibc-rs Rust implementation of the Inter-Blockchain Communication (IBC) protocol. This project comprises primarily four crates: The ibc crate defines t

Informal Systems 296 Dec 31, 2022
A Rust implementation of BIP-0039

bip39-rs A Rust implementation of BIP0039 Changes See the changelog file, or the Github releases for specific tags. Documentation Add bip39 to your Ca

Infincia LLC 49 Dec 9, 2022
Official Rust implementation of the Nimiq protocol

Nimiq Core implementation in Rust (core-rs) Rust implementation of the Nimiq Blockchain Core Nimiq is a frictionless payment protocol for the web. Thi

Nimiq 72 Sep 23, 2022
Rust implementation of Zcash protocol

The Parity Zcash client. Gitter Blog: Parity teams up with Zcash Foundation for Parity Zcash client Installing from source Installing the snap Running

Parity Technologies 183 Sep 8, 2022
A (mostly) pure-Rust implementation of various cryptographic algorithms.

Rust-Crypto A (mostly) pure-Rust implementation of various common cryptographic algorithms. Rust-Crypto seeks to create practical, auditable, pure-Rus

null 1.2k Dec 27, 2022
A pure-Rust implementation of group operations on Ristretto and Curve25519

curve25519-dalek A pure-Rust implementation of group operations on Ristretto and Curve25519. curve25519-dalek is a library providing group operations

dalek cryptography 611 Dec 25, 2022
[INACTIVE] TLS 1.2 implementation in Rust

suruga is Rust implementation of TLS 1.2. It currently implements some core parts of TLS 1.2, NIST P-256 ECDHE and chacha20-poly1305. Usage extern cra

klutzy/defunct 123 Dec 27, 2022
A prototype implementation of the Host Identity Protocol v2 for bare-metal systems, written in pure-rust.

Host Identity Protocol for bare-metal systems, using Rust I've been evaluating TLS replacements in constrained environments for a while now. Embedded

null 31 Dec 12, 2022
An implementation of the FP-Growth algorithm in pure Rust.

fp-growth-rs An implementation of the FP-Growth algorithm in pure Rust, which is inspired by enaeseth/python-fp-growth. Usage Add this to your Cargo.t

JmPotato 13 Dec 20, 2022
A pure-Rust implementation of various threshold secret sharing schemes

Threshold Secret Sharing Efficient pure-Rust library for secret sharing, offering efficient share generation and reconstruction for both traditional S

Snips 137 Dec 29, 2022
A Rust implementation of the Message Layer Security group messaging protocol

Molasses An extremely early implementation of the Message Layer Security group messaging protocol. This repo is based on draft 4 of the MLS protocol s

Trail of Bits 109 Dec 13, 2022
Pure Rust implementation of the RNCryptor cryptographic format by Rob Napier

rncryptor Rust Implementation of the RNCryptor spec This library implements the specification for the RNCryptor encrypted file format by Rob Napier. d

null 7 Jun 29, 2022
Implementation of the Web Cryptography specification in Rust.

[wip] webcrypto Implementation of the Web Cryptography specification in Rust. This crate hopes to ease interoperability between WASM and native target

Divy Srivastava 5 Mar 7, 2022
Implementation of Plonk by Hand in rust

plonk-by-fingers This is a toy implementation of the excellent Joshua Fitzgerald Plonk by hand (part2) (part3) tutorial all written from scratch, do n

adria0.eth 34 Dec 19, 2022
A Rust implementation of Trojan with QUIC tunnel, Lite-TLS and more.

Trojan-Oxide A Rust implementation of Trojan with QUIC tunnel, Lite-TLS and more. Overview Full support for the original Trojan Protocol, including TC

null 13 Oct 17, 2022
Pure Rust implementation of the Leighton Micali Signature scheme.

Leighton-Micali Hash-Based Signatures LMS implementation in Rust according to the IETF RFC 8554. This implementation is binary compatible with the ref

Fraunhofer AISEC 6 Jun 2, 2022
Rust implementation of Shamir's Secret Sharing

Horcrux - Rust implementation of Shamir's Secret Sharing This program is an example implementation of Shamir's Secret Sharing in Rust. You can find mo

null 13 Dec 29, 2022