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.
@@ -1,162 +1,95 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity 0.8.20;
2
+ pragma solidity 0.8.28;
3
3
 
4
4
  import "./IntegralMath.sol";
5
5
 
6
- contract AnalyticMath {
7
- uint8 internal constant MIN_PRECISION = 32;
8
- uint8 internal constant MAX_PRECISION = 127;
9
-
10
- uint256 internal constant FIXED_1 = 1 << MAX_PRECISION;
11
- uint256 internal constant FIXED_2 = 2 << MAX_PRECISION;
12
-
13
- // Auto-generated via 'PrintLn2ScalingFactors.py'
14
- uint256 internal constant LN2_NUMERATOR = 0x3f80fe03f80fe03f80fe03f80fe03f8;
15
- uint256 internal constant LN2_DENOMINATOR = 0x5b9de1d10bf4103d647b0955897ba80;
16
-
17
- // Auto-generated via 'PrintOptimalThresholds.py'
18
- uint256 internal constant OPT_LOG_MAX_VAL = 0x15bf0a8b1457695355fb8ac404e7a79e4;
19
- uint256 internal constant OPT_EXP_MAX_VAL = 0x800000000000000000000000000000000;
20
-
21
- uint256[MAX_PRECISION + 1] private maxExpArray;
22
-
23
- /**
24
- * @dev Should be executed either during construction or after construction (if too large for the constructor)
25
- */
26
- function init() public virtual {
27
- initMaxExpArray();
28
- }
6
+ library AnalyticMath {
7
+ // Auto-generated via 'PrintAnalyticMathConstants.py'
8
+ uint256 internal constant FIXED_1 = 0x0080000000000000000000000000000000;
9
+ uint256 internal constant LN2_MIN = 0x0058b90bfbe8e7bcd5e4f1d9cc01f97b57;
10
+ uint256 internal constant LN2_MAX = 0x0058b90bfbe8e7bcd5e4f1d9cc01f97b58;
11
+ uint256 internal constant LOG_MID = 0x015bf0a8b1457695355fb8ac404e7a79e4;
12
+ uint256 internal constant EXP_MID = 0x0400000000000000000000000000000000;
13
+ uint256 internal constant EXP_MAX = 0x2cb53f09f05cc627c85ddebfccfeb72758;
29
14
 
30
15
  /**
31
16
  * @dev Compute (a / b) ^ (c / d)
32
17
  */
33
- function pow(uint256 a, uint256 b, uint256 c, uint256 d) internal view returns (uint256, uint256) { unchecked {
34
- if (a >= b)
35
- return mulDivExp(mulDivLog(FIXED_1, a, b), c, d);
36
- (uint256 q, uint256 p) = mulDivExp(mulDivLog(FIXED_1, b, a), c, d);
37
- return (p, q);
18
+ function pow(uint256 a, uint256 b, uint256 c, uint256 d) internal pure returns (uint256, uint256) { unchecked {
19
+ if (b == 0 || d == 0)
20
+ revert("division by zero");
21
+ if (a == 0 || c == 0)
22
+ return (a ** c, 1);
23
+ if (a > b)
24
+ return (fixedExp(IntegralMath.mulDivF(fixedLog(IntegralMath.mulDivF(FIXED_1, a, b)), c, d)), FIXED_1);
25
+ if (b > a)
26
+ return (FIXED_1, fixedExp(IntegralMath.mulDivF(fixedLog(IntegralMath.mulDivF(FIXED_1, b, a)), c, d)));
27
+ return (1, 1);
38
28
  }}
39
29
 
40
30
  /**
41
31
  * @dev Compute log(a / b)
42
32
  */
43
33
  function log(uint256 a, uint256 b) internal pure returns (uint256, uint256) { unchecked {
44
- require(a >= b, "log: a < b");
45
- return (mulDivLog(FIXED_1, a, b), FIXED_1);
34
+ return (fixedLog(IntegralMath.mulDivF(FIXED_1, a, b)), FIXED_1);
46
35
  }}
47
36
 
48
37
  /**
49
- * @dev Compute e ^ (a / b)
38
+ * @dev Compute exp(a / b)
50
39
  */
51
- function exp(uint256 a, uint256 b) internal view returns (uint256, uint256) { unchecked {
52
- return mulDivExp(FIXED_1, a, b);
40
+ function exp(uint256 a, uint256 b) internal pure returns (uint256, uint256) { unchecked {
41
+ return (fixedExp(IntegralMath.mulDivF(FIXED_1, a, b)), FIXED_1);
53
42
  }}
54
43
 
55
44
  /**
56
45
  * @dev Compute log(x / FIXED_1) * FIXED_1
46
+ * Input range: FIXED_1 <= x <= 2 ^ 256 - 1
47
+ * Detailed description:
48
+ * - For x < LOG_MID, compute log(x)
49
+ * - For any other x, compute log(x / 2 ^ log2(x)) + log2(x) * log(2)
50
+ * - The value of log(2) is represented as floor(log(2) * FIXED_1)
51
+ * - With k = log2(x), this solution relies on the following identity:
52
+ * log(x) =
53
+ * log(x) + log(2 ^ k) - log(2 ^ k) =
54
+ * log(x) - log(2 ^ k) + log(2 ^ k) =
55
+ * log(x / log(2 ^ k)) + log(2 ^ k) =
56
+ * log(x / log(2 ^ k)) + k * log(2)
57
57
  */
58
58
  function fixedLog(uint256 x) internal pure returns (uint256) { unchecked {
59
- if (x < OPT_LOG_MAX_VAL) {
59
+ if (x < FIXED_1)
60
+ revert("fixedLog: x < min");
61
+ if (x < LOG_MID)
60
62
  return optimalLog(x);
61
- }
62
- else {
63
- return generalLog(x);
64
- }
65
- }}
66
-
67
- /**
68
- * @dev Compute e ^ (x / FIXED_1) * FIXED_1
69
- */
70
- function fixedExp(uint256 x) internal view returns (uint256, uint256) { unchecked {
71
- if (x < OPT_EXP_MAX_VAL) {
72
- return (optimalExp(x), 1 << MAX_PRECISION);
73
- }
74
- else {
75
- uint8 precision = findPosition(x);
76
- return (generalExp(x >> (MAX_PRECISION - precision), precision), 1 << precision);
77
- }
78
- }}
79
-
80
- /**
81
- * @dev Compute log(x / FIXED_1) * FIXED_1
82
- * This functions assumes that x >= FIXED_1, because the output would be negative otherwise
83
- */
84
- function generalLog(uint256 x) internal pure returns (uint256) { unchecked {
85
- uint256 res = 0;
86
-
87
- // if x >= 2, then we compute the integer part of log2(x), which is larger than 0
88
- if (x >= FIXED_2) {
89
- uint8 count = IntegralMath.floorLog2(x / FIXED_1);
90
- x >>= count; // now x < 2
91
- res = count * FIXED_1;
92
- }
93
-
94
- // if x > 1, then we compute the fraction part of log2(x), which is larger than 0
95
- if (x > FIXED_1) {
96
- for (uint8 i = MAX_PRECISION; i > 0; --i) {
97
- x = (x * x) / FIXED_1; // now 1 < x < 4
98
- if (x >= FIXED_2) {
99
- x >>= 1; // now 1 < x < 2
100
- res += 1 << (i - 1);
101
- }
102
- }
103
- }
104
-
105
- return res * LN2_NUMERATOR / LN2_DENOMINATOR;
63
+ uint8 count = IntegralMath.floorLog2(x / FIXED_1);
64
+ return optimalLog(x >> count) + count * LN2_MIN;
106
65
  }}
107
66
 
108
67
  /**
109
- * @dev Approximate e ^ x as (x ^ 0) / 0! + (x ^ 1) / 1! + ... + (x ^ n) / n!
110
- * Auto-generated via 'PrintFunctionGeneralExp.py'
68
+ * @dev Compute exp(x / FIXED_1) * FIXED_1
69
+ * Input range: 0 <= x <= EXP_MAX - 1
111
70
  * Detailed description:
112
- * - This function returns "e ^ (x / 2 ^ precision) * 2 ^ precision", that is, the result is upshifted for accuracy
113
- * - The global "maxExpArray" maps each "precision" to "((maximumExponent + 1) << (MAX_PRECISION - precision)) - 1"
114
- * - The maximum permitted value for "x" is therefore given by "maxExpArray[precision] >> (MAX_PRECISION - precision)"
71
+ * - For x < EXP_MID, compute exp(x)
72
+ * - For any other x, compute exp(x % log(2)) * 2 ^ (x / log(2))
73
+ * - The value of log(2) is represented as ceil(log(2) * FIXED_1)
74
+ * - With k = x / log(2), this solution relies on the following identity:
75
+ * exp(x) =
76
+ * exp(x) * 2 ^ k / 2 ^ k =
77
+ * exp(x) * 2 ^ k / exp(k * log(2)) =
78
+ * exp(x) / exp(k * log(2)) * 2 ^ k =
79
+ * exp(x - k * log(2)) * 2 ^ k
115
80
  */
116
- function generalExp(uint256 x, uint8 precision) internal pure returns (uint256) { unchecked {
117
- uint256 xi = x;
118
- uint256 res = 0;
119
-
120
- xi = (xi * x) >> precision; res += xi * 0x3442c4e6074a82f1797f72ac0000000; // add x^02 * (33! / 02!)
121
- xi = (xi * x) >> precision; res += xi * 0x116b96f757c380fb287fd0e40000000; // add x^03 * (33! / 03!)
122
- xi = (xi * x) >> precision; res += xi * 0x045ae5bdd5f0e03eca1ff4390000000; // add x^04 * (33! / 04!)
123
- xi = (xi * x) >> precision; res += xi * 0x00defabf91302cd95b9ffda50000000; // add x^05 * (33! / 05!)
124
- xi = (xi * x) >> precision; res += xi * 0x002529ca9832b22439efff9b8000000; // add x^06 * (33! / 06!)
125
- xi = (xi * x) >> precision; res += xi * 0x00054f1cf12bd04e516b6da88000000; // add x^07 * (33! / 07!)
126
- xi = (xi * x) >> precision; res += xi * 0x0000a9e39e257a09ca2d6db51000000; // add x^08 * (33! / 08!)
127
- xi = (xi * x) >> precision; res += xi * 0x000012e066e7b839fa050c309000000; // add x^09 * (33! / 09!)
128
- xi = (xi * x) >> precision; res += xi * 0x000001e33d7d926c329a1ad1a800000; // add x^10 * (33! / 10!)
129
- xi = (xi * x) >> precision; res += xi * 0x0000002bee513bdb4a6b19b5f800000; // add x^11 * (33! / 11!)
130
- xi = (xi * x) >> precision; res += xi * 0x00000003a9316fa79b88eccf2a00000; // add x^12 * (33! / 12!)
131
- xi = (xi * x) >> precision; res += xi * 0x0000000048177ebe1fa812375200000; // add x^13 * (33! / 13!)
132
- xi = (xi * x) >> precision; res += xi * 0x0000000005263fe90242dcbacf00000; // add x^14 * (33! / 14!)
133
- xi = (xi * x) >> precision; res += xi * 0x000000000057e22099c030d94100000; // add x^15 * (33! / 15!)
134
- xi = (xi * x) >> precision; res += xi * 0x0000000000057e22099c030d9410000; // add x^16 * (33! / 16!)
135
- xi = (xi * x) >> precision; res += xi * 0x00000000000052b6b54569976310000; // add x^17 * (33! / 17!)
136
- xi = (xi * x) >> precision; res += xi * 0x00000000000004985f67696bf748000; // add x^18 * (33! / 18!)
137
- xi = (xi * x) >> precision; res += xi * 0x000000000000003dea12ea99e498000; // add x^19 * (33! / 19!)
138
- xi = (xi * x) >> precision; res += xi * 0x00000000000000031880f2214b6e000; // add x^20 * (33! / 20!)
139
- xi = (xi * x) >> precision; res += xi * 0x000000000000000025bcff56eb36000; // add x^21 * (33! / 21!)
140
- xi = (xi * x) >> precision; res += xi * 0x000000000000000001b722e10ab1000; // add x^22 * (33! / 22!)
141
- xi = (xi * x) >> precision; res += xi * 0x0000000000000000001317c70077000; // add x^23 * (33! / 23!)
142
- xi = (xi * x) >> precision; res += xi * 0x00000000000000000000cba84aafa00; // add x^24 * (33! / 24!)
143
- xi = (xi * x) >> precision; res += xi * 0x00000000000000000000082573a0a00; // add x^25 * (33! / 25!)
144
- xi = (xi * x) >> precision; res += xi * 0x00000000000000000000005035ad900; // add x^26 * (33! / 26!)
145
- xi = (xi * x) >> precision; res += xi * 0x000000000000000000000002f881b00; // add x^27 * (33! / 27!)
146
- xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000001b29340; // add x^28 * (33! / 28!)
147
- xi = (xi * x) >> precision; res += xi * 0x00000000000000000000000000efc40; // add x^29 * (33! / 29!)
148
- xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000000007fe0; // add x^30 * (33! / 30!)
149
- xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000000000420; // add x^31 * (33! / 31!)
150
- xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000000000021; // add x^32 * (33! / 32!)
151
- xi = (xi * x) >> precision; res += xi * 0x0000000000000000000000000000001; // add x^33 * (33! / 33!)
152
-
153
- return res / 0x688589cc0e9505e2f2fee5580000000 + x + (1 << precision); // divide by 33! and then add x^1 / 1! + x^0 / 0!
81
+ function fixedExp(uint256 x) internal pure returns (uint256) { unchecked {
82
+ if (x < EXP_MID)
83
+ return optimalExp(x);
84
+ if (x < EXP_MAX)
85
+ return optimalExp(x % LN2_MAX) << (x / LN2_MAX);
86
+ revert("fixedExp: x > max");
154
87
  }}
155
88
 
156
89
  /**
157
90
  * @dev Compute log(x / FIXED_1) * FIXED_1
158
- * Input range: FIXED_1 <= x <= OPT_LOG_MAX_VAL - 1
159
- * Auto-generated via 'PrintFunctionOptimalLog.py'
91
+ * Input range: FIXED_1 <= x <= FIXED_1 * 4 - 1
92
+ * Auto-generated via 'PrintAnalyticMathOptimalLog.py'
160
93
  * Detailed description:
161
94
  * - Rewrite the input as a product of natural exponents and a single residual r, such that 1 < r < 2
162
95
  * - The natural logarithm of each (pre-calculated) exponent is the degree of the exponent
@@ -171,14 +104,14 @@ contract AnalyticMath {
171
104
  uint256 z;
172
105
  uint256 w;
173
106
 
174
- if (x >= 0xd3094c70f034de4b96ff7d5b6f99fcd9) {res += 0x40000000000000000000000000000000; x = x * FIXED_1 / 0xd3094c70f034de4b96ff7d5b6f99fcd9;} // add 1 / 2^1
175
- if (x >= 0xa45af1e1f40c333b3de1db4dd55f29a8) {res += 0x20000000000000000000000000000000; x = x * FIXED_1 / 0xa45af1e1f40c333b3de1db4dd55f29a8;} // add 1 / 2^2
176
- if (x >= 0x910b022db7ae67ce76b441c27035c6a2) {res += 0x10000000000000000000000000000000; x = x * FIXED_1 / 0x910b022db7ae67ce76b441c27035c6a2;} // add 1 / 2^3
177
- if (x >= 0x88415abbe9a76bead8d00cf112e4d4a9) {res += 0x08000000000000000000000000000000; x = x * FIXED_1 / 0x88415abbe9a76bead8d00cf112e4d4a9;} // add 1 / 2^4
178
- if (x >= 0x84102b00893f64c705e841d5d4064bd4) {res += 0x04000000000000000000000000000000; x = x * FIXED_1 / 0x84102b00893f64c705e841d5d4064bd4;} // add 1 / 2^5
179
- if (x >= 0x8204055aaef1c8bd5c3259f4822735a3) {res += 0x02000000000000000000000000000000; x = x * FIXED_1 / 0x8204055aaef1c8bd5c3259f4822735a3;} // add 1 / 2^6
180
- if (x >= 0x810100ab00222d861931c15e39b44e9a) {res += 0x01000000000000000000000000000000; x = x * FIXED_1 / 0x810100ab00222d861931c15e39b44e9a;} // add 1 / 2^7
181
- if (x >= 0x808040155aabbbe9451521693554f734) {res += 0x00800000000000000000000000000000; x = x * FIXED_1 / 0x808040155aabbbe9451521693554f734;} // add 1 / 2^8
107
+ if (x > 0xd3094c70f034de4b96ff7d5b6f99fcd8) {res |= 0x40000000000000000000000000000000; x = x * 0x0bc5ab1b16779be3575bd8f0520a9d5b9 / 0x1368b2fc6f9609fe7aceb46aa619b8003;} // add 2^(-1)
108
+ if (x > 0xa45af1e1f40c333b3de1db4dd55f29a7) {res |= 0x20000000000000000000000000000000; x = x * 0x1368b2fc6f9609fe7aceb46aa619a2ef6 / 0x18ebef9eac820ae8682b9793ac6cffa93;} // add 2^(-2)
109
+ if (x > 0x910b022db7ae67ce76b441c27035c6a1) {res |= 0x10000000000000000000000000000000; x = x * 0x18ebef9eac820ae8682b9793ac6d11622 / 0x1c3d6a24ed82218787d624d3e5eb9a8c6;} // add 2^(-3)
110
+ if (x > 0x88415abbe9a76bead8d00cf112e4d4a8) {res |= 0x08000000000000000000000000000000; x = x * 0x1c3d6a24ed82218787d624d3e5eba8beb / 0x1e0fabfbc702a3ce5e31fe0609358b04d;} // add 2^(-4)
111
+ if (x > 0x84102b00893f64c705e841d5d4064bd3) {res |= 0x04000000000000000000000000000000; x = x * 0x1e0fabfbc702a3ce5e31fe06093589585 / 0x1f03f56a88b5d7914b00abf9776270f29;} // add 2^(-5)
112
+ if (x > 0x8204055aaef1c8bd5c3259f4822735a2) {res |= 0x02000000000000000000000000000000; x = x * 0x1f03f56a88b5d7914b00abf9776267973 / 0x1f80feabfeefa4927d10bdd54ead4eaf0;} // add 2^(-6)
113
+ if (x > 0x810100ab00222d861931c15e39b44e99) {res |= 0x01000000000000000000000000000000; x = x * 0x1f80feabfeefa4927d10bdd54ead599f9 / 0x1fc03fd56aa224f97fcbf133298883271;} // add 2^(-7)
114
+ if (x > 0x808040155aabbbe9451521693554f733) {res |= 0x00800000000000000000000000000000; x = x * 0x1fc03fd56aa224f97fcbf13329886bd10 / 0x1fe00ffaabffbbc71ad1e1184afc01529;} // add 2^(-8)
182
115
 
183
116
  z = y = x - FIXED_1;
184
117
  w = y * y / FIXED_1;
@@ -195,9 +128,9 @@ contract AnalyticMath {
195
128
  }}
196
129
 
197
130
  /**
198
- * @dev Compute e ^ (x / FIXED_1) * FIXED_1
199
- * Input range: 0 <= x <= OPT_EXP_MAX_VAL - 1
200
- * Auto-generated via 'PrintFunctionOptimalExp.py'
131
+ * @dev Compute exp(x / FIXED_1) * FIXED_1
132
+ * Input range: 0 <= x <= EXP_MID - 1
133
+ * Auto-generated via 'PrintAnalyticMathOptimalExp.py'
201
134
  * Detailed description:
202
135
  * - Rewrite the input as a sum of binary exponents and a single residual r, as small as possible
203
136
  * - The exponentiation of each binary exponent is given (pre-calculated)
@@ -233,192 +166,13 @@ contract AnalyticMath {
233
166
  z = z * y / FIXED_1; res += z * 0x0000000000000001; // add y^20 * (20! / 20!)
234
167
  res = res / 0x21c3677c82b40000 + y + FIXED_1; // divide by 20! and then add y^1 / 1! + y^0 / 0!
235
168
 
236
- if ((x & 0x010000000000000000000000000000000) != 0) res = res * 0x1c3d6a24ed82218787d624d3e5eba95f9 / 0x18ebef9eac820ae8682b9793ac6d1e776; // multiply by e^2^(-3)
237
- if ((x & 0x020000000000000000000000000000000) != 0) res = res * 0x18ebef9eac820ae8682b9793ac6d1e778 / 0x1368b2fc6f9609fe7aceb46aa619baed4; // multiply by e^2^(-2)
238
- if ((x & 0x040000000000000000000000000000000) != 0) res = res * 0x1368b2fc6f9609fe7aceb46aa619baed5 / 0x0bc5ab1b16779be3575bd8f0520a9f21f; // multiply by e^2^(-1)
239
- if ((x & 0x080000000000000000000000000000000) != 0) res = res * 0x0bc5ab1b16779be3575bd8f0520a9f21e / 0x0454aaa8efe072e7f6ddbab84b40a55c9; // multiply by e^2^(+0)
240
- if ((x & 0x100000000000000000000000000000000) != 0) res = res * 0x0454aaa8efe072e7f6ddbab84b40a55c5 / 0x00960aadc109e7a3bf4578099615711ea; // multiply by e^2^(+1)
241
- if ((x & 0x200000000000000000000000000000000) != 0) res = res * 0x00960aadc109e7a3bf4578099615711d7 / 0x0002bf84208204f5977f9a8cf01fdce3d; // multiply by e^2^(+2)
242
- if ((x & 0x400000000000000000000000000000000) != 0) res = res * 0x0002bf84208204f5977f9a8cf01fdc307 / 0x0000003c6ab775dd0b95b4cbee7e65d11; // multiply by e^2^(+3)
169
+ if ((x & 0x010000000000000000000000000000000) != 0) res = res * 0x1c3d6a24ed82218787d624d3e5eba2a0f / 0x18ebef9eac820ae8682b9793ac6d1883a; // multiply by e^2^(-3)
170
+ if ((x & 0x020000000000000000000000000000000) != 0) res = res * 0x18ebef9eac820ae8682b9793ac6d1e726 / 0x1368b2fc6f9609fe7aceb46aa619bae94; // multiply by e^2^(-2)
171
+ if ((x & 0x040000000000000000000000000000000) != 0) res = res * 0x1368b2fc6f9609fe7aceb46aa6199a7d7 / 0x0bc5ab1b16779be3575bd8f0520a8b756; // multiply by e^2^(-1)
172
+ if ((x & 0x080000000000000000000000000000000) != 0) res = res * 0x0bc5ab1b16779be3575bd8f0520a67cd2 / 0x0454aaa8efe072e7f6ddbab84b409101a; // multiply by e^2^(+0)
173
+ if ((x & 0x100000000000000000000000000000000) != 0) res = res * 0x0454aaa8efe072e7f6ddbab84b408736f / 0x00960aadc109e7a3bf45780996156d0a3; // multiply by e^2^(+1)
174
+ if ((x & 0x200000000000000000000000000000000) != 0) res = res * 0x00960aadc109e7a3bf45780996149ade3 / 0x0002bf84208204f5977f9a8cf01fd8f74; // multiply by e^2^(+2)
243
175
 
244
176
  return res;
245
177
  }}
246
-
247
- /**
248
- * @dev The global "maxExpArray" is sorted in descending order, and therefore the following statements are equivalent:
249
- * - This function finds the position of [the smallest value in "maxExpArray" larger than or equal to "x"]
250
- * - This function finds the highest position of [a value in "maxExpArray" larger than or equal to "x"]
251
- * This function supports the rational approximation of "(a / b) ^ (c / d)" via "e ^ (log(a / b) * c / d)".
252
- * The value of "log(a / b)" is represented with an integer slightly smaller than "log(a / b) * 2 ^ precision".
253
- * The larger "precision" is, the more accurately this value represents the real value.
254
- * However, the larger "precision" is, the more bits are required in order to store this value.
255
- * And the exponentiation function, which takes "x" and calculates "e ^ x", is limited to a maximum exponent (a maximum value of "x").
256
- * This maximum exponent depends on the "precision" used, and it is given by "maxExpArray[precision] >> (MAX_PRECISION - precision)".
257
- * Hence we need to determine the highest precision which can be used for the given input, before calling the exponentiation function.
258
- * This allows us to compute the result with maximum accuracy and without exceeding 256 bits in any of the intermediate computations.
259
- */
260
- function findPosition(uint256 x) internal view returns (uint8) { unchecked {
261
- uint8 lo = MIN_PRECISION;
262
- uint8 hi = MAX_PRECISION;
263
-
264
- while (lo + 1 < hi) {
265
- uint8 mid = (lo + hi) / 2;
266
- if (maxExpArray[mid] >= x)
267
- lo = mid;
268
- else
269
- hi = mid;
270
- }
271
-
272
- if (maxExpArray[hi] >= x)
273
- return hi;
274
- if (maxExpArray[lo] >= x)
275
- return lo;
276
-
277
- revert("findPosition: x > max");
278
- }}
279
-
280
- /**
281
- * @dev Initialize internal data structure
282
- * Auto-generated via 'PrintMaxExpArray.py'
283
- */
284
- function initMaxExpArray() internal {
285
- // maxExpArray[ 0] = 0x6bffffffffffffffffffffffffffffffff;
286
- // maxExpArray[ 1] = 0x67ffffffffffffffffffffffffffffffff;
287
- // maxExpArray[ 2] = 0x637fffffffffffffffffffffffffffffff;
288
- // maxExpArray[ 3] = 0x5f6fffffffffffffffffffffffffffffff;
289
- // maxExpArray[ 4] = 0x5b77ffffffffffffffffffffffffffffff;
290
- // maxExpArray[ 5] = 0x57b3ffffffffffffffffffffffffffffff;
291
- // maxExpArray[ 6] = 0x5419ffffffffffffffffffffffffffffff;
292
- // maxExpArray[ 7] = 0x50a2ffffffffffffffffffffffffffffff;
293
- // maxExpArray[ 8] = 0x4d517fffffffffffffffffffffffffffff;
294
- // maxExpArray[ 9] = 0x4a233fffffffffffffffffffffffffffff;
295
- // maxExpArray[ 10] = 0x47165fffffffffffffffffffffffffffff;
296
- // maxExpArray[ 11] = 0x4429afffffffffffffffffffffffffffff;
297
- // maxExpArray[ 12] = 0x415bc7ffffffffffffffffffffffffffff;
298
- // maxExpArray[ 13] = 0x3eab73ffffffffffffffffffffffffffff;
299
- // maxExpArray[ 14] = 0x3c1771ffffffffffffffffffffffffffff;
300
- // maxExpArray[ 15] = 0x399e96ffffffffffffffffffffffffffff;
301
- // maxExpArray[ 16] = 0x373fc47fffffffffffffffffffffffffff;
302
- // maxExpArray[ 17] = 0x34f9e8ffffffffffffffffffffffffffff;
303
- // maxExpArray[ 18] = 0x32cbfd5fffffffffffffffffffffffffff;
304
- // maxExpArray[ 19] = 0x30b5057fffffffffffffffffffffffffff;
305
- // maxExpArray[ 20] = 0x2eb40f9fffffffffffffffffffffffffff;
306
- // maxExpArray[ 21] = 0x2cc8340fffffffffffffffffffffffffff;
307
- // maxExpArray[ 22] = 0x2af09481ffffffffffffffffffffffffff;
308
- // maxExpArray[ 23] = 0x292c5bddffffffffffffffffffffffffff;
309
- // maxExpArray[ 24] = 0x277abdcdffffffffffffffffffffffffff;
310
- // maxExpArray[ 25] = 0x25daf6657fffffffffffffffffffffffff;
311
- // maxExpArray[ 26] = 0x244c49c65fffffffffffffffffffffffff;
312
- // maxExpArray[ 27] = 0x22ce03cd5fffffffffffffffffffffffff;
313
- // maxExpArray[ 28] = 0x215f77c047ffffffffffffffffffffffff;
314
- // maxExpArray[ 29] = 0x1fffffffffffffffffffffffffffffffff;
315
- // maxExpArray[ 30] = 0x1eaefdbdabffffffffffffffffffffffff;
316
- // maxExpArray[ 31] = 0x1d6bd8b2ebffffffffffffffffffffffff;
317
- maxExpArray[ 32] = 0x1c35fedd14ffffffffffffffffffffffff;
318
- maxExpArray[ 33] = 0x1b0ce43b323fffffffffffffffffffffff;
319
- maxExpArray[ 34] = 0x19f0028ec1ffffffffffffffffffffffff;
320
- maxExpArray[ 35] = 0x18ded91f0e7fffffffffffffffffffffff;
321
- maxExpArray[ 36] = 0x17d8ec7f0417ffffffffffffffffffffff;
322
- maxExpArray[ 37] = 0x16ddc6556cdbffffffffffffffffffffff;
323
- maxExpArray[ 38] = 0x15ecf52776a1ffffffffffffffffffffff;
324
- maxExpArray[ 39] = 0x15060c256cb2ffffffffffffffffffffff;
325
- maxExpArray[ 40] = 0x1428a2f98d72ffffffffffffffffffffff;
326
- maxExpArray[ 41] = 0x13545598e5c23fffffffffffffffffffff;
327
- maxExpArray[ 42] = 0x1288c4161ce1dfffffffffffffffffffff;
328
- maxExpArray[ 43] = 0x11c592761c666fffffffffffffffffffff;
329
- maxExpArray[ 44] = 0x110a688680a757ffffffffffffffffffff;
330
- maxExpArray[ 45] = 0x1056f1b5bedf77ffffffffffffffffffff;
331
- maxExpArray[ 46] = 0x0faadceceeff8bffffffffffffffffffff;
332
- maxExpArray[ 47] = 0x0f05dc6b27edadffffffffffffffffffff;
333
- maxExpArray[ 48] = 0x0e67a5a25da4107fffffffffffffffffff;
334
- maxExpArray[ 49] = 0x0dcff115b14eedffffffffffffffffffff;
335
- maxExpArray[ 50] = 0x0d3e7a392431239fffffffffffffffffff;
336
- maxExpArray[ 51] = 0x0cb2ff529eb71e4fffffffffffffffffff;
337
- maxExpArray[ 52] = 0x0c2d415c3db974afffffffffffffffffff;
338
- maxExpArray[ 53] = 0x0bad03e7d883f69bffffffffffffffffff;
339
- maxExpArray[ 54] = 0x0b320d03b2c343d5ffffffffffffffffff;
340
- maxExpArray[ 55] = 0x0abc25204e02828dffffffffffffffffff;
341
- maxExpArray[ 56] = 0x0a4b16f74ee4bb207fffffffffffffffff;
342
- maxExpArray[ 57] = 0x09deaf736ac1f569ffffffffffffffffff;
343
- maxExpArray[ 58] = 0x0976bd9952c7aa957fffffffffffffffff;
344
- maxExpArray[ 59] = 0x09131271922eaa606fffffffffffffffff;
345
- maxExpArray[ 60] = 0x08b380f3558668c46fffffffffffffffff;
346
- maxExpArray[ 61] = 0x0857ddf0117efa215bffffffffffffffff;
347
- maxExpArray[ 62] = 0x07ffffffffffffffffffffffffffffffff;
348
- maxExpArray[ 63] = 0x07abbf6f6abb9d087fffffffffffffffff;
349
- maxExpArray[ 64] = 0x075af62cbac95f7dfa7fffffffffffffff;
350
- maxExpArray[ 65] = 0x070d7fb7452e187ac13fffffffffffffff;
351
- maxExpArray[ 66] = 0x06c3390ecc8af379295fffffffffffffff;
352
- maxExpArray[ 67] = 0x067c00a3b07ffc01fd6fffffffffffffff;
353
- maxExpArray[ 68] = 0x0637b647c39cbb9d3d27ffffffffffffff;
354
- maxExpArray[ 69] = 0x05f63b1fc104dbd39587ffffffffffffff;
355
- maxExpArray[ 70] = 0x05b771955b36e12f7235ffffffffffffff;
356
- maxExpArray[ 71] = 0x057b3d49dda84556d6f6ffffffffffffff;
357
- maxExpArray[ 72] = 0x054183095b2c8ececf30ffffffffffffff;
358
- maxExpArray[ 73] = 0x050a28be635ca2b888f77fffffffffffff;
359
- maxExpArray[ 74] = 0x04d5156639708c9db33c3fffffffffffff;
360
- maxExpArray[ 75] = 0x04a23105873875bd52dfdfffffffffffff;
361
- maxExpArray[ 76] = 0x0471649d87199aa990756fffffffffffff;
362
- maxExpArray[ 77] = 0x04429a21a029d4c1457cfbffffffffffff;
363
- maxExpArray[ 78] = 0x0415bc6d6fb7dd71af2cb3ffffffffffff;
364
- maxExpArray[ 79] = 0x03eab73b3bbfe282243ce1ffffffffffff;
365
- maxExpArray[ 80] = 0x03c1771ac9fb6b4c18e229ffffffffffff;
366
- maxExpArray[ 81] = 0x0399e96897690418f785257fffffffffff;
367
- maxExpArray[ 82] = 0x0373fc456c53bb779bf0ea9fffffffffff;
368
- maxExpArray[ 83] = 0x034f9e8e490c48e67e6ab8bfffffffffff;
369
- maxExpArray[ 84] = 0x032cbfd4a7adc790560b3337ffffffffff;
370
- maxExpArray[ 85] = 0x030b50570f6e5d2acca94613ffffffffff;
371
- maxExpArray[ 86] = 0x02eb40f9f620fda6b56c2861ffffffffff;
372
- maxExpArray[ 87] = 0x02cc8340ecb0d0f520a6af58ffffffffff;
373
- maxExpArray[ 88] = 0x02af09481380a0a35cf1ba02ffffffffff;
374
- maxExpArray[ 89] = 0x0292c5bdd3b92ec810287b1b3fffffffff;
375
- maxExpArray[ 90] = 0x0277abdcdab07d5a77ac6d6b9fffffffff;
376
- maxExpArray[ 91] = 0x025daf6654b1eaa55fd64df5efffffffff;
377
- maxExpArray[ 92] = 0x0244c49c648baa98192dce88b7ffffffff;
378
- maxExpArray[ 93] = 0x022ce03cd5619a311b2471268bffffffff;
379
- maxExpArray[ 94] = 0x0215f77c045fbe885654a44a0fffffffff;
380
- maxExpArray[ 95] = 0x01ffffffffffffffffffffffffffffffff;
381
- maxExpArray[ 96] = 0x01eaefdbdaaee7421fc4d3ede5ffffffff;
382
- maxExpArray[ 97] = 0x01d6bd8b2eb257df7e8ca57b09bfffffff;
383
- maxExpArray[ 98] = 0x01c35fedd14b861eb0443f7f133fffffff;
384
- maxExpArray[ 99] = 0x01b0ce43b322bcde4a56e8ada5afffffff;
385
- maxExpArray[100] = 0x019f0028ec1fff007f5a195a39dfffffff;
386
- maxExpArray[101] = 0x018ded91f0e72ee74f49b15ba527ffffff;
387
- maxExpArray[102] = 0x017d8ec7f04136f4e5615fd41a63ffffff;
388
- maxExpArray[103] = 0x016ddc6556cdb84bdc8d12d22e6fffffff;
389
- maxExpArray[104] = 0x015ecf52776a1155b5bd8395814f7fffff;
390
- maxExpArray[105] = 0x015060c256cb23b3b3cc3754cf40ffffff;
391
- maxExpArray[106] = 0x01428a2f98d728ae223ddab715be3fffff;
392
- maxExpArray[107] = 0x013545598e5c23276ccf0ede68034fffff;
393
- maxExpArray[108] = 0x01288c4161ce1d6f54b7f61081194fffff;
394
- maxExpArray[109] = 0x011c592761c666aa641d5a01a40f17ffff;
395
- maxExpArray[110] = 0x0110a688680a7530515f3e6e6cfdcdffff;
396
- maxExpArray[111] = 0x01056f1b5bedf75c6bcb2ce8aed428ffff;
397
- maxExpArray[112] = 0x00faadceceeff8a0890f3875f008277fff;
398
- maxExpArray[113] = 0x00f05dc6b27edad306388a600f6ba0bfff;
399
- maxExpArray[114] = 0x00e67a5a25da41063de1495d5b18cdbfff;
400
- maxExpArray[115] = 0x00dcff115b14eedde6fc3aa5353f2e4fff;
401
- maxExpArray[116] = 0x00d3e7a3924312399f9aae2e0f868f8fff;
402
- maxExpArray[117] = 0x00cb2ff529eb71e41582cccd5a1ee26fff;
403
- maxExpArray[118] = 0x00c2d415c3db974ab32a51840c0b67edff;
404
- maxExpArray[119] = 0x00bad03e7d883f69ad5b0a186184e06bff;
405
- maxExpArray[120] = 0x00b320d03b2c343d4829abd6075f0cc5ff;
406
- maxExpArray[121] = 0x00abc25204e02828d73c6e80bcdb1a95bf;
407
- maxExpArray[122] = 0x00a4b16f74ee4bb2040a1ec6c15fbbf2df;
408
- maxExpArray[123] = 0x009deaf736ac1f569deb1b5ae3f36c130f;
409
- maxExpArray[124] = 0x00976bd9952c7aa957f5937d790ef65037;
410
- maxExpArray[125] = 0x009131271922eaa6064b73a22d0bd4f2bf;
411
- maxExpArray[126] = 0x008b380f3558668c46c91c49a2f8e967b9;
412
- maxExpArray[127] = 0x00857ddf0117efa215952912839f6473e6;
413
- }
414
-
415
- // auxiliary function
416
- function mulDivLog(uint256 x, uint256 y, uint256 z) private pure returns (uint256) {
417
- return fixedLog(IntegralMath.mulDivF(x, y, z));
418
- }
419
-
420
- // auxiliary function
421
- function mulDivExp(uint256 x, uint256 y, uint256 z) private view returns (uint256, uint256) {
422
- return fixedExp(IntegralMath.mulDivF(x, y, z));
423
- }
424
178
  }
@@ -1,11 +1,11 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity 0.8.20;
2
+ pragma solidity 0.8.28;
3
3
 
4
4
  import "./AnalyticMath.sol";
5
+ import "./IntegralMath.sol";
5
6
 
6
- contract BondingCurve is AnalyticMath {
7
- uint256 private constant MIN_WEIGHT = 1;
8
- uint256 private constant MAX_WEIGHT = 1000000;
7
+ library BondingCurve {
8
+ uint256 internal constant MAX_WEIGHT = 1000000;
9
9
 
10
10
  /**
11
11
  * @dev Buy pool tokens with reserve tokens
@@ -17,18 +17,14 @@ contract BondingCurve is AnalyticMath {
17
17
  *
18
18
  * @return supply * ((1 + amount / balance) ^ (weight / MAX_WEIGHT) - 1)
19
19
  */
20
- function buy(uint256 supply, uint256 balance, uint256 weight, uint256 amount) public view returns (uint256) { unchecked {
21
- require(supply > 0, "invalid supply");
22
- require(balance > 0, "invalid balance");
23
- require(MIN_WEIGHT <= weight && weight <= MAX_WEIGHT, "invalid weight");
24
-
25
- if (amount == 0)
26
- return 0;
20
+ function buy(uint256 supply, uint256 balance, uint256 weight, uint256 amount) internal pure returns (uint256) { unchecked {
21
+ require(supply > 0 && balance > 0 && weight > 0, "invalid input");
22
+ require(weight <= MAX_WEIGHT, "weight out of bound");
27
23
 
28
24
  if (weight == MAX_WEIGHT)
29
25
  return IntegralMath.mulDivF(amount, supply, balance);
30
26
 
31
- (uint256 n, uint256 d) = pow(safeAdd(balance, amount), balance, weight, MAX_WEIGHT);
27
+ (uint256 n, uint256 d) = AnalyticMath.pow(safeAdd(balance, amount), balance, weight, MAX_WEIGHT);
32
28
  return IntegralMath.mulDivF(supply, n, d) - supply;
33
29
  }}
34
30
 
@@ -42,14 +38,10 @@ contract BondingCurve is AnalyticMath {
42
38
  *
43
39
  * @return balance * (1 - (1 - amount / supply) ^ (MAX_WEIGHT / weight))
44
40
  */
45
- function sell(uint256 supply, uint256 balance, uint256 weight, uint256 amount) public view returns (uint256) { unchecked {
46
- require(supply > 0, "invalid supply");
47
- require(balance > 0, "invalid balance");
48
- require(MIN_WEIGHT <= weight && weight <= MAX_WEIGHT, "invalid weight");
49
- require(amount <= supply, "invalid amount");
50
-
51
- if (amount == 0)
52
- return 0;
41
+ function sell(uint256 supply, uint256 balance, uint256 weight, uint256 amount) internal pure returns (uint256) { unchecked {
42
+ require(supply > 0 && balance > 0 && weight > 0, "invalid input");
43
+ require(weight <= MAX_WEIGHT, "weight out of bound");
44
+ require(amount <= supply, "amount larger than supply");
53
45
 
54
46
  if (amount == supply)
55
47
  return balance;
@@ -57,7 +49,7 @@ contract BondingCurve is AnalyticMath {
57
49
  if (weight == MAX_WEIGHT)
58
50
  return IntegralMath.mulDivF(amount, balance, supply);
59
51
 
60
- (uint256 n, uint256 d) = pow(supply, supply - amount, MAX_WEIGHT, weight);
52
+ (uint256 n, uint256 d) = AnalyticMath.pow(supply, supply - amount, MAX_WEIGHT, weight);
61
53
  return IntegralMath.mulDivF(balance, n - d, n);
62
54
  }}
63
55
 
@@ -72,16 +64,14 @@ contract BondingCurve is AnalyticMath {
72
64
  *
73
65
  * @return balance2 * (1 - (balance1 / (balance1 + amount)) ^ (weight1 / weight2))
74
66
  */
75
- function convert(uint256 balance1, uint256 weight1, uint256 balance2, uint256 weight2, uint256 amount) public view returns (uint256) { unchecked {
76
- require(0 < balance1, "invalid source balance");
77
- require(0 < balance2, "invalid target balance");
78
- require(MIN_WEIGHT <= weight1 && weight1 <= MAX_WEIGHT, "invalid source weight");
79
- require(MIN_WEIGHT <= weight2 && weight2 <= MAX_WEIGHT, "invalid target weight");
67
+ 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, "invalid input");
69
+ require(weight1 <= MAX_WEIGHT && weight2 <= MAX_WEIGHT, "weights out of bound");
80
70
 
81
71
  if (weight1 == weight2)
82
72
  return IntegralMath.mulDivF(balance2, amount, safeAdd(balance1, amount));
83
73
 
84
- (uint256 n, uint256 d) = pow(safeAdd(balance1, amount), balance1, weight1, weight2);
74
+ (uint256 n, uint256 d) = AnalyticMath.pow(safeAdd(balance1, amount), balance1, weight1, weight2);
85
75
  return IntegralMath.mulDivF(balance2, n - d, n);
86
76
  }}
87
77
 
@@ -95,18 +85,14 @@ contract BondingCurve is AnalyticMath {
95
85
  *
96
86
  * @return supply * ((amount / balance + 1) ^ (weights / MAX_WEIGHT) - 1)
97
87
  */
98
- function deposit(uint256 supply, uint256 balance, uint256 weights, uint256 amount) public view returns (uint256) { unchecked {
99
- require(supply > 0, "invalid supply");
100
- require(balance > 0, "invalid balance");
101
- require(MIN_WEIGHT * 2 <= weights && weights <= MAX_WEIGHT * 2, "invalid weights");
102
-
103
- if (amount == 0)
104
- return 0;
88
+ function deposit(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
89
+ require(supply > 0 && balance > 0 && weights > 0, "invalid input");
90
+ require(weights <= MAX_WEIGHT * 2, "weights out of bound");
105
91
 
106
92
  if (weights == MAX_WEIGHT)
107
93
  return IntegralMath.mulDivF(amount, supply, balance);
108
94
 
109
- (uint256 n, uint256 d) = pow(safeAdd(balance, amount), balance, weights, MAX_WEIGHT);
95
+ (uint256 n, uint256 d) = AnalyticMath.pow(safeAdd(balance, amount), balance, weights, MAX_WEIGHT);
110
96
  return IntegralMath.mulDivF(supply, n, d) - supply;
111
97
  }}
112
98
 
@@ -120,14 +106,10 @@ contract BondingCurve is AnalyticMath {
120
106
  *
121
107
  * @return balance * (1 - ((supply - amount) / supply) ^ (MAX_WEIGHT / weights))
122
108
  */
123
- function withdraw(uint256 supply, uint256 balance, uint256 weights, uint256 amount) public view returns (uint256) { unchecked {
124
- require(supply > 0, "invalid supply");
125
- require(balance > 0, "invalid balance");
126
- require(MIN_WEIGHT * 2 <= weights && weights <= MAX_WEIGHT * 2, "invalid weights");
127
- require(amount <= supply, "invalid amount");
128
-
129
- if (amount == 0)
130
- return 0;
109
+ function withdraw(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
110
+ require(supply > 0 && balance > 0 && weights > 0, "invalid input");
111
+ require(weights <= MAX_WEIGHT * 2, "weights out of bound");
112
+ require(amount <= supply, "amount larger than supply");
131
113
 
132
114
  if (amount == supply)
133
115
  return balance;
@@ -135,7 +117,7 @@ contract BondingCurve is AnalyticMath {
135
117
  if (weights == MAX_WEIGHT)
136
118
  return IntegralMath.mulDivF(amount, balance, supply);
137
119
 
138
- (uint256 n, uint256 d) = pow(supply, supply - amount, MAX_WEIGHT, weights);
120
+ (uint256 n, uint256 d) = AnalyticMath.pow(supply, supply - amount, MAX_WEIGHT, weights);
139
121
  return IntegralMath.mulDivF(balance, n - d, n);
140
122
  }}
141
123
 
@@ -149,18 +131,14 @@ contract BondingCurve is AnalyticMath {
149
131
  *
150
132
  * @return balance * (((supply + amount) / supply) ^ (MAX_WEIGHT / weights) - 1)
151
133
  */
152
- function invest(uint256 supply, uint256 balance, uint256 weights, uint256 amount) public view returns (uint256) { unchecked {
153
- require(supply > 0, "invalid supply");
154
- require(balance > 0, "invalid balance");
155
- require(MIN_WEIGHT * 2 <= weights && weights <= MAX_WEIGHT * 2, "invalid weights");
156
-
157
- if (amount == 0)
158
- return 0;
134
+ function invest(uint256 supply, uint256 balance, uint256 weights, uint256 amount) internal pure returns (uint256) { unchecked {
135
+ require(supply > 0 && balance > 0 && weights > 0, "invalid input");
136
+ require(weights <= MAX_WEIGHT * 2, "weights out of bound");
159
137
 
160
138
  if (weights == MAX_WEIGHT)
161
139
  return IntegralMath.mulDivC(amount, balance, supply);
162
140
 
163
- (uint256 n, uint256 d) = pow(safeAdd(supply, amount), supply, MAX_WEIGHT, weights);
141
+ (uint256 n, uint256 d) = AnalyticMath.pow(safeAdd(supply, amount), supply, MAX_WEIGHT, weights);
164
142
  return IntegralMath.mulDivC(balance, n, d) - balance;
165
143
  }}
166
144
  }