Skip to content

Commit 6e0deac

Browse files
author
Brandon Iles
committed
SafeMath v3.4.0
1 parent 7ffccb0 commit 6e0deac

File tree

1 file changed

+110
-84
lines changed

1 file changed

+110
-84
lines changed

contracts/SafeMath.sol

Lines changed: 110 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// SPDX-License-Identifier: MIT
22

3-
pragma solidity 0.6.11;
4-
5-
// From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol
6-
// Subject to the MIT license.
3+
pragma solidity >=0.6.0 <0.8.0;
74

85
/**
96
* @dev Wrappers over Solidity's arithmetic operations with added overflow
@@ -20,169 +17,198 @@ pragma solidity 0.6.11;
2017
*/
2118
library SafeMath {
2219
/**
23-
* @dev Returns the addition of two unsigned integers, reverting on overflow.
24-
*
25-
* Counterpart to Solidity's `+` operator.
20+
* @dev Returns the addition of two unsigned integers, with an overflow flag.
2621
*
27-
* Requirements:
28-
* - Addition cannot overflow.
22+
* _Available since v3.4._
2923
*/
30-
function add(uint256 a, uint256 b) internal pure returns (uint256) {
24+
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
3125
uint256 c = a + b;
32-
require(c >= a, "SafeMath: addition overflow");
33-
34-
return c;
26+
if (c < a) return (false, 0);
27+
return (true, c);
3528
}
3629

3730
/**
38-
* @dev Returns the addition of two unsigned integers, reverting with custom message on overflow.
39-
*
40-
* Counterpart to Solidity's `+` operator.
31+
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
4132
*
42-
* Requirements:
43-
* - Addition cannot overflow.
33+
* _Available since v3.4._
4434
*/
45-
function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
46-
uint256 c = a + b;
47-
require(c >= a, errorMessage);
35+
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
36+
if (b > a) return (false, 0);
37+
return (true, a - b);
38+
}
4839

49-
return c;
40+
/**
41+
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
42+
*
43+
* _Available since v3.4._
44+
*/
45+
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
46+
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
47+
// benefit is lost if 'b' is also tested.
48+
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
49+
if (a == 0) return (true, 0);
50+
uint256 c = a * b;
51+
if (c / a != b) return (false, 0);
52+
return (true, c);
5053
}
5154

5255
/**
53-
* @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative).
56+
* @dev Returns the division of two unsigned integers, with a division by zero flag.
5457
*
55-
* Counterpart to Solidity's `-` operator.
58+
* _Available since v3.4._
59+
*/
60+
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
61+
if (b == 0) return (false, 0);
62+
return (true, a / b);
63+
}
64+
65+
/**
66+
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
5667
*
57-
* Requirements:
58-
* - Subtraction cannot underflow.
68+
* _Available since v3.4._
5969
*/
60-
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
61-
return sub(a, b, "SafeMath: subtraction underflow");
70+
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
71+
if (b == 0) return (false, 0);
72+
return (true, a % b);
6273
}
6374

6475
/**
65-
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative).
76+
* @dev Returns the addition of two unsigned integers, reverting on
77+
* overflow.
6678
*
67-
* Counterpart to Solidity's `-` operator.
79+
* Counterpart to Solidity's `+` operator.
6880
*
6981
* Requirements:
70-
* - Subtraction cannot underflow.
82+
*
83+
* - Addition cannot overflow.
7184
*/
72-
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
73-
require(b <= a, errorMessage);
74-
uint256 c = a - b;
75-
85+
function add(uint256 a, uint256 b) internal pure returns (uint256) {
86+
uint256 c = a + b;
87+
require(c >= a, "SafeMath: addition overflow");
7688
return c;
7789
}
7890

7991
/**
80-
* @dev Returns the multiplication of two unsigned integers, reverting on overflow.
92+
* @dev Returns the subtraction of two unsigned integers, reverting on
93+
* overflow (when the result is negative).
8194
*
82-
* Counterpart to Solidity's `*` operator.
95+
* Counterpart to Solidity's `-` operator.
8396
*
8497
* Requirements:
85-
* - Multiplication cannot overflow.
98+
*
99+
* - Subtraction cannot overflow.
86100
*/
87-
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
88-
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
89-
// benefit is lost if 'b' is also tested.
90-
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
91-
if (a == 0) {
92-
return 0;
93-
}
94-
95-
uint256 c = a * b;
96-
require(c / a == b, "SafeMath: multiplication overflow");
97-
98-
return c;
101+
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
102+
require(b <= a, "SafeMath: subtraction overflow");
103+
return a - b;
99104
}
100105

