Aritmetic operations overflow (integer overflow)

I read somewhere that Sophia doesn’t have problems with integer overflow (like Python or Erlang number types). Is it true?
If yes then we don’t need to wrap basic aritmetic operations into the functions (like in this tutorial: https://dev.aepps.com/tutorials/sophia-fungible-token-contract.html).

If not - then I would like to discuss the possibility to update the VM to protect against number overflow.

For reference: Michelson (Tezos Smart Contract language) has arbitrary precision for numbers.
Furthermore, the mutez type which is domain specific type for token values, represented as int64 protects against the overflow:

Operations are limited to prevent overflow and mixing them with other numerical types by mistake

Yes, Sophia is safe in this aspect, the following is from protocol/sophia.md:

All operations are safe , in the sense that they behave as the corresponding operations on arbitrary-size integers and fail with arithmetic_error if the result cannot be represented by a 256-bit signed word. For example, 2 ^ 255 fails rather than wrapping around to -2²⁵⁵.

The division and remained operations also throw an arithmetic error if the second argument is zero.

Thanks @hanssv

Then the tutorials should be updated.

I’ve found the text I was referring to in wearedevelopers article

But FATE only has integers and they can be of any size, so they will never wrap,” he explains. “So there’s all these things that become simpler and safer with FATE.”

Would be interesting to put all this design goals in one place (eg protocol/contracts/fate.md)

If you like I can make a PR.

Yeah, arithmetic safety was added in the first hard fork (Minerva) - this made the tutorial outdated.

Contributions are always welcome, there is never enough time to cover all bases!

2 Likes

Done, I’ve created few PRs.

2 Likes