Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/modexp/LimbMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ library LimbMath {
bool doRefinement;
if (uHi >= vTop) {
qHat = type(uint256).max;
rHat = uLo + vTop;
// Algorithm D uses the wrapped sum below to detect carry in doRefinement.
unchecked {
rHat = uLo + vTop;
}
doRefinement = (rHat >= uLo);
} else {
(qHat, rHat) = div512by256(uHi, uLo, vTop);
Expand Down
15 changes: 15 additions & 0 deletions test/modexp/ModexpDeployed.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ contract ModexpDeployedTest is Test {
assertEq(outDep, outPre);
}

function test_identical_schoolbook_saturated_estimate_wrap() public view {
uint256 halfWord = uint256(1) << 255;
bytes memory input = _encodeInput(
abi.encodePacked(bytes32(halfWord), bytes32(halfWord), bytes32(0)),
hex"01",
abi.encodePacked(bytes32(halfWord), bytes32(halfWord + 1))
);

(bool okPre, bytes memory outPre) = _callPrecompile(input);
(bool okDep, bytes memory outDep) = _callDeployed(input);
assertTrue(okPre);
assertTrue(okDep);
assertEq(outDep, outPre);
}

function test_identical_even_modulus() public view {
// 3^7 mod 10
bytes memory input = _encodeInput(
Expand Down
Loading