Counters.sol increment does not need overflow protection

Counters.sol increment does not need overflow protection

Counters.sol increment:

    function increment(Counter storage counter) internal {
        counter._value += 1;
    }

API Documentation:

https://docs.openzeppelin.com/contracts/2.x/api/drafts#Counters
Since it is not possible to overflow a 256 bit integer with increments of one, increment can skip the SafeMath overflow check, thereby saving gas. This does assume however correct usage, in that the underlying _value is never directly accessed.

It is not possible to overflow a 256 bit integer with increments of one because of the following:

Paraphrasing https://security.stackexchange.com/a/82412: overflowing 256-bit counters will be impossible until computers are built from something other than matter and occupy something other than space.

Also see: Radical Asymmetry

1 Like