Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Montgomery's batch inversion optimization #559

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 28 additions & 1 deletion jolt-evm-verifier/src/subprotocols/Fr.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,34 @@ library FrLib {
return Fr.wrap(result);
}

// TODO: Montgomery's batch inversion trick
// Montgomery's batch inversion trick implementation
function batchInvert(Fr[] memory values) internal view returns (Fr[] memory) {
uint256 n = values.length;
if (n == 0) return new Fr[](0);
if (n == 1) return [invert(values[0])];

Fr[] memory products = new Fr[](n);
products[0] = values[0];

// Compute partial products
for (uint256 i = 1; i < n; i++) {
products[i] = products[i-1] * values[i];
}

// Invert the final product
Fr running = invert(products[n-1]);
Fr[] memory results = new Fr[](n);

// Unwind the products
for (uint256 i = n-1; i > 0; i--) {
results[i] = running * products[i-1];
running = running * values[i];
}
results[0] = running;

return results;
}

function div(Fr numerator, Fr denominator) internal view returns (Fr) {
return numerator * invert(denominator);
}
Expand Down