SafeMath.sol 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. pragma solidity ^0.5.0;
  2. /**
  3. * @title SafeMath
  4. * @dev Math operations with safety checks that revert on error
  5. */
  6. library SafeMath {
  7. int256 constant private INT256_MIN = -2**255;
  8. /**
  9. * @dev Multiplies two unsigned integers, reverts on overflow.
  10. */
  11. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  12. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  13. // benefit is lost if 'b' is also tested.
  14. // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
  15. if (a == 0) {
  16. return 0;
  17. }
  18. uint256 c = a * b;
  19. require(c / a == b);
  20. return c;
  21. }
  22. /**
  23. * @dev Multiplies two signed integers, reverts on overflow.
  24. */
  25. function mul(int256 a, int256 b) internal pure returns (int256) {
  26. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  27. // benefit is lost if 'b' is also tested.
  28. // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
  29. if (a == 0) {
  30. return 0;
  31. }
  32. require(!(a == -1 && b == INT256_MIN)); // This is the only case of overflow not detected by the check below
  33. int256 c = a * b;
  34. require(c / a == b);
  35. return c;
  36. }
  37. /**
  38. * @dev Integer division of two unsigned integers truncating the quotient, reverts on division by zero.
  39. */
  40. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  41. // Solidity only automatically asserts when dividing by 0
  42. require(b > 0);
  43. uint256 c = a / b;
  44. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  45. return c;
  46. }
  47. /**
  48. * @dev Integer division of two signed integers truncating the quotient, reverts on division by zero.
  49. */
  50. function div(int256 a, int256 b) internal pure returns (int256) {
  51. require(b != 0); // Solidity only automatically asserts when dividing by 0
  52. require(!(b == -1 && a == INT256_MIN)); // This is the only case of overflow
  53. int256 c = a / b;
  54. return c;
  55. }
  56. /**
  57. * @dev Subtracts two unsigned integers, reverts on overflow (i.e. if subtrahend is greater than minuend).
  58. */
  59. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  60. require(b <= a);
  61. uint256 c = a - b;
  62. return c;
  63. }
  64. /**
  65. * @dev Subtracts two signed integers, reverts on overflow.
  66. */
  67. function sub(int256 a, int256 b) internal pure returns (int256) {
  68. int256 c = a - b;
  69. require((b >= 0 && c <= a) || (b < 0 && c > a));
  70. return c;
  71. }
  72. /**
  73. * @dev Adds two unsigned integers, reverts on overflow.
  74. */
  75. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  76. uint256 c = a + b;
  77. require(c >= a);
  78. return c;
  79. }
  80. /**
  81. * @dev Adds two signed integers, reverts on overflow.
  82. */
  83. function add(int256 a, int256 b) internal pure returns (int256) {
  84. int256 c = a + b;
  85. require((b >= 0 && c >= a) || (b < 0 && c < a));
  86. return c;
  87. }
  88. /**
  89. * @dev Divides two unsigned integers and returns the remainder (unsigned integer modulo),
  90. * reverts when dividing by zero.
  91. */
  92. function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  93. require(b != 0);
  94. return a % b;
  95. }
  96. }