Math.t.sol 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 {
  7. assertEq(Math.ternary(f, a, b), f ? a : b);
  8. }
  9. // MIN & MAX
  10. function testSymbolicMinMax(uint256 a, uint256 b) public {
  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 {
  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 {
  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 {
  56. _testInvMod(value, p, true);
  57. }
  58. function testInvMod2(uint256 seed) public {
  59. uint256 p = 2; // prime
  60. _testInvMod(bound(seed, 1, p - 1), p, false);
  61. }
  62. function testInvMod17(uint256 seed) public {
  63. uint256 p = 17; // prime
  64. _testInvMod(bound(seed, 1, p - 1), p, false);
  65. }
  66. function testInvMod65537(uint256 seed) public {
  67. uint256 p = 65537; // prime
  68. _testInvMod(bound(seed, 1, p - 1), p, false);
  69. }
  70. function testInvModP256(uint256 seed) public {
  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 {
  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 {
  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 {
  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 {
  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 {
  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. function testMulDivDomain(uint256 x, uint256 y, uint256 d) public {
  168. (uint256 xyHi, ) = _mulHighLow(x, y);
  169. // Violate {testMulDiv} assumption (covers d is 0 and result overflow)
  170. vm.assume(xyHi >= d);
  171. // we are outside the scope of {testMulDiv}, we expect muldiv to revert
  172. vm.expectRevert(d == 0 ? stdError.divisionError : stdError.arithmeticError);
  173. Math.mulDiv(x, y, d);
  174. }
  175. // MOD EXP
  176. function testModExp(uint256 b, uint256 e, uint256 m) public {
  177. if (m == 0) {
  178. vm.expectRevert(stdError.divisionError);
  179. }
  180. uint256 result = Math.modExp(b, e, m);
  181. assertLt(result, m);
  182. assertEq(result, _nativeModExp(b, e, m));
  183. }
  184. function testTryModExp(uint256 b, uint256 e, uint256 m) public {
  185. (bool success, uint256 result) = Math.tryModExp(b, e, m);
  186. assertEq(success, m != 0);
  187. if (success) {
  188. assertLt(result, m);
  189. assertEq(result, _nativeModExp(b, e, m));
  190. } else {
  191. assertEq(result, 0);
  192. }
  193. }
  194. function testModExpMemory(uint256 b, uint256 e, uint256 m) public {
  195. if (m == 0) {
  196. vm.expectRevert(stdError.divisionError);
  197. }
  198. bytes memory result = Math.modExp(abi.encodePacked(b), abi.encodePacked(e), abi.encodePacked(m));
  199. assertEq(result.length, 0x20);
  200. uint256 res = abi.decode(result, (uint256));
  201. assertLt(res, m);
  202. assertEq(res, _nativeModExp(b, e, m));
  203. }
  204. function testTryModExpMemory(uint256 b, uint256 e, uint256 m) public {
  205. (bool success, bytes memory result) = Math.tryModExp(
  206. abi.encodePacked(b),
  207. abi.encodePacked(e),
  208. abi.encodePacked(m)
  209. );
  210. if (success) {
  211. assertEq(result.length, 0x20); // m is a uint256, so abi.encodePacked(m).length is 0x20
  212. uint256 res = abi.decode(result, (uint256));
  213. assertLt(res, m);
  214. assertEq(res, _nativeModExp(b, e, m));
  215. } else {
  216. assertEq(result.length, 0);
  217. }
  218. }
  219. function _nativeModExp(uint256 b, uint256 e, uint256 m) private pure returns (uint256) {
  220. if (m == 1) return 0;
  221. uint256 r = 1;
  222. while (e > 0) {
  223. if (e % 2 > 0) {
  224. r = mulmod(r, b, m);
  225. }
  226. b = mulmod(b, b, m);
  227. e >>= 1;
  228. }
  229. return r;
  230. }
  231. // Helpers
  232. function _asRounding(uint8 r) private pure returns (Math.Rounding) {
  233. vm.assume(r < uint8(type(Math.Rounding).max));
  234. return Math.Rounding(r);
  235. }
  236. function _mulHighLow(uint256 x, uint256 y) private pure returns (uint256 high, uint256 low) {
  237. (uint256 x0, uint256 x1) = (x & type(uint128).max, x >> 128);
  238. (uint256 y0, uint256 y1) = (y & type(uint128).max, y >> 128);
  239. // Karatsuba algorithm
  240. // https://en.wikipedia.org/wiki/Karatsuba_algorithm
  241. uint256 z2 = x1 * y1;
  242. uint256 z1a = x1 * y0;
  243. uint256 z1b = x0 * y1;
  244. uint256 z0 = x0 * y0;
  245. uint256 carry = ((z1a & type(uint128).max) + (z1b & type(uint128).max) + (z0 >> 128)) >> 128;
  246. high = z2 + (z1a >> 128) + (z1b >> 128) + carry;
  247. unchecked {
  248. low = x * y;
  249. }
  250. }
  251. function _addCarry(uint256 x, uint256 y) private pure returns (uint256 res, uint256 carry) {
  252. unchecked {
  253. res = x + y;
  254. }
  255. carry = res < x ? 1 : 0;
  256. }
  257. }