Skip to content

Commit

Permalink
Add integration tests (#445)
Browse files Browse the repository at this point in the history
* Add finalize method, calculation of builtin segment size, builtins parameters

* Add end_run method and helper methods, modify the builtins values to match layout small

* Fixed usage of constants in builtins, added checking range check limits

* remove vm traces

* Linter errors

* Debug finalization

* Debug range check limits calculation

* Fix builtins test errors

* Debug output segment finalisation, add annotations to what should be moved to the layout

* Modify mock integration test, add annotations to the functions

* Add annotations

* Add new annotations

* Add layouts

* Temporarily modify integration testsuite

* Fix layout problems

* removed unused layout plain

* Layout flag with layouts: plain, small, all_cairo (#435)

* Create layout flag with layouts plain and small

* Fix error handling

* Fix error handling

* Add layout flag to Nethermind vm in the integration tests

* Fix unit tests

* Fix builtins integration tests, add layouts to builtins tests, remove failing "slices" package

* Refactor code, remove unnecessary comments

* Fix split_int + add integration test

* Add sqrt integration test

* Add integration test for unsigned_div_rem

* Fix bug in MemContinueHint

* Fix unit test

* Add EC integration tests

* Fix scope update

* Replace unsupported "all_cairo" layout with "starknet_with_keccak"

* Lint the project

* Add builtin metadata and GetAllocatedSize to PoseidonBuiltin

* Add Poseidon Builtin to starknet_with_keccak layout

* Add blake tests

* Fixing blake bugs

* Fix integration tests for poseidon builtin

* Fix dict squash bugs

* Uncomment tests

* Uncomment tests

* Fix test

* Fix unit test

* More tests

* Add memcpy and memset integration tests

* Fix unit test

* Address first batch of comments

* Rename file

* Fix hinter.Immediate creation bug

* Fix GetAllocatedInstances bug

* Revert change

* Fix constant usage

* Fix RcUnits

* Fix merge issues

* Fix dictSquashCopyDictCode

* Fix range check fetch error

* Fix unit test

* Clean up tests

---------

Co-authored-by: MaksymMalicki <[email protected]>
Co-authored-by: MaksymMalicki <[email protected]>
Co-authored-by: Tristan <[email protected]>
  • Loading branch information
4 people authored Jun 26, 2024
1 parent 3c0fd0a commit 24924f2
Show file tree
Hide file tree
Showing 24 changed files with 425 additions and 152 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// inspired from the blake integration tests in the lambdaclass cairo-vm codebase

%builtins range_check bitwise

from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.cairo_blake2s.blake2s import blake2s
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin

func test_hash{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() {
alloc_locals;
let inputs: felt* = alloc();
assert inputs[0] = 'Hell';
assert inputs[1] = 'o Wo';
assert inputs[2] = 'rld';
let (local blake2s_ptr_start) = alloc();
let blake2s_ptr = blake2s_ptr_start;
let (output) = blake2s{range_check_ptr=range_check_ptr, blake2s_ptr=blake2s_ptr}(inputs, 9);
assert output.low = 219917655069954262743903159041439073909;
assert output.high = 296157033687865319468534978667166017272;
return ();
}

func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() {
test_hash();

return ();
}
70 changes: 70 additions & 0 deletions integration_tests/cairo_zero_hint_tests/dict_squash.small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
%builtins range_check

from starkware.cairo.common.default_dict import default_dict_new
from starkware.cairo.common.dict import dict_write, dict_update, dict_squash
from starkware.cairo.common.squash_dict import squash_dict
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.dict_access import DictAccess

func test_squash_dict{range_check_ptr}() -> () {
alloc_locals;

let (dict_start: DictAccess*) = alloc();

assert dict_start[0] = DictAccess(key=0, prev_value=100, new_value=100);
assert dict_start[1] = DictAccess(key=1, prev_value=50, new_value=50);
assert dict_start[2] = DictAccess(key=0, prev_value=100, new_value=200);
assert dict_start[3] = DictAccess(key=1, prev_value=50, new_value=100);
assert dict_start[4] = DictAccess(key=0, prev_value=200, new_value=300);
assert dict_start[5] = DictAccess(key=1, prev_value=100, new_value=150);

let dict_end = dict_start + 6 * DictAccess.SIZE;

// (dict_start, dict_end) now represents the dictionary
// {0: 100, 1: 50, 0: 200, 1: 100, 0: 300, 1: 150}.

// Squash the dictionary from an array of 6 DictAccess structs
// to an array of 2, with a single DictAccess entry per key.
let (local squashed_dict_start: DictAccess*) = alloc();
let (squashed_dict_end) = squash_dict{range_check_ptr=range_check_ptr}(
dict_start, dict_end, squashed_dict_start
);

// Check the values of the squashed_dict
// should be: {0: (100, 300), 1: (50, 150)}
assert squashed_dict_start[0] = DictAccess(key=0, prev_value=100, new_value=300);
assert squashed_dict_start[1] = DictAccess(key=1, prev_value=50, new_value=150);

return ();
}

func test_dict_squash{range_check_ptr}() -> () {
let (dict_start) = default_dict_new(17);
let dict_end = dict_start;
dict_write{dict_ptr=dict_end}(0, 1);
dict_write{dict_ptr=dict_end}(1, 10);
dict_write{dict_ptr=dict_end}(2, -2);
dict_update{dict_ptr=dict_end}(0, 1, 2);
dict_update{dict_ptr=dict_end}(0, 2, 3);
dict_update{dict_ptr=dict_end}(0, 3, 4);
dict_update{dict_ptr=dict_end}(1, 10, 15);
dict_update{dict_ptr=dict_end}(1, 15, 20);
dict_update{dict_ptr=dict_end}(1, 20, 25);
dict_update{dict_ptr=dict_end}(2, -2, -4);
dict_update{dict_ptr=dict_end}(2, -4, -8);
dict_update{dict_ptr=dict_end}(2, -8, -16);
let (squashed_dict_start, squashed_dict_end) = dict_squash{range_check_ptr=range_check_ptr}(
dict_start, dict_end
);
assert squashed_dict_end[0] = DictAccess(key=0, prev_value=1, new_value=4);
assert squashed_dict_end[1] = DictAccess(key=1, prev_value=10, new_value=25);
assert squashed_dict_end[2] = DictAccess(key=2, prev_value=-2, new_value=-16);
return ();
}

func main{range_check_ptr}() {
test_squash_dict();
test_dict_squash();

return ();
}
94 changes: 94 additions & 0 deletions integration_tests/cairo_zero_hint_tests/ec.small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
%builtins range_check

from starkware.cairo.common.cairo_secp.bigint import BigInt3
from starkware.cairo.common.cairo_secp.ec import EcPoint, ec_negate, compute_doubling_slope, compute_slope, ec_double, fast_ec_add

func test_ec_negate{range_check_ptr}() {
let p = EcPoint(BigInt3(1, 2, 3), BigInt3(1, 2, 3));
let (res) = ec_negate(p);

let p = EcPoint(
BigInt3(12424, 53151, 363737),
BigInt3(77371252455336267181195244, 77371252455336267181195261, 9671406556917033397649404),
);
let (res) = ec_negate(p);

return ();
}

func test_compute_doubling_slope{range_check_ptr}() {
let p = EcPoint(BigInt3(1, 2, 3), BigInt3(1, 2, 3));
let (res) = compute_doubling_slope(p);

let p = EcPoint(
BigInt3(12424, 53151, 363737),
BigInt3(77371252455336267181195244, 77371252455336267181195261, 9671406556917033397649404),
);
let (res) = compute_doubling_slope(p);

return ();
}

func test_compute_slope{range_check_ptr}() {
let p1 = EcPoint(BigInt3(1, 2, 3), BigInt3(1, 2, 3));
let p2 = EcPoint(
BigInt3(12424, 53151, 363737),
BigInt3(77371252455336267181195244, 77371252455336267181195261, 9671406556917033397649404),
);
let (res) = compute_slope(p1, p2);

let p1 = EcPoint(
BigInt3(17117865558768631194064792, 12501176021340589225372855, 9198697782662356105779718),
BigInt3(6441780312434748884571320, 57953919405111227542741658, 5457536640262350763842127)
);
let p2 = EcPoint(
BigInt3(12424, 53151, 363737),
BigInt3(77371252458936267181195765, 77561252455336267181195987, 9674506556917033397649657),
);
let (res) = compute_slope(p1, p2);

return ();
}

func test_ec_double{range_check_ptr}() {
let p = EcPoint(BigInt3(1, 2, 3), BigInt3(1, 2, 3));
let (res) = ec_double(p);

let p = EcPoint(
BigInt3(12424, 53151, 363737),
BigInt3(77371252455336267181195244, 77371252455336267181195261, 9671406556917033397649404),
);
let (res) = ec_double(p);

return ();
}

func test_fast_ec_add{range_check_ptr}() {
let p1 = EcPoint(BigInt3(1, 2, 3), BigInt3(1, 2, 3));
let p2 = EcPoint(
BigInt3(12424, 53151, 363737),
BigInt3(77371252455336267181195244, 77371252455336267181195261, 9671406556917033397649404),
);
let (res) = fast_ec_add(p1, p2);

let p1 = EcPoint(
BigInt3(17117865558768631194064792, 12501176021340589225372855, 9198697782662356105779718),
BigInt3(6441780312434748884571320, 57953919405111227542741658, 5457536640262350763842127)
);
let p2 = EcPoint(
BigInt3(12424, 53151, 363737),
BigInt3(77371252458936267181195765, 77561252455336267181195987, 9674506556917033397649657),
);
let (res) = fast_ec_add(p1, p2);

return ();
}

func main{range_check_ptr}() {
test_ec_negate();
test_compute_doubling_slope();
test_compute_slope();
test_ec_double();
test_fast_ec_add();
return ();
}
21 changes: 21 additions & 0 deletions integration_tests/cairo_zero_hint_tests/memcpy.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.registers import get_fp_and_pc

func main() {
alloc_locals;
let (__fp__, _) = get_fp_and_pc();
local numbers: (felt, felt, felt) = (1, 2, 3);
let dest: felt* = alloc();
memcpy(dst=dest, src=&numbers, len=3);
assert numbers[0] = dest[0];
assert numbers[1] = dest[1];
assert numbers[2] = dest[2];
return ();
}
30 changes: 30 additions & 0 deletions integration_tests/cairo_zero_hint_tests/memset.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.memset import memset
from starkware.cairo.common.bool import TRUE, FALSE

func check_array(array: felt*, value: felt, array_length: felt, iterator: felt) -> (r: felt) {
if (iterator == array_length) {
return (TRUE,);
}
if (array[iterator] != value) {
return (FALSE,);
}
return check_array(array, value, array_length, iterator + 1);
}

func main() {
alloc_locals;
let (local strings: felt*) = alloc();
memset(strings, 'Test', 20);
let check_string: felt = check_array(strings, 'Test', 20, 0);
assert check_string = TRUE;
assert strings[20] = 'can insert new value';
let numbers: felt* = alloc();
memset(numbers, 10, 100);
let check_string: felt = check_array(numbers, 10, 100, 0);
assert check_string = TRUE;
assert numbers[100] = 11;
return ();
}
30 changes: 30 additions & 0 deletions integration_tests/cairo_zero_hint_tests/set_add.small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%builtins range_check

from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.set import set_add

struct MyStruct {
a: felt,
b: felt,
}

func main{range_check_ptr}() {
alloc_locals;

// An array containing two structs.
let (local my_list: MyStruct*) = alloc();
assert my_list[0] = MyStruct(a=1, b=3);
assert my_list[1] = MyStruct(a=5, b=7);

// Suppose that we want to add the element
// MyStruct(a=2, b=3) to my_list, but only if it is not already
// present (for the purpose of the example the contents of the
// array are known, but this doesn't have to be the case)
let list_end: felt* = &my_list[2];
let (new_elm: MyStruct*) = alloc();
assert new_elm[0] = MyStruct(a=2, b=3);
set_add{set_end_ptr=list_end}(set_ptr=my_list, elm_size=MyStruct.SIZE, elm_ptr=new_elm);
assert my_list[2] = MyStruct(a=2, b=3);

return ();
}
25 changes: 25 additions & 0 deletions integration_tests/cairo_zero_hint_tests/split_int.small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%builtins range_check

from starkware.cairo.common.math import split_int
from starkware.cairo.common.alloc import alloc

func main{range_check_ptr: felt}() {
alloc_locals;
let value = 9876543210;
let n = 10;
let base = 10;
let bound = 100;
let output: felt* = alloc();
split_int(value, n, base, bound, output);
assert output[0] = 0;
assert output[1] = 1;
assert output[2] = 2;
assert output[3] = 3;
assert output[4] = 4;
assert output[5] = 5;
assert output[6] = 6;
assert output[7] = 7;
assert output[8] = 8;
assert output[9] = 9;
return ();
}
23 changes: 23 additions & 0 deletions integration_tests/cairo_zero_hint_tests/sqrt.small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%builtins range_check

from starkware.cairo.common.math import sqrt

func main{range_check_ptr: felt}() {
let val1 = sqrt(0);
assert val1 = 0;

let val2 = sqrt(1);
assert val2 = 1;

let val3 = sqrt(1024);
assert val3 = 32;

let val4 = sqrt(99999);
assert val4 = 316;

// 2**250 - 1
let val5 = sqrt(1809251394333065553493296640760748560207343510400633813116524750123642650623);
assert val5 = 42535295865117307932921825928971026431;

return ();
}
38 changes: 0 additions & 38 deletions integration_tests/cairo_zero_hint_tests/squash_dict.cairo

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%builtins range_check

from starkware.cairo.common.math import unsigned_div_rem

func main{range_check_ptr: felt}() {
let (q1, r1) = unsigned_div_rem(100, 5);
assert q1 = 20;
assert r1 = 0;

let (q2, r2) = unsigned_div_rem(100, 21);
assert q2 = 4;
assert r2 = 16;

let (q3, r3) = unsigned_div_rem(0, 1);
assert q3 = 0;
assert r3 = 0;

// 2**128
let (q4, r4) = unsigned_div_rem(340282366920938463463374607431768211456, 1234567);
assert q4 = 275628918415070598406870269035028;
assert r4 = 798580;

return ();
}
Loading

0 comments on commit 24924f2

Please sign in to comment.