1
1
// SPDX-License-Identifier: MIT
2
2
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 ;
7
4
8
5
/**
9
6
* @dev Wrappers over Solidity's arithmetic operations with added overflow
@@ -20,169 +17,198 @@ pragma solidity 0.6.11;
20
17
*/
21
18
library SafeMath {
22
19
/**
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.
26
21
*
27
- * Requirements:
28
- * - Addition cannot overflow.
22
+ * _Available since v3.4._
29
23
*/
30
- function add (uint256 a , uint256 b ) internal pure returns (uint256 ) {
24
+ function tryAdd (uint256 a , uint256 b ) internal pure returns (bool , uint256 ) {
31
25
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);
35
28
}
36
29
37
30
/**
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.
41
32
*
42
- * Requirements:
43
- * - Addition cannot overflow.
33
+ * _Available since v3.4._
44
34
*/
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
+ }
48
39
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);
50
53
}
51
54
52
55
/**
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 .
54
57
*
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.
56
67
*
57
- * Requirements:
58
- * - Subtraction cannot underflow.
68
+ * _Available since v3.4._
59
69
*/
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);
62
73
}
63
74
64
75
/**
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.
66
78
*
67
- * Counterpart to Solidity's `- ` operator.
79
+ * Counterpart to Solidity's `+ ` operator.
68
80
*
69
81
* Requirements:
70
- * - Subtraction cannot underflow.
82
+ *
83
+ * - Addition cannot overflow.
71
84
*/
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 " );
76
88
return c;
77
89
}
78
90
79
91
/**
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).
81
94
*
82
- * Counterpart to Solidity's `* ` operator.
95
+ * Counterpart to Solidity's `- ` operator.
83
96
*
84
97
* Requirements:
85
- * - Multiplication cannot overflow.
98
+ *
99
+ * - Subtraction cannot overflow.
86
100
*/
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;
99
104
}
100
105
101
106
/**
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.
103
109
*
104
110
* Counterpart to Solidity's `*` operator.
105
111
*
106
112
* Requirements:
113
+ *
107
114
* - Multiplication cannot overflow.
108
115
*/
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 ;
117
118
uint256 c = a * b;
118
- require (c / a == b, errorMessage);
119
-
119
+ require (c / a == b, "SafeMath: multiplication overflow " );
120
120
return c;
121
121
}
122
122
123
123
/**
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.
126
126
*
127
127
* Counterpart to Solidity's `/` operator. Note: this function uses a
128
128
* `revert` opcode (which leaves remaining gas untouched) while Solidity
129
129
* uses an invalid opcode to revert (consuming all remaining gas).
130
130
*
131
131
* Requirements:
132
+ *
132
133
* - The divisor cannot be zero.
133
134
*/
134
135
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;
136
138
}
137
139
138
140
/**
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.
141
143
*
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).
145
147
*
146
148
* Requirements:
149
+ *
147
150
* - The divisor cannot be zero.
148
151
*/
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
+ }
154
156
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;
156
173
}
157
174
158
175
/**
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.
161
178
*
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).
165
185
*
166
186
* Requirements:
187
+ *
167
188
* - The divisor cannot be zero.
168
189
*/
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;
171
193
}
172
194
173
195
/**
174
196
* @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}.
176
201
*
177
202
* Counterpart to Solidity's `%` operator. This function uses a `revert`
178
203
* opcode (which leaves remaining gas untouched) while Solidity uses an
179
204
* invalid opcode to revert (consuming all remaining gas).
180
205
*
181
206
* Requirements:
207
+ *
182
208
* - The divisor cannot be zero.
183
209
*/
184
210
function mod (uint256 a , uint256 b , string memory errorMessage ) internal pure returns (uint256 ) {
185
- require (b != 0 , errorMessage);
211
+ require (b > 0 , errorMessage);
186
212
return a % b;
187
213
}
188
214
}
0 commit comments