Skip to content

Conversation

@marmitar
Copy link
Contributor

Tests for mathematical properties of some functions in Common.sol. The implementation relies on the properties below.

For msb():

$$ x = 0 \implies \text{msb}(x) = 0 $$ $$ x > 0 \implies x \gg \text{msb}(x) = 1 $$

For sqrt():

$$ \left\lfloor\sqrt{x}\right\rfloor \leq \sqrt{x} < \left\lfloor\sqrt{x}\right\rfloor + 1 $$ $$ \left\lfloor\sqrt{x}\right\rfloor^2 \leq x < \left(\left\lfloor\sqrt{x}\right\rfloor + 1\right)^2 $$

@marmitar
Copy link
Contributor Author

Hi, @PaulRBerg. Any chance I get a review on this one?

@PaulRBerg
Copy link
Owner

Hi @marmitar, I'm so sorry for dropping the ball here. You have my word that by Saturday, this PR and the other will be both reviewed. Thank you.

Copy link
Owner

@PaulRBerg PaulRBerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some feedback below and pushed directly to your branch

will merge now to speed up the review process; if you have any further feedback feel free to open another PR @marmitar :)

Thanks again for your contribution!

/// @dev Collection of tests for the most significant bit function available in `Common.sol`.
contract Common_Sqrt_Test is Base_Test {
function testFuzz_Msb_FitsUint8(uint256 x) external pure {
assertLe(msb(x), type(uint8).max, "Common msb");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to use more specific test descriptions here

import { Base_Test } from "../../Base.t.sol";

/// @dev Collection of tests for the most significant bit function available in `Common.sol`.
contract Common_Sqrt_Test is Base_Test {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect test contract name

// This case cannot be tested nicely using 'vm.assume()',
// because the fuzzer fails to find enough valid input.
// That's why we have to embed it here with an 'if'.
vm.assertEq(sqrt(x), MAX_SQRT, "Common sqrt");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I''ll move this to another text function and use bound:

For broad checks, such as ensuring a uint256 falls within a certain range, you can bound your input with the modulo operator or Forge Standard's bound method.

@PaulRBerg PaulRBerg merged commit 36d4370 into PaulRBerg:main Aug 30, 2025
3 checks passed
@marmitar marmitar deleted the test/common branch August 30, 2025 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants