Files
uniswap-v2/contracts/libraries/SafeMath.sol
NoBey 3586e648b2
Some checks failed
CI / test (10.x, ubuntu-latest) (push) Has been cancelled
CI / test (12.x, ubuntu-latest) (push) Has been cancelled
init
2025-07-08 01:27:47 +08:00

43 lines
1.3 KiB
Solidity

pragma solidity =0.5.16;
/**
* @title SafeMath 安全数学运算库
* @notice 提供防止溢出的数学运算函数
* @dev 来自DappHub的ds-math库 (https://github.com/dapphub/ds-math)
* @dev 在Solidity 0.8.0之前,需要手动检查算术溢出
*/
library SafeMath {
/**
* @notice 安全加法,防止溢出
* @param x 第一个加数
* @param y 第二个加数
* @return z 两数之和
* @dev 如果结果溢出则回滚交易
*/
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x, 'ds-math-add-overflow');
}
/**
* @notice 安全减法,防止下溢
* @param x 被减数
* @param y 减数
* @return z 两数之差
* @dev 如果结果下溢则回滚交易
*/
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x, 'ds-math-sub-underflow');
}
/**
* @notice 安全乘法,防止溢出
* @param x 第一个乘数
* @param y 第二个乘数
* @return z 两数之积
* @dev 如果结果溢出则回滚交易,使用除法检查溢出
*/
function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, 'ds-math-mul-overflow');
}
}