Skip to content

Commit

Permalink
Merge branch 'main' into fix-felt-from-num
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta authored May 17, 2023
2 parents a46cfbc + 92a81f7 commit fbac71d
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 48 deletions.
1 change: 1 addition & 0 deletions .github/workflows/iai_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
pip install -r requirements.txt
sudo apt update
sudo apt install -y valgrind
cargo install --version 0.3.1 iai-callgrind-runner
# NB: this should never result in a hit, but rather just populate for PRs
- name: Initialize IAI cache for ${{ github.sha }}
uses: actions/cache@v3
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/iai_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
pip install -r requirements.txt
sudo apt update
sudo apt install -y valgrind
cargo install --version 0.3.1 iai-callgrind-runner
- name: Initialize IAI cache for ${{ github.event.pull_request.base.sha }}
uses: actions/cache@v3
id: cache-iai-results
Expand Down Expand Up @@ -61,6 +62,7 @@ jobs:
pip install -r requirements.txt
sudo apt update
sudo apt install -y valgrind
cargo install --version 0.3.1 iai-callgrind-runner
- name: Initialize IAI cache for ${{ github.event.pull_request.base.sha }}
uses: actions/cache@v3
id: cache-iai-results
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

* Fix implementation of `InitSquashData` and `ShouldSkipSquashLoop`

* Add more hints to `Cairo1HintProcessor` [#1143](https://github.com/lambdaclass/cairo-rs/pull/1143)
* Add more hints to `Cairo1HintProcessor` [#1171](https://github.com/lambdaclass/cairo-rs/pull/1171)
[#1143](https://github.com/lambdaclass/cairo-rs/pull/1143)

* `Cairo1HintProcessor` can now run the following hints:
* Felt252DictEntryInit
Expand All @@ -34,6 +35,7 @@
* GetCurrentAccessIndex
* ShouldContinueSquashLoop
* FieldSqrt
* Uint512DivMod

* Add some small considerations regarding Cairo 1 programs [#1144](https://github.com/lambdaclass/cairo-rs/pull/1144):

Expand Down
55 changes: 27 additions & 28 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ felt = { package = "cairo-felt", path = "./felt", version = "0.4.0", default-fea
bitvec = { version = "1", default-features = false, features = ["alloc"] }

# Dependencies for cairo-1-hints feature
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev = "44fdeb26231f5022f2441bf89018272460c4e326", optional = true}
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo.git", rev = "44fdeb26231f5022f2441bf89018272460c4e326", optional = true }
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev = "4afacfd574de1121d61dea28f8c73e1516c7e07d", optional = true}
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo.git", rev = "4afacfd574de1121d61dea28f8c73e1516c7e07d", optional = true }

# TODO: check these dependencies for wasm compatibility
ark-ff = {version = "0.4.0-alpha.7", default-features = false, optional = true}
Expand All @@ -98,7 +98,7 @@ rstest = { version = "0.17.0", default-features = false }
wasm-bindgen-test = "0.3.34"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
iai = "0.1"
iai-callgrind = "0.3.1"
rusty-hook = "0.11"
criterion = { version = "0.3", features = ["html_reports"] }
proptest = "1.0.0"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ $(cairo-repo-dir):
cd cairo; cargo b --release --bin starknet-compile --bin starknet-sierra-compile

cargo-deps:
cargo install --version 0.3.1 iai-callgrind-runner
cargo install --version 1.1.0 cargo-criterion
cargo install --version 0.6.1 flamegraph
cargo install --version 1.14.0 hyperfine
Expand Down
43 changes: 29 additions & 14 deletions bench/iai_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iai::{black_box, main};
use iai_callgrind::{black_box, main};

use cairo_vm::{
types::program::Program,
Expand All @@ -12,6 +12,7 @@ use mimalloc::MiMalloc;
#[global_allocator]
static ALLOC: MiMalloc = MiMalloc;

#[inline(never)]
fn parse_program() {
//Picked the biggest one at the time of writing
let program = include_bytes!("../cairo_programs/benchmarks/keccak_integration_benchmark.json");
Expand All @@ -20,25 +21,39 @@ fn parse_program() {
let _ = black_box(program);
}

fn build_many_runners() {
#[export_name = "helper::parse_program"]
#[inline(never)]
fn parse_program_helper() -> Program {
//Picked the biggest one at the time of writing
let program = include_bytes!("../cairo_programs/benchmarks/keccak_integration_benchmark.json");
let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap();
for _ in 0..100 {
let runner = CairoRunner::new(black_box(&program), "starknet_with_keccak", false).unwrap();
let _ = black_box(runner);
}
Program::from_bytes(program.as_slice(), Some("main")).unwrap()
}

fn load_program_data() {
#[inline(never)]
fn build_runner() {
let program = parse_program_helper();
let runner = CairoRunner::new(black_box(&program), "starknet_with_keccak", false).unwrap();
let _ = black_box(runner);
}

#[export_name = "helper::build_runner"]
#[inline(never)]
fn build_runner_helper() -> (CairoRunner, VirtualMachine) {
//Picked the biggest one at the time of writing
let program = include_bytes!("../cairo_programs/benchmarks/keccak_integration_benchmark.json");
let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap();
for _ in 0..100 {
let mut runner = CairoRunner::new(&program, "starknet_with_keccak", false).unwrap();
let mut vm = VirtualMachine::new(false);
_ = black_box(runner.initialize(black_box(&mut vm)).unwrap());
}
let runner = CairoRunner::new(&program, "starknet_with_keccak", false).unwrap();
let vm = VirtualMachine::new(false);
(runner, vm)
}

#[inline(never)]
fn load_program_data() {
let (mut runner, mut vm) = build_runner_helper();
_ = black_box(runner.initialize(black_box(&mut vm)).unwrap());
}

main!(parse_program, build_many_runners, load_program_data);
main!(
callgrind_args = "toggle-collect=helper::*";
functions = parse_program, build_runner, load_program_data
);
26 changes: 26 additions & 0 deletions cairo_programs/cairo-1-contracts/uint512_div_mod.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[contract]
mod UintDivMod {

use integer::{u512, u512_safe_div_rem_by_u256};

#[external]
fn div_mod() -> () {
let zero = u512 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 };
let one = u512 { limb0: 1, limb1: 0, limb2: 0, limb3: 0 };

let (q, r) = u512_safe_div_rem_by_u256(zero, integer::u256_as_non_zero(1));
assert(q == zero, '0 / 1 != 0');
assert(r == 0, '0 % 1 != 0');

let (q, r) = u512_safe_div_rem_by_u256(one, integer::u256_as_non_zero(1));
assert(q == one, '1 / 1 != 1');
assert(r == 0, '1 % 1 != 0');

let two = u512 {limb0: 0, limb1: 0, limb2: 0, limb3: 2};
let (q, r) = u512_safe_div_rem_by_u256(two, integer::u256_as_non_zero(1));
assert(q == two, '2/1 != 2');
assert(r == 0, '2/1 != 0');

return ();
}
}
Loading

0 comments on commit fbac71d

Please sign in to comment.