Ignore Safe Math (unchecked) – But Know What You Do!

Published by Mario Oettler on

Last Updated on 24. March 2025 by Mario Oettler

Solidity checks integer underflows and overflows by default. These checks cost gas. By switching of these checks, it is possible to safe some gas.

The following code snipped shows the difference.

pragma solidity 0.8.29;

contract Checks{
    uint256 a = 1;
    uint256 b = 1;
    function uncheckedCalc() public{
        unchecked{
            a = a + 1;
        }
    }

    function checkedCalc() public{
        b = b + 1;
    }
}
Checked/uncheckedGas Costs
Checked (safe)5320
Unchecked (unsafe)5161

Caution: Switching off under or overflow checks can result in vulnerable contracts. So, make sure that these checks are not necessary.

Categories:

https://blockchain-academy.hs-mittweida.de/wp-content/uploads/2021/04/logo_bcam_rgb_gross.png

Welcome

Blockchain Academy

Continue with credential

No account yet?

Powered by Hidy

Register with Hidy


Register
Sign in

We need the following credential to register:

please select:

To create a new account, we need the following data from you:

We would appreciate the following additional information from you for the creation of your account:

Welcome

Blockchain Academy

Continue with credential

Already have an account?

Powered by Hidy

Sign in with Hidy


Register
Sign in

We need the following credential to log in:

please select: