solidity-math-utils 1.4.0 → 1.5.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 +23 -25
- package/package.json +1 -1
- package/project/contracts/AdvancedMath.sol +1 -1
- package/project/contracts/AnalyticMath.sol +1 -1
- package/project/contracts/BondingCurve.sol +1 -1
- package/project/contracts/DynamicCurve.sol +1 -1
- package/project/contracts/FractionMath.sol +42 -45
- package/project/contracts/IntegralMath.sol +1 -1
package/README.md
CHANGED
|
@@ -86,33 +86,33 @@ For example, `floorCbrt(7)` returns 1, but the actual cubic root of 7 is ~1.91,
|
|
|
86
86
|
## FractionMath
|
|
87
87
|
|
|
88
88
|
This module implements the following interface:
|
|
89
|
-
- `function normalizedRatio(uint256 n, uint256 d, uint256 scale)` => `(uint256, uint256)`
|
|
90
|
-
- `function reducedRatio(uint256 n, uint256 d, uint256 max)` => `(uint256, uint256)`
|
|
91
|
-
- `function productRatio(uint256 n1, uint256 n2, uint256 d1, uint256 d2)` => `(uint256, uint256)`
|
|
92
89
|
- `function poweredRatio(uint256 n, uint256 d, uint256 exp, bool fast)` => `(uint256, uint256)`
|
|
90
|
+
- `function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd)` => `(uint256, uint256)`
|
|
91
|
+
- `function reducedRatio(uint256 n, uint256 d, uint256 max)` => `(uint256, uint256)`
|
|
92
|
+
- `function normalizedRatio(uint256 n, uint256 d, uint256 scale)` => `(uint256, uint256)`
|
|
93
93
|
|
|
94
|
-
###
|
|
94
|
+
### Powered Ratio
|
|
95
95
|
|
|
96
|
-
Function `
|
|
96
|
+
Function `poweredRatio` computes the power of a given ratio by a given exponent.
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
In order to avoid multiplication overflow, it may truncate the intermediate result on each iteration.
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
- `normalizedRatio(12, 34, 100)` returns `(26 74)`; the output ratio is smaller than the input ratio (26 / 74 = 0.351 < 0.352 = 12 / 34)
|
|
102
|
-
- `normalizedRatio(1234, 5678, 100)` returns `(18 82)`; the output ratio is larger than the input ratio (18 / 82 = 0.219 > 0.217 = 1234 / 5678)
|
|
100
|
+
Subsequently, the larger the input exponent is, the lower the accuracy of the output is likely to be.
|
|
103
101
|
|
|
104
|
-
|
|
102
|
+
The input argument `fast` allows to opt for either performance (using `true`) or accuracy (using `false`).
|
|
105
103
|
|
|
106
|
-
|
|
104
|
+
This library defines a maximum exponent of 4 bits (i.e., 15), which can be customized to fit the system requirements.
|
|
107
105
|
|
|
108
|
-
|
|
106
|
+
### Product Ratio
|
|
107
|
+
|
|
108
|
+
Function `productRatio` computes the product of two ratios as a single ratio whose components are not larger than 256 bits.
|
|
109
|
+
|
|
110
|
+
If either one of the intermediate components is larger than 256 bits, then both of them are reduced based on the larger one.
|
|
109
111
|
|
|
110
112
|
### Reduced Ratio
|
|
111
113
|
|
|
112
114
|
Function `reducedRatio` computes the nearest ratio whose components (numerator and denominator) are not larger than the input threshold.
|
|
113
115
|
|
|
114
|
-
Internally, it calls function `normalizedRatio` with the input threshold, but only if one of the components is larger than that threshold.
|
|
115
|
-
|
|
116
116
|
Note that function `reducedRatio` is not meant to replace GCD, nor does it strive to achieve better accuracy.
|
|
117
117
|
|
|
118
118
|
GCD is not being used here, because the time-complexity of this method depends on the bit-length of the input.
|
|
@@ -125,23 +125,21 @@ Reducing an input ratio by its GCD in advance (at your own expense) can most cer
|
|
|
125
125
|
|
|
126
126
|
However, without knowing specific characteristics of that ratio (e.g., each one of its components is a multiple of `0x1000`), doing so is generally useless.
|
|
127
127
|
|
|
128
|
-
###
|
|
129
|
-
|
|
130
|
-
Function `productRatio` computes the product of two ratios as a single ratio whose components are not larger than 256 bits.
|
|
131
|
-
|
|
132
|
-
If either one of the intermediate components is larger than 256 bits, then both of them are reduced based on the larger one.
|
|
128
|
+
### Normalized Ratio
|
|
133
129
|
|
|
134
|
-
|
|
130
|
+
Function `normalizedRatio` computes the nearest ratio whose sum of components (numerator + denominator) equals the input scale.
|
|
135
131
|
|
|
136
|
-
|
|
132
|
+
Note that the output ratio can be larger than the input ratio in some cases, and smaller than the input ratio in other cases.
|
|
137
133
|
|
|
138
|
-
|
|
134
|
+
For example:
|
|
135
|
+
- `normalizedRatio(12, 34, 100)` returns `(26 74)`; the output ratio is smaller than the input ratio (26 / 74 = 0.351 < 0.352 = 12 / 34)
|
|
136
|
+
- `normalizedRatio(1234, 5678, 100)` returns `(18 82)`; the output ratio is larger than the input ratio (18 / 82 = 0.219 > 0.217 = 1234 / 5678)
|
|
139
137
|
|
|
140
|
-
|
|
138
|
+
Keep in mind that it is an important consideration to take when choosing to use this function.
|
|
141
139
|
|
|
142
|
-
|
|
140
|
+
For example, when designing a sustainable financial model, it is imperative to never entitle more than the actual entitlement.
|
|
143
141
|
|
|
144
|
-
|
|
142
|
+
The same consideration applies for all the other functions in this module.
|
|
145
143
|
|
|
146
144
|
<br/><br/>
|
|
147
145
|
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.10;
|
|
3
3
|
|
|
4
4
|
import "./IntegralMath.sol";
|
|
5
5
|
|
|
@@ -13,15 +13,15 @@ library FractionMath {
|
|
|
13
13
|
/**
|
|
14
14
|
* @dev Compute the power of a given ratio
|
|
15
15
|
*
|
|
16
|
-
* @param
|
|
17
|
-
* @param
|
|
18
|
-
* @param exp
|
|
19
|
-
* @param fast
|
|
16
|
+
* @param n The ratio numerator
|
|
17
|
+
* @param d The ratio denominator
|
|
18
|
+
* @param exp The exponentiation value
|
|
19
|
+
* @param fast Opt for performance over accuracy
|
|
20
20
|
*
|
|
21
21
|
* @return The powered ratio numerator
|
|
22
22
|
* @return The powered ratio denominator
|
|
23
23
|
*/
|
|
24
|
-
function poweredRatio(uint256
|
|
24
|
+
function poweredRatio(uint256 n, uint256 d, uint256 exp, bool fast) internal pure returns (uint256, uint256) { unchecked {
|
|
25
25
|
require(exp <= MAX_EXP, "exp too large");
|
|
26
26
|
|
|
27
27
|
function (uint256, uint256, uint256, uint256) pure returns (uint256, uint256) safeRatio = fast ? mulRatio128 : productRatio;
|
|
@@ -29,13 +29,13 @@ library FractionMath {
|
|
|
29
29
|
uint256[MAX_EXP_BIT_LEN] memory ns;
|
|
30
30
|
uint256[MAX_EXP_BIT_LEN] memory ds;
|
|
31
31
|
|
|
32
|
-
(ns[0], ds[0]) = safeRatio(
|
|
32
|
+
(ns[0], ds[0]) = safeRatio(n, 1, d, 1);
|
|
33
33
|
for (uint256 i = 0; (exp >> i) > 1; ++i) {
|
|
34
34
|
(ns[i + 1], ds[i + 1]) = safeRatio(ns[i], ns[i], ds[i], ds[i]);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
n = 1;
|
|
38
|
+
d = 1;
|
|
39
39
|
|
|
40
40
|
for (uint256 i = 0; (exp >> i) > 0; ++i) {
|
|
41
41
|
if (((exp >> i) & 1) > 0) {
|
|
@@ -70,73 +70,70 @@ library FractionMath {
|
|
|
70
70
|
/**
|
|
71
71
|
* @dev Reduce the components of a given ratio
|
|
72
72
|
*
|
|
73
|
-
* @param
|
|
74
|
-
* @param
|
|
75
|
-
* @param max
|
|
73
|
+
* @param n The ratio numerator
|
|
74
|
+
* @param d The ratio denominator
|
|
75
|
+
* @param max The maximum desired value
|
|
76
76
|
*
|
|
77
77
|
* @return The reduced ratio numerator
|
|
78
78
|
* @return The reduced ratio denominator
|
|
79
79
|
*/
|
|
80
|
-
function reducedRatio(uint256
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
return (baseN, baseD);
|
|
80
|
+
function reducedRatio(uint256 n, uint256 d, uint256 max) internal pure returns (uint256, uint256) { unchecked {
|
|
81
|
+
uint256 scale = ((n > d ? n : d) - 1) / max + 1;
|
|
82
|
+
return (n / scale, d / scale);
|
|
85
83
|
}}
|
|
86
84
|
|
|
87
85
|
/**
|
|
88
86
|
* @dev Compute a normalized ratio as `scale * n / (n + d)` and `scale * d / (n + d)`
|
|
89
87
|
*
|
|
90
|
-
* @param
|
|
91
|
-
* @param
|
|
88
|
+
* @param n The ratio numerator
|
|
89
|
+
* @param d The ratio denominator
|
|
92
90
|
* @param scale The desired scale
|
|
93
91
|
*
|
|
94
92
|
* @return The normalized ratio numerator
|
|
95
93
|
* @return The normalized ratio denominator
|
|
96
94
|
*/
|
|
97
|
-
function normalizedRatio(uint256
|
|
98
|
-
if (
|
|
99
|
-
return estimatedRatio(
|
|
95
|
+
function normalizedRatio(uint256 n, uint256 d, uint256 scale) internal pure returns (uint256, uint256) { unchecked {
|
|
96
|
+
if (n <= d) {
|
|
97
|
+
return estimatedRatio(n, d, scale);
|
|
100
98
|
}
|
|
101
|
-
(
|
|
99
|
+
(d, n) = estimatedRatio(d, n, scale);
|
|
102
100
|
return (n, d);
|
|
103
101
|
}}
|
|
104
102
|
|
|
105
103
|
/**
|
|
106
104
|
* @dev Compute an estimated ratio as `scale * n / (n + d)` and `scale * d / (n + d)`, assuming that `n <= d`
|
|
107
105
|
*
|
|
108
|
-
* @param
|
|
109
|
-
* @param
|
|
106
|
+
* @param n The ratio numerator
|
|
107
|
+
* @param d The ratio denominator
|
|
110
108
|
* @param scale The desired scale
|
|
111
109
|
*
|
|
112
110
|
* @return The estimated ratio numerator
|
|
113
111
|
* @return The estimated ratio denominator
|
|
114
112
|
*/
|
|
115
|
-
function estimatedRatio(uint256
|
|
116
|
-
uint256
|
|
117
|
-
if (
|
|
118
|
-
// `
|
|
119
|
-
// `
|
|
120
|
-
uint256
|
|
121
|
-
|
|
122
|
-
|
|
113
|
+
function estimatedRatio(uint256 n, uint256 d, uint256 scale) private pure returns (uint256, uint256) { unchecked {
|
|
114
|
+
uint256 x = MAX_VAL / scale; // `MAX_VAL >= scale` hence `x >= 1`
|
|
115
|
+
if (x < n) {
|
|
116
|
+
// `x < n <= MAX_VAL` hence `x < MAX_VAL` hence `x + 1` is safe
|
|
117
|
+
// `x >= 1` hence `n / (x + 1) < MAX_VAL` hence `n / (x + 1) + 1` is safe
|
|
118
|
+
uint256 y = n / (x + 1) + 1;
|
|
119
|
+
n /= y; // we can now safely compute `n * scale`
|
|
120
|
+
d /= y;
|
|
123
121
|
}
|
|
124
122
|
|
|
125
|
-
if (
|
|
126
|
-
uint256
|
|
127
|
-
uint256
|
|
128
|
-
if (
|
|
129
|
-
// `
|
|
130
|
-
uint256
|
|
131
|
-
|
|
132
|
-
return (x, y);
|
|
123
|
+
if (n < d) {
|
|
124
|
+
uint256 p = n * scale;
|
|
125
|
+
uint256 q = unsafeAdd(n, d); // `n + d` can overflow
|
|
126
|
+
if (q >= n) {
|
|
127
|
+
// `n + d` did not overflow
|
|
128
|
+
uint256 r = IntegralMath.roundDiv(p, q);
|
|
129
|
+
return (r, scale - r); // `r = n * scale / (n + d) < scale`
|
|
133
130
|
}
|
|
134
|
-
if (
|
|
135
|
-
return (0, scale); // `
|
|
131
|
+
if (p < d - (d - n) / 2) {
|
|
132
|
+
return (0, scale); // `n * scale < (n + d) / 2 < MAX_VAL < n + d`
|
|
136
133
|
}
|
|
137
|
-
return (1, scale - 1); // `(
|
|
134
|
+
return (1, scale - 1); // `(n + d) / 2 < n * scale < MAX_VAL < n + d`
|
|
138
135
|
}
|
|
139
|
-
return (scale / 2, scale - scale / 2); // reflect the fact that initially `
|
|
136
|
+
return (scale / 2, scale - scale / 2); // reflect the fact that initially `n <= d`
|
|
140
137
|
}}
|
|
141
138
|
|
|
142
139
|
/**
|