1
+ // SPDX-License-Identifier: MIT
2
+ // Pulled from @openzeppelin/contracts/math/SafeMath.sol
3
+ pragma solidity ^ 0.7.0 ;
4
+
5
+ /* Library Imports */
6
+ import { Lib_SafeExecutionManagerWrapper } from "./Lib_SafeExecutionManagerWrapper.sol " ;
7
+
8
+ /**
9
+ * @title Lib_SafeMathWrapper
10
+ */
11
+
12
+ /**
13
+ * @dev Wrappers over Solidity's arithmetic operations with added overflow
14
+ * checks.
15
+ *
16
+ * Arithmetic operations in Solidity wrap on overflow. This can easily result
17
+ * in bugs, because programmers usually assume that an overflow raises an
18
+ * error, which is the standard behavior in high level programming languages.
19
+ * `SafeMath` restores this intuition by reverting the transaction when an
20
+ * operation overflows.
21
+ *
22
+ * Using this library instead of the unchecked operations eliminates an entire
23
+ * class of bugs, so it's recommended to use it always.
24
+ */
25
+
26
+ library Lib_SafeMathWrapper {
27
+ /**
28
+ * @dev Returns the addition of two unsigned integers, reverting on
29
+ * overflow.
30
+ *
31
+ * Counterpart to Solidity's `+` operator.
32
+ *
33
+ * Requirements:
34
+ *
35
+ * - Addition cannot overflow.
36
+ */
37
+ function add (uint256 a , uint256 b ) internal returns (uint256 ) {
38
+ uint256 c = a + b;
39
+ Lib_SafeExecutionManagerWrapper.safeREQUIRE (c >= a, "Lib_SafeMathWrapper: addition overflow " );
40
+
41
+ return c;
42
+ }
43
+
44
+ /**
45
+ * @dev Returns the subtraction of two unsigned integers, reverting on
46
+ * overflow (when the result is negative).
47
+ *
48
+ * Counterpart to Solidity's `-` operator.
49
+ *
50
+ * Requirements:
51
+ *
52
+ * - Subtraction cannot overflow.
53
+ */
54
+ function sub (uint256 a , uint256 b ) internal returns (uint256 ) {
55
+ return sub (a, b, "Lib_SafeMathWrapper: subtraction overflow " );
56
+ }
57
+
58
+ /**
59
+ * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
60
+ * overflow (when the result is negative).
61
+ *
62
+ * Counterpart to Solidity's `-` operator.
63
+ *
64
+ * Requirements:
65
+ *
66
+ * - Subtraction cannot overflow.
67
+ */
68
+ function sub (uint256 a , uint256 b , string memory errorMessage ) internal returns (uint256 ) {
69
+ Lib_SafeExecutionManagerWrapper.safeREQUIRE (b <= a, errorMessage);
70
+ uint256 c = a - b;
71
+
72
+ return c;
73
+ }
74
+
75
+ /**
76
+ * @dev Returns the multiplication of two unsigned integers, reverting on
77
+ * overflow.
78
+ *
79
+ * Counterpart to Solidity's `*` operator.
80
+ *
81
+ * Requirements:
82
+ *
83
+ * - Multiplication cannot overflow.
84
+ */
85
+ function mul (uint256 a , uint256 b ) internal returns (uint256 ) {
86
+ // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
87
+ // benefit is lost if 'b' is also tested.
88
+ // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
89
+ if (a == 0 ) {
90
+ return 0 ;
91
+ }
92
+
93
+ uint256 c = a * b;
94
+ Lib_SafeExecutionManagerWrapper.safeREQUIRE (c / a == b, "Lib_SafeMathWrapper: multiplication overflow " );
95
+
96
+ return c;
97
+ }
98
+
99
+ /**
100
+ * @dev Returns the integer division of two unsigned integers. Reverts on
101
+ * division by zero. The result is rounded towards zero.
102
+ *
103
+ * Counterpart to Solidity's `/` operator. Note: this function uses a
104
+ * `revert` opcode (which leaves remaining gas untouched) while Solidity
105
+ * uses an invalid opcode to revert (consuming all remaining gas).
106
+ *
107
+ * Requirements:
108
+ *
109
+ * - The divisor cannot be zero.
110
+ */
111
+ function div (uint256 a , uint256 b ) internal returns (uint256 ) {
112
+ return div (a, b, "Lib_SafeMathWrapper: division by zero " );
113
+ }
114
+
115
+ /**
116
+ * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
117
+ * division by zero. The result is rounded towards zero.
118
+ *
119
+ * Counterpart to Solidity's `/` operator. Note: this function uses a
120
+ * `revert` opcode (which leaves remaining gas untouched) while Solidity
121
+ * uses an invalid opcode to revert (consuming all remaining gas).
122
+ *
123
+ * Requirements:
124
+ *
125
+ * - The divisor cannot be zero.
126
+ */
127
+ function div (uint256 a , uint256 b , string memory errorMessage ) internal returns (uint256 ) {
128
+ Lib_SafeExecutionManagerWrapper.safeREQUIRE (b > 0 , errorMessage);
129
+ uint256 c = a / b;
130
+ // assert(a == b * c + a % b); // There is no case in which this doesn't hold
131
+
132
+ return c;
133
+ }
134
+
135
+ /**
136
+ * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
137
+ * Reverts when dividing by zero.
138
+ *
139
+ * Counterpart to Solidity's `%` operator. This function uses a `revert`
140
+ * opcode (which leaves remaining gas untouched) while Solidity uses an
141
+ * invalid opcode to revert (consuming all remaining gas).
142
+ *
143
+ * Requirements:
144
+ *
145
+ * - The divisor cannot be zero.
146
+ */
147
+ function mod (uint256 a , uint256 b ) internal returns (uint256 ) {
148
+ return mod (a, b, "Lib_SafeMathWrapper: modulo by zero " );
149
+ }
150
+
151
+ /**
152
+ * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
153
+ * Reverts with custom message when dividing by zero.
154
+ *
155
+ * Counterpart to Solidity's `%` operator. This function uses a `revert`
156
+ * opcode (which leaves remaining gas untouched) while Solidity uses an
157
+ * invalid opcode to revert (consuming all remaining gas).
158
+ *
159
+ * Requirements:
160
+ *
161
+ * - The divisor cannot be zero.
162
+ */
163
+ function mod (uint256 a , uint256 b , string memory errorMessage ) internal returns (uint256 ) {
164
+ Lib_SafeExecutionManagerWrapper.safeREQUIRE (b != 0 , errorMessage);
165
+ return a % b;
166
+ }
167
+ }
0 commit comments