feat: Karatsuba Montgomery multiplication for WASM (7-10% speedup)#22481
Open
ElusAegis wants to merge 1 commit intoAztecProtocol:nextfrom
Open
feat: Karatsuba Montgomery multiplication for WASM (7-10% speedup)#22481ElusAegis wants to merge 1 commit intoAztecProtocol:nextfrom
ElusAegis wants to merge 1 commit intoAztecProtocol:nextfrom
Conversation
5 tasks
Author
|
Note on |
Collaborator
|
Thanks, tentatively exciting. Will get a review here once CI passes! |
…ation Splits the 9-limb (29-bit) multiply into a 5+4 Karatsuba decomposition, reducing scalar multiplications from 81 to 66 (~18%). Key changes: - Replace schoolbook wasm_madd loop with Karatsuba (P_lo + P_hi + P_cross) - Reduction, carry propagation, and conversion are unchanged Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
6b9575f to
89ee83c
Compare
Author
|
@ludamad should be ready for the CI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace the 9×9 schoolbook Montgomery multiplication in the WASM code path (
montgomery_mulinfield_impl_generic.hpp) with a Karatsuba decomposition. The 9 limbs (29-bit) are split into a 5+4 structure:This reduces the total scalar multiplications from 81 to 66 (−18.5%). The reduction chain (
wasm_reduce_yuval×8 +wasm_reduce×1), carry propagation, and limb conversion are all unchanged.The unsigned subtractions in the combine step are safe: each
P_cross[k]expands toP_lo[k] + P_hi[k] + cross_terms, soP_cross[k] ≥ P_lo[k] + P_hi[k]always holds.What this PR changes
One file:
barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_generic.hppOnly the
#else(WASM) branch ofmontgomery_mulis modified. The multiplication section is replaced with the Karatsuba decomposition; everything else (reduction, carry propagation, limb conversion, all comments) is preserved exactly.Benchmark results
Tested on Apple M1 Pro. Both variants compiled to WASM using the
wasm-threadspreset (Release,-O3) and executed viawasmtimewith--wasm relaxed-simd -W threads=y -S threads=y. Results are averaged over 10 runs.Benchmark 1 — Montgomery Multiplication
Microbenchmark of
FqandFrfield multiplication. Latency measures sequential dependent multiplications (a *= bin a chain). Throughput measures 64 independent multiplications per iteration.Benchmark 2 — Multi-Scalar Multiplication (Pippenger)
MSM using
PippengerUnsafeat various point counts (5 repetitions each).Benchmark 3 — Full Proving (Chonk, 5 repetitions)
Test plan
prime_field_tests,ecc_tests) pass on CI