cairo-vm-go
cairo-vm-go copied to clipboard
Handling globals() in Cairo0 hints
This issue is only a writeup, on how other VMs handle fetching values from globals() in the Cairo0 hints.
In the Python VM, the initialize_vm method in cairo_runner provides a possibility to pass an optional argument, called static_locals. It is described as:
static_locals - dictionary holding static values for execution. They are available in all scopes., meaning that when the value is passed inside of that object, they will be present in each hint execution scope and used.
static_values are always updated in the constructor of the VirtualMachine, by these values and the ones provided in the constructor. static_locals are then always included in each step during, in the hint execution as globals.
My concern with this solution, is that there is no flag/possibility to pass the static_locals dynamically. They are loaded programatically for example here, which is in the entirely different package, called business logic on a wrapper class which extends from the basic CairoRunner.
What is also concerning, is that is can't find a place, where those static values are loaded into the scopes of rust and even go VMs. During hint execution, lambda VMs fetch constants like __usort_max_size from the current scope, but I don't see any entrypoint where those values are loaded, so that will always result in an error - ValueNotInScope.
After checking both solutions, there are a couple ways of handling this. We could either skip it entirely, since in other implementations there is no way of passing those values dynamically. We could load them from JSON and store them in some separate struct, maybe a field in hintrunner. We could also hardcode the values from the business logic package, but I'm not confident about that solution, since those values are passed externally and clearly are picked for some specific usecase.
@MaksymMalicki i took some times to check all other cairo VM implementations. Agree with all you said.
globals() is used to fetch some values in 5 hints:
- #252 :
if '__find_element_index' in globals(): - #253 :
if '__find_element_max_size' in globals(): - #294 and #288 :
if '__dict_manager' not in globals(): - #295 :
if '__squash_dict_max_size' in globals(): - #244 :
vm_enter_scope(dict(__usort_max_size = globals().get('__usort_max_size'))) - #286 :
if '__keccak_max_size' in globals():
Just sent a message to one of the lead of the lambda rust cairo vm to know how they inject these variables in their exec_scopes
Waiting for final confirmation but it seems that team from lambda rust cairo-vm answered that these variables are always accessible in globals()
From Lambdaclass : nothing adds variables in globals() "by default" during execution, so all checks related to globals() should not be done in the default Cairo VM
@cicr99 @rodrigo-pino
We hardcoded all values that can be found in globals() in our VM. With current information we have, if we dont set these values, there are potential safety issue.
Cairo Zero will be deprecated on the Starknet network.
Closing this issue for now.