Math.sol 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (utils/math/Math.sol)
  3. pragma solidity ^0.8.20;
  4. import {Panic} from "../Panic.sol";
  5. import {SafeCast} from "./SafeCast.sol";
  6. /**
  7. * @dev Standard math utilities missing in the Solidity language.
  8. */
  9. library Math {
  10. enum Rounding {
  11. Floor, // Toward negative infinity
  12. Ceil, // Toward positive infinity
  13. Trunc, // Toward zero
  14. Expand // Away from zero
  15. }
  16. /**
  17. * @dev Return the 512-bit addition of two uint256.
  18. *
  19. * The result is stored in two 256 variables such that sum = high * 2²⁵⁶ + low.
  20. */
  21. function add512(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
  22. assembly ("memory-safe") {
  23. low := add(a, b)
  24. high := lt(low, a)
  25. }
  26. }
  27. /**
  28. * @dev Return the 512-bit multiplication of two uint256.
  29. *
  30. * The result is stored in two 256 variables such that product = high * 2²⁵⁶ + low.
  31. */
  32. function mul512(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
  33. // 512-bit multiply [high low] = x * y. Compute the product mod 2²⁵⁶ and mod 2²⁵⁶ - 1, then use
  34. // the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
  35. // variables such that product = high * 2²⁵⁶ + low.
  36. assembly ("memory-safe") {
  37. let mm := mulmod(a, b, not(0))
  38. low := mul(a, b)
  39. high := sub(sub(mm, low), lt(mm, low))
  40. }
  41. }
  42. /**
  43. * @dev Returns the addition of two unsigned integers, with an success flag (no overflow).
  44. */
  45. function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
  46. unchecked {
  47. uint256 c = a + b;
  48. if (c < a) return (false, 0);
  49. return (true, c);
  50. }
  51. }
  52. /**
  53. * @dev Returns the subtraction of two unsigned integers, with an success flag (no overflow).
  54. */
  55. function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
  56. unchecked {
  57. if (b > a) return (false, 0);
  58. return (true, a - b);
  59. }
  60. }
  61. /**
  62. * @dev Returns the multiplication of two unsigned integers, with an success flag (no overflow).
  63. */
  64. function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
  65. unchecked {
  66. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  67. // benefit is lost if 'b' is also tested.
  68. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
  69. if (a == 0) return (true, 0);
  70. uint256 c = a * b;
  71. if (c / a != b) return (false, 0);
  72. return (true, c);
  73. }
  74. }
  75. /**
  76. * @dev Returns the division of two unsigned integers, with a success flag (no division by zero).
  77. */
  78. function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
  79. unchecked {
  80. if (b == 0) return (false, 0);
  81. return (true, a / b);
  82. }
  83. }
  84. /**
  85. * @dev Returns the remainder of dividing two unsigned integers, with a success flag (no division by zero).
  86. */
  87. function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
  88. unchecked {
  89. if (b == 0) return (false, 0);
  90. return (true, a % b);
  91. }
  92. }
  93. /**
  94. * @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
  95. *
  96. * IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
  97. * However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute
  98. * one branch when needed, making this function more expensive.
  99. */
  100. function ternary(bool condition, uint256 a, uint256 b) internal pure returns (uint256) {
  101. unchecked {
  102. // branchless ternary works because:
  103. // b ^ (a ^ b) == a
  104. // b ^ 0 == b
  105. return b ^ ((a ^ b) * SafeCast.toUint(condition));
  106. }
  107. }
  108. /**
  109. * @dev Returns the largest of two numbers.
  110. */
  111. function max(uint256 a, uint256 b) internal pure returns (uint256) {
  112. return ternary(a > b, a, b);
  113. }
  114. /**
  115. * @dev Returns the smallest of two numbers.
  116. */
  117. function min(uint256 a, uint256 b) internal pure returns (uint256) {
  118. return ternary(a < b, a, b);
  119. }
  120. /**
  121. * @dev Returns the average of two numbers. The result is rounded towards
  122. * zero.
  123. */
  124. function average(uint256 a, uint256 b) internal pure returns (uint256) {
  125. // (a + b) / 2 can overflow.
  126. return (a & b) + (a ^ b) / 2;
  127. }
  128. /**
  129. * @dev Returns the ceiling of the division of two numbers.
  130. *
  131. * This differs from standard division with `/` in that it rounds towards infinity instead
  132. * of rounding towards zero.
  133. */
  134. function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
  135. if (b == 0) {
  136. // Guarantee the same behavior as in a regular Solidity division.
  137. Panic.panic(Panic.DIVISION_BY_ZERO);
  138. }
  139. // The following calculation ensures accurate ceiling division without overflow.
  140. // Since a is non-zero, (a - 1) / b will not overflow.
  141. // The largest possible result occurs when (a - 1) / b is type(uint256).max,
  142. // but the largest value we can obtain is type(uint256).max - 1, which happens
  143. // when a = type(uint256).max and b = 1.
  144. unchecked {
  145. return SafeCast.toUint(a > 0) * ((a - 1) / b + 1);
  146. }
  147. }
  148. /**
  149. * @dev Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or
  150. * denominator == 0.
  151. *
  152. * Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by
  153. * Uniswap Labs also under MIT license.
  154. */
  155. function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
  156. unchecked {
  157. (uint256 high, uint256 low) = mul512(x, y);
  158. // Handle non-overflow cases, 256 by 256 division.
  159. if (high == 0) {
  160. // Solidity will revert if denominator == 0, unlike the div opcode on its own.
  161. // The surrounding unchecked block does not change this fact.
  162. // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
  163. return low / denominator;
  164. }
  165. // Make sure the result is less than 2²⁵⁶. Also prevents denominator == 0.
  166. if (denominator <= high) {
  167. Panic.panic(ternary(denominator == 0, Panic.DIVISION_BY_ZERO, Panic.UNDER_OVERFLOW));
  168. }
  169. ///////////////////////////////////////////////
  170. // 512 by 256 division.
  171. ///////////////////////////////////////////////
  172. // Make division exact by subtracting the remainder from [high low].
  173. uint256 remainder;
  174. assembly {
  175. // Compute remainder using mulmod.
  176. remainder := mulmod(x, y, denominator)
  177. // Subtract 256 bit number from 512 bit number.
  178. high := sub(high, gt(remainder, low))
  179. low := sub(low, remainder)
  180. }
  181. // Factor powers of two out of denominator and compute largest power of two divisor of denominator.
  182. // Always >= 1. See https://cs.stackexchange.com/q/138556/92363.
  183. uint256 twos = denominator & (0 - denominator);
  184. assembly {
  185. // Divide denominator by twos.
  186. denominator := div(denominator, twos)
  187. // Divide [high low] by twos.
  188. low := div(low, twos)
  189. // Flip twos such that it is 2²⁵⁶ / twos. If twos is zero, then it becomes one.
  190. twos := add(div(sub(0, twos), twos), 1)
  191. }
  192. // Shift in bits from high into low.
  193. low |= high * twos;
  194. // Invert denominator mod 2²⁵⁶. Now that denominator is an odd number, it has an inverse modulo 2²⁵⁶ such
  195. // that denominator * inv ≡ 1 mod 2²⁵⁶. Compute the inverse by starting with a seed that is correct for
  196. // four bits. That is, denominator * inv ≡ 1 mod 2⁴.
  197. uint256 inverse = (3 * denominator) ^ 2;
  198. // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also
  199. // works in modular arithmetic, doubling the correct bits in each step.
  200. inverse *= 2 - denominator * inverse; // inverse mod 2⁸
  201. inverse *= 2 - denominator * inverse; // inverse mod 2¹⁶
  202. inverse *= 2 - denominator * inverse; // inverse mod 2³²
  203. inverse *= 2 - denominator * inverse; // inverse mod 2⁶⁴
  204. inverse *= 2 - denominator * inverse; // inverse mod 2¹²⁸
  205. inverse *= 2 - denominator * inverse; // inverse mod 2²⁵⁶
  206. // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
  207. // This will give us the correct result modulo 2²⁵⁶. Since the preconditions guarantee that the outcome is
  208. // less than 2²⁵⁶, this is the final result. We don't need to compute the high bits of the result and high
  209. // is no longer required.
  210. result = low * inverse;
  211. return result;
  212. }
  213. }
  214. /**
  215. * @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
  216. */
  217. function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
  218. return mulDiv(x, y, denominator) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
  219. }
  220. /**
  221. * @dev Calculates floor(x * y >> n) with full precision. Throws if result overflows a uint256.
  222. */
  223. function mulShr(uint256 x, uint256 y, uint8 n) internal pure returns (uint256 result) {
  224. unchecked {
  225. (uint256 high, uint256 low) = mul512(x, y);
  226. if (high >= 1 << n) {
  227. Panic.panic(Panic.UNDER_OVERFLOW);
  228. }
  229. return (high << (256 - n)) | (low >> n);
  230. }
  231. }
  232. /**
  233. * @dev Calculates x * y >> n with full precision, following the selected rounding direction.
  234. */
  235. function mulShr(uint256 x, uint256 y, uint8 n, Rounding rounding) internal pure returns (uint256) {
  236. return mulShr(x, y, n) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, 1 << n) > 0);
  237. }
  238. /**
  239. * @dev Calculate the modular multiplicative inverse of a number in Z/nZ.
  240. *
  241. * If n is a prime, then Z/nZ is a field. In that case all elements are inversible, except 0.
  242. * If n is not a prime, then Z/nZ is not a field, and some elements might not be inversible.
  243. *
  244. * If the input value is not inversible, 0 is returned.
  245. *
  246. * NOTE: If you know for sure that n is (big) a prime, it may be cheaper to use Fermat's little theorem and get the
  247. * inverse using `Math.modExp(a, n - 2, n)`. See {invModPrime}.
  248. */
  249. function invMod(uint256 a, uint256 n) internal pure returns (uint256) {
  250. unchecked {
  251. if (n == 0) return 0;
  252. // The inverse modulo is calculated using the Extended Euclidean Algorithm (iterative version)
  253. // Used to compute integers x and y such that: ax + ny = gcd(a, n).
  254. // When the gcd is 1, then the inverse of a modulo n exists and it's x.
  255. // ax + ny = 1
  256. // ax = 1 + (-y)n
  257. // ax ≡ 1 (mod n) # x is the inverse of a modulo n
  258. // If the remainder is 0 the gcd is n right away.
  259. uint256 remainder = a % n;
  260. uint256 gcd = n;
  261. // Therefore the initial coefficients are:
  262. // ax + ny = gcd(a, n) = n
  263. // 0a + 1n = n
  264. int256 x = 0;
  265. int256 y = 1;
  266. while (remainder != 0) {
  267. uint256 quotient = gcd / remainder;
  268. (gcd, remainder) = (
  269. // The old remainder is the next gcd to try.
  270. remainder,
  271. // Compute the next remainder.
  272. // Can't overflow given that (a % gcd) * (gcd // (a % gcd)) <= gcd
  273. // where gcd is at most n (capped to type(uint256).max)
  274. gcd - remainder * quotient
  275. );
  276. (x, y) = (
  277. // Increment the coefficient of a.
  278. y,
  279. // Decrement the coefficient of n.
  280. // Can overflow, but the result is casted to uint256 so that the
  281. // next value of y is "wrapped around" to a value between 0 and n - 1.
  282. x - y * int256(quotient)
  283. );
  284. }
  285. if (gcd != 1) return 0; // No inverse exists.
  286. return ternary(x < 0, n - uint256(-x), uint256(x)); // Wrap the result if it's negative.
  287. }
  288. }
  289. /**
  290. * @dev Variant of {invMod}. More efficient, but only works if `p` is known to be a prime greater than `2`.
  291. *
  292. * From https://en.wikipedia.org/wiki/Fermat%27s_little_theorem[Fermat's little theorem], we know that if p is
  293. * prime, then `a**(p-1) ≡ 1 mod p`. As a consequence, we have `a * a**(p-2) ≡ 1 mod p`, which means that
  294. * `a**(p-2)` is the modular multiplicative inverse of a in Fp.
  295. *
  296. * NOTE: this function does NOT check that `p` is a prime greater than `2`.
  297. */
  298. function invModPrime(uint256 a, uint256 p) internal view returns (uint256) {
  299. unchecked {
  300. return Math.modExp(a, p - 2, p);
  301. }
  302. }
  303. /**
  304. * @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m)
  305. *
  306. * Requirements:
  307. * - modulus can't be zero
  308. * - underlying staticcall to precompile must succeed
  309. *
  310. * IMPORTANT: The result is only valid if the underlying call succeeds. When using this function, make
  311. * sure the chain you're using it on supports the precompiled contract for modular exponentiation
  312. * at address 0x05 as specified in https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise,
  313. * the underlying function will succeed given the lack of a revert, but the result may be incorrectly
  314. * interpreted as 0.
  315. */
  316. function modExp(uint256 b, uint256 e, uint256 m) internal view returns (uint256) {
  317. (bool success, uint256 result) = tryModExp(b, e, m);
  318. if (!success) {
  319. Panic.panic(Panic.DIVISION_BY_ZERO);
  320. }
  321. return result;
  322. }
  323. /**
  324. * @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m).
  325. * It includes a success flag indicating if the operation succeeded. Operation will be marked as failed if trying
  326. * to operate modulo 0 or if the underlying precompile reverted.
  327. *
  328. * IMPORTANT: The result is only valid if the success flag is true. When using this function, make sure the chain
  329. * you're using it on supports the precompiled contract for modular exponentiation at address 0x05 as specified in
  330. * https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise, the underlying function will succeed given the lack
  331. * of a revert, but the result may be incorrectly interpreted as 0.
  332. */
  333. function tryModExp(uint256 b, uint256 e, uint256 m) internal view returns (bool success, uint256 result) {
  334. if (m == 0) return (false, 0);
  335. assembly ("memory-safe") {
  336. let ptr := mload(0x40)
  337. // | Offset | Content | Content (Hex) |
  338. // |-----------|------------|--------------------------------------------------------------------|
  339. // | 0x00:0x1f | size of b | 0x0000000000000000000000000000000000000000000000000000000000000020 |
  340. // | 0x20:0x3f | size of e | 0x0000000000000000000000000000000000000000000000000000000000000020 |
  341. // | 0x40:0x5f | size of m | 0x0000000000000000000000000000000000000000000000000000000000000020 |
  342. // | 0x60:0x7f | value of b | 0x<.............................................................b> |
  343. // | 0x80:0x9f | value of e | 0x<.............................................................e> |
  344. // | 0xa0:0xbf | value of m | 0x<.............................................................m> |
  345. mstore(ptr, 0x20)
  346. mstore(add(ptr, 0x20), 0x20)
  347. mstore(add(ptr, 0x40), 0x20)
  348. mstore(add(ptr, 0x60), b)
  349. mstore(add(ptr, 0x80), e)
  350. mstore(add(ptr, 0xa0), m)
  351. // Given the result < m, it's guaranteed to fit in 32 bytes,
  352. // so we can use the memory scratch space located at offset 0.
  353. success := staticcall(gas(), 0x05, ptr, 0xc0, 0x00, 0x20)
  354. result := mload(0x00)
  355. }
  356. }
  357. /**
  358. * @dev Variant of {modExp} that supports inputs of arbitrary length.
  359. */
  360. function modExp(bytes memory b, bytes memory e, bytes memory m) internal view returns (bytes memory) {
  361. (bool success, bytes memory result) = tryModExp(b, e, m);
  362. if (!success) {
  363. Panic.panic(Panic.DIVISION_BY_ZERO);
  364. }
  365. return result;
  366. }
  367. /**
  368. * @dev Variant of {tryModExp} that supports inputs of arbitrary length.
  369. */
  370. function tryModExp(
  371. bytes memory b,
  372. bytes memory e,
  373. bytes memory m
  374. ) internal view returns (bool success, bytes memory result) {
  375. if (_zeroBytes(m)) return (false, new bytes(0));
  376. uint256 mLen = m.length;
  377. // Encode call args in result and move the free memory pointer
  378. result = abi.encodePacked(b.length, e.length, mLen, b, e, m);
  379. assembly ("memory-safe") {
  380. let dataPtr := add(result, 0x20)
  381. // Write result on top of args to avoid allocating extra memory.
  382. success := staticcall(gas(), 0x05, dataPtr, mload(result), dataPtr, mLen)
  383. // Overwrite the length.
  384. // result.length > returndatasize() is guaranteed because returndatasize() == m.length
  385. mstore(result, mLen)
  386. // Set the memory pointer after the returned data.
  387. mstore(0x40, add(dataPtr, mLen))
  388. }
  389. }
  390. /**
  391. * @dev Returns whether the provided byte array is zero.
  392. */
  393. function _zeroBytes(bytes memory byteArray) private pure returns (bool) {
  394. for (uint256 i = 0; i < byteArray.length; ++i) {
  395. if (byteArray[i] != 0) {
  396. return false;
  397. }
  398. }
  399. return true;
  400. }
  401. /**
  402. * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
  403. * towards zero.
  404. *
  405. * This method is based on Newton's method for computing square roots; the algorithm is restricted to only
  406. * using integer operations.
  407. */
  408. function sqrt(uint256 a) internal pure returns (uint256) {
  409. unchecked {
  410. // Take care of easy edge cases when a == 0 or a == 1
  411. if (a <= 1) {
  412. return a;
  413. }
  414. // In this function, we use Newton's method to get a root of `f(x) := x² - a`. It involves building a
  415. // sequence x_n that converges toward sqrt(a). For each iteration x_n, we also define the error between
  416. // the current value as `ε_n = | x_n - sqrt(a) |`.
  417. //
  418. // For our first estimation, we consider `e` the smallest power of 2 which is bigger than the square root
  419. // of the target. (i.e. `2**(e-1) ≤ sqrt(a) < 2**e`). We know that `e ≤ 128` because `(2¹²⁸)² = 2²⁵⁶` is
  420. // bigger than any uint256.
  421. //
  422. // By noticing that
  423. // `2**(e-1) ≤ sqrt(a) < 2**e → (2**(e-1))² ≤ a < (2**e)² → 2**(2*e-2) ≤ a < 2**(2*e)`
  424. // we can deduce that `e - 1` is `log2(a) / 2`. We can thus compute `x_n = 2**(e-1)` using a method similar
  425. // to the msb function.
  426. uint256 aa = a;
  427. uint256 xn = 1;
  428. if (aa >= (1 << 128)) {
  429. aa >>= 128;
  430. xn <<= 64;
  431. }
  432. if (aa >= (1 << 64)) {
  433. aa >>= 64;
  434. xn <<= 32;
  435. }
  436. if (aa >= (1 << 32)) {
  437. aa >>= 32;
  438. xn <<= 16;
  439. }
  440. if (aa >= (1 << 16)) {
  441. aa >>= 16;
  442. xn <<= 8;
  443. }
  444. if (aa >= (1 << 8)) {
  445. aa >>= 8;
  446. xn <<= 4;
  447. }
  448. if (aa >= (1 << 4)) {
  449. aa >>= 4;
  450. xn <<= 2;
  451. }
  452. if (aa >= (1 << 2)) {
  453. xn <<= 1;
  454. }
  455. // We now have x_n such that `x_n = 2**(e-1) ≤ sqrt(a) < 2**e = 2 * x_n`. This implies ε_n ≤ 2**(e-1).
  456. //
  457. // We can refine our estimation by noticing that the middle of that interval minimizes the error.
  458. // If we move x_n to equal 2**(e-1) + 2**(e-2), then we reduce the error to ε_n ≤ 2**(e-2).
  459. // This is going to be our x_0 (and ε_0)
  460. xn = (3 * xn) >> 1; // ε_0 := | x_0 - sqrt(a) | ≤ 2**(e-2)
  461. // From here, Newton's method give us:
  462. // x_{n+1} = (x_n + a / x_n) / 2
  463. //
  464. // One should note that:
  465. // x_{n+1}² - a = ((x_n + a / x_n) / 2)² - a
  466. // = ((x_n² + a) / (2 * x_n))² - a
  467. // = (x_n⁴ + 2 * a * x_n² + a²) / (4 * x_n²) - a
  468. // = (x_n⁴ + 2 * a * x_n² + a² - 4 * a * x_n²) / (4 * x_n²)
  469. // = (x_n⁴ - 2 * a * x_n² + a²) / (4 * x_n²)
  470. // = (x_n² - a)² / (2 * x_n)²
  471. // = ((x_n² - a) / (2 * x_n))²
  472. // ≥ 0
  473. // Which proves that for all n ≥ 1, sqrt(a) ≤ x_n
  474. //
  475. // This gives us the proof of quadratic convergence of the sequence:
  476. // ε_{n+1} = | x_{n+1} - sqrt(a) |
  477. // = | (x_n + a / x_n) / 2 - sqrt(a) |
  478. // = | (x_n² + a - 2*x_n*sqrt(a)) / (2 * x_n) |
  479. // = | (x_n - sqrt(a))² / (2 * x_n) |
  480. // = | ε_n² / (2 * x_n) |
  481. // = ε_n² / | (2 * x_n) |
  482. //
  483. // For the first iteration, we have a special case where x_0 is known:
  484. // ε_1 = ε_0² / | (2 * x_0) |
  485. // ≤ (2**(e-2))² / (2 * (2**(e-1) + 2**(e-2)))
  486. // ≤ 2**(2*e-4) / (3 * 2**(e-1))
  487. // ≤ 2**(e-3) / 3
  488. // ≤ 2**(e-3-log2(3))
  489. // ≤ 2**(e-4.5)
  490. //
  491. // For the following iterations, we use the fact that, 2**(e-1) ≤ sqrt(a) ≤ x_n:
  492. // ε_{n+1} = ε_n² / | (2 * x_n) |
  493. // ≤ (2**(e-k))² / (2 * 2**(e-1))
  494. // ≤ 2**(2*e-2*k) / 2**e
  495. // ≤ 2**(e-2*k)
  496. xn = (xn + a / xn) >> 1; // ε_1 := | x_1 - sqrt(a) | ≤ 2**(e-4.5) -- special case, see above
  497. xn = (xn + a / xn) >> 1; // ε_2 := | x_2 - sqrt(a) | ≤ 2**(e-9) -- general case with k = 4.5
  498. xn = (xn + a / xn) >> 1; // ε_3 := | x_3 - sqrt(a) | ≤ 2**(e-18) -- general case with k = 9
  499. xn = (xn + a / xn) >> 1; // ε_4 := | x_4 - sqrt(a) | ≤ 2**(e-36) -- general case with k = 18
  500. xn = (xn + a / xn) >> 1; // ε_5 := | x_5 - sqrt(a) | ≤ 2**(e-72) -- general case with k = 36
  501. xn = (xn + a / xn) >> 1; // ε_6 := | x_6 - sqrt(a) | ≤ 2**(e-144) -- general case with k = 72
  502. // Because e ≤ 128 (as discussed during the first estimation phase), we know have reached a precision
  503. // ε_6 ≤ 2**(e-144) < 1. Given we're operating on integers, then we can ensure that xn is now either
  504. // sqrt(a) or sqrt(a) + 1.
  505. return xn - SafeCast.toUint(xn > a / xn);
  506. }
  507. }
  508. /**
  509. * @dev Calculates sqrt(a), following the selected rounding direction.
  510. */
  511. function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
  512. unchecked {
  513. uint256 result = sqrt(a);
  514. return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a);
  515. }
  516. }
  517. /**
  518. * @dev Return the log in base 2 of a positive value rounded towards zero.
  519. * Returns 0 if given 0.
  520. */
  521. function log2(uint256 x) internal pure returns (uint256 r) {
  522. // If value has upper 128 bits set, log2 result is at least 128
  523. r = SafeCast.toUint(x > 0xffffffffffffffffffffffffffffffff) << 7;
  524. // If upper 64 bits of 128-bit half set, add 64 to result
  525. r |= SafeCast.toUint((x >> r) > 0xffffffffffffffff) << 6;
  526. // If upper 32 bits of 64-bit half set, add 32 to result
  527. r |= SafeCast.toUint((x >> r) > 0xffffffff) << 5;
  528. // If upper 16 bits of 32-bit half set, add 16 to result
  529. r |= SafeCast.toUint((x >> r) > 0xffff) << 4;
  530. // If upper 8 bits of 16-bit half set, add 8 to result
  531. r |= SafeCast.toUint((x >> r) > 0xff) << 3;
  532. // If upper 4 bits of 8-bit half set, add 4 to result
  533. r |= SafeCast.toUint((x >> r) > 0xf) << 2;
  534. // Shifts value right by the current result and use it as an index into this lookup table:
  535. //
  536. // | x (4 bits) | index | table[index] = MSB position |
  537. // |------------|---------|-----------------------------|
  538. // | 0000 | 0 | table[0] = 0 |
  539. // | 0001 | 1 | table[1] = 0 |
  540. // | 0010 | 2 | table[2] = 1 |
  541. // | 0011 | 3 | table[3] = 1 |
  542. // | 0100 | 4 | table[4] = 2 |
  543. // | 0101 | 5 | table[5] = 2 |
  544. // | 0110 | 6 | table[6] = 2 |
  545. // | 0111 | 7 | table[7] = 2 |
  546. // | 1000 | 8 | table[8] = 3 |
  547. // | 1001 | 9 | table[9] = 3 |
  548. // | 1010 | 10 | table[10] = 3 |
  549. // | 1011 | 11 | table[11] = 3 |
  550. // | 1100 | 12 | table[12] = 3 |
  551. // | 1101 | 13 | table[13] = 3 |
  552. // | 1110 | 14 | table[14] = 3 |
  553. // | 1111 | 15 | table[15] = 3 |
  554. //
  555. // The lookup table is represented as a 32-byte value with the MSB positions for 0-15 in the last 16 bytes.
  556. assembly ("memory-safe") {
  557. r := or(r, byte(shr(r, x), 0x0000010102020202030303030303030300000000000000000000000000000000))
  558. }
  559. }
  560. /**
  561. * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
  562. * Returns 0 if given 0.
  563. */
  564. function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
  565. unchecked {
  566. uint256 result = log2(value);
  567. return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value);
  568. }
  569. }
  570. /**
  571. * @dev Return the log in base 10 of a positive value rounded towards zero.
  572. * Returns 0 if given 0.
  573. */
  574. function log10(uint256 value) internal pure returns (uint256) {
  575. uint256 result = 0;
  576. unchecked {
  577. if (value >= 10 ** 64) {
  578. value /= 10 ** 64;
  579. result += 64;
  580. }
  581. if (value >= 10 ** 32) {
  582. value /= 10 ** 32;
  583. result += 32;
  584. }
  585. if (value >= 10 ** 16) {
  586. value /= 10 ** 16;
  587. result += 16;
  588. }
  589. if (value >= 10 ** 8) {
  590. value /= 10 ** 8;
  591. result += 8;
  592. }
  593. if (value >= 10 ** 4) {
  594. value /= 10 ** 4;
  595. result += 4;
  596. }
  597. if (value >= 10 ** 2) {
  598. value /= 10 ** 2;
  599. result += 2;
  600. }
  601. if (value >= 10 ** 1) {
  602. result += 1;
  603. }
  604. }
  605. return result;
  606. }
  607. /**
  608. * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
  609. * Returns 0 if given 0.
  610. */
  611. function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
  612. unchecked {
  613. uint256 result = log10(value);
  614. return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value);
  615. }
  616. }
  617. /**
  618. * @dev Return the log in base 256 of a positive value rounded towards zero.
  619. * Returns 0 if given 0.
  620. *
  621. * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
  622. */
  623. function log256(uint256 x) internal pure returns (uint256 r) {
  624. // If value has upper 128 bits set, log2 result is at least 128
  625. r = SafeCast.toUint(x > 0xffffffffffffffffffffffffffffffff) << 7;
  626. // If upper 64 bits of 128-bit half set, add 64 to result
  627. r |= SafeCast.toUint((x >> r) > 0xffffffffffffffff) << 6;
  628. // If upper 32 bits of 64-bit half set, add 32 to result
  629. r |= SafeCast.toUint((x >> r) > 0xffffffff) << 5;
  630. // If upper 16 bits of 32-bit half set, add 16 to result
  631. r |= SafeCast.toUint((x >> r) > 0xffff) << 4;
  632. // Add 1 if upper 8 bits of 16-bit half set, and divide accumulated result by 8
  633. return (r >> 3) | SafeCast.toUint((x >> r) > 0xff);
  634. }
  635. /**
  636. * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
  637. * Returns 0 if given 0.
  638. */
  639. function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
  640. unchecked {
  641. uint256 result = log256(value);
  642. return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
  643. }
  644. }
  645. /**
  646. * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
  647. */
  648. function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
  649. return uint8(rounding) % 2 == 1;
  650. }
  651. }