Math.t.sol 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Test, stdError} 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. // INV
  47. function testInvMod(uint256 value, uint256 p) public {
  48. _testInvMod(value, p, true);
  49. }
  50. function testInvMod2(uint256 seed) public {
  51. uint256 p = 2; // prime
  52. _testInvMod(bound(seed, 1, p - 1), p, false);
  53. }
  54. function testInvMod17(uint256 seed) public {
  55. uint256 p = 17; // prime
  56. _testInvMod(bound(seed, 1, p - 1), p, false);
  57. }
  58. function testInvMod65537(uint256 seed) public {
  59. uint256 p = 65537; // prime
  60. _testInvMod(bound(seed, 1, p - 1), p, false);
  61. }
  62. function testInvModP256(uint256 seed) public {
  63. uint256 p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff; // prime
  64. _testInvMod(bound(seed, 1, p - 1), p, false);
  65. }
  66. function _testInvMod(uint256 value, uint256 p, bool allowZero) private {
  67. uint256 inverse = Math.invMod(value, p);
  68. if (inverse != 0) {
  69. assertEq(mulmod(value, inverse, p), 1);
  70. assertLt(inverse, p);
  71. } else {
  72. assertTrue(allowZero);
  73. }
  74. }
  75. // LOG2
  76. function testLog2(uint256 input, uint8 r) public {
  77. Math.Rounding rounding = _asRounding(r);
  78. uint256 result = Math.log2(input, rounding);
  79. if (input == 0) {
  80. assertEq(result, 0);
  81. } else if (_powerOf2Bigger(result, input)) {
  82. assertTrue(Math.unsignedRoundsUp(rounding));
  83. assertTrue(_powerOf2Smaller(result - 1, input));
  84. } else if (_powerOf2Smaller(result, input)) {
  85. assertFalse(Math.unsignedRoundsUp(rounding));
  86. assertTrue(_powerOf2Bigger(result + 1, input));
  87. } else {
  88. assertEq(2 ** result, input);
  89. }
  90. }
  91. function _powerOf2Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  92. return value >= 256 || 2 ** value > ref; // 2**256 overflows uint256
  93. }
  94. function _powerOf2Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  95. return 2 ** value < ref;
  96. }
  97. // LOG10
  98. function testLog10(uint256 input, uint8 r) public {
  99. Math.Rounding rounding = _asRounding(r);
  100. uint256 result = Math.log10(input, rounding);
  101. if (input == 0) {
  102. assertEq(result, 0);
  103. } else if (_powerOf10Bigger(result, input)) {
  104. assertTrue(Math.unsignedRoundsUp(rounding));
  105. assertTrue(_powerOf10Smaller(result - 1, input));
  106. } else if (_powerOf10Smaller(result, input)) {
  107. assertFalse(Math.unsignedRoundsUp(rounding));
  108. assertTrue(_powerOf10Bigger(result + 1, input));
  109. } else {
  110. assertEq(10 ** result, input);
  111. }
  112. }
  113. function _powerOf10Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  114. return value >= 78 || 10 ** value > ref; // 10**78 overflows uint256
  115. }
  116. function _powerOf10Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  117. return 10 ** value < ref;
  118. }
  119. // LOG256
  120. function testLog256(uint256 input, uint8 r) public {
  121. Math.Rounding rounding = _asRounding(r);
  122. uint256 result = Math.log256(input, rounding);
  123. if (input == 0) {
  124. assertEq(result, 0);
  125. } else if (_powerOf256Bigger(result, input)) {
  126. assertTrue(Math.unsignedRoundsUp(rounding));
  127. assertTrue(_powerOf256Smaller(result - 1, input));
  128. } else if (_powerOf256Smaller(result, input)) {
  129. assertFalse(Math.unsignedRoundsUp(rounding));
  130. assertTrue(_powerOf256Bigger(result + 1, input));
  131. } else {
  132. assertEq(256 ** result, input);
  133. }
  134. }
  135. function _powerOf256Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  136. return value >= 32 || 256 ** value > ref; // 256**32 overflows uint256
  137. }
  138. function _powerOf256Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  139. return 256 ** value < ref;
  140. }
  141. // MULDIV
  142. function testMulDiv(uint256 x, uint256 y, uint256 d) public {
  143. // Full precision for x * y
  144. (uint256 xyHi, uint256 xyLo) = _mulHighLow(x, y);
  145. // Assume result won't overflow (see {testMulDivDomain})
  146. // This also checks that `d` is positive
  147. vm.assume(xyHi < d);
  148. // Perform muldiv
  149. uint256 q = Math.mulDiv(x, y, d);
  150. // Full precision for q * d
  151. (uint256 qdHi, uint256 qdLo) = _mulHighLow(q, d);
  152. // Add remainder of x * y / d (computed as rem = (x * y % d))
  153. (uint256 qdRemLo, uint256 c) = _addCarry(qdLo, mulmod(x, y, d));
  154. uint256 qdRemHi = qdHi + c;
  155. // Full precision check that x * y = q * d + rem
  156. assertEq(xyHi, qdRemHi);
  157. assertEq(xyLo, qdRemLo);
  158. }
  159. function testMulDivDomain(uint256 x, uint256 y, uint256 d) public {
  160. (uint256 xyHi, ) = _mulHighLow(x, y);
  161. // Violate {testMulDiv} assumption (covers d is 0 and result overflow)
  162. vm.assume(xyHi >= d);
  163. // we are outside the scope of {testMulDiv}, we expect muldiv to revert
  164. vm.expectRevert(d == 0 ? stdError.divisionError : stdError.arithmeticError);
  165. Math.mulDiv(x, y, d);
  166. }
  167. // MOD EXP
  168. function testModExp(uint256 b, uint256 e, uint256 m) public {
  169. if (m == 0) {
  170. vm.expectRevert(stdError.divisionError);
  171. }
  172. uint256 result = Math.modExp(b, e, m);
  173. assertLt(result, m);
  174. assertEq(result, _nativeModExp(b, e, m));
  175. }
  176. function testTryModExp(uint256 b, uint256 e, uint256 m) public {
  177. (bool success, uint256 result) = Math.tryModExp(b, e, m);
  178. assertEq(success, m != 0);
  179. if (success) {
  180. assertLt(result, m);
  181. assertEq(result, _nativeModExp(b, e, m));
  182. } else {
  183. assertEq(result, 0);
  184. }
  185. }
  186. function _nativeModExp(uint256 b, uint256 e, uint256 m) private pure returns (uint256) {
  187. if (m == 1) return 0;
  188. uint256 r = 1;
  189. while (e > 0) {
  190. if (e % 2 > 0) {
  191. r = mulmod(r, b, m);
  192. }
  193. b = mulmod(b, b, m);
  194. e >>= 1;
  195. }
  196. return r;
  197. }
  198. // Helpers
  199. function _asRounding(uint8 r) private pure returns (Math.Rounding) {
  200. vm.assume(r < uint8(type(Math.Rounding).max));
  201. return Math.Rounding(r);
  202. }
  203. function _mulHighLow(uint256 x, uint256 y) private pure returns (uint256 high, uint256 low) {
  204. (uint256 x0, uint256 x1) = (x & type(uint128).max, x >> 128);
  205. (uint256 y0, uint256 y1) = (y & type(uint128).max, y >> 128);
  206. // Karatsuba algorithm
  207. // https://en.wikipedia.org/wiki/Karatsuba_algorithm
  208. uint256 z2 = x1 * y1;
  209. uint256 z1a = x1 * y0;
  210. uint256 z1b = x0 * y1;
  211. uint256 z0 = x0 * y0;
  212. uint256 carry = ((z1a & type(uint128).max) + (z1b & type(uint128).max) + (z0 >> 128)) >> 128;
  213. high = z2 + (z1a >> 128) + (z1b >> 128) + carry;
  214. unchecked {
  215. low = x * y;
  216. }
  217. }
  218. function _addCarry(uint256 x, uint256 y) private pure returns (uint256 res, uint256 carry) {
  219. unchecked {
  220. res = x + y;
  221. }
  222. carry = res < x ? 1 : 0;
  223. }
  224. }