Math.t.sol 7.0 KB

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