@pendle/core-v2 0.2.1 → 0.2.3

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.
Files changed (40) hide show
  1. package/contracts/core/PendleBaseToken.sol +0 -4
  2. package/contracts/core/PendleMarket.sol +4 -7
  3. package/contracts/core/PendleMarketFactory.sol +2 -2
  4. package/contracts/core/PendleOwnershipToken.sol +0 -2
  5. package/contracts/core/router/PendleRouterProxy.sol +15 -1
  6. package/contracts/core/router/PendleRouterStaticUpg.sol +82 -0
  7. package/contracts/interfaces/IPRouterStatic.sol +35 -0
  8. package/contracts/libraries/math/FixedPoint.sol +4 -7
  9. package/contracts/libraries/math/LogExpMath.sol +377 -365
  10. package/contracts/libraries/math/MarketMathLib.sol +25 -17
  11. package/package.json +1 -1
  12. package/typechain-types/IPRouterStatic.ts +307 -0
  13. package/typechain-types/IPRouterView.ts +307 -0
  14. package/typechain-types/PendleBaseToken.ts +0 -30
  15. package/typechain-types/PendleMarket.ts +19 -46
  16. package/typechain-types/PendleMarketFactory.ts +5 -5
  17. package/typechain-types/PendleOwnershipToken.ts +0 -30
  18. package/typechain-types/PendleRouterProxy.ts +21 -0
  19. package/typechain-types/PendleRouterStaticUpg.ts +307 -0
  20. package/typechain-types/PendleRouterViewUpg.ts +307 -0
  21. package/typechain-types/PendleYieldToken.ts +0 -30
  22. package/typechain-types/factories/IPRouterStatic__factory.ts +190 -0
  23. package/typechain-types/factories/IPRouterView__factory.ts +187 -0
  24. package/typechain-types/factories/PendleAaveV3SCY__factory.ts +1 -1
  25. package/typechain-types/factories/PendleBaseToken__factory.ts +0 -38
  26. package/typechain-types/factories/PendleBenQiErc20SCY__factory.ts +1 -1
  27. package/typechain-types/factories/PendleBtrflyScy__factory.ts +1 -1
  28. package/typechain-types/factories/PendleMarketFactory__factory.ts +2 -2
  29. package/typechain-types/factories/PendleMarket__factory.ts +19 -57
  30. package/typechain-types/factories/PendleOwnershipToken__factory.ts +1 -39
  31. package/typechain-types/factories/PendleRouterCoreUpg__factory.ts +1 -1
  32. package/typechain-types/factories/PendleRouterProxy__factory.ts +23 -1
  33. package/typechain-types/factories/PendleRouterStaticUpg__factory.ts +269 -0
  34. package/typechain-types/factories/PendleRouterViewUpg__factory.ts +265 -0
  35. package/typechain-types/factories/PendleRouterYTUpg__factory.ts +1 -1
  36. package/typechain-types/factories/PendleYearnVaultScy__factory.ts +1 -1
  37. package/typechain-types/factories/PendleYieldContractFactory__factory.ts +1 -1
  38. package/typechain-types/factories/PendleYieldToken__factory.ts +1 -39
  39. package/typechain-types/hardhat.d.ts +18 -0
  40. package/typechain-types/index.ts +4 -0
