SafeMath.sol 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. pragma solidity ^0.5.0;
  2. /**
  3. * @dev Wrappers over Solidity's arithmetic operations with added overflow
  4. * checks.
  5. *
  6. * Arithmetic operations in Solidity wrap on overflow. This can easily result
  7. * in bugs, because programmers usually assume that an overflow raises an
  8. * error, which is the standard behavior in high level programming languages.
  9. * `SafeMath` restores this intuition by reverting the transaction when an
  10. * operation overflows.
  11. *
  12. * Using this library instead of the unchecked operations eliminates an entire
  13. * class of bugs, so it's recommended to use it always.
  14. */
  15. library SafeMath {
  16. /**
  17. * @dev Returns the addition of two unsigned integers, reverting on
  18. * overflow.
  19. *
  20. * Counterpart to Solidity's `+` operator.
  21. *
  22. * Requirements:
  23. * - Addition cannot overflow.
  24. */
  25. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  26. uint256 c = a + b;
  27. require(c >= a, "SafeMath: addition overflow");
  28. return c;
  29. }
  30. /**
  31. * @dev Returns the subtraction of two unsigned integers, reverting on
  32. * overflow (when the result is negative).
  33. *
  34. * Counterpart to Solidity's `-` operator.
  35. *
  36. * Requirements:
  37. * - Subtraction cannot overflow.
  38. */
  39. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  40. require(b <= a, "SafeMath: subtraction overflow");
  41. uint256 c = a - b;
  42. return c;
  43. }
  44. /**
  45. * @dev Returns the multiplication of two unsigned integers, reverting on
  46. * overflow.
  47. *
  48. * Counterpart to Solidity's `*` operator.
  49. *
  50. * Requirements:
  51. * - Multiplication cannot overflow.
  52. */
  53. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  54. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  55. // benefit is lost if 'b' is also tested.
  56. // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
  57. if (a == 0) {
  58. return 0;
  59. }
  60. uint256 c = a * b;
  61. require(c / a == b, "SafeMath: multiplication overflow");
  62. return c;
  63. }
  64. /**
  65. * @dev Returns the integer division of two unsigned integers. Reverts on
  66. * division by zero. The result is rounded towards zero.
  67. *
  68. * Counterpart to Solidity's `/` operator. Note: this function uses a
  69. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  70. * uses an invalid opcode to revert (consuming all remaining gas).
  71. *
  72. * Requirements:
  73. * - The divisor cannot be zero.
  74. */
  75. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  76. // Solidity only automatically asserts when dividing by 0
  77. require(b > 0, "SafeMath: division by zero");
  78. uint256 c = a / b;
  79. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  80. return c;
  81. }
  82. /**
  83. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  84. * Reverts when dividing by zero.
  85. *
  86. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  87. * opcode (which leaves remaining gas untouched) while Solidity uses an
  88. * invalid opcode to revert (consuming all remaining gas).
  89. *
  90. * Requirements:
  91. * - The divisor cannot be zero.
  92. */
  93. function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  94. require(b != 0, "SafeMath: modulo by zero");
  95. return a % b;
  96. }
  97. }