From 0fd79c675e0825c5b625acebaaf219d4fa16a6a6 Mon Sep 17 00:00:00 2001 From: Johann Date: Mon, 11 Mar 2024 13:14:34 -0700 Subject: [PATCH 1/2] example just for math*.cairo files --- Makefile | 16 + cairo_programs/math_cmp.cairo | 62 ++++ .../math_cmp_and_pow_integration_tests.cairo | 120 ++++++++ cairo_programs/math_integration_tests.cairo | 266 +++++++++++++++++ src/integration_tests.zig | 275 +++++++++++------- 5 files changed, 630 insertions(+), 109 deletions(-) create mode 100644 cairo_programs/math_cmp.cairo create mode 100644 cairo_programs/math_cmp_and_pow_integration_tests.cairo create mode 100644 cairo_programs/math_integration_tests.cairo diff --git a/Makefile b/Makefile index 1a2ac54f..06574195 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -39,6 +42,19 @@ test-filter: build-integration-test: @zig build integration_test + +# Task for running the custom integration process +run-custom-integration-test: + # 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 diff --git a/cairo_programs/math_cmp.cairo b/cairo_programs/math_cmp.cairo new file mode 100644 index 00000000..f2dedf79 --- /dev/null +++ b/cairo_programs/math_cmp.cairo @@ -0,0 +1,62 @@ +%builtins range_check + +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 (); +} diff --git a/cairo_programs/math_cmp_and_pow_integration_tests.cairo b/cairo_programs/math_cmp_and_pow_integration_tests.cairo new file mode 100644 index 00000000..60c86339 --- /dev/null +++ b/cairo_programs/math_cmp_and_pow_integration_tests.cairo @@ -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 (); +} diff --git a/cairo_programs/math_integration_tests.cairo b/cairo_programs/math_integration_tests.cairo new file mode 100644 index 00000000..4a76d348 --- /dev/null +++ b/cairo_programs/math_integration_tests.cairo @@ -0,0 +1,266 @@ +%builtins range_check + +from starkware.cairo.common.alloc import alloc +from starkware.cairo.common.math import ( + assert_not_zero, + assert_not_equal, + assert_nn, + assert_le, + assert_lt, + assert_nn_le, + assert_in_range, + assert_250_bit, + split_felt, + assert_le_felt, + assert_lt_felt, + abs_value, + sign, + unsigned_div_rem, + signed_div_rem, + split_int, + sqrt, + horner_eval, +) + +func fill_array(array_start: felt*, base: felt, step: felt, iter: felt, last: felt) -> () { + if (iter == last) { + return (); + } + assert array_start[iter] = base + step; + return fill_array(array_start, base + step, step, iter + 1, last); +} + +func test_assert_nn{range_check_ptr}(array_start: felt*, iter: felt, last: felt) -> () { + if (iter == last) { + return (); + } + assert_nn(array_start[iter]); + return test_assert_nn(array_start, iter + 1, last); +} + +func test_assert_not_zero(array_start: felt*, iter: felt, last: felt) -> () { + if (iter == last) { + return (); + } + assert_not_zero(array_start[iter]); + return test_assert_not_zero(array_start, iter + 1, last); +} + +func test_assert_not_equal(array_a: felt*, array_b: felt*, iter: felt, size: felt) -> () { + if (iter == size) { + return (); + } + assert_not_equal(array_a[iter], array_b[iter]); + return test_assert_not_equal(array_a, array_b, iter + 1, size); +} + +func test_assert_le{range_check_ptr}(array_a: felt*, array_b: felt*, iter: felt, size: felt) -> () { + if (iter == size) { + return (); + } + assert_le(array_a[iter], array_b[iter]); + return test_assert_le(array_a, array_b, iter + 1, size); +} + +func test_assert_lt{range_check_ptr}(array_a: felt*, array_b: felt*, iter: felt, size: felt) -> () { + if (iter == size) { + return (); + } + assert_lt(array_a[iter], array_b[iter]); + return test_assert_lt(array_a, array_b, iter + 1, size); +} + +func test_assert_nn_le{range_check_ptr}(array_a: felt*, array_b: felt*, iter: felt, size: felt) -> ( + ) { + if (iter == size) { + return (); + } + assert_nn_le(array_a[iter], array_b[iter]); + return test_assert_nn_le(array_a, array_b, iter + 1, size); +} + +func test_assert_in_range{range_check_ptr}( + array_start: felt*, iter: felt, size: felt, lower: felt, upper: felt +) { + if (iter == size) { + return (); + } + assert_in_range{range_check_ptr=range_check_ptr}(array_start[iter], lower, upper); + return test_assert_in_range{range_check_ptr=range_check_ptr}( + array_start, iter + 1, size, lower, upper + ); +} + +func test_assert_250_bit{range_check_ptr}(array_start: felt*, iter: felt, last: felt) -> () { + if (iter == last) { + return (); + } + assert_250_bit(array_start[iter]); + return test_assert_250_bit(array_start, iter + 1, last); +} + +func test_split_felt{range_check_ptr}(array_start: felt*, iter: felt, last: felt) -> () { + alloc_locals; + + if (iter == last) { + return (); + } + let (x: felt, y: felt) = split_felt(array_start[iter]); + assert array_start[iter] = x * (2 ** 128) + y; + return test_split_felt(array_start, iter + 1, last); +} + +func test_assert_le_felt{range_check_ptr}( + array_a: felt*, array_b: felt*, iter: felt, last: felt +) -> () { + if (iter == last) { + return (); + } + assert_le_felt(array_a[iter], array_b[iter]); + return test_assert_le_felt(array_a, array_b, iter + 1, last); +} + +func test_assert_lt_felt{range_check_ptr}( + array_a: felt*, array_b: felt*, iter: felt, last: felt +) -> () { + if (iter == last) { + return (); + } + assert_lt_felt(array_a[iter], array_b[iter]); + return test_assert_lt_felt(array_a, array_b, iter + 1, last); +} + +func test_abs_value{range_check_ptr}(array_a: felt*, array_b: felt*, iter: felt, last: felt) -> () { + alloc_locals; + if (iter == last) { + return (); + } + let abs_a: felt = abs_value(array_a[iter]); + let abs_b: felt = abs_value(array_b[iter]); + assert abs_a = abs_b; + return test_abs_value(array_a, array_b, iter + 1, last); +} + +func test_same_sign{range_check_ptr}(array_a: felt*, array_b: felt*, iter: felt, last: felt) -> () { + alloc_locals; + if (iter == last) { + return (); + } + let sign_a: felt = sign(array_a[iter]); + let sign_b: felt = sign(array_b[iter]); + assert sign_a = sign_b; + return test_same_sign(array_a, array_b, iter + 1, last); +} + +func test_diff_sign{range_check_ptr}(array_a: felt*, array_b: felt*, iter: felt, last: felt) -> () { + alloc_locals; + if (iter == last) { + return (); + } + let sign_a: felt = sign(array_a[iter]); + let sign_b: felt = sign(array_b[iter]); + assert sign_a = -sign_b; + return test_diff_sign(array_a, array_b, iter + 1, last); +} + +func test_sqrt{range_check_ptr}(array_start: felt*, iter: felt, last: felt) -> () { + alloc_locals; + if (iter == last) { + return (); + } + let n_sqrt: felt = sqrt(array_start[iter]); + assert n_sqrt * n_sqrt = array_start[iter]; + return test_sqrt(array_start, iter + 1, last); +} + +func test_split_int{range_check_ptr}(array_start: felt*, iter: felt, last: felt) -> () { + alloc_locals; + + if (iter == last) { + return (); + } + let (output: felt*) = alloc(); + split_int(array_start[iter], 4, 8, 8, output); + assert array_start[iter] = output[0] + output[1] * 8 + output[2] * 64 + output[3] * 512; + return test_split_int(array_start, iter + 1, last); +} + +func test_horner_eval{range_check_ptr}(array_start: felt*, iter: felt, last: felt) -> () { + alloc_locals; + + if (iter == last) { + return (); + } + + let res: felt = horner_eval(3, array_start + iter, 3); + assert res = array_start[iter] + 3 * array_start[iter + 1] + 9 * array_start[iter + 2]; + return test_horner_eval(array_start, iter + 1, last); +} + +func test_{range_check_ptr}(array_start: felt*, iter: felt, last: felt) -> () { + if (iter == last) { + return (); + } + // unsigned_div_rem{range_check_ptr}(value, div) -> (q : felt, r : felt):, + // signed_div_rem{range_check_ptr}(value, div, bound) -> (q : felt, r : felt):, + return test_(array_start, iter + 1, last); +} + +func run_tests{range_check_ptr}(array_len: felt) -> () { + alloc_locals; + + assert_lt(1, array_len); + + let (array: felt*) = alloc(); + fill_array(array, 0, 3, 0, array_len); + + let (array_neg: felt*) = alloc(); + fill_array(array_neg, 0, -3, 0, array_len); + + test_assert_nn(array, 0, array_len); + // test_assert_nn(array_neg, 0, array_len) + + test_assert_not_zero(array, 1, array_len); + test_assert_not_zero(array_neg, 1, array_len); + + test_assert_not_equal(array, array + 1, 0, array_len - 1); + test_assert_not_equal(array + 1, array, 0, array_len - 1); + test_assert_not_equal(array_neg, array_neg + 1, 0, array_len - 1); + test_assert_not_equal(array_neg + 1, array_neg, 0, array_len - 1); + + test_assert_le(array, array, 0, array_len - 1); + test_assert_le(array, array + 1, 0, array_len - 1); + test_assert_lt(array, array + 1, 0, array_len - 1); + test_assert_nn_le(array, array + 1, 0, array_len - 1); + + test_assert_le_felt(array, array, 0, array_len - 1); + test_assert_le_felt(array, array + 1, 0, array_len - 1); + test_assert_lt_felt(array, array + 1, 0, array_len - 1); + + test_abs_value(array, array, 0, array_len); + test_abs_value(array_neg, array_neg, 0, array_len); + test_abs_value(array_neg, array, 0, array_len); + test_abs_value(array, array_neg, 0, array_len); + + test_same_sign(array, array, 0, array_len); + test_same_sign(array + 1, array + 2, 0, array_len - 2); + test_same_sign(array_neg, array_neg, 0, array_len); + test_same_sign(array_neg + 1, array_neg + 2, 0, array_len - 2); + + test_diff_sign(array + 1, array_neg + 1, 0, array_len - 1); + test_diff_sign(array_neg + 1, array + 1, 0, array_len - 1); + + // test_assert_in_range(array, 0, array_len, 0, array[array_len - 1]) + test_assert_250_bit(array, 0, array_len); + test_split_felt(array, 0, array_len); + test_split_int(array, 0, array_len); + + test_horner_eval(array, 0, array_len - 2); + + return (); +} + +func main{range_check_ptr}() { + run_tests(10); + return (); +} diff --git a/src/integration_tests.zig b/src/integration_tests.zig index f052559a..ce06b054 100644 --- a/src/integration_tests.zig +++ b/src/integration_tests.zig @@ -5,119 +5,176 @@ const CairoVM = @import("vm/core.zig").CairoVM; const CairoRunner = @import("vm/runners/cairo_runner.zig").CairoRunner; const HintProcessor = @import("./hint_processor/hint_processor_def.zig").CairoVMHintProcessor; -pub fn main() void { +const CairoTestProgram = struct { + pathname: []const u8, + layout: []const u8, + extensive_hints: bool = false, +}; + +pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // Given const allocator = gpa.allocator(); - const cairo_programs = [_]struct { - pathname: []const u8, - layout: []const u8, - extensive_hints: bool = false, - }{ - .{ .pathname = "cairo_programs/abs_value_array_compiled.json", .layout = "all_cairo" }, - // TODO: UnknownOp0 error - // .{ .pathname = "cairo_programs/array_sum_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/assert_lt_felt.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/assert_250_bit_element_array_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/assert_le_felt_hint_compiled.json", .layout = "all_cairo" }, - // .{ .pathname = "cairo_programs/assert_le_felt_old_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/assert_nn_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/assert_not_zero_compiled.json", .layout = "all_cairo" }, - // TODO: merge bigint hint - // .{ .pathname = "cairo_programs/bigint_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/big_struct_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/bitand_hint_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/bitwise_output_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/bitwise_builtin_test.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/bitwise_recursion_compiled.json", .layout = "all_cairo" }, - // TODO: merge blake hint - // .{ .pathname = "cairo_programs/blake2s_felts.json", .layout = "all_cairo" }, - // .{ .pathname = "cairo_programs/blake2s_hello_world_hash.json", .layout = "all_cairo" }, - // .{ .pathname = "cairo_programs/blake2s_integration_tests.json", .layout = "all_cairo" }, - // TODO: implement cairo keccak - // .{ .pathname = "cairo_programs/cairo_finalize_keccak_compiled.json", .layout = "all_cairo" }, - // .{ .pathname = "cairo_programs/cairo_finalize_keccak_block_size_1000.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/call_function_assign_param_by_name.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp1 error - // .{ .pathname = "cairo_programs/chained_ec_op.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/common_signature.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp1 error - // .{ .pathname = "cairo_programs/compare_arrays.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/compare_different_arrays.json", .layout = "all_cairo" }, - // TODO: NoDst error - // .{ .pathname = "cairo_programs/compare_greater_array.json", .layout = "all_cairo" }, - // TODO: NoDst error - // .{ .pathname = "cairo_programs/compare_lesser_array.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 - // .{ .pathname = "cairo_programs/compute_doubling_slope_v2.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 - // .{ .pathname = "cairo_programs/compute_slope_v2.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/dict.json", .layout = "all_cairo" }, - // TODO: RelocatableAdd error - // .{ .pathname = "cairo_programs/dict_integration_tests.json", .layout = "all_cairo" }, - // TODO: RelocatableAdd error - // .{ .pathname = "cairo_programs/dict_squash.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/dict_store_cast_ptr.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/dict_update.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/div_mod_n.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/ec_double_assign_new_x_v3.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/ec_double_slope.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/ec_double_v4.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/ec_negate.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp1 error - // .{ .pathname = "cairo_programs/ec_op.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/ec_recover.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/ed25519_ec.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/ed25519_field.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/efficient_secp256r1_ec.json", .layout = "all_cairo" }, - // TODO: merge blake hint - // .{ .pathname = "cairo_programs/example_blake2s.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/example_program.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/factorial.json", .layout = "plain" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/fast_ec_add_v2.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/fast_ec_add_v3.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/field_arithmetic.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/fibonacci.json", .layout = "plain" }, - // TODO: FailedToComputeOp0 error - // .{ .pathname = "cairo_programs/field_arithmetic.json", .layout = "all_cairo" }, - // TODO: merge blake hint - // .{ .pathname = "cairo_programs/finalize_blake2s.json", .layout = "all_cairo" }, - // TODO: merge blake hint - // .{ .pathname = "cairo_programs/finalize_blake2s_v2_hint.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/find_element.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/fq.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp1 error - // .{ .pathname = "cairo_programs/fq_test.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/function_return.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/function_return_if_print.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/function_return_to_variable.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/garaga.json", .layout = "all_cairo" }, - // TODO: FailedToComputeOp1 error - // .{ .pathname = "cairo_programs/highest_bitlen.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/if_and_prime.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/if_in_function.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/if_list.json", .layout = "all_cairo" }, - // TODO: UnknownOp0 error - // .{ .pathname = "cairo_programs/if_reloc_equal.json", .layout = "all_cairo" }, - - // .{ .pathname = "cairo_programs/keccak_compiled.json", .layout = "all_cairo" }, - .{ .pathname = "cairo_programs/keccak_builtin.json", .layout = "all_cairo" }, - // .{ .pathname = "cairo_programs/keccak_integration_tests.json", .layout = "all_cairo" }, - // .{ .pathname = "cairo_programs/keccak_copy_inputs.json", .layout = "all_cairo" }, - }; + // Refactor: Handle command line arguments + const args = try std.process.argsAlloc(allocator); + defer std.process.argsFree(allocator, args); + + var test_files_list: ?[]const u8 = null; + if (args.len > 1) { + // TODO: Parse command-line arguments more rigorously with a CLI utility + test_files_list = args[1]; + } + + var cairo_programs: []CairoTestProgram = undefined; // This will be defined after reading from the test file or defaulting to all tests. + + // If there are specific tests to run, read them from the file. + if (test_files_list) |list| { + var file = try std.fs.cwd().openFile(list, .{}); + defer file.close(); + const fileSize = try file.getEndPos(); + const buffer = try allocator.alloc(u8, fileSize); + defer allocator.free(buffer); + + const bytes_read = try file.read(buffer); + // TODO add error handing here for empty file case + if (bytes_read == 0) return; + + // Split the buffer by new lines and create cairo_programs array. + // Split the buffer by new lines and collect lines into a vector because we can't get .len from an iterator. + var lines = std.ArrayList([]const u8).init(allocator); + defer lines.deinit(); // Ensure allocated memory is freed appropriately + + // Splitting the buffer into lines and adding them to 'lines' ArrayList. + var splitIterator = std.mem.split(u8, buffer, "\n"); + while (splitIterator.next()) |line| { + try lines.append(line); + } + + // Now, lines.items gives us the array and lines.len gives us the count. + cairo_programs = try allocator.alloc(CairoTestProgram, lines.items.len); + + for (lines.items, 0..) |line, i| { + cairo_programs[i] = CairoTestProgram{ + .pathname = try std.fmt.allocPrint(allocator, "cairo_programs/{s}.json", .{line}), + .layout = "all_cairo", // Assuming 'all_cairo' layout for simplicity; adjust if necessary. + .extensive_hints = false, + }; + } + + } else { + // Default set of cairo_programs if no command line arguments are provided + // this is just for demonstration purposes + var temp_array = [_]CairoTestProgram{ + .{ .pathname = "cairo_programs/abs_value_array_compiled.json", .layout = "all_cairo" }, + // TODO: UnknownOp0 error + // .{ .pathname = "cairo_programs/array_sum_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/assert_lt_felt.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/assert_250_bit_element_array_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/assert_le_felt_hint_compiled.json", .layout = "all_cairo" }, + // .{ .pathname = "cairo_programs/assert_le_felt_old_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/assert_nn_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/assert_not_zero_compiled.json", .layout = "all_cairo" }, + // TODO: merge bigint hint + // .{ .pathname = "cairo_programs/bigint_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/big_struct_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/bitand_hint_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/bitwise_output_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/bitwise_builtin_test.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/bitwise_recursion_compiled.json", .layout = "all_cairo" }, + // TODO: merge blake hint + // .{ .pathname = "cairo_programs/blake2s_felts.json", .layout = "all_cairo" }, + // .{ .pathname = "cairo_programs/blake2s_hello_world_hash.json", .layout = "all_cairo" }, + // .{ .pathname = "cairo_programs/blake2s_integration_tests.json", .layout = "all_cairo" }, + // TODO: implement cairo keccak + // .{ .pathname = "cairo_programs/cairo_finalize_keccak_compiled.json", .layout = "all_cairo" }, + // .{ .pathname = "cairo_programs/cairo_finalize_keccak_block_size_1000.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/call_function_assign_param_by_name.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp1 error + // .{ .pathname = "cairo_programs/chained_ec_op.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/common_signature.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp1 error + // .{ .pathname = "cairo_programs/compare_arrays.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/compare_different_arrays.json", .layout = "all_cairo" }, + // TODO: NoDst error + // .{ .pathname = "cairo_programs/compare_greater_array.json", .layout = "all_cairo" }, + // TODO: NoDst error + // .{ .pathname = "cairo_programs/compare_lesser_array.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 + // .{ .pathname = "cairo_programs/compute_doubling_slope_v2.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 + // .{ .pathname = "cairo_programs/compute_slope_v2.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/dict.json", .layout = "all_cairo" }, + // TODO: RelocatableAdd error + // .{ .pathname = "cairo_programs/dict_integration_tests.json", .layout = "all_cairo" }, + // TODO: RelocatableAdd error + // .{ .pathname = "cairo_programs/dict_squash.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/dict_store_cast_ptr.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/dict_update.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/div_mod_n.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/ec_double_assign_new_x_v3.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/ec_double_slope.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/ec_double_v4.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/ec_negate.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp1 error + // .{ .pathname = "cairo_programs/ec_op.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/ec_recover.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/ed25519_ec.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/ed25519_field.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/efficient_secp256r1_ec.json", .layout = "all_cairo" }, + // TODO: merge blake hint + // .{ .pathname = "cairo_programs/example_blake2s.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/example_program.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/factorial.json", .layout = "plain" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/fast_ec_add_v2.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/fast_ec_add_v3.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/field_arithmetic.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/fibonacci.json", .layout = "plain" }, + // TODO: FailedToComputeOp0 error + // .{ .pathname = "cairo_programs/field_arithmetic.json", .layout = "all_cairo" }, + // TODO: merge blake hint + // .{ .pathname = "cairo_programs/finalize_blake2s.json", .layout = "all_cairo" }, + // TODO: merge blake hint + // .{ .pathname = "cairo_programs/finalize_blake2s_v2_hint.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/find_element.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/fq.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp1 error + // .{ .pathname = "cairo_programs/fq_test.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/function_return.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/function_return_if_print.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/function_return_to_variable.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/garaga.json", .layout = "all_cairo" }, + // TODO: FailedToComputeOp1 error + // .{ .pathname = "cairo_programs/highest_bitlen.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/if_and_prime.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/if_in_function.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/if_list.json", .layout = "all_cairo" }, + // TODO: UnknownOp0 error + // .{ .pathname = "cairo_programs/if_reloc_equal.json", .layout = "all_cairo" }, + + // .{ .pathname = "cairo_programs/keccak_compiled.json", .layout = "all_cairo" }, + .{ .pathname = "cairo_programs/keccak_builtin.json", .layout = "all_cairo" }, + // .{ .pathname = "cairo_programs/keccak_integration_tests.json", .layout = "all_cairo" }, + // .{ .pathname = "cairo_programs/keccak_copy_inputs.json", .layout = "all_cairo" }, + }; + cairo_programs = temp_array[0..]; + + } + + + var ok_count: usize = 0; var fail_count: usize = 0; From 8b4b344bff380b32c62c170ec6cb28b703d46f1f Mon Sep 17 00:00:00 2001 From: Johann Date: Mon, 11 Mar 2024 13:35:46 -0700 Subject: [PATCH 2/2] a push just for the linter god --- src/integration_tests.zig | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/integration_tests.zig b/src/integration_tests.zig index ce06b054..ae554b45 100644 --- a/src/integration_tests.zig +++ b/src/integration_tests.zig @@ -6,9 +6,9 @@ const CairoRunner = @import("vm/runners/cairo_runner.zig").CairoRunner; const HintProcessor = @import("./hint_processor/hint_processor_def.zig").CairoVMHintProcessor; const CairoTestProgram = struct { - pathname: []const u8, - layout: []const u8, - extensive_hints: bool = false, + pathname: []const u8, + layout: []const u8, + extensive_hints: bool = false, }; pub fn main() !void { @@ -26,7 +26,7 @@ pub fn main() !void { test_files_list = args[1]; } - var cairo_programs: []CairoTestProgram = undefined; // This will be defined after reading from the test file or defaulting to all tests. + var cairo_programs: []CairoTestProgram = undefined; // This will be defined after reading from the test file or defaulting to all tests. // If there are specific tests to run, read them from the file. if (test_files_list) |list| { @@ -35,7 +35,7 @@ pub fn main() !void { const fileSize = try file.getEndPos(); const buffer = try allocator.alloc(u8, fileSize); defer allocator.free(buffer); - + const bytes_read = try file.read(buffer); // TODO add error handing here for empty file case if (bytes_read == 0) return; @@ -61,7 +61,6 @@ pub fn main() !void { .extensive_hints = false, }; } - } else { // Default set of cairo_programs if no command line arguments are provided // this is just for demonstration purposes @@ -163,18 +162,14 @@ pub fn main() !void { .{ .pathname = "cairo_programs/if_list.json", .layout = "all_cairo" }, // TODO: UnknownOp0 error // .{ .pathname = "cairo_programs/if_reloc_equal.json", .layout = "all_cairo" }, - + // .{ .pathname = "cairo_programs/keccak_compiled.json", .layout = "all_cairo" }, .{ .pathname = "cairo_programs/keccak_builtin.json", .layout = "all_cairo" }, // .{ .pathname = "cairo_programs/keccak_integration_tests.json", .layout = "all_cairo" }, // .{ .pathname = "cairo_programs/keccak_copy_inputs.json", .layout = "all_cairo" }, }; - cairo_programs = temp_array[0..]; - + cairo_programs = temp_array[0..]; } - - - var ok_count: usize = 0; var fail_count: usize = 0;