Math.t.sol 12 KB

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