101106
/**
102-
* @dev Returns the multiplication of two unsigned integers, reverting on overflow.
107+
* @dev Returns the multiplication of two unsigned integers, reverting on
108+
* overflow.
103109
*
104110
* Counterpart to Solidity's `*` operator.
105111
*
106112
* Requirements:
113+
*
107114
* - Multiplication cannot overflow.
108115
*/
109-
function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
110-
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
111-
// benefit is lost if 'b' is also tested.
112-
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
113-
if (a == 0) {
114-
return 0;
115-
}
116-
116+
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
117+
if (a == 0) return 0;
117118
uint256 c = a * b;
118-
require(c / a == b, errorMessage);
119-
119+
require(c / a == b, "SafeMath: multiplication overflow");
120120
return c;
121121
}
122122

123123
/**
124-
* @dev Returns the integer division of two unsigned integers.
125-
* Reverts on division by zero. The result is rounded towards zero.
124+
* @dev Returns the integer division of two unsigned integers, reverting on
125+
* division by zero. The result is rounded towards zero.
126126
*
127127
* Counterpart to Solidity's `/` operator. Note: this function uses a
128128
* `revert` opcode (which leaves remaining gas untouched) while Solidity
129129
* uses an invalid opcode to revert (consuming all remaining gas).
130130
*
131131
* Requirements:
132+
*
132133
* - The divisor cannot be zero.
133134
*/
134135
function div(uint256 a, uint256 b) internal pure returns (uint256) {
135-
return div(a, b, "SafeMath: division by zero");
136+
require(b > 0, "SafeMath: division by zero");
137+
return a / b;
136138
}
137139

138140
/**
139-
* @dev Returns the integer division of two unsigned integers.
140-
* Reverts with custom message on division by zero. The result is rounded towards zero.
141+
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
142+
* reverting when dividing by zero.
141143
*
142-
* Counterpart to Solidity's `/` operator. Note: this function uses a
143-
* `revert` opcode (which leaves remaining gas untouched) while Solidity
144-
* uses an invalid opcode to revert (consuming all remaining gas).
144+
* Counterpart to Solidity's `%` operator. This function uses a `revert`
145+
* opcode (which leaves remaining gas untouched) while Solidity uses an
146+
* invalid opcode to revert (consuming all remaining gas).
145147
*
146148
* Requirements:
149+
*
147150
* - The divisor cannot be zero.
148151
*/
149-
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
150-
// Solidity only automatically asserts when dividing by 0
151-
require(b > 0, errorMessage);
152-
uint256 c = a / b;
153-
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
152+
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
153+
require(b > 0, "SafeMath: modulo by zero");
154+
return a % b;
155+
}
154156

155-
return c;
157+
/**
158+
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
159+
* overflow (when the result is negative).
160+
*
161+
* CAUTION: This function is deprecated because it requires allocating memory for the error
162+
* message unnecessarily. For custom revert reasons use {trySub}.
163+
*
164+
* Counterpart to Solidity's `-` operator.
165+
*
166+
* Requirements:
167+
*
168+
* - Subtraction cannot overflow.
169+
*/
170+
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
171+
require(b <= a, errorMessage);
172+
return a - b;
156173
}
157174

158175
/**
159-
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
160-
* Reverts when dividing by zero.
176+
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
177+
* division by zero. The result is rounded towards zero.
161178
*
162-
* Counterpart to Solidity's `%` operator. This function uses a `revert`
163-
* opcode (which leaves remaining gas untouched) while Solidity uses an
164-
* invalid opcode to revert (consuming all remaining gas).
179+
* CAUTION: This function is deprecated because it requires allocating memory for the error
180+
* message unnecessarily. For custom revert reasons use {tryDiv}.
181+
*
182+
* Counterpart to Solidity's `/` operator. Note: this function uses a
183+
* `revert` opcode (which leaves remaining gas untouched) while Solidity
184+
* uses an invalid opcode to revert (consuming all remaining gas).
165185
*
166186
* Requirements:
187+
*
167188
* - The divisor cannot be zero.
168189
*/
169-
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
170-
return mod(a, b, "SafeMath: modulo by zero");
190+
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
191+
require(b > 0, errorMessage);
192+
return a / b;
171193
}
172194

173195
/**
174196
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
175-
* Reverts with custom message when dividing by zero.
197+
* reverting with custom message when dividing by zero.
198+
*
199+
* CAUTION: This function is deprecated because it requires allocating memory for the error
200+
* message unnecessarily. For custom revert reasons use {tryMod}.
176201
*
177202
* Counterpart to Solidity's `%` operator. This function uses a `revert`
178203
* opcode (which leaves remaining gas untouched) while Solidity uses an
179204
* invalid opcode to revert (consuming all remaining gas).
180205
*
181206
* Requirements:
207+
*
182208
* - The divisor cannot be zero.
183209
*/
184210
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
185-
require(b != 0, errorMessage);
211+
require(b > 0, errorMessage);
186212
return a % b;
187213
}
188214
}

0 commit comments

Comments
 (0)