Skip to content

Commit

Permalink
Merge branch 'main' of github.com:lambdaclass/cairo-rs into add-vm-ex…
Browse files Browse the repository at this point in the history
…ception-to-run-from-entrypoint
  • Loading branch information
fmoletta committed Jan 27, 2023
2 parents 0c795f3 + 195f9ce commit f478452
Show file tree
Hide file tree
Showing 33 changed files with 1,180 additions and 576 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: publish

on:
push:
tags:
- '*'

jobs:
publish:
name: Publish
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Publish crate cairo-felt
env:
CRATES_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --token ${CRATES_TOKEN} --all-features --manifest-path ./felt/Cargo.toml
# FIXME: there should be a better way to make sure the index in crates.io is updated before publishing
# cairo-vm but right now the step throws timeout and fails.
- name: wait for index in crates.io
run: sleep 120
- name: Publish crate cairo-vm
env:
CRATES_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --token ${CRATES_TOKEN} --all-features -p cairo-vm

29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,40 @@

#### Upcoming Changes

<<<<<<< HEAD
* Add `VmException` to `CairoRunner::run_from_entrypoint`[#775](https://github.com/lambdaclass/cairo-rs/pull/775)
* Public Api Changes:
* Change error return type of `CairoRunner::run_from_entrypoint` to `CairoRunError`.
* Convert `VirtualMachineError`s outputed during the vm run to `VmException` in `CairoRunner::run_from_entrypoint`.
* Make `VmException` fields public
=======
* Fix `BuiltinRunner::final_stack` and remove quick fix [#778](https://github.com/lambdaclass/cairo-rs/pull/778)
* Public Api changes:
* Various changes to public `BuiltinRunner` method's signatures:
* `final_stack(&self, vm: &VirtualMachine, pointer: Relocatable) -> Result<(Relocatable, usize), RunnerError>` to `final_stack(&mut self, segments: &MemorySegmentManager, memory: &Memory, pointer: Relocatable) -> Result<Relocatable,RunnerError>`.
* `get_used_cells(&self, vm: &VirtualMachine) -> Result<usize, MemoryError>` to `get_used_cells(&self, segments: &MemorySegmentManager) -> Result<usize, MemoryError>`.
* `get_used_instances(&self, vm: &VirtualMachine) -> Result<usize, MemoryError>` to `get_used_instances(&self, segments: &MemorySegmentManager) -> Result<usize, MemoryError>`.
* Bugfixes:
* `BuiltinRunner::final_stack` now updates the builtin's stop_ptr instead of returning it. This replaces the bugfix on PR #768.

#### [0.1.3] - 2023-01-26
* Add secure_run flag + integrate verify_secure_runner into cairo-run [#771](https://github.com/lambdaclass/cairo-rs/pull/777)
* Public Api changes:
* Add command_line argument `secure_run`
* Add argument `secure_run: Option<bool>` to `cairo_run`
* `verify_secure_runner` is now called inside `cairo-run` when `secure_run` is set to true or when it not set and the run is not on `proof_mode`
* Bugfixes:
* `EcOpBuiltinRunner::deduce_memory_cell` now checks that both points are on the curve instead of only the first one
* `EcOpBuiltinRunner::deduce_memory_cell` now returns the values of the point coordinates instead of the indices when a `PointNotOnCurve` error is returned

* Refactor `Refactor verify_secure_runner` [#768](https://github.com/lambdaclass/cairo-rs/pull/768)
* Public Api changes:
* Remove builtin name from the return value of `BuiltinRunner::get_memory_segment_addresses`
* Simplify the return value of `CairoRunner::get_builtin_segments_info` to `Vec<(usize, usize)>`
* CairoRunner::read_return_values now receives a mutable reference to VirtualMachine
* Bugfixes:
* CairoRunner::read_return_values now updates the `stop_ptr` of each builtin after calling `BuiltinRunner::final_stack`
>>>>>>> 195f9ce1eaaa66093207078525e5158e78ce0590
* Use CairoArg enum instead of Any in CairoRunner::run_from_entrypoint [#686](https://github.com/lambdaclass/cairo-rs/pull/686)
* Public Api changes:
Expand Down
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ members = [".", "felt", "./deps/parse-hyperlinks"]

[package]
name = "cairo-vm"
version = "0.1.1"
version = "0.1.3"
edition = "2021"
license = "MIT"
license = "Apache-2.0"
description = "Blazing fast Cairo interpreter"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Cargo doesn't comply with [semver](https://semver.org/), so we advise to pin the

## 📖 About

Cairo VM is the virtual machine for the [Cairo language](www.cairo-lang.org/).
Cairo VM is the virtual machine for the [Cairo language](https://www.cairo-lang.org/).

There's an older version of [Cairo VM](https://github.com/starkware-libs/cairo-lang) written in Python, which is **currently in production**.

Expand Down Expand Up @@ -96,7 +96,7 @@ To run a compiled .json program through the VM, call the executable giving it th
```bash
target/release/cairo-rs-run cairo_programs/abs_value_array_compiled.json --layout all
```
The flag `--layout` determines which built-in is going to be used. More info about layouts [here](https://www.cairo-lang.org/docs/how_cairo_works/builtins.html#layouts).
The flag `--layout` determines which builtins can be used. More info about layouts [here](https://www.cairo-lang.org/docs/how_cairo_works/builtins.html#layouts).

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

Expand All @@ -118,7 +118,7 @@ Currently, as this VM is under construction, it's missing some of the features o
There are two ways to use non-standard hints in this VM:

- Extend the cairo-rs code and build your own binary using the interface hint processor
- Use [cairo-rs-py](https://github.com/lambdaclass/cairo-rs-py) which supports running any arbitrary hint in a Python interpreter.
- Use [cairo-rs-py](https://github.com/lambdaclass/cairo-rs-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-rs repository you would first need to prepare a couple of things.
Expand Down
1 change: 1 addition & 0 deletions bench/criterion_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn criterion_benchmarks(c: &mut Criterion) {
false,
"all",
false,
None,
&mut hint_executor,
)
})
Expand Down
1 change: 1 addition & 0 deletions bench/iai_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ macro_rules! iai_bench_expand_prog {
false,
"all",
false,
None,
&mut hint_executor,
)
}
Expand Down
2 changes: 0 additions & 2 deletions cairo_programs/bad_programs/ec_op_not_in_curve.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ func test_ec_op_point_not_on_curve{
assert ec_op_ptr[0].p = p;
assert ec_op_ptr[0].q = EcPoint(x=p.x, y=p.y + 1);
assert ec_op_ptr[0].m = 7;
assert ec_op_ptr[0].r.x = ec_op_ptr[0].r.x;
assert ec_op_ptr[0].r.y = ec_op_ptr[0].r.y;
let ec_op_ptr = &ec_op_ptr[1];
return ();
}
Expand Down
2 changes: 0 additions & 2 deletions cairo_programs/bad_programs/ec_op_same_x.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func test_ec_op_invalid_input{
0x4fad269cbf860980e38768fe9cb6b0b9ab03ee3fe84cfde2eccce597c874fd8,
);
assert ec_op_ptr[0].m = 8;
assert ec_op_ptr[0].r.x = ec_op_ptr[0].r.x;
assert ec_op_ptr[0].r.y = ec_op_ptr[0].r.y;
let ec_op_ptr = &ec_op_ptr[1];
return ();
}
Expand Down
4 changes: 2 additions & 2 deletions felt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "cairo-felt"
version = "0.1.0"
version = "0.1.3"
edition = "2021"
license = "MIT"
license = "Apache-2.0"
description = "Field elements representation for the Cairo VM"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions felt/proptest-regressions/lib.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 1bf12114a6bfc70e8f949318158595883c33bc6fb35862852bc5144547ac3557 # shrinks to ref x = "0"
cc 3a938be1bdfbb188c7c1f0dc097d8089fe8b26a98b76c013014651353d2926d0 # shrinks to ref x = "10000000000000000000000000000000"
6 changes: 5 additions & 1 deletion felt/src/bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ impl FeltOps for FeltBigInt<FIELD_HIGH, FIELD_LOW> {
}

fn from_bytes_be(bytes: &[u8]) -> FeltBigInt<FIELD_HIGH, FIELD_LOW> {
FeltBigInt::<FIELD_HIGH, FIELD_LOW>::new(BigUint::from_bytes_be(bytes))
let mut value = BigUint::from_bytes_be(bytes);
if value >= *CAIRO_PRIME {
value = value.mod_floor(&CAIRO_PRIME);
}
Self::from(value)
}

fn to_str_radix(&self, radix: u32) -> String {
Expand Down
10 changes: 10 additions & 0 deletions felt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,16 @@ mod test {
let p = &BigUint::parse_bytes(PRIME_STR[2..].as_bytes(), 16).unwrap();
prop_assert!(&x.to_biguint() < p);
}

#[test]
// Property-based test that ensures, for 100 felt values that are randomly generated each time tests are run, that a felt created using Felt::from_bytes_be doesn't fall outside the range [0, p].
// In this and some of the following tests, The value of {x} can be either [0] or a very large number, in order to try to overflow the value of {p} and thus ensure the modular arithmetic is working correctly.
fn from_bytes_be_in_range(ref x in "(0|[1-9][0-9]*)") {
let x = &Felt::from_bytes_be(x.as_bytes());
let max_felt = &Felt::max_value();
prop_assert!(x <= max_felt);
}

#[test]
// Property-based test that ensures, for 100 felt values that are randomly generated each time tests are run, that the negative of a felt doesn't fall outside the range [0, p].
fn neg_in_range(ref x in "(0|[1-9][0-9]*)") {
Expand Down
13 changes: 12 additions & 1 deletion src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
cairo_run_errors::CairoRunError, runner_errors::RunnerError, vm_exception::VmException,
},
runners::cairo_runner::CairoRunner,
security::verify_secure_runner,
trace::trace_entry::RelocatedTraceEntry,
vm_core::VirtualMachine,
},
Expand All @@ -17,20 +18,24 @@ use std::{
path::Path,
};

#[allow(clippy::too_many_arguments)]
pub fn cairo_run(
path: &Path,
entrypoint: &str,
trace_enabled: bool,
print_output: bool,
layout: &str,
proof_mode: bool,
secure_run: Option<bool>,
hint_executor: &mut dyn HintProcessor,
) -> Result<CairoRunner, CairoRunError> {
let program = match Program::from_file(path, Some(entrypoint)) {
Ok(program) => program,
Err(error) => return Err(CairoRunError::Program(error)),
};

let secure_run = secure_run.unwrap_or(!proof_mode);

let mut cairo_runner = CairoRunner::new(&program, layout, proof_mode)?;
let mut vm = VirtualMachine::new(trace_enabled);
let end = cairo_runner.initialize(&mut vm)?;
Expand All @@ -41,10 +46,13 @@ pub fn cairo_run(
cairo_runner.end_run(false, false, &mut vm, hint_executor)?;

vm.verify_auto_deductions()?;
cairo_runner.read_return_values(&mut vm)?;
if proof_mode {
cairo_runner.read_return_values(&vm)?;
cairo_runner.finalize_segments(&mut vm)?;
}
if secure_run {
verify_secure_runner(&cairo_runner, true, &mut vm)?;
}
cairo_runner.relocate(&mut vm)?;

if print_output {
Expand Down Expand Up @@ -212,6 +220,7 @@ mod tests {
false,
"plain",
false,
None,
&mut hint_processor
)
.is_err());
Expand All @@ -230,6 +239,7 @@ mod tests {
false,
"plain",
false,
None,
&mut hint_processor
)
.is_err());
Expand All @@ -248,6 +258,7 @@ mod tests {
false,
"plain",
false,
None,
&mut hint_processor
)
.is_err());
Expand Down
2 changes: 1 addition & 1 deletion src/hint_processor/hint_processor_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn get_ptr_from_reference(
}
}

///Returns the value given by a reference as an Option<MaybeRelocatable>
//Returns the value given by a reference as an Option<MaybeRelocatable>
pub fn get_maybe_relocatable_from_reference(
vm: &VirtualMachine,
hint_reference: &HintReference,
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct Args {
layout: String,
#[structopt(long = "--proof_mode")]
proof_mode: bool,
#[structopt(long = "--secure_run")]
secure_run: Option<bool>,
}

fn validate_layout(value: &str) -> Result<(), String> {
Expand All @@ -54,6 +56,7 @@ fn main() -> Result<(), CairoRunError> {
args.print_output,
&args.layout,
args.proof_mode,
args.secure_run,
&mut hint_executor,
) {
Ok(runner) => runner,
Expand Down
Loading

0 comments on commit f478452

Please sign in to comment.