@@ -89,54 +89,56 @@ library LogExpMath {
89
89
  * Reverts if ln(x) * y is smaller than `MIN_NATURAL_EXPONENT`, or larger than `MAX_NATURAL_EXPONENT`.
90
90
  */
91
91
  function pow(uint256 x, uint256 y) internal pure returns (uint256) {
92
- if (y == 0) {
93
- // We solve the 0^0 indetermination by making it equal one.
94
- return uint256(ONE_18);
92
+ unchecked {
93
+ if (y == 0) {
94
+ // We solve the 0^0 indetermination by making it equal one.
95
+ return uint256(ONE_18);
96
+ }
97
+
98
+ if (x == 0) {
99
+ return 0;
100
+ }
101
+
102
+ // Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to
103
+ // arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means
104
+ // x^y = exp(y * ln(x)).
105
+
106
+ // The ln function takes a signed value, so we need to make sure x fits in the signed 256 bit range.
107
+ require(x < 2**255, "X out of bounds");
108
+ int256 x_int256 = int256(x);
109
+
110
+ // We will compute y * ln(x) in a single step. Depending on the value of x, we can either use ln or ln_36. In
111
+ // both cases, we leave the division by ONE_18 (due to fixed point multiplication) to the end.
112
+
113
+ // This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 256 bit range.
114
+ require(y < MILD_EXPONENT_BOUND, "Y out of bounds");
115
+ int256 y_int256 = int256(y);
116
+
117
+ int256 logx_times_y;
118
+ if (LN_36_LOWER_BOUND < x_int256 && x_int256 < LN_36_UPPER_BOUND) {
119
+ int256 ln_36_x = _ln_36(x_int256);
120
+
121
+ // ln_36_x has 36 decimal places, so multiplying by y_int256 isn't as straightforward, since we can't just
122
+ // bring y_int256 to 36 decimal places, as it might overflow. Instead, we perform two 18 decimal
123
+ // multiplications and add the results: one with the first 18 decimals of ln_36_x, and one with the
124
+ // (downscaled) last 18 decimals.
125
+ logx_times_y = ((ln_36_x / ONE_18) *
126
+ y_int256 +
127
+ ((ln_36_x % ONE_18) * y_int256) /
128
+ ONE_18);
129
+ } else {
130
+ logx_times_y = _ln(x_int256) * y_int256;
131
+ }
132
+ logx_times_y /= ONE_18;
133
+
134
+ // Finally, we compute exp(y * ln(x)) to arrive at x^y
135
+ require(
136
+ MIN_NATURAL_EXPONENT <= logx_times_y && logx_times_y <= MAX_NATURAL_EXPONENT,
137
+ "Product out of bounds"
138
+ );
139
+
140
+ return uint256(exp(logx_times_y));
95
141
  }
96
-
97
- if (x == 0) {
98
- return 0;
99
- }
100
-
101
- // Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to
102
- // arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means
103
- // x^y = exp(y * ln(x)).
104
-
105
- // The ln function takes a signed value, so we need to make sure x fits in the signed 256 bit range.
106
- require(x < 2**255, "X out of bounds");
107
- int256 x_int256 = int256(x);
108
-
109
- // We will compute y * ln(x) in a single step. Depending on the value of x, we can either use ln or ln_36. In
110
- // both cases, we leave the division by ONE_18 (due to fixed point multiplication) to the end.
111
-
112
- // This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 256 bit range.
113
- require(y < MILD_EXPONENT_BOUND, "Y out of bounds");
114
- int256 y_int256 = int256(y);
115
-
116
- int256 logx_times_y;
117
- if (LN_36_LOWER_BOUND < x_int256 && x_int256 < LN_36_UPPER_BOUND) {
118
- int256 ln_36_x = _ln_36(x_int256);
119
-
120
- // ln_36_x has 36 decimal places, so multiplying by y_int256 isn't as straightforward, since we can't just
121
- // bring y_int256 to 36 decimal places, as it might overflow. Instead, we perform two 18 decimal
122
- // multiplications and add the results: one with the first 18 decimals of ln_36_x, and one with the
123
- // (downscaled) last 18 decimals.
124
- logx_times_y = ((ln_36_x / ONE_18) *
125
- y_int256 +
126
- ((ln_36_x % ONE_18) * y_int256) /
127
- ONE_18);
128
- } else {
129
- logx_times_y = _ln(x_int256) * y_int256;
130
- }
131
- logx_times_y /= ONE_18;
132
-
133
- // Finally, we compute exp(y * ln(x)) to arrive at x^y
134
- require(
135
- MIN_NATURAL_EXPONENT <= logx_times_y && logx_times_y <= MAX_NATURAL_EXPONENT,
136
- "Product out of bounds"
137
- );
138
-
139
- return uint256(exp(logx_times_y));
140
142
  }
141
143
 
142
144
  /**
@@ -145,179 +147,185 @@ library LogExpMath {
145
147
  * Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`.
146
148
  */
147
149
  function exp(int256 x) internal pure returns (int256) {
148
- require(x >= MIN_NATURAL_EXPONENT && x <= MAX_NATURAL_EXPONENT, "Invalid exponent");
149
-
150
- if (x < 0) {
151
- // We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it
152
- // fits in the signed 256 bit range (as it is larger than MIN_NATURAL_EXPONENT).
153
- // Fixed point division requires multiplying by ONE_18.
154
- return ((ONE_18 * ONE_18) / exp(-x));
155
- }
156
-
157
- // First, we use the fact that e^(x+y) = e^x * e^y to decompose x into a sum of powers of two, which we call x_n,
158
- // where x_n == 2^(7 - n), and e^x_n = a_n has been precomputed. We choose the first x_n, x0, to equal 2^7
159
- // because all larger powers are larger than MAX_NATURAL_EXPONENT, and therefore not present in the
160
- // decomposition.
161
- // At the end of this process we will have the product of all e^x_n = a_n that apply, and the remainder of this
162
- // decomposition, which will be lower than the smallest x_n.
163
- // exp(x) = k_0 * a_0 * k_1 * a_1 * ... + k_n * a_n * exp(remainder), where each k_n equals either 0 or 1.
164
- // We mutate x by subtracting x_n, making it the remainder of the decomposition.
165
-
166
- // The first two a_n (e^(2^7) and e^(2^6)) are too large if stored as 18 decimal numbers, and could cause
167
- // intermediate overflows. Instead we store them as plain integers, with 0 decimals.
168
- // Additionally, x0 + x1 is larger than MAX_NATURAL_EXPONENT, which means they will not both be present in the
169
- // decomposition.
170
-
171
- // For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct
172
- // it and compute the accumulated product.
173
-
174
- int256 firstAN;
175
- if (x >= x0) {
176
- x -= x0;
177
- firstAN = a0;
178
- } else if (x >= x1) {
179
- x -= x1;
180
- firstAN = a1;
181
- } else {
182
- firstAN = 1; // One with no decimal places
150
+ unchecked {
151
+ require(x >= MIN_NATURAL_EXPONENT && x <= MAX_NATURAL_EXPONENT, "Invalid exponent");
152
+
153
+ if (x < 0) {
154
+ // We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it
155
+ // fits in the signed 256 bit range (as it is larger than MIN_NATURAL_EXPONENT).
156
+ // Fixed point division requires multiplying by ONE_18.
157
+ return ((ONE_18 * ONE_18) / exp(-x));
158
+ }
159
+
160
+ // First, we use the fact that e^(x+y) = e^x * e^y to decompose x into a sum of powers of two, which we call x_n,
161
+ // where x_n == 2^(7 - n), and e^x_n = a_n has been precomputed. We choose the first x_n, x0, to equal 2^7
162
+ // because all larger powers are larger than MAX_NATURAL_EXPONENT, and therefore not present in the
163
+ // decomposition.
164
+ // At the end of this process we will have the product of all e^x_n = a_n that apply, and the remainder of this
165
+ // decomposition, which will be lower than the smallest x_n.
166
+ // exp(x) = k_0 * a_0 * k_1 * a_1 * ... + k_n * a_n * exp(remainder), where each k_n equals either 0 or 1.
167
+ // We mutate x by subtracting x_n, making it the remainder of the decomposition.
168
+
169
+ // The first two a_n (e^(2^7) and e^(2^6)) are too large if stored as 18 decimal numbers, and could cause
170
+ // intermediate overflows. Instead we store them as plain integers, with 0 decimals.
171
+ // Additionally, x0 + x1 is larger than MAX_NATURAL_EXPONENT, which means they will not both be present in the
172
+ // decomposition.
173
+
174
+ // For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct
175
+ // it and compute the accumulated product.
176
+
177
+ int256 firstAN;
178
+ if (x >= x0) {
179
+ x -= x0;
180
+ firstAN = a0;
181
+ } else if (x >= x1) {
182
+ x -= x1;
183
+ firstAN = a1;
184
+ } else {
185
+ firstAN = 1; // One with no decimal places
186
+ }
187
+
188
+ // We now transform x into a 20 decimal fixed point number, to have enhanced precision when computing the
189
+ // smaller terms.
190
+ x *= 100;
191
+
192
+ // `product` is the accumulated product of all a_n (except a0 and a1), which starts at 20 decimal fixed point
193
+ // one. Recall that fixed point multiplication requires dividing by ONE_20.
194
+ int256 product = ONE_20;
195
+
196
+ if (x >= x2) {
197
+ x -= x2;
198
+ product = (product * a2) / ONE_20;
199
+ }
200
+ if (x >= x3) {
201
+ x -= x3;
202
+ product = (product * a3) / ONE_20;
203
+ }
204
+ if (x >= x4) {
205
+ x -= x4;
206
+ product = (product * a4) / ONE_20;
207
+ }
208
+ if (x >= x5) {
209
+ x -= x5;
210
+ product = (product * a5) / ONE_20;
211
+ }
212
+ if (x >= x6) {
213
+ x -= x6;
214
+ product = (product * a6) / ONE_20;
215
+ }
216
+ if (x >= x7) {
217
+ x -= x7;
218
+ product = (product * a7) / ONE_20;
219
+ }
220
+ if (x >= x8) {
221
+ x -= x8;
222
+ product = (product * a8) / ONE_20;
223
+ }
224
+ if (x >= x9) {
225
+ x -= x9;
226
+ product = (product * a9) / ONE_20;
227
+ }
228
+
229
+ // x10 and x11 are unnecessary here since we have high enough precision already.
230
+
231
+ // Now we need to compute e^x, where x is small (in particular, it is smaller than x9). We use the Taylor series
232
+ // expansion for e^x: 1 + x + (x^2 / 2!) + (x^3 / 3!) + ... + (x^n / n!).
233
+
234
+ int256 seriesSum = ONE_20; // The initial one in the sum, with 20 decimal places.
235
+ int256 term; // Each term in the sum, where the nth term is (x^n / n!).
236
+
237
+ // The first term is simply x.
238
+ term = x;
239
+ seriesSum += term;
240
+
241
+ // Each term (x^n / n!) equals the previous one times x, divided by n. Since x is a fixed point number,
242
+ // multiplying by it requires dividing by ONE_20, but dividing by the non-fixed point n values does not.
243
+
244
+ term = ((term * x) / ONE_20) / 2;
245
+ seriesSum += term;
246
+
247
+ term = ((term * x) / ONE_20) / 3;
248
+ seriesSum += term;
249
+
250
+ term = ((term * x) / ONE_20) / 4;
251
+ seriesSum += term;
252
+
253
+ term = ((term * x) / ONE_20) / 5;
254
+ seriesSum += term;
255
+
256
+ term = ((term * x) / ONE_20) / 6;
257
+ seriesSum += term;
258
+
259
+ term = ((term * x) / ONE_20) / 7;
260
+ seriesSum += term;
261
+
262
+ term = ((term * x) / ONE_20) / 8;
263
+ seriesSum += term;
264
+
265
+ term = ((term * x) / ONE_20) / 9;
266
+ seriesSum += term;
267
+
268
+ term = ((term * x) / ONE_20) / 10;
269
+ seriesSum += term;
270
+
271
+ term = ((term * x) / ONE_20) / 11;
272
+ seriesSum += term;
273
+
274
+ term = ((term * x) / ONE_20) / 12;
275
+ seriesSum += term;
276
+
277
+ // 12 Taylor terms are sufficient for 18 decimal precision.
278
+
279
+ // We now have the first a_n (with no decimals), and the product of all other a_n present, and the Taylor
280
+ // approximation of the exponentiation of the remainder (both with 20 decimals). All that remains is to multiply
281
+ // all three (one 20 decimal fixed point multiplication, dividing by ONE_20, and one integer multiplication),
282
+ // and then drop two digits to return an 18 decimal value.
283
+
284
+ return (((product * seriesSum) / ONE_20) * firstAN) / 100;
183
285
  }
184
-
185
- // We now transform x into a 20 decimal fixed point number, to have enhanced precision when computing the
186
- // smaller terms.
187
- x *= 100;
188
-
189
- // `product` is the accumulated product of all a_n (except a0 and a1), which starts at 20 decimal fixed point
190
- // one. Recall that fixed point multiplication requires dividing by ONE_20.
191
- int256 product = ONE_20;
192
-
193
- if (x >= x2) {
194
- x -= x2;
195
- product = (product * a2) / ONE_20;
196
- }
197
- if (x >= x3) {
198
- x -= x3;
199
- product = (product * a3) / ONE_20;
200
- }
201
- if (x >= x4) {
202
- x -= x4;
203
- product = (product * a4) / ONE_20;
204
- }
205
- if (x >= x5) {
206
- x -= x5;
207
- product = (product * a5) / ONE_20;
208
- }
209
- if (x >= x6) {
210
- x -= x6;
211
- product = (product * a6) / ONE_20;
212
- }
213
- if (x >= x7) {
214
- x -= x7;
215
- product = (product * a7) / ONE_20;
216
- }
217
- if (x >= x8) {
218
- x -= x8;
219
- product = (product * a8) / ONE_20;
220
- }
221
- if (x >= x9) {
222
- x -= x9;
223
- product = (product * a9) / ONE_20;
224
- }
225
-
226
- // x10 and x11 are unnecessary here since we have high enough precision already.
227
-
228
- // Now we need to compute e^x, where x is small (in particular, it is smaller than x9). We use the Taylor series
229
- // expansion for e^x: 1 + x + (x^2 / 2!) + (x^3 / 3!) + ... + (x^n / n!).
230
-
231
- int256 seriesSum = ONE_20; // The initial one in the sum, with 20 decimal places.
232
- int256 term; // Each term in the sum, where the nth term is (x^n / n!).
233
-
234
- // The first term is simply x.
235
- term = x;
236
- seriesSum += term;
237
-
238
- // Each term (x^n / n!) equals the previous one times x, divided by n. Since x is a fixed point number,
239
- // multiplying by it requires dividing by ONE_20, but dividing by the non-fixed point n values does not.
240
-
241
- term = ((term * x) / ONE_20) / 2;
242
- seriesSum += term;
243
-
244
- term = ((term * x) / ONE_20) / 3;
245
- seriesSum += term;
246
-
247
- term = ((term * x) / ONE_20) / 4;
248
- seriesSum += term;
249
-
250
- term = ((term * x) / ONE_20) / 5;
251
- seriesSum += term;
252
-
253
- term = ((term * x) / ONE_20) / 6;
254
- seriesSum += term;
255
-
256
- term = ((term * x) / ONE_20) / 7;
257
- seriesSum += term;
258
-
259
- term = ((term * x) / ONE_20) / 8;
260
- seriesSum += term;
261
-
262
- term = ((term * x) / ONE_20) / 9;
263
- seriesSum += term;
264
-
265
- term = ((term * x) / ONE_20) / 10;
266
- seriesSum += term;
267
-
268
- term = ((term * x) / ONE_20) / 11;
269
- seriesSum += term;
270
-
271
- term = ((term * x) / ONE_20) / 12;
272
- seriesSum += term;
273
-
274
- // 12 Taylor terms are sufficient for 18 decimal precision.
275
-
276
- // We now have the first a_n (with no decimals), and the product of all other a_n present, and the Taylor
277
- // approximation of the exponentiation of the remainder (both with 20 decimals). All that remains is to multiply
278
- // all three (one 20 decimal fixed point multiplication, dividing by ONE_20, and one integer multiplication),
279
- // and then drop two digits to return an 18 decimal value.
280
-
281
- return (((product * seriesSum) / ONE_20) * firstAN) / 100;
282
286
  }
283
287
 
284
288
  /**
285
289
  * @dev Logarithm (log(arg, base), with signed 18 decimal fixed point base and argument.
286
290
  */
287
291
  function log(int256 arg, int256 base) internal pure returns (int256) {
288
- // This performs a simple base change: log(arg, base) = ln(arg) / ln(base).
289
-
290
- // Both logBase and logArg are computed as 36 decimal fixed point numbers, either by using ln_36, or by
291
- // upscaling.
292
-
293
- int256 logBase;
294
- if (LN_36_LOWER_BOUND < base && base < LN_36_UPPER_BOUND) {
295
- logBase = _ln_36(base);
296
- } else {
297
- logBase = _ln(base) * ONE_18;
292
+ unchecked {
293
+ // This performs a simple base change: log(arg, base) = ln(arg) / ln(base).
294
+
295
+ // Both logBase and logArg are computed as 36 decimal fixed point numbers, either by using ln_36, or by
296
+ // upscaling.
297
+
298
+ int256 logBase;
299
+ if (LN_36_LOWER_BOUND < base && base < LN_36_UPPER_BOUND) {
300
+ logBase = _ln_36(base);
301
+ } else {
302
+ logBase = _ln(base) * ONE_18;
303
+ }
304
+
305
+ int256 logArg;
306
+ if (LN_36_LOWER_BOUND < arg && arg < LN_36_UPPER_BOUND) {
307
+ logArg = _ln_36(arg);
308
+ } else {
309
+ logArg = _ln(arg) * ONE_18;
310
+ }
311
+
312
+ // When dividing, we multiply by ONE_18 to arrive at a result with 18 decimal places
313
+ return (logArg * ONE_18) / logBase;
298
314
  }
299
-
300
- int256 logArg;
301
- if (LN_36_LOWER_BOUND < arg && arg < LN_36_UPPER_BOUND) {
302
- logArg = _ln_36(arg);
303
- } else {
304
- logArg = _ln(arg) * ONE_18;
305
- }
306
-
307
- // When dividing, we multiply by ONE_18 to arrive at a result with 18 decimal places
308
- return (logArg * ONE_18) / logBase;
309
315
  }
310
316
 
311
317
  /**
312
318
  * @dev Natural logarithm (ln(a)) with signed 18 decimal fixed point argument.
313
319
  */
314
320
  function ln(int256 a) internal pure returns (int256) {
315
- // The real natural logarithm is not defined for negative numbers or zero.
316
- require(a > 0, "out of bounds");
317
- if (LN_36_LOWER_BOUND < a && a < LN_36_UPPER_BOUND) {
318
- return _ln_36(a) / ONE_18;
319
- } else {
320
- return _ln(a);
321
+ unchecked {
322
+ // The real natural logarithm is not defined for negative numbers or zero.
323
+ require(a > 0, "out of bounds");
324
+ if (LN_36_LOWER_BOUND < a && a < LN_36_UPPER_BOUND) {
325
+ return _ln_36(a) / ONE_18;
326
+ } else {
327
+ return _ln(a);
328
+ }
321
329
  }
322
330
  }
323
331
 
@@ -325,137 +333,139 @@ library LogExpMath {
325
333
  * @dev Internal natural logarithm (ln(a)) with signed 18 decimal fixed point argument.
326
334
  */
327
335
  function _ln(int256 a) private pure returns (int256) {
328
- if (a < ONE_18) {
329
- // Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). If a is less
330
- // than one, 1/a will be greater than one, and this if statement will not be entered in the recursive call.
331
- // Fixed point division requires multiplying by ONE_18.
332
- return (-_ln((ONE_18 * ONE_18) / a));
333
- }
334
-
335
- // First, we use the fact that ln^(a * b) = ln(a) + ln(b) to decompose ln(a) into a sum of powers of two, which
336
- // we call x_n, where x_n == 2^(7 - n), which are the natural logarithm of precomputed quantities a_n (that is,
337
- // ln(a_n) = x_n). We choose the first x_n, x0, to equal 2^7 because the exponential of all larger powers cannot
338
- // be represented as 18 fixed point decimal numbers in 256 bits, and are therefore larger than a.
339
- // At the end of this process we will have the sum of all x_n = ln(a_n) that apply, and the remainder of this
340
- // decomposition, which will be lower than the smallest a_n.
341
- // ln(a) = k_0 * x_0 + k_1 * x_1 + ... + k_n * x_n + ln(remainder), where each k_n equals either 0 or 1.
342
- // We mutate a by subtracting a_n, making it the remainder of the decomposition.
343
-
344
- // For reasons related to how `exp` works, the first two a_n (e^(2^7) and e^(2^6)) are not stored as fixed point
345
- // numbers with 18 decimals, but instead as plain integers with 0 decimals, so we need to multiply them by
346
- // ONE_18 to convert them to fixed point.
347
- // For each a_n, we test if that term is present in the decomposition (if a is larger than it), and if so divide
348
- // by it and compute the accumulated sum.
349
-
350
- int256 sum = 0;
351
- if (a >= a0 * ONE_18) {
352
- a /= a0; // Integer, not fixed point division
353
- sum += x0;
354
- }
355
-
356
- if (a >= a1 * ONE_18) {
357
- a /= a1; // Integer, not fixed point division
358
- sum += x1;
359
- }
360
-
361
- // All other a_n and x_n are stored as 20 digit fixed point numbers, so we convert the sum and a to this format.
362
- sum *= 100;
363
- a *= 100;
364
-
365
- // Because further a_n are 20 digit fixed point numbers, we multiply by ONE_20 when dividing by them.
366
-
367
- if (a >= a2) {
368
- a = (a * ONE_20) / a2;
369
- sum += x2;
370
- }
371
-
372
- if (a >= a3) {
373
- a = (a * ONE_20) / a3;
374
- sum += x3;
375
- }
376
-
377
- if (a >= a4) {
378
- a = (a * ONE_20) / a4;
379
- sum += x4;
380
- }
381
-
382
- if (a >= a5) {
383
- a = (a * ONE_20) / a5;
384
- sum += x5;
385
- }
386
-
387
- if (a >= a6) {
388
- a = (a * ONE_20) / a6;
389
- sum += x6;
390
- }
336
+ unchecked {
337
+ if (a < ONE_18) {
338
+ // Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). If a is less
339
+ // than one, 1/a will be greater than one, and this if statement will not be entered in the recursive call.
340
+ // Fixed point division requires multiplying by ONE_18.
341
+ return (-_ln((ONE_18 * ONE_18) / a));
342
+ }
343
+
344
+ // First, we use the fact that ln^(a * b) = ln(a) + ln(b) to decompose ln(a) into a sum of powers of two, which
345
+ // we call x_n, where x_n == 2^(7 - n), which are the natural logarithm of precomputed quantities a_n (that is,
346
+ // ln(a_n) = x_n). We choose the first x_n, x0, to equal 2^7 because the exponential of all larger powers cannot
347
+ // be represented as 18 fixed point decimal numbers in 256 bits, and are therefore larger than a.
348
+ // At the end of this process we will have the sum of all x_n = ln(a_n) that apply, and the remainder of this
349
+ // decomposition, which will be lower than the smallest a_n.
350
+ // ln(a) = k_0 * x_0 + k_1 * x_1 + ... + k_n * x_n + ln(remainder), where each k_n equals either 0 or 1.
351
+ // We mutate a by subtracting a_n, making it the remainder of the decomposition.
352
+
353
+ // For reasons related to how `exp` works, the first two a_n (e^(2^7) and e^(2^6)) are not stored as fixed point
354
+ // numbers with 18 decimals, but instead as plain integers with 0 decimals, so we need to multiply them by
355
+ // ONE_18 to convert them to fixed point.
356
+ // For each a_n, we test if that term is present in the decomposition (if a is larger than it), and if so divide
357
+ // by it and compute the accumulated sum.
358
+
359
+ int256 sum = 0;
360
+ if (a >= a0 * ONE_18) {
361
+ a /= a0; // Integer, not fixed point division
362
+ sum += x0;
363
+ }
364
+
365
+ if (a >= a1 * ONE_18) {
366
+ a /= a1; // Integer, not fixed point division
367
+ sum += x1;
368
+ }
369
+
370
+ // All other a_n and x_n are stored as 20 digit fixed point numbers, so we convert the sum and a to this format.
371
+ sum *= 100;
372
+ a *= 100;
373
+
374
+ // Because further a_n are 20 digit fixed point numbers, we multiply by ONE_20 when dividing by them.
375
+
376
+ if (a >= a2) {
377
+ a = (a * ONE_20) / a2;
378
+ sum += x2;
379
+ }
380
+
381
+ if (a >= a3) {
382
+ a = (a * ONE_20) / a3;
383
+ sum += x3;
384
+ }
385
+
386
+ if (a >= a4) {
387
+ a = (a * ONE_20) / a4;
388
+ sum += x4;
389
+ }
390
+
391
+ if (a >= a5) {
392
+ a = (a * ONE_20) / a5;
393
+ sum += x5;
394
+ }
395
+
396
+ if (a >= a6) {
397
+ a = (a * ONE_20) / a6;
398
+ sum += x6;
399
+ }
400
+
401
+ if (a >= a7) {
402
+ a = (a * ONE_20) / a7;
403
+ sum += x7;
404
+ }
405
+
406
+ if (a >= a8) {
407
+ a = (a * ONE_20) / a8;
408
+ sum += x8;
409
+ }
410
+
411
+ if (a >= a9) {
412
+ a = (a * ONE_20) / a9;
413
+ sum += x9;
414
+ }
415
+
416
+ if (a >= a10) {
417
+ a = (a * ONE_20) / a10;
418
+ sum += x10;
419
+ }
420
+
421
+ if (a >= a11) {
422
+ a = (a * ONE_20) / a11;
423
+ sum += x11;
424
+ }
425
+
426
+ // a is now a small number (smaller than a_11, which roughly equals 1.06). This means we can use a Taylor series
427
+ // that converges rapidly for values of `a` close to one - the same one used in ln_36.
428
+ // Let z = (a - 1) / (a + 1).
429
+ // ln(a) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
430
+
431
+ // Recall that 20 digit fixed point division requires multiplying by ONE_20, and multiplication requires
432
+ // division by ONE_20.
433
+ int256 z = ((a - ONE_20) * ONE_20) / (a + ONE_20);
434
+ int256 z_squared = (z * z) / ONE_20;
435
+
436
+ // num is the numerator of the series: the z^(2 * n + 1) term
437
+ int256 num = z;
438
+
439
+ // seriesSum holds the accumulated sum of each term in the series, starting with the initial z
440
+ int256 seriesSum = num;
441
+
442
+ // In each step, the numerator is multiplied by z^2
443
+ num = (num * z_squared) / ONE_20;
444
+ seriesSum += num / 3;
445
+
446
+ num = (num * z_squared) / ONE_20;
447
+ seriesSum += num / 5;
448
+
449
+ num = (num * z_squared) / ONE_20;
450
+ seriesSum += num / 7;
451
+
452
+ num = (num * z_squared) / ONE_20;
453
+ seriesSum += num / 9;
454
+
455
+ num = (num * z_squared) / ONE_20;
456
+ seriesSum += num / 11;
391
457
 
392
- if (a >= a7) {
393
- a = (a * ONE_20) / a7;
394
- sum += x7;
395
- }
396
-
397
- if (a >= a8) {
398
- a = (a * ONE_20) / a8;
399
- sum += x8;
400
- }
401
-
402
- if (a >= a9) {
403
- a = (a * ONE_20) / a9;
404
- sum += x9;
405
- }
458
+ // 6 Taylor terms are sufficient for 36 decimal precision.
459
+
460
+ // Finally, we multiply by 2 (non fixed point) to compute ln(remainder)
461
+ seriesSum *= 2;
406
462
 
407
- if (a >= a10) {
408
- a = (a * ONE_20) / a10;
409
- sum += x10;
410
- }
463
+ // We now have the sum of all x_n present, and the Taylor approximation of the logarithm of the remainder (both
464
+ // with 20 decimals). All that remains is to sum these two, and then drop two digits to return a 18 decimal
465
+ // value.
411
466
 
412
- if (a >= a11) {
413
- a = (a * ONE_20) / a11;
414
- sum += x11;
467
+ return (sum + seriesSum) / 100;
415
468
  }
416
-
417
- // a is now a small number (smaller than a_11, which roughly equals 1.06). This means we can use a Taylor series
418
- // that converges rapidly for values of `a` close to one - the same one used in ln_36.
419
- // Let z = (a - 1) / (a + 1).
420
- // ln(a) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
421
-
422
- // Recall that 20 digit fixed point division requires multiplying by ONE_20, and multiplication requires
423
- // division by ONE_20.
424
- int256 z = ((a - ONE_20) * ONE_20) / (a + ONE_20);
425
- int256 z_squared = (z * z) / ONE_20;
426
-
427
- // num is the numerator of the series: the z^(2 * n + 1) term
428
- int256 num = z;
429
-
430
- // seriesSum holds the accumulated sum of each term in the series, starting with the initial z
431
- int256 seriesSum = num;
432
-
433
- // In each step, the numerator is multiplied by z^2
434
- num = (num * z_squared) / ONE_20;
435
- seriesSum += num / 3;
436
-
437
- num = (num * z_squared) / ONE_20;
438
- seriesSum += num / 5;
439
-
440
- num = (num * z_squared) / ONE_20;
441
- seriesSum += num / 7;
442
-
443
- num = (num * z_squared) / ONE_20;
444
- seriesSum += num / 9;
445
-
446
- num = (num * z_squared) / ONE_20;
447
- seriesSum += num / 11;
448
-
449
- // 6 Taylor terms are sufficient for 36 decimal precision.
450
-
451
- // Finally, we multiply by 2 (non fixed point) to compute ln(remainder)
452
- seriesSum *= 2;
453
-
454
- // We now have the sum of all x_n present, and the Taylor approximation of the logarithm of the remainder (both
455
- // with 20 decimals). All that remains is to sum these two, and then drop two digits to return a 18 decimal
456
- // value.
457
-
458
- return (sum + seriesSum) / 100;
459
469
  }
460
470
 
461
471
  /**
@@ -465,51 +475,53 @@ library LogExpMath {
465
475
  * Should only be used if x is between LN_36_LOWER_BOUND and LN_36_UPPER_BOUND.
466
476
  */
467
477
  function _ln_36(int256 x) private pure returns (int256) {
468
- // Since ln(1) = 0, a value of x close to one will yield a very small result, which makes using 36 digits
469
- // worthwhile.
478
+ unchecked {
479
+ // Since ln(1) = 0, a value of x close to one will yield a very small result, which makes using 36 digits
480
+ // worthwhile.
470
481
 
471
- // First, we transform x to a 36 digit fixed point value.
472
- x *= ONE_18;
482
+ // First, we transform x to a 36 digit fixed point value.
483
+ x *= ONE_18;
473
484
 
474
- // We will use the following Taylor expansion, which converges very rapidly. Let z = (x - 1) / (x + 1).
475
- // ln(x) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
485
+ // We will use the following Taylor expansion, which converges very rapidly. Let z = (x - 1) / (x + 1).
486
+ // ln(x) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
476
487
 
477
- // Recall that 36 digit fixed point division requires multiplying by ONE_36, and multiplication requires
478
- // division by ONE_36.
479
- int256 z = ((x - ONE_36) * ONE_36) / (x + ONE_36);
480
- int256 z_squared = (z * z) / ONE_36;
488
+ // Recall that 36 digit fixed point division requires multiplying by ONE_36, and multiplication requires
489
+ // division by ONE_36.
490
+ int256 z = ((x - ONE_36) * ONE_36) / (x + ONE_36);
491
+ int256 z_squared = (z * z) / ONE_36;
481
492
 
482
- // num is the numerator of the series: the z^(2 * n + 1) term
483
- int256 num = z;
493
+ // num is the numerator of the series: the z^(2 * n + 1) term
494
+ int256 num = z;
484
495
 
485
- // seriesSum holds the accumulated sum of each term in the series, starting with the initial z
486
- int256 seriesSum = num;
496
+ // seriesSum holds the accumulated sum of each term in the series, starting with the initial z
497
+ int256 seriesSum = num;
487
498
 
488
- // In each step, the numerator is multiplied by z^2
489
- num = (num * z_squared) / ONE_36;
490
- seriesSum += num / 3;
499
+ // In each step, the numerator is multiplied by z^2
500
+ num = (num * z_squared) / ONE_36;
501
+ seriesSum += num / 3;
491
502
 
492
- num = (num * z_squared) / ONE_36;
493
- seriesSum += num / 5;
503
+ num = (num * z_squared) / ONE_36;
504
+ seriesSum += num / 5;
494
505
 
495
- num = (num * z_squared) / ONE_36;
496
- seriesSum += num / 7;
506
+ num = (num * z_squared) / ONE_36;
507
+ seriesSum += num / 7;
497
508
 
498
- num = (num * z_squared) / ONE_36;
499
- seriesSum += num / 9;
509
+ num = (num * z_squared) / ONE_36;
510
+ seriesSum += num / 9;
500
511
 
501
- num = (num * z_squared) / ONE_36;
502
- seriesSum += num / 11;
512
+ num = (num * z_squared) / ONE_36;
513
+ seriesSum += num / 11;
503
514
 
504
- num = (num * z_squared) / ONE_36;
505
- seriesSum += num / 13;
515
+ num = (num * z_squared) / ONE_36;
516
+ seriesSum += num / 13;
506
517
 
507
- num = (num * z_squared) / ONE_36;
508
- seriesSum += num / 15;
518
+ num = (num * z_squared) / ONE_36;
519
+ seriesSum += num / 15;
509
520
 
510
- // 8 Taylor terms are sufficient for 36 decimal precision.
521
+ // 8 Taylor terms are sufficient for 36 decimal precision.
511
522
 
512
- // All that remains is multiplying by 2 (non fixed point).
513
- return seriesSum * 2;
523
+ // All that remains is multiplying by 2 (non fixed point).
524
+ return seriesSum * 2;
525
+ }
514
526
  }
515
527
  }