solidity-math-utils 1.10.0 → 1.11.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 +8 -0
- package/package.json +3 -3
- package/project/contracts/IntegralMath.sol +47 -0
package/README.md
CHANGED
|
@@ -46,6 +46,8 @@ This module implements the following interface:
|
|
|
46
46
|
- `function roundDiv(uint256 n, uint256 d)` => `(uint256)`
|
|
47
47
|
- `function mulDivF(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
48
48
|
- `function mulDivC(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
49
|
+
- `function mulDivExF(uint256 x, uint256 y, uint256 z, uint256 w)` => `(uint256)`
|
|
50
|
+
- `function mulDivExC(uint256 x, uint256 y, uint256 z, uint256 w)` => `(uint256)`
|
|
49
51
|
|
|
50
52
|
Function `floorLog2(n)` computes the largest integer smaller than or equal to the binary logarithm of `n`.
|
|
51
53
|
|
|
@@ -63,8 +65,14 @@ Function `mulDivF(x, y, z)` computes the largest integer smaller than or equal t
|
|
|
63
65
|
|
|
64
66
|
Function `mulDivC(x, y, z)` computes the smallest integer larger than or equal to `x * y / z`.
|
|
65
67
|
|
|
68
|
+
Function `mulDivExF(x, y, z, w)` computes the largest integer smaller than or equal to `(x * y) / (z * w)`.
|
|
69
|
+
|
|
70
|
+
Function `mulDivExC(x, y, z, w)` computes the smallest integer larger than or equal to `(x * y) / (z * w)`.
|
|
71
|
+
|
|
66
72
|
Note that function `mulDivF` and function `mulDivC` revert when the **actual** result is larger than 256 bits.
|
|
67
73
|
|
|
74
|
+
Note that function `mulDivExF` and function `mulDivExC` revert when the **actual** result is larger than 256 bits.
|
|
75
|
+
|
|
68
76
|
Note that function `floorSqrt` and function `ceilSqrt` are guaranteed to return the correct output for every input.
|
|
69
77
|
|
|
70
78
|
However, when compared with the **actual** square root, smaller input generally yields relatively lower accuracy of the output.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solidity-math-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Solidity Mathematical Utilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"@nomiclabs/hardhat-truffle5": "2.0.7",
|
|
22
22
|
"@nomiclabs/hardhat-web3": "2.0.0",
|
|
23
23
|
"decimal.js": "10.4.3",
|
|
24
|
-
"hardhat": "2.
|
|
25
|
-
"solidity-coverage": "0.8.
|
|
24
|
+
"hardhat": "2.16.1",
|
|
25
|
+
"solidity-coverage": "0.8.4"
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -117,6 +117,44 @@ library IntegralMath {
|
|
|
117
117
|
return w;
|
|
118
118
|
}}
|
|
119
119
|
|
|
120
|
+
/**
|
|
121
|
+
* @dev Compute the largest integer smaller than or equal to `(x * y) / (z * w)`
|
|
122
|
+
*/
|
|
123
|
+
function mulDivExF(uint256 x, uint256 y, uint256 z, uint256 w) internal pure returns (uint256) { unchecked {
|
|
124
|
+
(uint256 zwh, uint256 zwl) = mul512(z, w);
|
|
125
|
+
if (zwh > 0) {
|
|
126
|
+
uint256 res = 0;
|
|
127
|
+
(uint256 xyh, uint256 xyl) = mul512(x, y);
|
|
128
|
+
if (xyh > zwh) {
|
|
129
|
+
uint8 xyhn = floorLog2(xyh);
|
|
130
|
+
uint8 zwhn = floorLog2(zwh);
|
|
131
|
+
while (xyhn > zwhn) {
|
|
132
|
+
uint8 n = xyhn - zwhn - 1;
|
|
133
|
+
res += 1 << n; // set `res = res + 2 ^ n`
|
|
134
|
+
(xyh, xyl) = sub512Ex(xyh, xyl, (zwh << n) | (zwl >> (256 - n)), zwl << n); // set `xy = xy - zw * 2 ^ n`
|
|
135
|
+
xyhn = floorLog2(xyh);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (xyh > zwh || (xyh == zwh && xyl >= zwl)) // `xy >= zw`
|
|
139
|
+
return res + 1;
|
|
140
|
+
return res;
|
|
141
|
+
}
|
|
142
|
+
return mulDivF(x, y, zwl);
|
|
143
|
+
}}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @dev Compute the smallest integer larger than or equal to `(x * y) / (z * w)`
|
|
147
|
+
*/
|
|
148
|
+
function mulDivExC(uint256 x, uint256 y, uint256 z, uint256 w) internal pure returns (uint256) { unchecked {
|
|
149
|
+
uint256 v = mulDivExF(x, y, z, w);
|
|
150
|
+
(uint256 xyh, uint256 xyl) = mul512(x, y);
|
|
151
|
+
(uint256 zwh, uint256 zwl) = mul512(z, w);
|
|
152
|
+
(uint256 vzwlh, uint256 vzwll) = mul512(v, zwl);
|
|
153
|
+
if (xyh == v * zwh + vzwlh && xyl == vzwll)
|
|
154
|
+
return v;
|
|
155
|
+
return safeAdd(v, 1);
|
|
156
|
+
}}
|
|
157
|
+
|
|
120
158
|
/**
|
|
121
159
|
* @dev Compute the value of `x * y`
|
|
122
160
|
*/
|
|
@@ -155,4 +193,13 @@ library IntegralMath {
|
|
|
155
193
|
x = unsafeMul(x, unsafeSub(2, unsafeMul(x, d))); // `x = x * (2 - x * d) mod 2 ^ 256`
|
|
156
194
|
return x;
|
|
157
195
|
}}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @dev Compute the value of `(2 ^ 256 * xh + xl) - (2 ^ 256 * yh + yl)`, where `2 ^ 256 * xh + xl >= 2 ^ 256 * yh + yl`
|
|
199
|
+
*/
|
|
200
|
+
function sub512Ex(uint256 xh, uint256 xl, uint256 yh, uint256 yl) private pure returns (uint256, uint256) { unchecked {
|
|
201
|
+
if (xl >= yl)
|
|
202
|
+
return (xh - yh, xl - yl);
|
|
203
|
+
return (xh - yh - 1, unsafeSub(xl, yl));
|
|
204
|
+
}}
|
|
158
205
|
}
|