ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Solidity SafeMath Integer Addition

Solidity

Goal -- WPM

Ready
Exercise Algorithm Area
1pragma solidity ^0.8.0;
2
3contract SafeMath {
4
5/**
6* @dev Adds two unsigned integers, reverting on overflow.
7* This is equivalent to `a + b`, where any overflow is reverted.
8*/
9function add(uint256 a, uint256 b) internal pure returns (uint256) {
10uint256 c = a + b;
11require(c >= a, "SafeMath: addition overflow");
12return c;
13}
14
15/**
16* @dev Subtracts two unsigned integers, reverting on underflow.
17* This is equivalent to `a - b`, where any underflow is reverted.
18*/
19function sub(uint256 a, uint256 b) internal pure returns (uint256) {
20require(b <= a, "SafeMath: subtraction underflow");
21uint256 c = a - b;
22return c;
23}
24
25/**
26* @dev Multiplies two unsigned integers, reverting on overflow.
27* This is equivalent to `a * b`, where any overflow is reverted.
28*/
29function mul(uint256 a, uint256 b) internal pure returns (uint256) {
30if (a == 0) {
31return 0;
32}
33uint256 c = a * b;
34require(c / a == b, "SafeMath: multiplication overflow");
35return c;
36}
37
38/**
39* @dev Divides two unsigned integers, reverting on division by zero.
40* This is equivalent to `a / b`, where any division by zero is reverted.
41*/
42function div(uint256 a, uint256 b) internal pure returns (uint256) {
43require(b != 0, "SafeMath: division by zero");
44uint256 c = a / b;
45return c;
46}
47
48/**
49* @dev Returns the remainder of division, reverting when dividing by zero.
50* This is equivalent to `a % b`, where any division by zero is reverted.
51*/
52function mod(uint256 a, uint256 b) internal pure returns (uint256) {
53require(b != 0, "SafeMath: modulo by zero");
54return a % b;
55}
56}
Algorithm description viewbox

Solidity SafeMath Integer Addition

Algorithm description:

This Solidity contract provides safe arithmetic operations for unsigned integers. It prevents common overflow and underflow errors that can occur in smart contracts, ensuring that calculations do not wrap around unexpectedly. This is crucial for financial applications and any contract where precise numerical integrity is required.

Algorithm explanation:

The `SafeMath` contract implements several utility functions for basic arithmetic operations: `add`, `sub`, `mul`, `div`, and `mod`. Each function includes checks to prevent common integer vulnerabilities. For addition, it checks if the result `c` is less than `a`, which indicates an overflow. For subtraction, it ensures `b` is not greater than `a` to prevent underflow. Multiplication checks if `c / a` equals `b`, catching overflows. Division and modulo operations explicitly check for division by zero. These checks ensure that any operation that would result in an invalid state will cause the transaction to revert, maintaining the integrity of the contract's state. The time complexity for each operation is O(1) as they involve a fixed number of arithmetic operations and comparisons. The space complexity is also O(1).

Pseudocode:

Function add(a, b):
  c = a + b
  If c < a:
    Revert "Overflow"
  Return c

Function sub(a, b):
  If b > a:
    Revert "Underflow"
  c = a - b
  Return c

Function mul(a, b):
  If a == 0:
    Return 0
  c = a * b
  If c / a != b:
    Revert "Overflow"
  Return c

Function div(a, b):
  If b == 0:
    Revert "Division by zero"
  c = a / b
  Return c

Function mod(a, b):
  If b == 0:
    Revert "Modulo by zero"
  Return a % b