Skip to content

Commit

Permalink
Change the Dictionary used in dict hints to store MaybeRelocatable
Browse files Browse the repository at this point in the history
…instead of `BigInt` (#687)

* Start get_traceback_entries + add convenience methos

* Add fn is_call_instruction

* add code

* Refactor code

* Clippy

* Add get_traceback method

* Fix get_error_attr_value

* Add traceback to VmException

* Make traceback non-optional

* Add tests for is_call_instruction

* Add traceback to error display

* Add test + fix logic for get_traceback_entries

* Code refactor

* Add one more test for get_traceback_entries

* Fix string format + add test for get_traceback

* Improve fn

* Add reference to is_call_instruction signature

* Add reference to immediate in decode_instruction + remove clone

* Fix hint_processor mutability in tests

* Add Location::get_location_marks

* Fix method to_string_with_contents

* Fix string format

* Fix string format

* Update traceback tests

* Add tests for Location::to_string_with_contents()

* Fix intermediate string format

* Fix test

* Add tests for Location::get_location_marks()

* Update VmException display

* Fix string format

* Fix string format

* Remove debug print

* Fix Display

* Implement Display for MaybeRelocatable

* Add real-case test for VmException Display

* Remove debug format from erros containing MaybeRelocatable and Relocatable

* Add tests for display implementation

* Update Changelog

* Clippy

* Remove unnecessary &

* Add hint location to InstructionLocation

* Use InstructionLocation instead of Location in insruction_locations field of Program

* Add hint location logic to get_location

* Add rought version of VirtualMachineError::Hint

* Add test for error display on HintError

* Add test for get_location with hint_index

* Start refactor

* Update changelog

* Finnish changing hint fns to HintError

* Update custom hint example

* Fix changelog format

* Add changelog entry for this PR

* Add non-typed helpers for ids variables

* Swap BigInt for MaybeRelocatable in Dictionary

* Update dict_hint_utils + dict macros in test_utils

* Fix tests

* Clippy

* Add test for dict_write with relocatable

* Add tests on hint_utils.rs

* Add tetss on hint_processor_utils.rs

* Add integration test for bug case

* Update changelog

* Fix eof

* Remove debug print
  • Loading branch information
fmoletta committed Jan 6, 2023
1 parent 9bf61ea commit 6be6863
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 155 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
* Add new error type `HintError` [#676](https://github.com/lambdaclass/cairo-rs/pull/676)
* Public Api changes:
* `HintProcessor::execute_hint()` now returns a `HintError` instead of a `VirtualMachineError`
* helper functions on `hint_processor_utils.rs` now return a `HintError`
* Helper functions on `hint_processor_utils.rs` now return a `HintError`

* Change the Dictionary used in dict hints to store MaybeRelocatable instead of BigInt [#687](https://github.com/lambdaclass/cairo-rs/pull/687)
* Public Api changes:
* `DictManager`, its dictionaries, and all dict module hints implemented in rust now use `MaybeRelocatable` for keys and values instead of `BigInt`
* Add helper functions that allow extracting ids variables as `MaybeRelocatable`: `get_maybe_relocatable_from_var_name` & `get_maybe_relocatable_from_reference`
* Change inner value type of dict-related `HintError` variants to `MaybeRelocatable`
40 changes: 40 additions & 0 deletions cairo_programs/dict_store_cast_ptr.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from starkware.cairo.common.dict_access import DictAccess
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.default_dict import default_dict_new
from starkware.cairo.common.dict import dict_write, dict_read

struct Structure {
a: felt,
b: felt,
c: felt,
}

func main() {
// Create dictionary
let (dictionary: DictAccess*) = default_dict_new(default_value=0);
// Create & initialize struct_pointer
let (struct_ptr: Structure*) = alloc();
assert struct_ptr[0] = Structure(1, 2, 3);
// Cast ptr to felt and store it in the dictionary
tempvar struct_ptr_cast_felt = cast(struct_ptr, felt);
dict_write{dict_ptr=dictionary}(key=0, new_value=struct_ptr_cast_felt);
// Read the casted ptr from the dictionary and compare it to the one we stored
let read_struct_ptr_cast_felt: felt = dict_read{dict_ptr=dictionary}(key=0);
assert struct_ptr_cast_felt = read_struct_ptr_cast_felt;
// Cast he ptr back to Structure* and check its value
let read_struct_ptr_cast_struct_ptr = cast(read_struct_ptr_cast_felt, Structure*);
assert struct_ptr = read_struct_ptr_cast_struct_ptr;
// Confirm that the ptr still leads to the data we initialized
assert read_struct_ptr_cast_struct_ptr[0].a = 1;
assert read_struct_ptr_cast_struct_ptr[0].b = 2;
assert read_struct_ptr_cast_struct_ptr[0].c = 3;
// Now we do the same, but we read the struct_ptr from the dictionary as a Struct*
// without an explicit cast
let read_struct_ptr: Structure* = dict_read{dict_ptr=dictionary}(key=0);
assert struct_ptr = read_struct_ptr;
// Confirm that the ptr still leads to the data we initialized
assert read_struct_ptr[0].a = 1;
assert read_struct_ptr[0].b = 2;
assert read_struct_ptr[0].c = 3;
return ();
}
Loading

0 comments on commit 6be6863

Please sign in to comment.