solidity-math-utils 1.11.0 → 2.0.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/README.md +49 -89
- package/package.json +3 -3
- package/project/contracts/AdvancedMath.sol +309 -187
- package/project/contracts/AnalyticMath.sol +75 -321
- package/project/contracts/BondingCurve.sol +30 -52
- package/project/contracts/DynamicCurve.sol +73 -31
- package/project/contracts/FractionMath.sol +99 -65
- package/project/contracts/IntegralMath.sol +73 -4
- package/project/contracts/common/{Uint.sol → Uint256.sol} +1 -6
- package/readme/AdvancedMath.pdf +0 -0
- package/readme/AdvancedMath.txt +0 -44
- package/readme/DynamicCurve.pdf +0 -0
- package/readme/DynamicCurve.txt +0 -77
|
@@ -1,72 +1,266 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.28;
|
|
3
3
|
|
|
4
4
|
import "./AnalyticMath.sol";
|
|
5
5
|
import "./FractionMath.sol";
|
|
6
|
+
import "./IntegralMath.sol";
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
uint256 internal constant LAMBERT_CONV_RADIUS = 0x002f16ac6c59de6f8d5d6f63c1482a7c86;
|
|
10
|
-
uint256 internal constant LAMBERT_POS2_SAMPLE = 0x0003060c183060c183060c183060c18306;
|
|
11
|
-
uint256 internal constant LAMBERT_POS2_MAXVAL = 0x01af16ac6c59de6f8d5d6f63c1482a7c80;
|
|
12
|
-
uint256 internal constant LAMBERT_POS3_MAXVAL = 0x6b22d43e72c326539cceeef8bb48f255ff;
|
|
8
|
+
library AdvancedMath {
|
|
9
|
+
uint256 internal constant FIXED_1 = AnalyticMath.FIXED_1;
|
|
13
10
|
|
|
14
|
-
|
|
11
|
+
// Auto-generated via 'PrintAdvancedMathConstants.py'
|
|
12
|
+
uint256 internal constant LAMBERT_NEG1_MAXVAL = 0x002e9e207581ee21a2fdc7e0328c907634;
|
|
13
|
+
uint256 internal constant LAMBERT_NEG2_MAXVAL = 0x002f16ac6c59de6f8d5d6f63c1482a7c86;
|
|
14
|
+
uint256 internal constant LAMBERT_NEG2_SAMPLE = 0x0000080954b9100531c21c3bf872e8228e;
|
|
15
|
+
uint256 internal constant LAMBERT_NEG2_T_SIZE = 0x0000000000000000000000000000000011;
|
|
16
|
+
uint256 internal constant LAMBERT_NEG2_T_MASK = 0xffffffffffffffffffffffffffffffffff;
|
|
17
|
+
uint256 internal constant LAMBERT_POS1_MAXVAL = 0x002f16ac6c59de6f8d5d6f63c1482a7c86;
|
|
18
|
+
uint256 internal constant LAMBERT_POS2_MAXVAL = 0x0c2f16ac6c59de6f8d5d6f63c1482a7c56;
|
|
19
|
+
uint256 internal constant LAMBERT_POS2_SAMPLE = 0x00183060c183060c183060c183060c1830;
|
|
20
|
+
uint256 internal constant LAMBERT_POS2_T_SIZE = 0x0000000000000000000000000000000010;
|
|
21
|
+
uint256 internal constant LAMBERT_POS2_T_MASK = 0x00ffffffffffffffffffffffffffffffff;
|
|
22
|
+
bytes internal constant LAMBERT_NEG2_VALUES = hex"012ff3f5b94a776318e70a4b0be31da9a5"
|
|
23
|
+
hex"01314a4702ae8ae09a01a03f6933dcf01f"
|
|
24
|
+
hex"0132afb4b8985b32518cd371f2b5c6de56"
|
|
25
|
+
hex"0134260eac628aa55b0b35bd5ba1df0e6e"
|
|
26
|
+
hex"0135af881e5cbc17f5de16bb05816826c0"
|
|
27
|
+
hex"01374ed8520d0404e91fe8e683b8d8173e"
|
|
28
|
+
hex"0139076a4780adf3619a57f1b6e40e3f20"
|
|
29
|
+
hex"013adda534e8e43471fe0ae32b8bbf0cfc"
|
|
30
|
+
hex"013cd75f834551aa105eff810be8afd15e"
|
|
31
|
+
hex"013efc9f6e12358e1f8c040050efadd3e2"
|
|
32
|
+
hex"014158f4e68e9217def733dc63fba7eba9"
|
|
33
|
+
hex"0143fe22b87c9944205ebc3be59f410691"
|
|
34
|
+
hex"01470a158303eabda73222305243e7c7b2"
|
|
35
|
+
hex"014ab733a071f0dfc80ab6c42b29b5ce07"
|
|
36
|
+
hex"014f9c668b59483575a0daad8006952948"
|
|
37
|
+
hex"015aa59d425cbd5ed5a6a64da5a1108555";
|
|
38
|
+
bytes internal constant LAMBERT_POS2_VALUES = hex"60e393c68d20b1bd09deaabc0373b9c5"
|
|
39
|
+
hex"577b97aa1fe222bb452fdf111b1f0be2"
|
|
40
|
+
hex"5035f241d6eae0cd7bacba119993de7b"
|
|
41
|
+
hex"4a5cbc96a05589cb4d86be1db3168364"
|
|
42
|
+
hex"4585c8b3f8fe489c6e1833ca47871384"
|
|
43
|
+
hex"416e1b785d13eba07a08f3f18876a5ab"
|
|
44
|
+
hex"3de8f65ac388101ddf718a6f5c1eff65"
|
|
45
|
+
hex"3ad71c1c77e34fa32a9f184967eccbf6"
|
|
46
|
+
hex"3821f57dbd2763256c1a99bbd2051378"
|
|
47
|
+
hex"35b8b28d1a73dc27500ffe35559cc028"
|
|
48
|
+
hex"338e82ce00e2496262c64457535ba1a1"
|
|
49
|
+
hex"31996ec6b07b4a83421b5ebc4ab4e1f1"
|
|
50
|
+
hex"2fd19346ed17dac61219ce0c2c5ac4b0"
|
|
51
|
+
hex"2e309a221c12ba361e3ed695167feee2"
|
|
52
|
+
hex"2cb15ad3a1eb65f6d74a75da09a1b6c5"
|
|
53
|
+
hex"2b4f95cd46904d05d72bdcde337d9cc7"
|
|
54
|
+
hex"2a07c206cde2c9467e42ebdfc395e285"
|
|
55
|
+
hex"28d6e752efcbae881bb2163e088236de"
|
|
56
|
+
hex"27ba81d008f57136440e87f44fb95f59"
|
|
57
|
+
hex"26b06bf4b3ca8a8e6b5f1207cf63e484"
|
|
58
|
+
hex"25b6cd7b40b306b014cc93eb0fd34b1b"
|
|
59
|
+
hex"24cc0df118d221d961d0e3c7c2cc74d8"
|
|
60
|
+
hex"23eeca079ec9f001c7488cb0210969d3"
|
|
61
|
+
hex"231dcb016e2797ddce1ba75a69081655"
|
|
62
|
+
hex"2257ffc1f84048f54f9072007f255977"
|
|
63
|
+
hex"219c772441c4b78d49d6bf72dea3ab56"
|
|
64
|
+
hex"20ea5b53c3a2de83f69655808913322d"
|
|
65
|
+
hex"2040edf2ba98675afaf33ad7b36a7956"
|
|
66
|
+
hex"1f9f84e53efd744e8c01d02e4f1071c8"
|
|
67
|
+
hex"1f0587a1831d516e4979ad85cd0b3e5a"
|
|
68
|
+
hex"1e726cec66b24906997bda3801535ebd"
|
|
69
|
+
hex"1de5b8eebff06b21b543b19c5fa002d9"
|
|
70
|
+
hex"1d5efb93b9235c7a64079fab2ad709ba"
|
|
71
|
+
hex"1cddcf23ba8eb156b9b26750904cf312"
|
|
72
|
+
hex"1c61d711c357ed86ebefbfee82d3264a"
|
|
73
|
+
hex"1beabef2fb396b13011f8ea801422ad9"
|
|
74
|
+
hex"1b783999c85b7cd9ce638b020080d100"
|
|
75
|
+
hex"1b0a004ee7fd8c81ac47b6c04894bb04"
|
|
76
|
+
hex"1a9fd223fef4735d6e1130db2341072b"
|
|
77
|
+
hex"1a39735bdda3a6d7e2b3d1dd5d800661"
|
|
78
|
+
hex"19d6ace55419021d7827019f435c30c5"
|
|
79
|
+
hex"19774be5f6227a7ba0b2e9c5d78c13c8"
|
|
80
|
+
hex"191b21529a9dc854272046bfe7592441"
|
|
81
|
+
hex"18c20193b99c0f235d36b322031296a9"
|
|
82
|
+
hex"186bc43415f31f813caf1fb4300e23c9"
|
|
83
|
+
hex"181843985b6794d75c8c626a51041977"
|
|
84
|
+
hex"17c75cbe8d10c6063ecfe590c8a0fd84"
|
|
85
|
+
hex"1778ef0449cafac4a5cf259d93863693"
|
|
86
|
+
hex"172cdbf30ff93902a95a8146b585c655"
|
|
87
|
+
hex"16e30711c7afebcb40109634b86d8799"
|
|
88
|
+
hex"169b55baf39bccd230485248f4995e18"
|
|
89
|
+
hex"1655aef6fe5d98ce041a10e7ca963ad3"
|
|
90
|
+
hex"1611fb5a2c46df8f23786225867f6d08"
|
|
91
|
+
hex"15d024e5c8ecb766b45e74cdc8172794"
|
|
92
|
+
hex"159016ec3550613e36a46d481b57d4a2"
|
|
93
|
+
hex"1551bdf786c6d4b006e447ee93fe60bf"
|
|
94
|
+
hex"151507b27099e70da8f1197ca7a94b1e"
|
|
95
|
+
hex"14d9e2d33ad6bc2f8f1e6a949032aae3"
|
|
96
|
+
hex"14a03f0890117064244d99699b33162a"
|
|
97
|
+
hex"14680ce7f240e96bd25479e66a1a1872"
|
|
98
|
+
hex"14313dddac5beb6ae0dc9ab15e6325a4"
|
|
99
|
+
hex"13fbc41e1b3092ef02d998374545ec41"
|
|
100
|
+
hex"13c792982c23053712ec80994a00d48b"
|
|
101
|
+
hex"13949ce8f42d5c83b87967913b857db2"
|
|
102
|
+
hex"1362d75044b414864af094d1c98c5045"
|
|
103
|
+
hex"133236a6269740b8ed72a7216b473521"
|
|
104
|
+
hex"1302b05126670d0c809cfdf596bf59aa"
|
|
105
|
+
hex"12d43a3d5ed4e587f9bf9f3546a14cc9"
|
|
106
|
+
hex"12a6cad4306bf1e228555efcf9e981b3"
|
|
107
|
+
hex"127a58f49753c126c34fccd0fb33a40a"
|
|
108
|
+
hex"124edbec11664bc999baaffb949ae84c"
|
|
109
|
+
hex"12244b70083d14d53392cc3d4aec745c"
|
|
110
|
+
hex"11fa9f97b40d9011194c94a270e6c582"
|
|
111
|
+
hex"11d1d0d66d3fb456945cf53f8cddd542"
|
|
112
|
+
hex"11a9d7f6639ed17b5ab53f819bad9299"
|
|
113
|
+
hex"1182ae13b2dfe31bd4041aed11d1bc02"
|
|
114
|
+
hex"115c4c97ccfcbb784459d0fc547c65dc"
|
|
115
|
+
hex"1136ad3533932db11c651bec0f863f9f"
|
|
116
|
+
hex"1111c9e37a1528d2e3a11bbe1671030d"
|
|
117
|
+
hex"10ed9cdb8b2486b23f5197f432dfedde"
|
|
118
|
+
hex"10ca20942bf4f38da855bc16829f7176"
|
|
119
|
+
hex"10a74fbeb90156f2ef7da0e6ff154ef2"
|
|
120
|
+
hex"1085254417c9ec4689192684fd24d9fb"
|
|
121
|
+
hex"10639c41d9adf82e9a962729d9c5354b"
|
|
122
|
+
hex"1042b0078c48d2121c45356ef952a884"
|
|
123
|
+
hex"10225c143406bfe3646184b22ff0b242"
|
|
124
|
+
hex"10029c13edebbcadcd02f4bf6077e934"
|
|
125
|
+
hex"0fe36bddb5c56d8889ed2aa2164563dd"
|
|
126
|
+
hex"0fc4c7714e3aeceb64a83c0fbd5b3409"
|
|
127
|
+
hex"0fa6aaf54861476764473f34e82d3a9c"
|
|
128
|
+
hex"0f8912b528ab0b6a790407fa5f9bf8b1"
|
|
129
|
+
hex"0f6bfb1fa7349e64712ab860554d7b78"
|
|
130
|
+
hex"0f4f60c509968df5add12deae5027309"
|
|
131
|
+
hex"0f334055948b2a0dc667a844c6723b23"
|
|
132
|
+
hex"0f1796a013d5b15214dc09abf2a0592b"
|
|
133
|
+
hex"0efc609076f78738f15c360223f8c79d"
|
|
134
|
+
hex"0ee19b2e815b8e957ce06c5e16449ca8"
|
|
135
|
+
hex"0ec7439c8cb9187b324eb1d2758ee4d3"
|
|
136
|
+
hex"0ead57165c8714db919b9ccd15e45159"
|
|
137
|
+
hex"0e93d2f0016d88ff1e5182897a357740"
|
|
138
|
+
hex"0e7ab494cbb6ec0c4cbc602eb4bb78e3"
|
|
139
|
+
hex"0e61f9864bd512e429ac81729beeec69"
|
|
140
|
+
hex"0e499f5b601dee819305eb00790b9c9a"
|
|
141
|
+
hex"0e31a3bf4ef3ab0dadc11b18d962c820"
|
|
142
|
+
hex"0e1a0470ec99d736c6436de2abaf3b64"
|
|
143
|
+
hex"0e02bf41cc063d700f4cb7817df3dfd7"
|
|
144
|
+
hex"0debd2157a081b83117c46ea1dc579d1"
|
|
145
|
+
hex"0dd53ae0c22b7df344fa51532b60dd98"
|
|
146
|
+
hex"0dbef7a8fcc8c7c9c5332ac339c9f88d"
|
|
147
|
+
hex"0da9068365b9ee4b304fb9666adccf46"
|
|
148
|
+
hex"0d9365947b37bb14237ee7d1802ffa2f"
|
|
149
|
+
hex"0d7e130f64698ce1098c56ddad89fb56"
|
|
150
|
+
hex"0d690d355f399d48cad4f9225f1a3f9d"
|
|
151
|
+
hex"0d5452553506d42a1f2f7d5cf97679d9"
|
|
152
|
+
hex"0d3fe0cab5d3b39f329597f217bf7928"
|
|
153
|
+
hex"0d2bb6fe3997f32783f2b5d8a3acd0c9"
|
|
154
|
+
hex"0d17d364275fffa6cb83e0553ca42663"
|
|
155
|
+
hex"0d04347c81ead056e793c81ed9aa583e"
|
|
156
|
+
hex"0cf0d8d2797b5e9e40afe04977971655"
|
|
157
|
+
hex"0cddbefc029796ff51a1c94f6c2e5589"
|
|
158
|
+
hex"0ccae5997172d28b9d7969676b6cfb06"
|
|
159
|
+
hex"0cb84b5519c5d86400e5ebbcb895c475"
|
|
160
|
+
hex"0ca5eee2f2da13533492e3978c84b997"
|
|
161
|
+
hex"0c93cf003f91157bbee2579fcf0a33f3"
|
|
162
|
+
hex"0c81ea733a34b805f2bf6edd83fca2cd"
|
|
163
|
+
hex"0c70400ac3df22f2e73d2a54bee3b3ee"
|
|
164
|
+
hex"0c5ece9e174cd59d6c32f900259dd921"
|
|
165
|
+
hex"0c4d950c7eed66993aa3f25a85ea9e43";
|
|
166
|
+
uint256 internal constant LAMBERT_EXACT_LIMIT = 0x001abb58194f861a372fc4a5e2a64806c77ee7849dd0a347ea7ddf2ba582711b;
|
|
15
167
|
|
|
16
168
|
/**
|
|
17
|
-
* @dev
|
|
169
|
+
* @dev Solve x * (a / b) ^ x = c / d
|
|
170
|
+
* Solution: x = W(log(a / b) * c / d) / (log(a / b) * c / d) * c / d
|
|
18
171
|
*/
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
172
|
+
function solveExact(uint256 a, uint256 b, uint256 c, uint256 d) internal pure returns (uint256, uint256) { unchecked {
|
|
173
|
+
return solve(a, b, c, d, lambertNegExact, lambertPosExact);
|
|
174
|
+
}}
|
|
23
175
|
|
|
24
176
|
/**
|
|
25
177
|
* @dev Solve x * (a / b) ^ x = c / d
|
|
26
178
|
* Solution: x = W(log(a / b) * c / d) / (log(a / b) * c / d) * c / d
|
|
27
179
|
*/
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
180
|
+
function solveQuick(uint256 a, uint256 b, uint256 c, uint256 d) internal pure returns (uint256, uint256) { unchecked {
|
|
181
|
+
return solve(a, b, c, d, lambertNegQuick, lambertPosQuick);
|
|
182
|
+
}}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @dev Compute W(-x / FIXED_1) / (-x / FIXED_1) * FIXED_1
|
|
186
|
+
* Input range: 1 <= x <= LAMBERT_NEG2_MAXVAL
|
|
187
|
+
*/
|
|
188
|
+
function lambertNegExact(uint256 x) internal pure returns (uint256) { unchecked {
|
|
189
|
+
require(x > 0, "lambertNegExact: x < min");
|
|
190
|
+
if (x <= LAMBERT_NEG2_MAXVAL) {
|
|
191
|
+
uint256 y = x;
|
|
192
|
+
for (uint256 i = 0; i < 8; ++i) {
|
|
193
|
+
uint256 e = AnalyticMath.fixedExp(y);
|
|
194
|
+
y = (x * e - y * y) / (FIXED_1 - y);
|
|
195
|
+
}
|
|
196
|
+
return IntegralMath.mulDivF(FIXED_1, y, x);
|
|
197
|
+
}
|
|
198
|
+
revert("lambertNegExact: x > max");
|
|
199
|
+
}}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @dev Compute W(x / FIXED_1) / (x / FIXED_1) * FIXED_1
|
|
203
|
+
* Input range: 1 <= x <= 2 ^ 256 - 1
|
|
204
|
+
*/
|
|
205
|
+
function lambertPosExact(uint256 x) internal pure returns (uint256) { unchecked {
|
|
206
|
+
require(x > 0, "lambertPosExact: x < min");
|
|
207
|
+
if (x <= LAMBERT_EXACT_LIMIT) {
|
|
208
|
+
uint256 y;
|
|
209
|
+
if (x < FIXED_1) {
|
|
210
|
+
uint256 p = AnalyticMath.fixedExp(x);
|
|
211
|
+
uint256 q = IntegralMath.mulDivF(x, p, FIXED_1);
|
|
212
|
+
y = IntegralMath.mulDivF(x, q + FIXED_1, q + p);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
uint256 p = AnalyticMath.fixedLog(x);
|
|
216
|
+
uint256 q = IntegralMath.mulDivF(p, p, FIXED_1);
|
|
217
|
+
y = IntegralMath.mulDivF(FIXED_1, q + FIXED_1, p + FIXED_1);
|
|
218
|
+
}
|
|
219
|
+
for (uint256 i = 0; i < 7; ++i) {
|
|
220
|
+
uint256 e = AnalyticMath.fixedExp(y);
|
|
221
|
+
uint256 f = IntegralMath.mulDivF(y, e, FIXED_1);
|
|
222
|
+
uint256 g = IntegralMath.mulDivF(y, f, FIXED_1);
|
|
223
|
+
y = IntegralMath.mulDivF(FIXED_1, g + x, f + e);
|
|
224
|
+
}
|
|
225
|
+
return IntegralMath.mulDivF(FIXED_1, y, x);
|
|
226
|
+
}
|
|
227
|
+
return lambertPos3(x);
|
|
34
228
|
}}
|
|
35
229
|
|
|
36
230
|
/**
|
|
37
231
|
* @dev Compute W(-x / FIXED_1) / (-x / FIXED_1) * FIXED_1
|
|
38
|
-
* Input range: 1 <= x <=
|
|
232
|
+
* Input range: 1 <= x <= LAMBERT_NEG2_MAXVAL
|
|
39
233
|
*/
|
|
40
|
-
function
|
|
41
|
-
require(x > 0, "
|
|
42
|
-
if (x <=
|
|
234
|
+
function lambertNegQuick(uint256 x) internal pure returns (uint256) { unchecked {
|
|
235
|
+
require(x > 0, "lambertNegQuick: x < min");
|
|
236
|
+
if (x <= LAMBERT_NEG1_MAXVAL)
|
|
43
237
|
return lambertNeg1(x);
|
|
44
|
-
|
|
238
|
+
if (x <= LAMBERT_NEG2_MAXVAL)
|
|
239
|
+
return lambertNeg2(x);
|
|
240
|
+
revert("lambertNegQuick: x > max");
|
|
45
241
|
}}
|
|
46
242
|
|
|
47
243
|
/**
|
|
48
244
|
* @dev Compute W(x / FIXED_1) / (x / FIXED_1) * FIXED_1
|
|
49
|
-
* Input range: 1 <= x <=
|
|
245
|
+
* Input range: 1 <= x <= 2 ^ 256 - 1
|
|
50
246
|
*/
|
|
51
|
-
function
|
|
52
|
-
require(x > 0, "
|
|
53
|
-
if (x <=
|
|
247
|
+
function lambertPosQuick(uint256 x) internal pure returns (uint256) { unchecked {
|
|
248
|
+
require(x > 0, "lambertPosQuick: x < min");
|
|
249
|
+
if (x <= LAMBERT_POS1_MAXVAL)
|
|
54
250
|
return lambertPos1(x);
|
|
55
251
|
if (x <= LAMBERT_POS2_MAXVAL)
|
|
56
252
|
return lambertPos2(x);
|
|
57
|
-
|
|
58
|
-
return lambertPos3(x);
|
|
59
|
-
revert("lambertPos: x > max");
|
|
253
|
+
return lambertPos3(x);
|
|
60
254
|
}}
|
|
61
255
|
|
|
62
256
|
/**
|
|
63
257
|
* @dev Compute W(-x / FIXED_1) / (-x / FIXED_1) * FIXED_1
|
|
64
|
-
* Input range: 1 <= x <=
|
|
65
|
-
* Auto-generated via '
|
|
258
|
+
* Input range: 1 <= x <= LAMBERT_NEG1_MAXVAL
|
|
259
|
+
* Auto-generated via 'PrintAdvancedMathLambertNeg1.py'
|
|
66
260
|
*/
|
|
67
261
|
function lambertNeg1(uint256 x) internal pure returns (uint256) { unchecked {
|
|
68
|
-
uint256 xi = x;
|
|
69
262
|
uint256 res = 0;
|
|
263
|
+
uint256 xi = x;
|
|
70
264
|
|
|
71
265
|
xi = (xi * x) / FIXED_1; res += xi * 0x00000000014d29a73a6e7b02c3668c7b0880000000; // add x^(03-1) * (34! * 03^(03-1) / 03!)
|
|
72
266
|
xi = (xi * x) / FIXED_1; res += xi * 0x0000000002504a0cd9a7f7215b60f9be4800000000; // add x^(04-1) * (34! * 04^(04-1) / 04!)
|
|
@@ -101,17 +295,17 @@ contract AdvancedMath is AnalyticMath {
|
|
|
101
295
|
xi = (xi * x) / FIXED_1; res += xi * 0x5b0485a76f6646c2039db1507cdd51b08649680822; // add x^(33-1) * (34! * 33^(33-1) / 33!)
|
|
102
296
|
xi = (xi * x) / FIXED_1; res += xi * 0xec983c46c49545bc17efa6b5b0055e242200000000; // add x^(34-1) * (34! * 34^(34-1) / 34!)
|
|
103
297
|
|
|
104
|
-
return res / 0xde1bc4d19efcac82445da75b00000000 +
|
|
298
|
+
return res / 0xde1bc4d19efcac82445da75b00000000 + FIXED_1 + x; // divide by 34! and then add x^(1-1) * (1^(1-1) / 1!) + x^(2-1) * (2^(2-1) / 2!)
|
|
105
299
|
}}
|
|
106
300
|
|
|
107
301
|
/**
|
|
108
302
|
* @dev Compute W(x / FIXED_1) / (x / FIXED_1) * FIXED_1
|
|
109
|
-
* Input range: 1 <= x <=
|
|
110
|
-
* Auto-generated via '
|
|
303
|
+
* Input range: 1 <= x <= LAMBERT_POS1_MAXVAL
|
|
304
|
+
* Auto-generated via 'PrintAdvancedMathLambertPos1.py'
|
|
111
305
|
*/
|
|
112
306
|
function lambertPos1(uint256 x) internal pure returns (uint256) { unchecked {
|
|
307
|
+
uint256 res = 0;
|
|
113
308
|
uint256 xi = x;
|
|
114
|
-
uint256 res = (FIXED_1 - x) * 0xde1bc4d19efcac82445da75b00000000; // x^(1-1) * (34! * 1^(1-1) / 1!) - x^(2-1) * (34! * 2^(2-1) / 2!)
|
|
115
309
|
|
|
116
310
|
xi = (xi * x) / FIXED_1; res += xi * 0x00000000014d29a73a6e7b02c3668c7b0880000000; // add x^(03-1) * (34! * 03^(03-1) / 03!)
|
|
117
311
|
xi = (xi * x) / FIXED_1; res -= xi * 0x0000000002504a0cd9a7f7215b60f9be4800000000; // sub x^(04-1) * (34! * 04^(04-1) / 04!)
|
|
@@ -146,171 +340,99 @@ contract AdvancedMath is AnalyticMath {
|
|
|
146
340
|
xi = (xi * x) / FIXED_1; res += xi * 0x5b0485a76f6646c2039db1507cdd51b08649680822; // add x^(33-1) * (34! * 33^(33-1) / 33!)
|
|
147
341
|
xi = (xi * x) / FIXED_1; res -= xi * 0xec983c46c49545bc17efa6b5b0055e242200000000; // sub x^(34-1) * (34! * 34^(34-1) / 34!)
|
|
148
342
|
|
|
149
|
-
return res / 0xde1bc4d19efcac82445da75b00000000; // divide by 34!
|
|
343
|
+
return res / 0xde1bc4d19efcac82445da75b00000000 + FIXED_1 - x; // divide by 34! and then add x^(1-1) * (1^(1-1) / 1!) - x^(2-1) * (2^(2-1) / 2!)
|
|
344
|
+
}}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* @dev Compute W(-x / FIXED_1) / (-x / FIXED_1) * FIXED_1
|
|
348
|
+
* Input range: LAMBERT_NEG1_MAXVAL + 1 <= x <= LAMBERT_NEG2_MAXVAL
|
|
349
|
+
*/
|
|
350
|
+
function lambertNeg2(uint256 x) internal pure returns (uint256) { unchecked {
|
|
351
|
+
bytes memory values = LAMBERT_NEG2_VALUES;
|
|
352
|
+
uint256 y = x - LAMBERT_NEG1_MAXVAL - 1;
|
|
353
|
+
uint256 i = y / LAMBERT_NEG2_SAMPLE;
|
|
354
|
+
uint256 a = LAMBERT_NEG2_SAMPLE * (i + 0);
|
|
355
|
+
uint256 b = LAMBERT_NEG2_SAMPLE * (i + 1);
|
|
356
|
+
uint256 c = LAMBERT_NEG2_T_SIZE * (i + 1);
|
|
357
|
+
uint256 d = LAMBERT_NEG2_T_SIZE * (i + 2);
|
|
358
|
+
uint256 e = read(values, c) & LAMBERT_NEG2_T_MASK;
|
|
359
|
+
uint256 f = read(values, d) & LAMBERT_NEG2_T_MASK;
|
|
360
|
+
uint256 g = e * (b - y);
|
|
361
|
+
uint256 h = f * (y - a);
|
|
362
|
+
return (g + h) / LAMBERT_NEG2_SAMPLE;
|
|
150
363
|
}}
|
|
151
364
|
|
|
152
365
|
/**
|
|
153
366
|
* @dev Compute W(x / FIXED_1) / (x / FIXED_1) * FIXED_1
|
|
154
|
-
* Input range:
|
|
367
|
+
* Input range: LAMBERT_POS1_MAXVAL + 1 <= x <= LAMBERT_POS2_MAXVAL
|
|
155
368
|
*/
|
|
156
|
-
function lambertPos2(uint256 x) internal
|
|
157
|
-
|
|
369
|
+
function lambertPos2(uint256 x) internal pure returns (uint256) { unchecked {
|
|
370
|
+
bytes memory values = LAMBERT_POS2_VALUES;
|
|
371
|
+
uint256 y = x - LAMBERT_POS1_MAXVAL - 1;
|
|
158
372
|
uint256 i = y / LAMBERT_POS2_SAMPLE;
|
|
159
|
-
uint256 a = LAMBERT_POS2_SAMPLE * i;
|
|
373
|
+
uint256 a = LAMBERT_POS2_SAMPLE * (i + 0);
|
|
160
374
|
uint256 b = LAMBERT_POS2_SAMPLE * (i + 1);
|
|
161
|
-
uint256 c =
|
|
162
|
-
uint256 d =
|
|
163
|
-
|
|
375
|
+
uint256 c = LAMBERT_POS2_T_SIZE * (i + 1);
|
|
376
|
+
uint256 d = LAMBERT_POS2_T_SIZE * (i + 2);
|
|
377
|
+
uint256 e = read(values, c) & LAMBERT_POS2_T_MASK;
|
|
378
|
+
uint256 f = read(values, d) & LAMBERT_POS2_T_MASK;
|
|
379
|
+
uint256 g = e * (b - y);
|
|
380
|
+
uint256 h = f * (y - a);
|
|
381
|
+
return (g + h) / LAMBERT_POS2_SAMPLE;
|
|
164
382
|
}}
|
|
165
383
|
|
|
166
384
|
/**
|
|
167
385
|
* @dev Compute W(x / FIXED_1) / (x / FIXED_1) * FIXED_1
|
|
168
|
-
* Input range: LAMBERT_POS2_MAXVAL + 1 <= x <=
|
|
386
|
+
* Input range: LAMBERT_POS2_MAXVAL + 1 <= x <= 2 ^ 256 - 1
|
|
169
387
|
*/
|
|
170
388
|
function lambertPos3(uint256 x) internal pure returns (uint256) { unchecked {
|
|
171
|
-
uint256
|
|
172
|
-
uint256
|
|
173
|
-
|
|
389
|
+
uint256 a = AnalyticMath.fixedLog(x);
|
|
390
|
+
uint256 b = AnalyticMath.fixedLog(a);
|
|
391
|
+
uint256 c = IntegralMath.mulDivF(b, b + (a - FIXED_1) * 2, a);
|
|
392
|
+
uint256 d = IntegralMath.mulDivF(FIXED_1 / 2, c, a);
|
|
393
|
+
uint256 e = IntegralMath.mulDivF(FIXED_1, a - b + d, x);
|
|
394
|
+
return e;
|
|
174
395
|
}}
|
|
175
396
|
|
|
176
397
|
/**
|
|
177
|
-
* @dev
|
|
178
|
-
* Auto-generated via 'PrintLambertArray.py'
|
|
398
|
+
* @dev Solve the equation x * (a / b) ^ x = c / d for x
|
|
179
399
|
*/
|
|
180
|
-
function
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
lambertArray[ 10] = 0x557fe8241b3a31c83c732f1cdff4a1c5;
|
|
192
|
-
lambertArray[ 11] = 0x548d868026504875d6e59bbe95fc2a6b;
|
|
193
|
-
lambertArray[ 12] = 0x53a2465ce347cf34d05a867c17dd3088;
|
|
194
|
-
lambertArray[ 13] = 0x52bdce5dcd4faed59c7f5511cf8f8acc;
|
|
195
|
-
lambertArray[ 14] = 0x51dfcb453c07f8da817606e7885f7c3e;
|
|
196
|
-
lambertArray[ 15] = 0x5107ef6b0a5a2be8f8ff15590daa3cce;
|
|
197
|
-
lambertArray[ 16] = 0x5035f241d6eae0cd7bacba119993de7b;
|
|
198
|
-
lambertArray[ 17] = 0x4f698fe90d5b53d532171e1210164c66;
|
|
199
|
-
lambertArray[ 18] = 0x4ea288ca297a0e6a09a0eee240e16c85;
|
|
200
|
-
lambertArray[ 19] = 0x4de0a13fdcf5d4213fc398ba6e3becde;
|
|
201
|
-
lambertArray[ 20] = 0x4d23a145eef91fec06b06140804c4808;
|
|
202
|
-
lambertArray[ 21] = 0x4c6b5430d4c1ee5526473db4ae0f11de;
|
|
203
|
-
lambertArray[ 22] = 0x4bb7886c240562eba11f4963a53b4240;
|
|
204
|
-
lambertArray[ 23] = 0x4b080f3f1cb491d2d521e0ea4583521e;
|
|
205
|
-
lambertArray[ 24] = 0x4a5cbc96a05589cb4d86be1db3168364;
|
|
206
|
-
lambertArray[ 25] = 0x49b566d40243517658d78c33162d6ece;
|
|
207
|
-
lambertArray[ 26] = 0x4911e6a02e5507a30f947383fd9a3276;
|
|
208
|
-
lambertArray[ 27] = 0x487216c2b31be4adc41db8a8d5cc0c88;
|
|
209
|
-
lambertArray[ 28] = 0x47d5d3fc4a7a1b188cd3d788b5c5e9fc;
|
|
210
|
-
lambertArray[ 29] = 0x473cfce4871a2c40bc4f9e1c32b955d0;
|
|
211
|
-
lambertArray[ 30] = 0x46a771ca578ab878485810e285e31c67;
|
|
212
|
-
lambertArray[ 31] = 0x4615149718aed4c258c373dc676aa72d;
|
|
213
|
-
lambertArray[ 32] = 0x4585c8b3f8fe489c6e1833ca47871384;
|
|
214
|
-
lambertArray[ 33] = 0x44f972f174e41e5efb7e9d63c29ce735;
|
|
215
|
-
lambertArray[ 34] = 0x446ff970ba86d8b00beb05ecebf3c4dc;
|
|
216
|
-
lambertArray[ 35] = 0x43e9438ec88971812d6f198b5ccaad96;
|
|
217
|
-
lambertArray[ 36] = 0x436539d11ff7bea657aeddb394e809ef;
|
|
218
|
-
lambertArray[ 37] = 0x42e3c5d3e5a913401d86f66db5d81c2c;
|
|
219
|
-
lambertArray[ 38] = 0x4264d2395303070ea726cbe98df62174;
|
|
220
|
-
lambertArray[ 39] = 0x41e84a9a593bb7194c3a6349ecae4eea;
|
|
221
|
-
lambertArray[ 40] = 0x416e1b785d13eba07a08f3f18876a5ab;
|
|
222
|
-
lambertArray[ 41] = 0x40f6322ff389d423ba9dd7e7e7b7e809;
|
|
223
|
-
lambertArray[ 42] = 0x40807cec8a466880ecf4184545d240a4;
|
|
224
|
-
lambertArray[ 43] = 0x400cea9ce88a8d3ae668e8ea0d9bf07f;
|
|
225
|
-
lambertArray[ 44] = 0x3f9b6ae8772d4c55091e0ed7dfea0ac1;
|
|
226
|
-
lambertArray[ 45] = 0x3f2bee253fd84594f54bcaafac383a13;
|
|
227
|
-
lambertArray[ 46] = 0x3ebe654e95208bb9210c575c081c5958;
|
|
228
|
-
lambertArray[ 47] = 0x3e52c1fc5665635b78ce1f05ad53c086;
|
|
229
|
-
lambertArray[ 48] = 0x3de8f65ac388101ddf718a6f5c1eff65;
|
|
230
|
-
lambertArray[ 49] = 0x3d80f522d59bd0b328ca012df4cd2d49;
|
|
231
|
-
lambertArray[ 50] = 0x3d1ab193129ea72b23648a161163a85a;
|
|
232
|
-
lambertArray[ 51] = 0x3cb61f68d32576c135b95cfb53f76d75;
|
|
233
|
-
lambertArray[ 52] = 0x3c5332d9f1aae851a3619e77e4cc8473;
|
|
234
|
-
lambertArray[ 53] = 0x3bf1e08edbe2aa109e1525f65759ef73;
|
|
235
|
-
lambertArray[ 54] = 0x3b921d9cff13fa2c197746a3dfc4918f;
|
|
236
|
-
lambertArray[ 55] = 0x3b33df818910bfc1a5aefb8f63ae2ac4;
|
|
237
|
-
lambertArray[ 56] = 0x3ad71c1c77e34fa32a9f184967eccbf6;
|
|
238
|
-
lambertArray[ 57] = 0x3a7bc9abf2c5bb53e2f7384a8a16521a;
|
|
239
|
-
lambertArray[ 58] = 0x3a21dec7e76369783a68a0c6385a1c57;
|
|
240
|
-
lambertArray[ 59] = 0x39c9525de6c9cdf7c1c157ca4a7a6ee3;
|
|
241
|
-
lambertArray[ 60] = 0x39721bad3dc85d1240ff0190e0adaac3;
|
|
242
|
-
lambertArray[ 61] = 0x391c324344d3248f0469eb28dd3d77e0;
|
|
243
|
-
lambertArray[ 62] = 0x38c78df7e3c796279fb4ff84394ab3da;
|
|
244
|
-
lambertArray[ 63] = 0x387426ea4638ae9aae08049d3554c20a;
|
|
245
|
-
lambertArray[ 64] = 0x3821f57dbd2763256c1a99bbd2051378;
|
|
246
|
-
lambertArray[ 65] = 0x37d0f256cb46a8c92ff62fbbef289698;
|
|
247
|
-
lambertArray[ 66] = 0x37811658591ffc7abdd1feaf3cef9b73;
|
|
248
|
-
lambertArray[ 67] = 0x37325aa10e9e82f7df0f380f7997154b;
|
|
249
|
-
lambertArray[ 68] = 0x36e4b888cfb408d873b9a80d439311c6;
|
|
250
|
-
lambertArray[ 69] = 0x3698299e59f4bb9de645fc9b08c64cca;
|
|
251
|
-
lambertArray[ 70] = 0x364ca7a5012cb603023b57dd3ebfd50d;
|
|
252
|
-
lambertArray[ 71] = 0x36022c928915b778ab1b06aaee7e61d4;
|
|
253
|
-
lambertArray[ 72] = 0x35b8b28d1a73dc27500ffe35559cc028;
|
|
254
|
-
lambertArray[ 73] = 0x357033e951fe250ec5eb4e60955132d7;
|
|
255
|
-
lambertArray[ 74] = 0x3528ab2867934e3a21b5412e4c4f8881;
|
|
256
|
-
lambertArray[ 75] = 0x34e212f66c55057f9676c80094a61d59;
|
|
257
|
-
lambertArray[ 76] = 0x349c66289e5b3c4b540c24f42fa4b9bb;
|
|
258
|
-
lambertArray[ 77] = 0x34579fbbd0c733a9c8d6af6b0f7d00f7;
|
|
259
|
-
lambertArray[ 78] = 0x3413bad2e712288b924b5882b5b369bf;
|
|
260
|
-
lambertArray[ 79] = 0x33d0b2b56286510ef730e213f71f12e9;
|
|
261
|
-
lambertArray[ 80] = 0x338e82ce00e2496262c64457535ba1a1;
|
|
262
|
-
lambertArray[ 81] = 0x334d26a96b373bb7c2f8ea1827f27a92;
|
|
263
|
-
lambertArray[ 82] = 0x330c99f4f4211469e00b3e18c31475ea;
|
|
264
|
-
lambertArray[ 83] = 0x32ccd87d6486094999c7d5e6f33237d8;
|
|
265
|
-
lambertArray[ 84] = 0x328dde2dd617b6665a2e8556f250c1af;
|
|
266
|
-
lambertArray[ 85] = 0x324fa70e9adc270f8262755af5a99af9;
|
|
267
|
-
lambertArray[ 86] = 0x32122f443110611ca51040f41fa6e1e3;
|
|
268
|
-
lambertArray[ 87] = 0x31d5730e42c0831482f0f1485c4263d8;
|
|
269
|
-
lambertArray[ 88] = 0x31996ec6b07b4a83421b5ebc4ab4e1f1;
|
|
270
|
-
lambertArray[ 89] = 0x315e1ee0a68ff46bb43ec2b85032e876;
|
|
271
|
-
lambertArray[ 90] = 0x31237fe7bc4deacf6775b9efa1a145f8;
|
|
272
|
-
lambertArray[ 91] = 0x30e98e7f1cc5a356e44627a6972ea2ff;
|
|
273
|
-
lambertArray[ 92] = 0x30b04760b8917ec74205a3002650ec05;
|
|
274
|
-
lambertArray[ 93] = 0x3077a75c803468e9132ce0cf3224241d;
|
|
275
|
-
lambertArray[ 94] = 0x303fab57a6a275c36f19cda9bace667a;
|
|
276
|
-
lambertArray[ 95] = 0x3008504beb8dcbd2cf3bc1f6d5a064f0;
|
|
277
|
-
lambertArray[ 96] = 0x2fd19346ed17dac61219ce0c2c5ac4b0;
|
|
278
|
-
lambertArray[ 97] = 0x2f9b7169808c324b5852fd3d54ba9714;
|
|
279
|
-
lambertArray[ 98] = 0x2f65e7e711cf4b064eea9c08cbdad574;
|
|
280
|
-
lambertArray[ 99] = 0x2f30f405093042ddff8a251b6bf6d103;
|
|
281
|
-
lambertArray[100] = 0x2efc931a3750f2e8bfe323edfe037574;
|
|
282
|
-
lambertArray[101] = 0x2ec8c28e46dbe56d98685278339400cb;
|
|
283
|
-
lambertArray[102] = 0x2e957fd933c3926d8a599b602379b851;
|
|
284
|
-
lambertArray[103] = 0x2e62c882c7c9ed4473412702f08ba0e5;
|
|
285
|
-
lambertArray[104] = 0x2e309a221c12ba361e3ed695167feee2;
|
|
286
|
-
lambertArray[105] = 0x2dfef25d1f865ae18dd07cfea4bcea10;
|
|
287
|
-
lambertArray[106] = 0x2dcdcee821cdc80decc02c44344aeb31;
|
|
288
|
-
lambertArray[107] = 0x2d9d2d8562b34944d0b201bb87260c83;
|
|
289
|
-
lambertArray[108] = 0x2d6d0c04a5b62a2c42636308669b729a;
|
|
290
|
-
lambertArray[109] = 0x2d3d6842c9a235517fc5a0332691528f;
|
|
291
|
-
lambertArray[110] = 0x2d0e402963fe1ea2834abc408c437c10;
|
|
292
|
-
lambertArray[111] = 0x2cdf91ae602647908aff975e4d6a2a8c;
|
|
293
|
-
lambertArray[112] = 0x2cb15ad3a1eb65f6d74a75da09a1b6c5;
|
|
294
|
-
lambertArray[113] = 0x2c8399a6ab8e9774d6fcff373d210727;
|
|
295
|
-
lambertArray[114] = 0x2c564c4046f64edba6883ca06bbc4535;
|
|
296
|
-
lambertArray[115] = 0x2c2970c431f952641e05cb493e23eed3;
|
|
297
|
-
lambertArray[116] = 0x2bfd0560cd9eb14563bc7c0732856c18;
|
|
298
|
-
lambertArray[117] = 0x2bd1084ed0332f7ff4150f9d0ef41a2c;
|
|
299
|
-
lambertArray[118] = 0x2ba577d0fa1628b76d040b12a82492fb;
|
|
300
|
-
lambertArray[119] = 0x2b7a5233cd21581e855e89dc2f1e8a92;
|
|
301
|
-
lambertArray[120] = 0x2b4f95cd46904d05d72bdcde337d9cc7;
|
|
302
|
-
lambertArray[121] = 0x2b2540fc9b4d9abba3faca6691914675;
|
|
303
|
-
lambertArray[122] = 0x2afb5229f68d0830d8be8adb0a0db70f;
|
|
304
|
-
lambertArray[123] = 0x2ad1c7c63a9b294c5bc73a3ba3ab7a2b;
|
|
305
|
-
lambertArray[124] = 0x2aa8a04ac3cbe1ee1c9c86361465dbb8;
|
|
306
|
-
lambertArray[125] = 0x2a7fda392d725a44a2c8aeb9ab35430d;
|
|
307
|
-
lambertArray[126] = 0x2a57741b18cde618717792b4faa216db;
|
|
308
|
-
lambertArray[127] = 0x2a2f6c81f5d84dd950a35626d6d5503a;
|
|
309
|
-
}
|
|
400
|
+
function solve(
|
|
401
|
+
uint256 a, uint256 b, uint256 c, uint256 d,
|
|
402
|
+
function (uint256) pure returns (uint256) lambertNeg,
|
|
403
|
+
function (uint256) pure returns (uint256) lambertPos
|
|
404
|
+
) private pure returns (uint256, uint256) { unchecked {
|
|
405
|
+
if (a > b)
|
|
406
|
+
return call(lambertPos, a, b, c, d);
|
|
407
|
+
if (b > a)
|
|
408
|
+
return call(lambertNeg, b, a, c, d);
|
|
409
|
+
return (c, d);
|
|
410
|
+
}}
|
|
310
411
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
412
|
+
/**
|
|
413
|
+
* @dev Return f(log(x / y) * z / w * FIXED_1) * z / w / FIXED_1
|
|
414
|
+
*/
|
|
415
|
+
function call(
|
|
416
|
+
function (uint256) pure returns (uint256) f,
|
|
417
|
+
uint256 x, uint256 y, uint256 z, uint256 w
|
|
418
|
+
) private pure returns (uint256, uint256) { unchecked {
|
|
419
|
+
return FractionMath.productRatio(
|
|
420
|
+
f(
|
|
421
|
+
IntegralMath.mulDivF(
|
|
422
|
+
AnalyticMath.fixedLog(
|
|
423
|
+
IntegralMath.mulDivF(
|
|
424
|
+
FIXED_1, x, y
|
|
425
|
+
)
|
|
426
|
+
), z, w
|
|
427
|
+
)
|
|
428
|
+
), z, w, FIXED_1
|
|
429
|
+
);
|
|
430
|
+
}}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* @dev Read 32 bytes from a given chunk of data at a given offset
|
|
434
|
+
*/
|
|
435
|
+
function read(bytes memory data, uint256 offset) private pure returns (uint256 result) { unchecked {
|
|
436
|
+
assembly {result := mload(add(data, offset))}
|
|
437
|
+
}}
|
|
316
438
|
}
|