Math.t.sol 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. function testSymbolicTernary(bool f, uint256 a, uint256 b) public pure {
  7. assertEq(Math.ternary(f, a, b), f ? a : b);
  8. }
  9. // MIN & MAX
  10. function testSymbolicMinMax(uint256 a, uint256 b) public pure {
  11. assertEq(Math.min(a, b), a < b ? a : b);
  12. assertEq(Math.max(a, b), a > b ? a : b);
  13. }
  14. // CEILDIV
  15. function testCeilDiv(uint256 a, uint256 b) public pure {
  16. vm.assume(b > 0);
  17. uint256 result = Math.ceilDiv(a, b);
  18. if (result == 0) {
  19. assertEq(a, 0);
  20. } else {
  21. uint256 expect = a / b;
  22. if (expect * b < a) {
  23. expect += 1;
  24. }
  25. assertEq(result, expect);
  26. }
  27. }
  28. // SQRT
  29. function testSqrt(uint256 input, uint8 r) public pure {
  30. Math.Rounding rounding = _asRounding(r);
  31. uint256 result = Math.sqrt(input, rounding);
  32. // square of result is bigger than input
  33. if (_squareBigger(result, input)) {
  34. assertTrue(Math.unsignedRoundsUp(rounding));
  35. assertTrue(_squareSmaller(result - 1, input));
  36. }
  37. // square of result is smaller than input
  38. else if (_squareSmaller(result, input)) {
  39. assertFalse(Math.unsignedRoundsUp(rounding));
  40. assertTrue(_squareBigger(result + 1, input));
  41. }
  42. // input is perfect square
  43. else {
  44. assertEq(result * result, input);
  45. }
  46. }
  47. function _squareBigger(uint256 value, uint256 ref) private pure returns (bool) {
  48. (bool noOverflow, uint256 square) = Math.tryMul(value, value);
  49. return !noOverflow || square > ref;
  50. }
  51. function _squareSmaller(uint256 value, uint256 ref) private pure returns (bool) {
  52. return value * value < ref;
  53. }
  54. // INV
  55. function testInvMod(uint256 value, uint256 p) public pure {
  56. _testInvMod(value, p, true);
  57. }
  58. function testInvMod2(uint256 seed) public pure {
  59. uint256 p = 2; // prime
  60. _testInvMod(bound(seed, 1, p - 1), p, false);
  61. }
  62. function testInvMod17(uint256 seed) public pure {
  63. uint256 p = 17; // prime
  64. _testInvMod(bound(seed, 1, p - 1), p, false);
  65. }
  66. function testInvMod65537(uint256 seed) public pure {
  67. uint256 p = 65537; // prime
  68. _testInvMod(bound(seed, 1, p - 1), p, false);
  69. }
  70. function testInvModP256(uint256 seed) public pure {
  71. uint256 p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff; // prime
  72. _testInvMod(bound(seed, 1, p - 1), p, false);
  73. }
  74. function _testInvMod(uint256 value, uint256 p, bool allowZero) private pure {
  75. uint256 inverse = Math.invMod(value, p);
  76. if (inverse != 0) {
  77. assertEq(mulmod(value, inverse, p), 1);
  78. assertLt(inverse, p);
  79. } else {
  80. assertTrue(allowZero);
  81. }
  82. }
  83. // LOG2
  84. function testLog2(uint256 input, uint8 r) public pure {
  85. Math.Rounding rounding = _asRounding(r);
  86. uint256 result = Math.log2(input, rounding);
  87. if (input == 0) {
  88. assertEq(result, 0);
  89. } else if (_powerOf2Bigger(result, input)) {
  90. assertTrue(Math.unsignedRoundsUp(rounding));
  91. assertTrue(_powerOf2Smaller(result - 1, input));
  92. } else if (_powerOf2Smaller(result, input)) {
  93. assertFalse(Math.unsignedRoundsUp(rounding));
  94. assertTrue(_powerOf2Bigger(result + 1, input));
  95. } else {
  96. assertEq(2 ** result, input);
  97. }
  98. }
  99. function _powerOf2Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  100. return value >= 256 || 2 ** value > ref; // 2**256 overflows uint256
  101. }
  102. function _powerOf2Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  103. return 2 ** value < ref;
  104. }
  105. // LOG10
  106. function testLog10(uint256 input, uint8 r) public pure {
  107. Math.Rounding rounding = _asRounding(r);
  108. uint256 result = Math.log10(input, rounding);
  109. if (input == 0) {
  110. assertEq(result, 0);
  111. } else if (_powerOf10Bigger(result, input)) {
  112. assertTrue(Math.unsignedRoundsUp(rounding));
  113. assertTrue(_powerOf10Smaller(result - 1, input));
  114. } else if (_powerOf10Smaller(result, input)) {
  115. assertFalse(Math.unsignedRoundsUp(rounding));
  116. assertTrue(_powerOf10Bigger(result + 1, input));
  117. } else {
  118. assertEq(10 ** result, input);
  119. }
  120. }
  121. function _powerOf10Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  122. return value >= 78 || 10 ** value > ref; // 10**78 overflows uint256
  123. }
  124. function _powerOf10Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  125. return 10 ** value < ref;
  126. }
  127. // LOG256
  128. function testLog256(uint256 input, uint8 r) public pure {
  129. Math.Rounding rounding = _asRounding(r);
  130. uint256 result = Math.log256(input, rounding);
  131. if (input == 0) {
  132. assertEq(result, 0);
  133. } else if (_powerOf256Bigger(result, input)) {
  134. assertTrue(Math.unsignedRoundsUp(rounding));
  135. assertTrue(_powerOf256Smaller(result - 1, input));
  136. } else if (_powerOf256Smaller(result, input)) {
  137. assertFalse(Math.unsignedRoundsUp(rounding));
  138. assertTrue(_powerOf256Bigger(result + 1, input));
  139. } else {
  140. assertEq(256 ** result, input);
  141. }
  142. }
  143. function _powerOf256Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  144. return value >= 32 || 256 ** value > ref; // 256**32 overflows uint256
  145. }
  146. function _powerOf256Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  147. return 256 ** value < ref;
  148. }
  149. // MULDIV
  150. function testMulDiv(uint256 x, uint256 y, uint256 d) public pure {
  151. // Full precision for x * y
  152. (uint256 xyHi, uint256 xyLo) = _mulHighLow(x, y);
  153. // Assume result won't overflow (see {testMulDivDomain})
  154. // This also checks that `d` is positive
  155. vm.assume(xyHi < d);
  156. // Perform muldiv
  157. uint256 q = Math.mulDiv(x, y, d);
  158. // Full precision for q * d
  159. (uint256 qdHi, uint256 qdLo) = _mulHighLow(q, d);
  160. // Add remainder of x * y / d (computed as rem = (x * y % d))
  161. (uint256 qdRemLo, uint256 c) = _addCarry(qdLo, mulmod(x, y, d));
  162. uint256 qdRemHi = qdHi + c;
  163. // Full precision check that x * y = q * d + rem
  164. assertEq(xyHi, qdRemHi);
  165. assertEq(xyLo, qdRemLo);
  166. }
  167. /// forge-config: default.allow_internal_expect_revert = true
  168. function testMulDivDomain(uint256 x, uint256 y, uint256 d) public {
  169. (uint256 xyHi, ) = _mulHighLow(x, y);
  170. // Violate {testMulDiv} assumption (covers d is 0 and result overflow)
  171. vm.assume(xyHi >= d);
  172. // we are outside the scope of {testMulDiv}, we expect muldiv to revert
  173. vm.expectRevert(d == 0 ? stdError.divisionError : stdError.arithmeticError);
  174. Math.mulDiv(x, y, d);
  175. }
  176. // MOD EXP
  177. /// forge-config: default.allow_internal_expect_revert = true
  178. function testModExp(uint256 b, uint256 e, uint256 m) public {
  179. if (m == 0) {
  180. vm.expectRevert(stdError.divisionError);
  181. }
  182. uint256 result = Math.modExp(b, e, m);
  183. assertLt(result, m);
  184. assertEq(result, _nativeModExp(b, e, m));
  185. }
  186. function testTryModExp(uint256 b, uint256 e, uint256 m) public view {
  187. (bool success, uint256 result) = Math.tryModExp(b, e, m);
  188. assertEq(success, m != 0);
  189. if (success) {
  190. assertLt(result, m);
  191. assertEq(result, _nativeModExp(b, e, m));
  192. } else {
  193. assertEq(result, 0);
  194. }
  195. }
  196. /// forge-config: default.allow_internal_expect_revert = true
  197. function testModExpMemory(uint256 b, uint256 e, uint256 m) public {
  198. if (m == 0) {
  199. vm.expectRevert(stdError.divisionError);
  200. }
  201. bytes memory result = Math.modExp(abi.encodePacked(b), abi.encodePacked(e), abi.encodePacked(m));
  202. assertEq(result.length, 0x20);
  203. uint256 res = abi.decode(result, (uint256));
  204. assertLt(res, m);
  205. assertEq(res, _nativeModExp(b, e, m));
  206. }
  207. function testTryModExpMemory(uint256 b, uint256 e, uint256 m) public view {
  208. (bool success, bytes memory result) = Math.tryModExp(
  209. abi.encodePacked(b),
  210. abi.encodePacked(e),
  211. abi.encodePacked(m)
  212. );
  213. if (success) {
  214. assertEq(result.length, 0x20); // m is a uint256, so abi.encodePacked(m).length is 0x20
  215. uint256 res = abi.decode(result, (uint256));
  216. assertLt(res, m);
  217. assertEq(res, _nativeModExp(b, e, m));
  218. } else {
  219. assertEq(result.length, 0);
  220. }
  221. }
  222. function _nativeModExp(uint256 b, uint256 e, uint256 m) private pure returns (uint256) {
  223. if (m == 1) return 0;
  224. uint256 r = 1;
  225. while (e > 0) {
  226. if (e % 2 > 0) {
  227. r = mulmod(r, b, m);
  228. }
  229. b = mulmod(b, b, m);
  230. e >>= 1;
  231. }
  232. return r;
  233. }
  234. // Helpers
  235. function _asRounding(uint8 r) private pure returns (Math.Rounding) {
  236. vm.assume(r < uint8(type(Math.Rounding).max));
  237. return Math.Rounding(r);
  238. }
  239. function _mulHighLow(uint256 x, uint256 y) private pure returns (uint256 high, uint256 low) {
  240. (uint256 x0, uint256 x1) = (x & type(uint128).max, x >> 128);
  241. (uint256 y0, uint256 y1) = (y & type(uint128).max, y >> 128);
  242. // Karatsuba algorithm
  243. // https://en.wikipedia.org/wiki/Karatsuba_algorithm
  244. uint256 z2 = x1 * y1;
  245. uint256 z1a = x1 * y0;
  246. uint256 z1b = x0 * y1;
  247. uint256 z0 = x0 * y0;
  248. uint256 carry = ((z1a & type(uint128).max) + (z1b & type(uint128).max) + (z0 >> 128)) >> 128;
  249. high = z2 + (z1a >> 128) + (z1b >> 128) + carry;
  250. unchecked {
  251. low = x * y;
  252. }
  253. }
  254. function _addCarry(uint256 x, uint256 y) private pure returns (uint256 res, uint256 carry) {
  255. unchecked {
  256. res = x + y;
  257. }
  258. carry = res < x ? 1 : 0;
  259. }
  260. }