#100DaysOfSolidity Unchecked Math in Solidity #45

#100DaysOfSolidity Series 045 “Unchecked Math” : Boosting Efficiency and Saving Gas ⚡️

Solidity Academy
4 min readJul 21, 2023

In this article, we’ll explore the fascinating world of “Unchecked Math” in Solidity 0.8. Learn how this powerful feature can boost efficiency, save gas costs, and enhance your Solidity smart contracts. Let’s dive in!

#100DaysOfSolidity Series 045 “Unchecked Math” : Boosting Efficiency and Saving Gas ⚡️

Solidity 0.8 introduced a game-changing feature called “Unchecked Math” that allows developers to disable overflow and underflow checks on arithmetic operations. By utilizing the `unchecked` keyword, you can perform arithmetic calculations without throwing errors. 🧮 This not only enhances efficiency but also saves valuable gas in the process. In this article, we’ll delve into the concept of Unchecked Math, discuss its benefits, and provide code samples to illustrate its usage.

## The Need for Unchecked Math ⚠️

Earlier versions of Solidity would throw errors when arithmetic operations resulted in overflow or underflow. While this ensured safety, it also increased gas costs. Checking for overflow and underflow necessitates additional computational steps, leading to higher gas consumption for each arithmetic operation.

To address this issue, Solidity 0.8 introduced the `unchecked` keyword, which allows developers to disable overflow and underflow checks. By leveraging Unchecked Math, you can significantly reduce gas costs and make your contracts more efficient. 🚀

## Benefits of Unchecked Math 🌟

Utilizing Unchecked Math in Solidity 0.8 offers several advantages:

1. **Gas Savings**: Disabling overflow and underflow checks with Unchecked Math eliminates the need for additional computational steps. This results in substantial gas savings, reducing the overall cost of executing your smart contracts.

2. **Improved Efficiency**: With fewer computational steps, Unchecked Math enhances the efficiency of arithmetic operations within Solidity contracts. This optimization is particularly beneficial when dealing with large-scale arithmetic calculations.

Now, let’s explore some code samples to understand how Unchecked Math works in Solidity 0.8. 🖥️

## Code Samples 💻

We’ll be using a sample contract called `UncheckedMath` to demonstrate various scenarios where Unchecked Math can be employed. Take a look at the contract code below:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract UncheckedMath {
function add(uint x, uint y) external pure returns (uint) {
// 22291 gas
// return x + y;
// 22103 gas
unchecked {
return x + y;
}
}
function sub(uint x, uint y) external pure returns (uint) {
// 22329 gas
// return x - y;
// 22147 gas
unchecked {
return x - y;
}
}
function sumOfCubes(uint x, uint y) external pure returns (uint) {
// Wrap complex math logic inside unchecked
unchecked {
uint x3 = x * x * x;
uint y3 = y * y * y;
return x3 + y3;
}
}
}

Within the `UncheckedMath` contract, we have three functions: `add`, `sub`, and `sumOfCubes`. Let’s analyze each of them to understand how Unchecked Math is implemented.

### Addition without Check ➕

The `add` function performs an addition operation between two input parameters, `x` and `y`. The version without Unchecked Math (commented out) consumes 22,291 gas, while the version utilizing Unchecked Math consumes only 22,103 gas. By disabling the check, we achieve a gas saving of 188 gas, resulting in more efficient contract execution. 📉

### Subtraction without Check ➖

The `sub` function subtracts the value of `y` from `x`. Similar to the `add` function, the version without Unchecked Math (commented out) consumes 22,329 gas, whereas the version using Unchecked Math consumes only 22,147 gas. Here again, by disabling the check, we achieve a gas saving of 182 gas.

### Complex Math Logic 🔄

The `sumOfCubes` function demonstrates a scenario where complex math logic is involved. Inside the `unchecked` block, the function calculates the cube of `x` and `y` separately and then returns their sum. By wrapping the entire complex logic inside the `unchecked` block, we ensure that the overflow and underflow checks are disabled throughout the calculation. This not only saves gas but also improves the efficiency of the contract.

## Conclusion 🎉

In this article, we explored the concept of Unchecked Math in Solidity 0.8 and its immense benefits. By disabling overflow and underflow checks using the `unchecked` keyword, you can optimize your Solidity smart contracts, achieve significant gas savings, and enhance their overall efficiency. 💪✨

However, it’s crucial to note that Unchecked Math removes safety checks. Therefore, it should be used judiciously and only when you are confident that the arithmetic operations will not lead to unintended consequences.

As a Solidity developer, understanding and utilizing features like Unchecked Math empowers you to write more efficient and cost-effective smart contracts. So go ahead, experiment with Unchecked Math, and optimize your Solidity code to deliver high-performance blockchain applications! 🚀🔥

Thank you for reading! If you found this article useful, don’t forget to give it a 👏 and share it with others. If you have any questions or feedback, please leave a comment below. Happy coding in Solidity! 💻💡

--

--

Solidity Academy

Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/ SEND US Your Products to Review! solidity101@gmail.com