cairo-vm
cairo-vm copied to clipboard
Remove use of `usize` from the VM core struct
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
-
Get rid of all the
usizein the cairo-vm stuct. I think it's still okay to useusizewhen we call language method likelen()but we should convert those as soon as possible to types that have a fixed behaviour regardless of the machine. -
Ban the use of
as.assilence the value overflows. This is not something we want at all in a blockchain context. We should use the very explicittry_from/intoinstead 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.