solidity-math-utils 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/project/contracts/AdvancedMath.sol +14 -7
- package/project/contracts/AnalyticMath.sol +8 -4
- package/project/contracts/BondingCurve.sol +20 -15
- package/project/contracts/DynamicCurve.sol +7 -4
- package/project/contracts/FractionMath.sol +4 -2
- package/project/contracts/IntegralMath.sol +5 -3
- package/project/contracts/common/Uint256.sol +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solidity-math-utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Solidity Mathematical Utilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"@nomiclabs/hardhat-truffle5": "2.1.0",
|
|
22
22
|
"@nomiclabs/hardhat-web3": "2.1.0",
|
|
23
23
|
"decimal.js": "10.6.0",
|
|
24
|
-
"hardhat": "2.28.
|
|
24
|
+
"hardhat": "2.28.6",
|
|
25
25
|
"solidity-coverage": "0.8.17"
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.34;
|
|
3
3
|
|
|
4
4
|
import "./AnalyticMath.sol";
|
|
5
5
|
import "./FractionMath.sol";
|
|
6
6
|
import "./IntegralMath.sol";
|
|
7
7
|
|
|
8
8
|
library AdvancedMath {
|
|
9
|
+
error LambertNegExactBelowMin();
|
|
10
|
+
error LambertNegExactAboveMax();
|
|
11
|
+
error LambertPosExactBelowMin();
|
|
12
|
+
error LambertNegQuickBelowMin();
|
|
13
|
+
error LambertNegQuickAboveMax();
|
|
14
|
+
error LambertPosQuickBelowMin();
|
|
15
|
+
|
|
9
16
|
uint8 internal constant SCALE_1 = AnalyticMath.SCALE_1;
|
|
10
17
|
uint256 internal constant FIXED_1 = AnalyticMath.FIXED_1;
|
|
11
18
|
|
|
@@ -187,7 +194,7 @@ library AdvancedMath {
|
|
|
187
194
|
* Input range: 1 <= x <= LAMBERT_NEG2_MAXVAL
|
|
188
195
|
*/
|
|
189
196
|
function lambertNegExact(uint256 x) internal pure returns (uint256) { unchecked {
|
|
190
|
-
require(x > 0,
|
|
197
|
+
require(x > 0, LambertNegExactBelowMin());
|
|
191
198
|
if (x <= LAMBERT_NEG2_MAXVAL) {
|
|
192
199
|
uint256 y = x;
|
|
193
200
|
for (uint256 i = 0; i < 8; ++i) {
|
|
@@ -196,7 +203,7 @@ library AdvancedMath {
|
|
|
196
203
|
}
|
|
197
204
|
return IntegralMath.mulDivF(FIXED_1, y, x);
|
|
198
205
|
}
|
|
199
|
-
revert(
|
|
206
|
+
revert LambertNegExactAboveMax();
|
|
200
207
|
}}
|
|
201
208
|
|
|
202
209
|
/**
|
|
@@ -204,7 +211,7 @@ library AdvancedMath {
|
|
|
204
211
|
* Input range: 1 <= x <= 2 ^ 256 - 1
|
|
205
212
|
*/
|
|
206
213
|
function lambertPosExact(uint256 x) internal pure returns (uint256) { unchecked {
|
|
207
|
-
require(x > 0,
|
|
214
|
+
require(x > 0, LambertPosExactBelowMin());
|
|
208
215
|
if (x <= LAMBERT_EXACT_LIMIT) {
|
|
209
216
|
uint256 y;
|
|
210
217
|
if (x < FIXED_1) {
|
|
@@ -233,12 +240,12 @@ library AdvancedMath {
|
|
|
233
240
|
* Input range: 1 <= x <= LAMBERT_NEG2_MAXVAL
|
|
234
241
|
*/
|
|
235
242
|
function lambertNegQuick(uint256 x) internal pure returns (uint256) { unchecked {
|
|
236
|
-
require(x > 0,
|
|
243
|
+
require(x > 0, LambertNegQuickBelowMin());
|
|
237
244
|
if (x <= LAMBERT_NEG1_MAXVAL)
|
|
238
245
|
return lambertNeg1(x);
|
|
239
246
|
if (x <= LAMBERT_NEG2_MAXVAL)
|
|
240
247
|
return lambertNeg2(x);
|
|
241
|
-
revert(
|
|
248
|
+
revert LambertNegQuickAboveMax();
|
|
242
249
|
}}
|
|
243
250
|
|
|
244
251
|
/**
|
|
@@ -246,7 +253,7 @@ library AdvancedMath {
|
|
|
246
253
|
* Input range: 1 <= x <= 2 ^ 256 - 1
|
|
247
254
|
*/
|
|
248
255
|
function lambertPosQuick(uint256 x) internal pure returns (uint256) { unchecked {
|
|
249
|
-
require(x > 0,
|
|
256
|
+
require(x > 0, LambertPosQuickBelowMin());
|
|
250
257
|
if (x <= LAMBERT_POS1_MAXVAL)
|
|
251
258
|
return lambertPos1(x);
|
|
252
259
|
if (x <= LAMBERT_POS2_MAXVAL)
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.34;
|
|
3
3
|
|
|
4
4
|
import "./IntegralMath.sol";
|
|
5
5
|
|
|
6
6
|
library AnalyticMath {
|
|
7
|
+
error DivisionByZero();
|
|
8
|
+
error FixedLogBelowMin();
|
|
9
|
+
error FixedExpAboveMax();
|
|
10
|
+
|
|
7
11
|
// Auto-generated via 'PrintAnalyticMathConstants.py'
|
|
8
12
|
uint8 internal constant SCALE_1 = 0x000000000000000000000000000000007f;
|
|
9
13
|
uint256 internal constant FIXED_1 = 0x0080000000000000000000000000000000;
|
|
@@ -18,7 +22,7 @@ library AnalyticMath {
|
|
|
18
22
|
*/
|
|
19
23
|
function pow(uint256 a, uint256 b, uint256 c, uint256 d) internal pure returns (uint256, uint256) { unchecked {
|
|
20
24
|
if (b == 0 || d == 0)
|
|
21
|
-
revert(
|
|
25
|
+
revert DivisionByZero();
|
|
22
26
|
if (a == 0 || c == 0)
|
|
23
27
|
return (a ** c, 1);
|
|
24
28
|
if (a > b)
|
|
@@ -58,7 +62,7 @@ library AnalyticMath {
|
|
|
58
62
|
*/
|
|
59
63
|
function fixedLog(uint256 x) internal pure returns (uint256) { unchecked {
|
|
60
64
|
if (x < FIXED_1)
|
|
61
|
-
revert(
|
|
65
|
+
revert FixedLogBelowMin();
|
|
62
66
|
if (x < LOG_MID)
|
|
63
67
|
return optimalLog(x);
|
|
64
68
|
uint8 count = IntegralMath.floorLog2(x / FIXED_1);
|
|
@@ -84,7 +88,7 @@ library AnalyticMath {
|
|
|
84
88
|
return optimalExp(x);
|
|
85
89
|
if (x < EXP_MAX)
|
|
86
90
|
return optimalExp(x % LN2_MAX) << (x / LN2_MAX);
|
|
87
|
-
revert(
|
|
91
|
+
revert FixedExpAboveMax();
|
|
88
92
|
}}
|
|
89
93
|
|
|
90
94
|
/**
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.34;
|
|
3
3
|
|
|
4
4
|
import "./AnalyticMath.sol";
|
|
5
5
|
import "./IntegralMath.sol";
|
|
6
6
|
|
|
7
7
|
library BondingCurve {
|
|
8
|
+
error InvalidInput();
|
|
9
|
+
error WeightOutOfBound();
|
|
10
|
+
error WeightsOutOfBound();
|
|
11
|
+
error AmountLargerThanSupply();
|
|
12
|
+
|
|
8
13
|
uint256 internal constant MAX_WEIGHT = 1000000;
|
|
9
14
|
|
|
10
15
|
/**
|
|
@@ -18,8 +23,8 @@ library BondingCurve {
|
|
|
18
23
|
* @return supply * ((1 + amount / balance) ^ (weight / MAX_WEIGHT) - 1)
|
|
19
24
|
*/
|
|
20
25
|
function buy(uint256 supply, uint256 balance, uint256 weight, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
21
|
-
require(supply > 0 && balance > 0 && weight > 0,
|
|
22
|
-
require(weight <= MAX_WEIGHT,
|
|
26
|
+
require(supply > 0 && balance > 0 && weight > 0, InvalidInput());
|
|
27
|
+
require(weight <= MAX_WEIGHT, WeightOutOfBound());
|
|
23
28
|
|
|
24
29
|
if (weight == MAX_WEIGHT)
|
|
25
30
|
return IntegralMath.mulDivF(amount, supply, balance);
|
|
@@ -39,9 +44,9 @@ library BondingCurve {
|
|
|
39
44
|
* @return balance * (1 - (1 - amount / supply) ^ (MAX_WEIGHT / weight))
|
|
40
45
|
*/
|
|
41
46
|
function sell(uint256 supply, uint256 balance, uint256 weight, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
42
|
-
require(supply > 0 && balance > 0 && weight > 0,
|
|
43
|
-
require(weight <= MAX_WEIGHT,
|
|
44
|
-
require(amount <= supply,
|
|
47
|
+
require(supply > 0 && balance > 0 && weight > 0, InvalidInput());
|
|
48
|
+
require(weight <= MAX_WEIGHT, WeightOutOfBound());
|
|
49
|
+
require(amount <= supply, AmountLargerThanSupply());
|
|
45
50
|
|
|
46
51
|
if (amount == supply)
|
|
47
52
|
return balance;
|
|
@@ -65,8 +70,8 @@ library BondingCurve {
|
|
|
65
70
|
* @return balance2 * (1 - (balance1 / (balance1 + amount)) ^ (weight1 / weight2))
|
|
66
71
|
*/
|
|
67
72
|
function convert(uint256 balance1, uint256 weight1, uint256 balance2, uint256 weight2, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
68
|
-
require(balance1 > 0 && balance2 > 0 && weight1 > 0 && weight2 > 0,
|
|
69
|
-
require(weight1 <= MAX_WEIGHT && weight2 <= MAX_WEIGHT,
|
|
73
|
+
require(balance1 > 0 && balance2 > 0 && weight1 > 0 && weight2 > 0, InvalidInput());
|
|
74
|
+
require(weight1 <= MAX_WEIGHT && weight2 <= MAX_WEIGHT, WeightsOutOfBound());
|
|
70
75
|
|
|
71
76
|
if (weight1 == weight2)
|
|
72
77
|
return IntegralMath.mulDivF(balance2, amount, safeAdd(balance1, amount));
|
|
@@ -86,8 +91,8 @@ library BondingCurve {
|
|
|
86
91
|
* @return supply * ((amount / balance + 1) ^ (weights / MAX_WEIGHT) - 1)
|
|
87
92
|
*/
|
|
88
93
|
function deposit(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
89
|
-
require(supply > 0 && balance > 0 && weights > 0,
|
|
90
|
-
require(weights <= MAX_WEIGHT * 2,
|
|
94
|
+
require(supply > 0 && balance > 0 && weights > 0, InvalidInput());
|
|
95
|
+
require(weights <= MAX_WEIGHT * 2, WeightsOutOfBound());
|
|
91
96
|
|
|
92
97
|
if (weights == MAX_WEIGHT)
|
|
93
98
|
return IntegralMath.mulDivF(amount, supply, balance);
|
|
@@ -107,9 +112,9 @@ library BondingCurve {
|
|
|
107
112
|
* @return balance * (1 - ((supply - amount) / supply) ^ (MAX_WEIGHT / weights))
|
|
108
113
|
*/
|
|
109
114
|
function withdraw(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
110
|
-
require(supply > 0 && balance > 0 && weights > 0,
|
|
111
|
-
require(weights <= MAX_WEIGHT * 2,
|
|
112
|
-
require(amount <= supply,
|
|
115
|
+
require(supply > 0 && balance > 0 && weights > 0, InvalidInput());
|
|
116
|
+
require(weights <= MAX_WEIGHT * 2, WeightsOutOfBound());
|
|
117
|
+
require(amount <= supply, AmountLargerThanSupply());
|
|
113
118
|
|
|
114
119
|
if (amount == supply)
|
|
115
120
|
return balance;
|
|
@@ -132,8 +137,8 @@ library BondingCurve {
|
|
|
132
137
|
* @return balance * (((supply + amount) / supply) ^ (MAX_WEIGHT / weights) - 1)
|
|
133
138
|
*/
|
|
134
139
|
function invest(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
|
|
135
|
-
require(supply > 0 && balance > 0 && weights > 0,
|
|
136
|
-
require(weights <= MAX_WEIGHT * 2,
|
|
140
|
+
require(supply > 0 && balance > 0 && weights > 0, InvalidInput());
|
|
141
|
+
require(weights <= MAX_WEIGHT * 2, WeightsOutOfBound());
|
|
137
142
|
|
|
138
143
|
if (weights == MAX_WEIGHT)
|
|
139
144
|
return IntegralMath.mulDivC(amount, balance, supply);
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.34;
|
|
3
3
|
|
|
4
4
|
import "./AdvancedMath.sol";
|
|
5
5
|
import "./FractionMath.sol";
|
|
6
6
|
|
|
7
7
|
library DynamicCurve {
|
|
8
|
+
error InvalidRate();
|
|
9
|
+
error InvalidBalance();
|
|
10
|
+
|
|
8
11
|
uint256 internal constant MAX_WEIGHT = 1000000;
|
|
9
12
|
|
|
10
13
|
// Abstract:
|
|
@@ -92,10 +95,10 @@ library DynamicCurve {
|
|
|
92
95
|
function (uint256, uint256, uint256, uint256) pure returns (uint256, uint256) AdvancedMath_solveFunction
|
|
93
96
|
) private pure returns (uint256, uint256) { unchecked {
|
|
94
97
|
if (t == s)
|
|
95
|
-
require(t > 0 || r > 0,
|
|
98
|
+
require(t > 0 || r > 0, InvalidBalance());
|
|
96
99
|
else
|
|
97
|
-
require(t > 0 && s > 0 && r > 0,
|
|
98
|
-
require(q > 0 && p > 0,
|
|
100
|
+
require(t > 0 && s > 0 && r > 0, InvalidBalance());
|
|
101
|
+
require(q > 0 && p > 0, InvalidRate());
|
|
99
102
|
|
|
100
103
|
(uint256 tq, uint256 rp) = FractionMath.productRatio(t, q, r, p);
|
|
101
104
|
(uint256 xn, uint256 xd) = AdvancedMath_solveFunction(s, t, tq, rp);
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.34;
|
|
3
3
|
|
|
4
4
|
import "./IntegralMath.sol";
|
|
5
5
|
|
|
6
6
|
library FractionMath {
|
|
7
|
+
error ExpTooLarge();
|
|
8
|
+
|
|
7
9
|
uint256 internal constant MAX_EXP_BIT_LEN = 4;
|
|
8
10
|
uint256 internal constant MAX_EXP = 2 ** MAX_EXP_BIT_LEN - 1;
|
|
9
11
|
uint256 internal constant MAX_UINT128 = type(uint128).max;
|
|
@@ -105,7 +107,7 @@ library FractionMath {
|
|
|
105
107
|
uint256 n, uint256 d, uint256 exp,
|
|
106
108
|
function (uint256, uint256, uint256, uint256) pure returns (uint256, uint256) safeRatio
|
|
107
109
|
) private pure returns (uint256, uint256) { unchecked {
|
|
108
|
-
require(exp <= MAX_EXP,
|
|
110
|
+
require(exp <= MAX_EXP, ExpTooLarge());
|
|
109
111
|
|
|
110
112
|
uint256[MAX_EXP_BIT_LEN] memory ns;
|
|
111
113
|
uint256[MAX_EXP_BIT_LEN] memory ds;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity ^0.8.
|
|
2
|
+
pragma solidity ^0.8.34;
|
|
3
3
|
|
|
4
4
|
import "./common/Uint256.sol";
|
|
5
5
|
|
|
6
6
|
library IntegralMath {
|
|
7
|
+
error Overflow();
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* @dev Compute the largest integer smaller than or equal to the binary logarithm of `n`
|
|
9
11
|
*/
|
|
@@ -134,7 +136,7 @@ library IntegralMath {
|
|
|
134
136
|
*/
|
|
135
137
|
function mulShrF(uint256 x, uint256 y, uint8 s) internal pure returns (uint256) { unchecked {
|
|
136
138
|
(uint256 xyh, uint256 xyl) = mul512(x, y);
|
|
137
|
-
require(xyh < 1 << s);
|
|
139
|
+
require(xyh < 1 << s, Overflow());
|
|
138
140
|
return (xyh << (256 - s)) | (xyl >> s);
|
|
139
141
|
}}
|
|
140
142
|
|
|
@@ -167,7 +169,7 @@ library IntegralMath {
|
|
|
167
169
|
uint256 r = inv256(z / p); // `z / p = 1 mod 2` hence `inverse(z / p) = 1 mod 2 ^ 256`
|
|
168
170
|
return unsafeMul(q, r); // `q * r = (n / p) * inverse(z / p) = n / z`
|
|
169
171
|
}
|
|
170
|
-
revert(); // `x * y / z >= 2 ^ 256`
|
|
172
|
+
revert Overflow(); // `x * y / z >= 2 ^ 256`
|
|
171
173
|
}}
|
|
172
174
|
|
|
173
175
|
/**
|