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

Implement all SQUASH_DICT+ hints #218

Merged
merged 100 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 97 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
05e796e
First commit
toni-calvin Sep 8, 2023
cd72445
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into execut…
toni-calvin Sep 8, 2023
1ae3ab0
Add testing
toni-calvin Sep 12, 2023
adecc7d
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into execut…
toni-calvin Sep 12, 2023
548a741
Add test
toni-calvin Sep 12, 2023
d4195c9
Add tests
toni-calvin Sep 12, 2023
bd2a68c
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into execut…
toni-calvin Sep 12, 2023
2cfe447
Implement `ASSERT_NOT_EQUAL` hint
fmoletta Sep 12, 2023
8a25e3d
Add unit tests
fmoletta Sep 12, 2023
d0c3024
Add integration test
fmoletta Sep 12, 2023
02d6a1d
Add tests
toni-calvin Sep 13, 2023
30cb98d
Add tests
toni-calvin Sep 13, 2023
d7105f1
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into execut…
toni-calvin Sep 13, 2023
6c8d346
Add test
toni-calvin Sep 13, 2023
7fb733d
Add minor changes
toni-calvin Sep 13, 2023
ada05b6
Implement vm_enter_scope hint and test
toni-calvin Sep 13, 2023
acf7184
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into execut…
toni-calvin Sep 13, 2023
2fa4299
Changes attributes visibility and removes unused methods.
toni-calvin Sep 13, 2023
21f0280
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into vm-exi…
toni-calvin Sep 13, 2023
8c0161c
Merge branch 'execution-scopes' of github.com:lambdaclass/cairo-vm.go…
toni-calvin Sep 13, 2023
e6a46b9
Fix string
fmoletta Sep 13, 2023
e3a334a
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into assert…
fmoletta Sep 13, 2023
1073103
Add newline at eof
fmoletta Sep 13, 2023
8bef8be
Remove extra line
fmoletta Sep 13, 2023
f4375f1
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into dict-h…
fmoletta Sep 13, 2023
51f26d8
deletes duplicate code
toni-calvin Sep 13, 2023
fba1db2
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into vm-exi…
toni-calvin Sep 13, 2023
07b4d1d
Remove initial_dict from default-dict_new
fmoletta Sep 13, 2023
d12937b
fix test
fmoletta Sep 13, 2023
ceeafc0
Implement defaultDictNew
fmoletta Sep 13, 2023
a5a61b7
merge files
toni-calvin Sep 13, 2023
603bd0d
Integrate ExecutionScopes into hintProcessor logic
fmoletta Sep 13, 2023
a0866d0
add test
toni-calvin Sep 13, 2023
c7c8b49
Fixes + add test
fmoletta Sep 13, 2023
ba62930
Add test
fmoletta Sep 13, 2023
6b4f740
Remove commented fn
fmoletta Sep 13, 2023
8c80b5a
Implement dictRead
fmoletta Sep 13, 2023
1f7d9a8
Add tests
fmoletta Sep 13, 2023
4e35bf1
Add test
fmoletta Sep 13, 2023
199313e
Store DictTrackers by reference to allow modifications
fmoletta Sep 13, 2023
fe57bc2
Add test file
fmoletta Sep 13, 2023
3fe6ed7
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into dict-h…
fmoletta Sep 13, 2023
9c5d72e
Revert "Implement `ASSERT_NOT_EQUAL` hint"
fmoletta Sep 13, 2023
ba6e3a0
Revert "Add unit tests"
fmoletta Sep 13, 2023
dea259c
Remove leftovers from base branch
fmoletta Sep 13, 2023
705efbf
Remove leftovers from base branch
fmoletta Sep 13, 2023
1f12ebb
Fix eof
fmoletta Sep 13, 2023
613fc41
Implement DictUpdate
fmoletta Sep 13, 2023
f27926e
Add unit tests
fmoletta Sep 13, 2023
effc207
Add integration test
fmoletta Sep 13, 2023
7ba2fbc
Dont ignore errors
fmoletta Sep 13, 2023
d1f7ed4
Fix DictWrite hint
fmoletta Sep 13, 2023
59a6f0d
Merge branch 'dict-hint-1' into dict-hint-2
fmoletta Sep 13, 2023
3765aed
Add hint codes
fmoletta Sep 13, 2023
2003d91
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into dict-h…
fmoletta Sep 14, 2023
9cd9d46
Merge branch 'dict-hint-1' into dict-hint-2
fmoletta Sep 14, 2023
abf4af9
Merge branch 'dict-hint-2' into dict-hint-3
fmoletta Sep 14, 2023
3208fac
Implement DivRem, ModFloor, DivFloor
fmoletta Sep 14, 2023
011e296
solve conflicts
fmoletta Sep 14, 2023
40a475e
Add Felt.Cmp
fmoletta Sep 14, 2023
10212d2
Merge branch 'main' into dict-hint-1
fmoletta Sep 14, 2023
56cfd5f
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into vm-ent…
toni-calvin Sep 14, 2023
8a3e0d1
Implmenet squash_dict hint
fmoletta Sep 14, 2023
e148bc4
add test
toni-calvin Sep 14, 2023
bb37911
Start test
fmoletta Sep 14, 2023
3ccfc6c
Complete test
fmoletta Sep 14, 2023
bb8e871
Add fix + test
fmoletta Sep 14, 2023
f64f7ff
Add Fix + Test
fmoletta Sep 14, 2023
14fb716
Add tests
fmoletta Sep 14, 2023
434773d
Implement SQUASH_DICT_INNER_FIRST_ITERATION
fmoletta Sep 14, 2023
19d6bea
Add test
fmoletta Sep 14, 2023
9fc2bf7
Add test
fmoletta Sep 14, 2023
0ae2320
Implement hint
fmoletta Sep 14, 2023
11377da
Add test
fmoletta Sep 14, 2023
5f2b604
Test
fmoletta Sep 14, 2023
2ae857b
Add Hint + tests
fmoletta Sep 14, 2023
9e12934
Add tests
fmoletta Sep 14, 2023
2487454
Add hint + test
fmoletta Sep 14, 2023
d0648b2
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into vm-ent…
toni-calvin Sep 15, 2023
a27db21
remove ExecutionScopes.Data() method
toni-calvin Sep 15, 2023
1faa5c1
Merge branch 'dict-hint-1' into dict-hint-2
fmoletta Sep 15, 2023
259b35a
Add hint + test
fmoletta Sep 15, 2023
7bd668a
More tests
fmoletta Sep 15, 2023
989c868
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into dict-h…
fmoletta Sep 15, 2023
4763388
Merge branch 'dict-hint-2' of github.com:lambdaclass/cairo-vm.go into…
fmoletta Sep 15, 2023
77cb385
Merge branch 'dict-hint-2' into dict-hint-3
fmoletta Sep 15, 2023
dd6d556
Add integration test
fmoletta Sep 15, 2023
c1c02d8
Merge remote-tracking branch 'origin/vm-enter-scope-hint' into dict-h…
fmoletta Sep 15, 2023
ae00d5a
Fix test
fmoletta Sep 15, 2023
8d39fe5
Fix hint code
fmoletta Sep 15, 2023
d166edd
Fix string
fmoletta Sep 15, 2023
686491f
Check map is empty instead of map exists
fmoletta Sep 18, 2023
b617780
Fix tests
fmoletta Sep 18, 2023
1e30c0b
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into dict-h…
fmoletta Sep 18, 2023
2b2a447
Fix fmt
fmoletta Sep 18, 2023
27229b0
Fix comments
fmoletta Sep 18, 2023
39c7e3b
Move up check
fmoletta Sep 18, 2023
5e3ecb2
Implement `DICT_SQUASH_COPY_DICT`, `DICT_SQUASH_UPDATE_PTR` & `DICT_…
fmoletta Sep 18, 2023
2b5c416
Merge branch 'main' into dict-hint-3
fmoletta Sep 18, 2023
a2e4185
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into dict-h…
fmoletta Sep 19, 2023
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
33 changes: 33 additions & 0 deletions cairo_programs/squash_dict.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%builtins range_check

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 main{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 ();
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/pkg/errors v0.9.1
golang.org/x/crypto v0.12.0
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc
)

require (
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6S
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18 changes: 18 additions & 0 deletions pkg/hints/dict_hint_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@ const DICT_READ = "dict_tracker = __dict_manager.get_tracker(ids.dict_ptr)\ndict
const DICT_WRITE = "dict_tracker = __dict_manager.get_tracker(ids.dict_ptr)\ndict_tracker.current_ptr += ids.DictAccess.SIZE\nids.dict_ptr.prev_value = dict_tracker.data[ids.key]\ndict_tracker.data[ids.key] = ids.new_value"

const DICT_UPDATE = "# Verify dict pointer and prev value.\ndict_tracker = __dict_manager.get_tracker(ids.dict_ptr)\ncurrent_value = dict_tracker.data[ids.key]\nassert current_value == ids.prev_value, \\\n f'Wrong previous value in dict. Got {ids.prev_value}, expected {current_value}.'\n\n# Update value.\ndict_tracker.data[ids.key] = ids.new_value\ndict_tracker.current_ptr += ids.DictAccess.SIZE"

const SQUASH_DICT = "dict_access_size = ids.DictAccess.SIZE\naddress = ids.dict_accesses.address_\nassert ids.ptr_diff % dict_access_size == 0, \\\n 'Accesses array size must be divisible by DictAccess.SIZE'\nn_accesses = ids.n_accesses\nif '__squash_dict_max_size' in globals():\n assert n_accesses <= __squash_dict_max_size, \\\n f'squash_dict() can only be used with n_accesses<={__squash_dict_max_size}. ' \\\n f'Got: n_accesses={n_accesses}.'\n# A map from key to the list of indices accessing it.\naccess_indices = {}\nfor i in range(n_accesses):\n key = memory[address + dict_access_size * i]\n access_indices.setdefault(key, []).append(i)\n# Descending list of keys.\nkeys = sorted(access_indices.keys(), reverse=True)\n# Are the keys used bigger than range_check bound.\nids.big_keys = 1 if keys[0] >= range_check_builtin.bound else 0\nids.first_key = key = keys.pop()"

const SQUASH_DICT_INNER_SKIP_LOOP = "ids.should_skip_loop = 0 if current_access_indices else 1"

const SQUASH_DICT_INNER_FIRST_ITERATION = "current_access_indices = sorted(access_indices[key])[::-1]\ncurrent_access_index = current_access_indices.pop()\nmemory[ids.range_check_ptr] = current_access_index"

const SQUASH_DICT_INNER_CHECK_ACCESS_INDEX = "new_access_index = current_access_indices.pop()\nids.loop_temps.index_delta_minus1 = new_access_index - current_access_index - 1\ncurrent_access_index = new_access_index"

const SQUASH_DICT_INNER_CONTINUE_LOOP = "ids.loop_temps.should_continue = 1 if current_access_indices else 0"

const SQUASH_DICT_INNER_ASSERT_LEN_KEYS = "assert len(keys) == 0"

const SQUASH_DICT_INNER_LEN_ASSERT = "assert len(current_access_indices) == 0"

const SQUASH_DICT_INNER_USED_ACCESSES_ASSERT = "assert ids.n_used_accesses == len(access_indices[key])"

const SQUASH_DICT_INNER_NEXT_KEY = "assert len(keys) > 0, 'No keys left but remaining_accesses > 0.'\nids.next_key = key = keys.pop()"
18 changes: 18 additions & 0 deletions pkg/hints/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any,
return dictWrite(data.Ids, execScopes, vm)
case DICT_UPDATE:
return dictUpdate(data.Ids, execScopes, vm)
case SQUASH_DICT:
return squashDict(data.Ids, execScopes, vm)
case SQUASH_DICT_INNER_SKIP_LOOP:
return squashDictInnerSkipLoop(data.Ids, execScopes, vm)
case SQUASH_DICT_INNER_FIRST_ITERATION:
return squashDictInnerFirstIteration(data.Ids, execScopes, vm)
case SQUASH_DICT_INNER_CHECK_ACCESS_INDEX:
return squashDictInnerCheckAccessIndex(data.Ids, execScopes, vm)
case SQUASH_DICT_INNER_CONTINUE_LOOP:
return squashDictInnerContinueLoop(data.Ids, execScopes, vm)
case SQUASH_DICT_INNER_ASSERT_LEN_KEYS:
return squashDictInnerAssertLenKeys(execScopes)
case SQUASH_DICT_INNER_LEN_ASSERT:
return squashDictInnerLenAssert(execScopes)
case SQUASH_DICT_INNER_USED_ACCESSES_ASSERT:
return squashDictInnerUsedAccessesAssert(data.Ids, execScopes, vm)
case SQUASH_DICT_INNER_NEXT_KEY:
return squashDictInnerNextKey(data.Ids, execScopes, vm)
case VM_EXIT_SCOPE:
return vm_exit_scope(execScopes)
case ASSERT_NOT_EQUAL:
Expand Down
Loading