Math.t.sol 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "forge-std/Test.sol";
  4. import "../../../contracts/utils/math/Math.sol";
  5. import "../../../contracts/utils/math/SafeMath.sol";
  6. contract MathTest is Test {
  7. // CEILDIV
  8. function testCeilDiv(uint256 a, uint256 b) public {
  9. vm.assume(b > 0);
  10. uint256 result = Math.ceilDiv(a, b);
  11. if (result == 0) {
  12. assertEq(a, 0);
  13. } else {
  14. uint256 maxdiv = UINT256_MAX / b;
  15. bool overflow = maxdiv * b < a;
  16. assertTrue(a > b * (result - 1));
  17. assertTrue(overflow ? result == maxdiv + 1 : a <= b * result);
  18. }
  19. }
  20. // SQRT
  21. function testSqrt(uint256 input, uint8 r) public {
  22. Math.Rounding rounding = _asRounding(r);
  23. uint256 result = Math.sqrt(input, rounding);
  24. // square of result is bigger than input
  25. if (_squareBigger(result, input)) {
  26. assertTrue(rounding == Math.Rounding.Up);
  27. assertTrue(_squareSmaller(result - 1, input));
  28. }
  29. // square of result is smaller than input
  30. else if (_squareSmaller(result, input)) {
  31. assertFalse(rounding == Math.Rounding.Up);
  32. assertTrue(_squareBigger(result + 1, input));
  33. }
  34. // input is perfect square
  35. else {
  36. assertEq(result * result, input);
  37. }
  38. }
  39. function _squareBigger(uint256 value, uint256 ref) private pure returns (bool) {
  40. (bool noOverflow, uint256 square) = SafeMath.tryMul(value, value);
  41. return !noOverflow || square > ref;
  42. }
  43. function _squareSmaller(uint256 value, uint256 ref) private pure returns (bool) {
  44. return value * value < ref;
  45. }
  46. // LOG2
  47. function testLog2(uint256 input, uint8 r) public {
  48. Math.Rounding rounding = _asRounding(r);
  49. uint256 result = Math.log2(input, rounding);
  50. if (input == 0) {
  51. assertEq(result, 0);
  52. } else if (_powerOf2Bigger(result, input)) {
  53. assertTrue(rounding == Math.Rounding.Up);
  54. assertTrue(_powerOf2Smaller(result - 1, input));
  55. } else if (_powerOf2Smaller(result, input)) {
  56. assertFalse(rounding == Math.Rounding.Up);
  57. assertTrue(_powerOf2Bigger(result + 1, input));
  58. } else {
  59. assertEq(2**result, input);
  60. }
  61. }
  62. function _powerOf2Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  63. return value >= 256 || 2**value > ref; // 2**256 overflows uint256
  64. }
  65. function _powerOf2Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  66. return 2**value < ref;
  67. }
  68. // LOG10
  69. function testLog10(uint256 input, uint8 r) public {
  70. Math.Rounding rounding = _asRounding(r);
  71. uint256 result = Math.log10(input, rounding);
  72. if (input == 0) {
  73. assertEq(result, 0);
  74. } else if (_powerOf10Bigger(result, input)) {
  75. assertTrue(rounding == Math.Rounding.Up);
  76. assertTrue(_powerOf10Smaller(result - 1, input));
  77. } else if (_powerOf10Smaller(result, input)) {
  78. assertFalse(rounding == Math.Rounding.Up);
  79. assertTrue(_powerOf10Bigger(result + 1, input));
  80. } else {
  81. assertEq(10**result, input);
  82. }
  83. }
  84. function _powerOf10Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  85. return value >= 78 || 10**value > ref; // 10**78 overflows uint256
  86. }
  87. function _powerOf10Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  88. return 10**value < ref;
  89. }
  90. // LOG256
  91. function testLog256(uint256 input, uint8 r) public {
  92. Math.Rounding rounding = _asRounding(r);
  93. uint256 result = Math.log256(input, rounding);
  94. if (input == 0) {
  95. assertEq(result, 0);
  96. } else if (_powerOf256Bigger(result, input)) {
  97. assertTrue(rounding == Math.Rounding.Up);
  98. assertTrue(_powerOf256Smaller(result - 1, input));
  99. } else if (_powerOf256Smaller(result, input)) {
  100. assertFalse(rounding == Math.Rounding.Up);
  101. assertTrue(_powerOf256Bigger(result + 1, input));
  102. } else {
  103. assertEq(256**result, input);
  104. }
  105. }
  106. function _powerOf256Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  107. return value >= 32 || 256**value > ref; // 256**32 overflows uint256
  108. }
  109. function _powerOf256Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  110. return 256**value < ref;
  111. }
  112. // MULDIV
  113. function testMulDiv(
  114. uint256 x,
  115. uint256 y,
  116. uint256 d
  117. ) public {
  118. // Full precision for x * y
  119. (uint256 xyHi, uint256 xyLo) = _mulHighLow(x, y);
  120. // Assume result won't overflow (see {testMulDivDomain})
  121. // This also checks that `d` is positive
  122. vm.assume(xyHi < d);
  123. // Perform muldiv
  124. uint256 q = Math.mulDiv(x, y, d);
  125. // Full precision for q * d
  126. (uint256 qdHi, uint256 qdLo) = _mulHighLow(q, d);
  127. // Add remainder of x * y / d (computed as rem = (x * y % d))
  128. (uint256 qdRemLo, uint256 c) = _addCarry(qdLo, _mulmod(x, y, d));
  129. uint256 qdRemHi = qdHi + c;
  130. // Full precision check that x * y = q * d + rem
  131. assertEq(xyHi, qdRemHi);
  132. assertEq(xyLo, qdRemLo);
  133. }
  134. function testMulDivDomain(
  135. uint256 x,
  136. uint256 y,
  137. uint256 d
  138. ) public {
  139. (uint256 xyHi, ) = _mulHighLow(x, y);
  140. // Violate {testMulDiv} assumption (covers d is 0 and result overflow)
  141. vm.assume(xyHi >= d);
  142. // we are outside the scope of {testMulDiv}, we expect muldiv to revert
  143. try this.muldiv(x, y, d) returns (uint256) {
  144. fail();
  145. } catch {}
  146. }
  147. // External call
  148. function muldiv(
  149. uint256 x,
  150. uint256 y,
  151. uint256 d
  152. ) external pure returns (uint256) {
  153. return Math.mulDiv(x, y, d);
  154. }
  155. // Helpers
  156. function _asRounding(uint8 r) private returns (Math.Rounding) {
  157. vm.assume(r < uint8(type(Math.Rounding).max));
  158. return Math.Rounding(r);
  159. }
  160. function _mulmod(
  161. uint256 x,
  162. uint256 y,
  163. uint256 z
  164. ) private pure returns (uint256 r) {
  165. assembly {
  166. r := mulmod(x, y, z)
  167. }
  168. }
  169. function _mulHighLow(uint256 x, uint256 y) private pure returns (uint256 high, uint256 low) {
  170. (uint256 x0, uint256 x1) = (x & type(uint128).max, x >> 128);
  171. (uint256 y0, uint256 y1) = (y & type(uint128).max, y >> 128);
  172. // Karatsuba algorithm
  173. // https://en.wikipedia.org/wiki/Karatsuba_algorithm
  174. uint256 z2 = x1 * y1;
  175. uint256 z1a = x1 * y0;
  176. uint256 z1b = x0 * y1;
  177. uint256 z0 = x0 * y0;
  178. uint256 carry = ((z1a & type(uint128).max) + (z1b & type(uint128).max) + (z0 >> 128)) >> 128;
  179. high = z2 + (z1a >> 128) + (z1b >> 128) + carry;
  180. unchecked {
  181. low = x * y;
  182. }
  183. }
  184. function _addCarry(uint256 x, uint256 y) private pure returns (uint256 res, uint256 carry) {
  185. unchecked {
  186. res = x + y;
  187. }
  188. carry = res < x ? 1 : 0;
  189. }
  190. }