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 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
- ### Normalized Ratio
94
+ ### Powered Ratio
95
95
 
96
- Function `normalizedRatio` computes the nearest ratio whose sum of components (numerator + denominator) equals the input scale.
96
+ Function `poweredRatio` computes the power of a given ratio by a given exponent.
97
97
 
98
- Note that the output ratio can be larger than the input ratio in some cases, and smaller than the input ratio in other cases.
98
+ In order to avoid multiplication overflow, it may truncate the intermediate result on each iteration.
99
99
 
100
- For example:
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
- Keep in mind that it is an important consideration to take when choosing to use this function.
102
+ The input argument `fast` allows to opt for either performance (using `true`) or accuracy (using `false`).
105
103
 
106
- For example, when designing a sustainable financial model, it is imperative to never entitle more than the actual entitlement.
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
- The same consideration applies for all the other functions in this module, since each one of them uses this function internally.
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
- ### Product Ratio
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
- ### Powered Ratio
130
+ Function `normalizedRatio` computes the nearest ratio whose sum of components (numerator + denominator) equals the input scale.
135
131
 
136
- Function `poweredRatio` computes the power of a given ratio by a given exponent.
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
- In order to avoid multiplication overflow, it may truncate the intermediate result on each iteration.
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
- Subsequently, the larger the input exponent is, the lower the accuracy of the output is likely to be.
138
+ Keep in mind that it is an important consideration to take when choosing to use this function.
141
139
 
142
- The input argument `fast` allows to opt for either accuracy (using `false`) or performance (using `true`).
140
+ For example, when designing a sustainable financial model, it is imperative to never entitle more than the actual entitlement.
143
141
 
144
- This library defines a maximum exponent of 4 bits (i.e., 15), which can be customized to fit the system requirements.
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,6 +1,6 @@
1
1
  {
2
2
  "name": "solidity-math-utils",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Solidity Mathematical Utilities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity 0.8.6;
2
+ pragma solidity 0.8.10;
3
3
 
4
4
  import "./AnalyticMath.sol";
5
5
  import "./FractionMath.sol";
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity 0.8.6;
2
+ pragma solidity 0.8.10;
3
3
 
4
4
  import "./IntegralMath.sol";
5
5
 
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity 0.8.6;
2
+ pragma solidity 0.8.10;
3
3
 
4
4
  import "./AnalyticMath.sol";
5
5
 
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity 0.8.6;
2
+ pragma solidity 0.8.10;
3
3
 
4
4
  import "./AdvancedMath.sol";
5
5
 
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity 0.8.6;
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 baseN The ratio numerator
17
- * @param baseD The ratio denominator
18
- * @param exp The exponentiation value
19
- * @param fast Opt for accuracy or performance
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 baseN, uint256 baseD, uint256 exp, bool fast) internal pure returns (uint256, uint256) { unchecked {
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(baseN, 1, baseD, 1);
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
- uint256 n = 1;
38
- uint256 d = 1;
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 baseN The ratio numerator
74
- * @param baseD The ratio denominator
75
- * @param max The maximum desired value
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 baseN, uint256 baseD, uint256 max) internal pure returns (uint256, uint256) { unchecked {
81
- if (baseN > max || baseD > max) {
82
- return normalizedRatio(baseN, baseD, max);
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 baseN The ratio numerator
91
- * @param baseD The ratio denominator
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 baseN, uint256 baseD, uint256 scale) internal pure returns (uint256, uint256) { unchecked {
98
- if (baseN <= baseD) {
99
- return estimatedRatio(baseN, baseD, scale);
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
- (uint256 d, uint256 n) = estimatedRatio(baseD, baseN, scale);
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 baseN The ratio numerator
109
- * @param baseD The ratio denominator
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 baseN, uint256 baseD, uint256 scale) private pure returns (uint256, uint256) { unchecked {
116
- uint256 maxN = MAX_VAL / scale; // `MAX_VAL >= scale` hence `maxN >= 1`
117
- if (maxN < baseN) {
118
- // `maxN < baseN <= MAX_VAL` hence `maxN < MAX_VAL` hence `maxN + 1` is safe
119
- // `maxN + 1 >= 2` hence `baseN / (maxN + 1) < MAX_VAL` hence `baseN / (maxN + 1) + 1` is safe
120
- uint256 c = baseN / (maxN + 1) + 1;
121
- baseN /= c; // we can now safely compute `baseN * scale`
122
- baseD /= c;
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 (baseN != baseD) {
126
- uint256 n = baseN * scale;
127
- uint256 d = unsafeAdd(baseN, baseD); // `baseN + baseD` can overflow
128
- if (d >= baseN) {
129
- // `baseN + baseD` did not overflow
130
- uint256 x = IntegralMath.roundDiv(n, d); // we can now safely compute `scale - x`
131
- uint256 y = scale - x;
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 (n < baseD - (baseD - baseN) / 2) {
135
- return (0, scale); // `baseN * scale < (baseN + baseD) / 2 < MAX_VAL < baseN + baseD`
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); // `(baseN + baseD) / 2 < baseN * scale < MAX_VAL < baseN + baseD`
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 `baseN <= baseD`
136
+ return (scale / 2, scale - scale / 2); // reflect the fact that initially `n <= d`
140
137
  }}
141
138
 
142
139
  /**
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: SEE LICENSE IN LICENSE
2
- pragma solidity 0.8.6;
2
+ pragma solidity 0.8.10;
3
3
 
4
4
  import "./common/Uint.sol";
5
5