Skip to content

Another attempt on SafeMath assert->require but for a different reason #1120

@leonardoalt

Description

@leonardoalt

The different reason is the formal verification module we're building in Solidity. I'll start with a general argument from my side, then I'll talk about the FV module.

I've read the multiple discussions in different issues/PRs, and first of all I need to say that I do disagree with having asserts in SafeMath. What the code does is filtering inputs, therefore requires should be used.
I've also read that the idea is that the users should filter their input, and SafeMath would use the assertions to enforce no overflow. Well, nobody does it, and the reason is that the asserts in SafeMath already do kind of the same, so why would people bother? Moreover, if users filter their inputs with require(b <= a), for example, then having an extra assert(b <= a) from SafeMath's sub is just a waste of gas.

Assuming that users do not filter their input and rely on SafeMath to guarantee no underflows/overflows, this breaks formal verification. The reason is that the verifier sees no filtering (no assumptions on the input), and then sees the assertion assert(b <= a) which it tries to prove, since it is a verification target. The verifier will report that the assertion is not true for all cases, and easily provide counterexamples, since there are no assumptions on a and b.

So, from a verification perspective it would also make more sense to have requires in SafeMath, such that the underflow/overflow cases would be simply filtered out of the execution path, and the rest of the path would have the safe invariant due to an assumption, not a target.

I'd love to hear your thoughts on it, even though it's a repetitive discussion...

Metadata

Metadata

Assignees

No one assigned

    Labels

    contractsSmart contract code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions