Math.t.sol 11 KB

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