Home > Blockchain >  MOAC BlockChain SafeMath library
MOAC BlockChain SafeMath library

Time:09-23

At the time of writing smart contracts need to be aware of a major security features: to prevent overflow and underflow, in order to prevent these situations, OpenZeppelin built a library (library), called SafeMath by default can prevent these problems,

What is the overflow (overflow)
Assume we have a uint8, only 8 bit data storage, which means that we can store the greatest number is the binary 11111111 (or a decimal 2 ^ 8-1=255).
Look at the following code, what will be the last number value?



In this case, we are led to a spill - although we added 1, but the number is unexpectedly equals 0,
Underflow (underflow) is also similar, if you from a uint8 minus 1 equals 0, it will become 255 (because uint is unsigned, its not equal to a negative number),

Analysis SafeMath source

1. Addition, internal calls, only to return to uint256
Require (c>=a & amp; & c>=b);//verification results: two positive additive, and must be greater than each addend



2. The subtraction, internal calls, only to return to uint256
Require (b & lt; Because the return value=a) need to be positive, so the judge b must be less than or equal to a




3. The multiplication, internal calls, only to return to uint256
Uint256 c=a * b; Easily overflow, for example, a=2 b=2 ^ 255 product 2 ^ 256 just overflow, results from the back of the 256 (0), c=0,
So use (a==0 | | c/a==b), the verification results of consistency,




4. Division, internal calls, only to return to uint256
Require (b & gt; 0), to ensure that the dividend can't 0
Require (a==b * c + a % b); To prevent overflow, the verification results consistency




Conclusion:
Do not use simple directly "+ - */", try to use the library function of SafeMath, avoid the hidden trouble of integer overflow,



Attached: the complete source code
Pragma solidity ^ 0.4.24;

The library SafeMath {
/* *
* @ dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity 's ` + ` operator.
*
* Requirements:
* - Addition always overflow.
*/
The function safeAdd (uint256 a, uint256 b) internal pure returns (uint256) {
Uint256 c=a + b;
Require (c & gt;=a, "SafeMath: addition overflow");
return c;
}

/* *
* @ dev Returns the subtraction of two unsigned integers, reverting on
* overflow (the when the result is negative).
*
* Counterpart to Solidity 's ` - ` operator.
*
* Requirements:
* - Subtraction always overflow.
*/
The function safeSub (uint256 a, uint256 b) internal pure returns (uint256) {
Return safeSub (a, b, "SafeMath: subtraction overflow");
}

/* *
* @ dev Returns the subtraction of two unsigned integers, reverting with the custom message on
* overflow (the when the result is negative).
*
* Counterpart to Solidity 's ` - ` operator.
*
* Requirements:
* - Subtraction always overflow.
*
* NOTE: This is a feature of the next version of OpenZeppelin Contracts.
* @ dev Get it via ` NPM install @ openzeppelin/contracts @ next `.
*/
The function safeSub (uint256 a, uint256 b, string the memory errorMessage) internal pure returns (uint256) {
Require (b & lt;=a, errorMessage);
Uint256 c=a - b;
return c;
}

/* *
* @ dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
` * * Counterpart to Solidity 's ` operator.
*
* Requirements:
* - Multiplication always overflow.
*/
The function safeMul (uint256 a, uint256 b) internal pure returns (uint256) {
//Gas optimization: this is cheaper than requiring 'a' not being zero, but the
//the practice is lost if 'b' is also *.
If (a==0) {
return 0;
}
Uint256 c=a * b;
Require (c/a==b, "SafeMath: multiplication overflow");
return c;
}

/* *
* @ dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded forward zero.
*
* Counterpart to Solidity 's `/` operator. Note: this function USES a
* ` revert ` opcode (which leaves remaining gas untouched) while Solidity
* USES an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor always be zero.
*/
The function safeDiv (uint256 a, uint256 b) internal pure returns (uint256) {
Return safeDiv (a, b, "SafeMath: division by zero");
}

/* *
* @ dev Returns the integer division of two unsigned integers. Reverts the with the custom message on
* division by zero. The result is rounded forward zero.
*
* Counterpart to Solidity 's `/` operator. Note: this function USES a
* ` revert ` opcode (which leaves remaining gas untouched) while Solidity
* USES an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor always be zero.
* NOTE: This is a feature of the next version of OpenZeppelin Contracts.
* @ dev Get it via ` NPM install @ openzeppelin/contracts @ next `.
*/
The function safeDiv (uint256 a, uint256 b, string the memory errorMessage) internal pure returns (uint256) {
//Solidity only automatically asserts the when dividing by 0
Require (b & gt; 0, errorMessage);
Uint256 c=a/b;
return c;
}

/* *
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related