NethermindEth / cairo-vm-go

A virtual machine for Cairo written in Go
MIT License
73 stars 43 forks source link

Handling globals() in Cairo0 hints #455

Open MaksymMalicki opened 1 month ago

MaksymMalicki commented 1 month ago

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.

TAdev0 commented 3 weeks ago

@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:

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