Skip to content

Commit bde5ec3

Browse files
authored
Merge pull request #267 from PaulRBerg/docs/latex-cases-in-common
docs: latex cases for msb and sqrt
2 parents 36d4370 + 34e90b5 commit bde5ec3

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/Common.sol

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,16 @@ function exp2(uint256 x) pure returns (uint256 result) {
298298
///
299299
/// @dev See the note on "msb" in this Wikipedia article: https://en.wikipedia.org/wiki/Find_first_set
300300
///
301-
/// Each step in this implementation is equivalent to this high-level code:
301+
/// The implementation satisfies these properties:
302+
///
303+
/// $$
304+
/// \begin{cases}
305+
/// x = 0 \implies \text{msb}(x) = 0 \\
306+
/// x > 0 \implies x \gg \text{msb}(x) = 1
307+
/// \end{cases}
308+
/// $$
309+
///
310+
/// Each step below is equivalent to this high-level code:
302311
///
303312
/// ```solidity
304313
/// if (x >= 2 ** 128) {
@@ -307,16 +316,16 @@ function exp2(uint256 x) pure returns (uint256 result) {
307316
/// }
308317
/// ```
309318
///
310-
/// Where 128 is replaced with each respective power of two factor. See the full high-level implementation here:
311-
/// https://gist.github.com/PaulRBerg/f932f8693f2733e30c4d479e8e980948
312-
///
313-
/// The Yul instructions used below are:
319+
/// Where 128 is replaced with each respective power of two factor. The Yul instructions used below are:
314320
///
315321
/// - "gt" is "greater than"
316322
/// - "or" is the OR bitwise operator
317323
/// - "shl" is "shift left"
318324
/// - "shr" is "shift right"
319325
///
326+
/// See the full high-level implementation here:
327+
/// https://gist.github.com/PaulRBerg/f932f8693f2733e30c4d479e8e980948
328+
///
320329
/// @param x The uint256 number for which to find the index of the most significant bit.
321330
/// @return result The index of the most significant bit as a uint256.
322331
/// @custom:smtchecker abstract-function-nondet
@@ -482,8 +491,8 @@ function mulDiv(uint256 x, uint256 y, uint256 denominator) pure returns (uint256
482491
///
483492
/// $$
484493
/// \begin{cases}
485-
/// x * y = MAX\_UINT256 * UNIT \\
486-
/// (x * y) \% UNIT \geq \frac{UNIT}{2}
494+
/// x * y = {MAX\_UINT256} * {UNIT} \\
495+
/// (x * y) \% {UNIT} \geq \frac{UNIT}{2}
487496
/// \end{cases}
488497
/// $$
489498
///
@@ -588,6 +597,15 @@ function mulDivSigned(int256 x, int256 y, int256 denominator) pure returns (int2
588597
///
589598
/// @dev See https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
590599
///
600+
/// The implementation satisfies these properties:
601+
///
602+
/// $$
603+
/// \begin{cases}
604+
/// \lfloor\sqrt{x}\rfloor \leq \sqrt{x} < \lfloor\sqrt{x}\rfloor + 1 \\[0.5em]
605+
/// \lfloor\sqrt{x}\rfloor^2 \leq x < (\lfloor\sqrt{x}\rfloor + 1)^2
606+
/// \end{cases}
607+
/// $$
608+
///
591609
/// Notes:
592610
/// - If x is not a perfect square, the result is rounded down.
593611
/// - Credits to OpenZeppelin for the explanations in comments below.

0 commit comments

Comments
 (0)