NethermindEth / cairo-vm-go

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

Fix how lastMemIndex is calculated in DecodeMemory #397

Closed har777 closed 3 months ago

har777 commented 3 months ago

Previously the max memory index in a memory file was calculated assuming that the last address in the file would represent the highest index.

// calculate the max memory index
lastContentInd := len(content) - (addrSize + feltSize)
lastMemIndex := binary.LittleEndian.Uint64(content[lastContentInd : lastContentInd+addrSize])

But this is not always the case. The memory file generated by the python vm can have the addresses in the file written out of order. That means the last address in the file need not be the largest address.

This PR changes that logic. Now the code will iterate over all the addresses in the memory file and find the largest one before initialising the decoded memory array and populating it.

Related to https://github.com/NethermindEth/cairo-vm-go/issues/395