Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example just for math*.cairo files #455

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ $(CAIRO_VM_CLI):

build_cairo_vm_cli: | $(CAIRO_VM_CLI)
BENCH_DIR=cairo_programs/benchmarks
# Define the directory containing your Cairo programs
CAIRO_PROGRAMS_DIR := cairo_programs


# Creates a pyenv and installs cairo-lang
deps:
Expand Down Expand Up @@ -39,6 +42,19 @@ test-filter:
build-integration-test:
@zig build integration_test


# Task for running the custom integration process
run-custom-integration-test:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for the sake of demonstration i created a separate entry point to run the integration tests that

  • specifically look up all the cairo files matching an arbitary glob
  • write all matches to a text file
  • compile all matches
  • pass the text file into the integration_test runner to actually run

# Find all 'math*.cairo' files and write them to a list
@find $(CAIRO_PROGRAMS_DIR) -name "math*.cairo" | sed 's|.*/||' | sed 's|\.cairo$$||' > $(CAIRO_PROGRAMS_DIR)/math_programs_list.txt

# Iterate over the list and compile each program
@while read file; do \
cairo-compile --cairo_path="$(CAIRO_PROGRAMS_DIR)" "$(CAIRO_PROGRAMS_DIR)/$$file.cairo" --output "$(CAIRO_PROGRAMS_DIR)/$$file.json" --proof_mode; \
done < $(CAIRO_PROGRAMS_DIR)/math_programs_list.txt
./zig-out/bin/integration_test $(CAIRO_PROGRAMS_DIR)/math_programs_list.txt


run-integration-test:
@zig build integration_test
./zig-out/bin/integration_test
Expand Down
62 changes: 62 additions & 0 deletions cairo_programs/math_cmp.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
%builtins range_check
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we commit cairo files. i arbitrarily chose to just move the files in the great https://github.com/lambdaclass/cairo-vm/tree/main/cairo_programs

that matched "math.cairo


from starkware.cairo.common.math_cmp import (
is_not_zero,
is_nn,
is_le,
is_nn_le,
is_in_range,
is_le_felt,
)

func main{range_check_ptr: felt}() {
// is_not_zero
let a = is_not_zero(10);
assert a = 1;
let b = is_not_zero(1);
assert b = 1;
let c = is_not_zero(0);
assert c = 0;

// is_nn
let d = is_nn(0);
assert d = 1;
let e = is_nn(88);
assert e = 1;
let f = is_nn(-88);
assert f = 0;

// is_le
let g = is_le(1, 2);
assert g = 1;
let h = is_le(2, 2);
assert h = 1;
let i = is_le(56, 20);
assert i = 0;

// is_nn_le
let j = is_nn_le(1, 2);
assert j = 1;
let k = is_nn_le(2, 2);
assert k = 1;
let l = is_nn_le(56, 20);
assert l = 0;

// is_in_range
let m = is_in_range(1, 2, 3);
assert m = 0;
let n = is_in_range(2, 2, 5);
assert n = 1;
let o = is_in_range(56, 20, 120);
assert o = 1;

// is_le_felt
let p = is_le_felt(1, 2);
assert p = 1;
let q = is_le_felt(2, 2);
assert q = 1;
let r = is_le_felt(56, 20);
assert r = 0;

return ();
}
120 changes: 120 additions & 0 deletions cairo_programs/math_cmp_and_pow_integration_tests.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
%builtins range_check

from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.pow import pow
from starkware.cairo.common.math_cmp import (
is_not_zero,
is_nn,
is_le,
is_nn_le,
is_in_range,
is_le_felt,
)

const CONSTANT = 3 ** 10;

func fill_array_with_pow{range_check_ptr}(
array_start: felt*, base: felt, step: felt, exp: felt, iter: felt, last: felt
) -> () {
if (iter == last) {
return ();
}
let (res) = pow(base + step, exp);
assert array_start[iter] = res;
return fill_array_with_pow(array_start, base + step, step, exp, iter + 1, last);
}

func test_is_not_zero{range_check_ptr}(
base_array: felt*, new_array: felt*, iter: felt, last: felt
) -> () {
if (iter == last) {
return ();
}
let res = is_not_zero(base_array[iter]);
assert new_array[iter] = res;
return test_is_not_zero(base_array, new_array, iter + 1, last);
}

func test_is_nn{range_check_ptr}(base_array: felt*, new_array: felt*, iter: felt, last: felt) -> (
) {
if (iter == last) {
return ();
}
let res = is_nn(base_array[iter]);
assert new_array[iter] = res;
return test_is_nn(base_array, new_array, iter + 1, last);
}

func test_is_le{range_check_ptr}(base_array: felt*, new_array: felt*, iter: felt, last: felt) -> (
) {
if (iter == last) {
return ();
}
let res = is_le(base_array[iter], CONSTANT);
assert new_array[iter] = res;
return test_is_le(base_array, new_array, iter + 1, last);
}

func test_is_nn_le{range_check_ptr}(
base_array: felt*, new_array: felt*, iter: felt, last: felt
) -> () {
if (iter == last) {
return ();
}
let res = is_nn_le(base_array[iter], CONSTANT);
assert new_array[iter] = res;
return test_is_nn_le(base_array, new_array, iter + 1, last);
}

func test_is_in_range{range_check_ptr}(
base_array: felt*, new_array: felt*, iter: felt, last: felt
) -> () {
if (iter == last) {
return ();
}
let res = is_in_range(CONSTANT, base_array[iter], base_array[iter + 1]);
assert new_array[iter] = res;
return test_is_in_range(base_array, new_array, iter + 1, last);
}

func test_is_le_felt{range_check_ptr}(
base_array: felt*, new_array: felt*, iter: felt, last: felt
) -> () {
if (iter == last) {
return ();
}
let res = is_le_felt(base_array[iter], CONSTANT);
assert new_array[iter] = res;
return test_is_le_felt(base_array, new_array, iter + 1, last);
}

func run_tests{range_check_ptr}(array_len: felt) -> () {
alloc_locals;
let (array: felt*) = alloc();
fill_array_with_pow(array, 0, 3, 3, 0, array_len);

let (array_is_not_zero: felt*) = alloc();
test_is_not_zero(array, array_is_not_zero, 0, array_len);

let (array_is_nn: felt*) = alloc();
test_is_nn(array, array_is_nn, 0, array_len);

let (array_is_le: felt*) = alloc();
test_is_le(array, array_is_le, 0, array_len);

let (array_is_nn_le: felt*) = alloc();
test_is_nn_le(array, array_is_nn_le, 0, array_len);

let (array_is_in_range: felt*) = alloc();
test_is_in_range(array, array_is_in_range, 0, array_len - 1);

let (array_is_le_felt: felt*) = alloc();
test_is_le_felt(array, array_is_le_felt, 0, array_len);

return ();
}

func main{range_check_ptr}() {
run_tests(10);
return ();
}
Loading
Loading