+20-100% performance with special BigInt reduction#117
+20-100% performance with special BigInt reduction#117georg95 wants to merge 7 commits intopaulmillr:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a significant performance optimization for the ed25519 implementation by adding a specialized modular reduction function M_fast that leverages the special form of the ed25519 prime P = 2^255 - 19. The optimization replaces generic modular reduction (M(a * b)) with a Barrett-style reduction optimized for this specific prime when the inputs are known to be products of two field elements.
Changes:
- Added
M_fastfunction implementing specialized Barrett reduction for P = 2^255 - 19 - Replaced
M()withM_fast()for multiplication results in hot-path operations (point equality, doubling, addition, and square root computations) - Added intermediate
M()wrapping of sums before squaring to ensure M_fast's input constraints are met
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| index.ts | Introduces M_fast function and optimizes point arithmetic operations (equals, double, add), toAffine conversion, and modular exponentiation helpers (pow2, pow_2_252_3, uvRatio) |
| index.js | Mirrors the TypeScript changes with identical optimizations to maintain consistency between compiled and source versions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
It's special case for reducing numbers of order Algorithm can be derived:
In code: I just checked, and replacing |
|
Is the technique used elsewhere, or described in some kind of paper? Or did you came up with it on your own? |
Gemini pro suggested me it, when tried to speed up points decompression for monero blockchain. PS: test failing because assertion failed at |
|
AI is great, but we will have to investigate whether the algo is legit; or may have some weird edge cases which won't get catched by tests. From my research, a similar algorithm is described in Handbook of Applied Cryptography, 14.47-14.50. Need to dive into that and think a bit.
|
|
Here are 2 statements about this reduction:
r === (r >> 255n) * (2n**255n) + (r & P_MASK)
((r >> 255n) * (2n**255n) + (r & P_MASK)) %P === ((r >> 255n) * 19n + (r & P_MASK)) % PThey should be both be true to not produce invalid results. For negative numbers first stament is false, for example. |
Turned out, special reduction even with BigInt is twice as fast than just
% PBenchmark difference, tested on Apple M4, node.js 24.11.1: