cairo-vm icon indicating copy to clipboard operation
cairo-vm copied to clipboard

Remove use of `usize` from the VM core struct

Open tdelabro opened this issue 1 year ago • 12 comments

Problem

The VM execution should be deterministic regardless of if it is executed on a 16, 32 or 64 bit processor. Having usize being used in order to define some of the core VM types (segment offset, ap, fp) is problematic because it could lead to different execution on different machines. Which, in a decentralized blockchain architecture will lead to hard forks.

Recomandation

  1. Get rid of all the usize in the cairo-vm stuct. I think it's still okay to use usize when we call language method like len() but we should convert those as soon as possible to types that have a fixed behaviour regardless of the machine.

  2. Ban the use of as. as silence the value overflows. This is not something we want at all in a blockchain context. We should use the very explicit try_from/into instead and handle errors accordingly.

Impl

I would reccomand we introduce a new wrapper struct: SegmentOffset(u64) that will be used instead of usize in all Relocatable, ap and fp. This will require impl a lot of conversion methods on it, but it will give us the level of security we need for such a core component of our infrastructure.

tdelabro avatar Nov 28 '23 15:11 tdelabro