NethermindEth / cairo-vm-go

A virtual machine for Cairo written in Go
MIT License
79 stars 49 forks source link

`UsortBody` hint is wrongly implemented #414

Closed TAdev0 closed 3 months ago

TAdev0 commented 3 months ago

UsortBody hint includes the following python code:

input_ptr = ids.input
....
for i in range(input_len):
    val = memory[input_ptr + i]
        ....

Our current implementation does:

inputBasePtr, err := hinter.ResolveAsAddress(vm, input)
...
for i := uint64(0); i < inputLenValue; i++ {
    val, err := vm.Memory.ReadFromAddressAsElement(inputBasePtr)
    ....

The problem is that memory[x] python code refers to the value at offset x on the execution segment.

Currently, our implementation actually does val =ids.input_ptr (+i) , which is not the expected behaviour.

inputBasePtr should be resolved as felt and used to specify an offset and read at that offset.

To confirm this, we can see that the AddressAp function in vm.go does the following:

func (ctx *Context) AddressAp() mem.MemoryAddress {
    return mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: ctx.Ap}
}

so basically, when the hint includes memory[ap] , we read (or write) at offset ap in the memory segment.