Math.t.sol 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 expect = a / b;
  14. if (expect * b < a) {
  15. expect += 1;
  16. }
  17. assertEq(result, expect);
  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(Math.unsignedRoundsUp(rounding));
  27. assertTrue(_squareSmaller(result - 1, input));
  28. }
  29. // square of result is smaller than input
  30. else if (_squareSmaller(result, input)) {
  31. assertFalse(Math.unsignedRoundsUp(rounding));
  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) = Math.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(Math.unsignedRoundsUp(rounding));
  54. assertTrue(_powerOf2Smaller(result - 1, input));
  55. } else if (_powerOf2Smaller(result, input)) {
  56. assertFalse(Math.unsignedRoundsUp(rounding));
  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(Math.unsignedRoundsUp(rounding));
  76. assertTrue(_powerOf10Smaller(result - 1, input));
  77. } else if (_powerOf10Smaller(result, input)) {
  78. assertFalse(Math.unsignedRoundsUp(rounding));
  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(Math.unsignedRoundsUp(rounding));
  98. assertTrue(_powerOf256Smaller(result - 1, input));
  99. } else if (_powerOf256Smaller(result, input)) {
  100. assertFalse(Math.unsignedRoundsUp(rounding));
  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(uint256 x, uint256 y, uint256 d) public {
  114. // Full precision for x * y
  115. (uint256 xyHi, uint256 xyLo) = _mulHighLow(x, y);
  116. // Assume result won't overflow (see {testMulDivDomain})
  117. // This also checks that `d` is positive
  118. vm.assume(xyHi < d);
  119. // Perform muldiv
  120. uint256 q = Math.mulDiv(x, y, d);
  121. // Full precision for q * d
  122. (uint256 qdHi, uint256 qdLo) = _mulHighLow(q, d);
  123. // Add remainder of x * y / d (computed as rem = (x * y % d))
  124. (uint256 qdRemLo, uint256 c) = _addCarry(qdLo, _mulmod(x, y, d));
  125. uint256 qdRemHi = qdHi + c;
  126. // Full precision check that x * y = q * d + rem
  127. assertEq(xyHi, qdRemHi);
  128. assertEq(xyLo, qdRemLo);
  129. }
  130. function testMulDivDomain(uint256 x, uint256 y, uint256 d) public {
  131. (uint256 xyHi, ) = _mulHighLow(x, y);
  132. // Violate {testMulDiv} assumption (covers d is 0 and result overflow)
  133. vm.assume(xyHi >= d);
  134. // we are outside the scope of {testMulDiv}, we expect muldiv to revert
  135. try this.muldiv(x, y, d) returns (uint256) {
  136. fail();
  137. } catch {}
  138. }
  139. // External call
  140. function muldiv(uint256 x, uint256 y, uint256 d) external pure returns (uint256) {
  141. return Math.mulDiv(x, y, d);
  142. }
  143. // Helpers
  144. function _asRounding(uint8 r) private pure returns (Math.Rounding) {
  145. vm.assume(r < uint8(type(Math.Rounding).max));
  146. return Math.Rounding(r);
  147. }
  148. function _mulmod(uint256 x, uint256 y, uint256 z) private pure returns (uint256 r) {
  149. assembly {
  150. r := mulmod(x, y, z)
  151. }
  152. }
  153. function _mulHighLow(uint256 x, uint256 y) private pure returns (uint256 high, uint256 low) {
  154. (uint256 x0, uint256 x1) = (x & type(uint128).max, x >> 128);
  155. (uint256 y0, uint256 y1) = (y & type(uint128).max, y >> 128);
  156. // Karatsuba algorithm
  157. // https://en.wikipedia.org/wiki/Karatsuba_algorithm
  158. uint256 z2 = x1 * y1;
  159. uint256 z1a = x1 * y0;
  160. uint256 z1b = x0 * y1;
  161. uint256 z0 = x0 * y0;
  162. uint256 carry = ((z1a & type(uint128).max) + (z1b & type(uint128).max) + (z0 >> 128)) >> 128;
  163. high = z2 + (z1a >> 128) + (z1b >> 128) + carry;
  164. unchecked {
  165. low = x * y;
  166. }
  167. }
  168. function _addCarry(uint256 x, uint256 y) private pure returns (uint256 res, uint256 carry) {
  169. unchecked {
  170. res = x + y;
  171. }
  172. carry = res < x ? 1 : 0;
  173. }
  174. }