Repository navigation
secp256k1: Faster scalar mult via width-5 wNAF. - #3740
Merged
Merged
Conversation
davecgh
commented
Jul 20, 2026
davecgh
force-pushed
the
secp256k1_wnaf
branch
8 times, most recently
from
July 27, 2026 07:01
a72d768 to
561688b
Compare
jholdstock
approved these changes
Jul 28, 2026
With the ultimate goal of optimizing scalar multiplication by reducing the average number of point additions, this commit adds code to convert a balanced scalar representative into width-5 windowed non-adjacent form (wNAF). The current scalar multiplication implementation uses ordinary non-adjacent form (NAF), which corresponds to a window width of 2. This commit introduces only the width-5 wNAF recoder and does not yet integrate it into scalar multiplication. Future commits will add comprehensive tests and integrate the new implementation.
This adds comprehensive tests for the new wNAF implementation to help verify correctness. The tests assert the resulting encoding satisfies the required mathematical properties and reconstructs the original value for both deterministic edge cases and randomized balanced scalar representatives. The deterministic edge cases exercise enough small values to exhaustively test every residue modulo 2^w multiple times, and include additional edge cases such as carries introduced by the recoding, maximum values, and alternating bit patterns.
This updates the non-constant-time scalar multiplication implementation to use the new width-5 wNAF recoding. In particular, because wNAF requires precomputed odd multiples that were not needed by the previous implementation, this introduces a new dedicated precomputed table type along with a new helper to construct the required multiples. The previous implementation only needed to swap between two precomputed points depending on the scalar signs. However, since the new wNAF implementation uses larger precomputed tables, it instead tracks whether each decomposed scalar was negated and applies the corresponding sign adjustment to the appropriate half of each table. The following benchmark shows a before and after comparison of scalar multiplication: name old time/op new time/op delta --------------------------------------------------------------------------- ScalarMultNonConst 94.4µs ± 1% 84.3µs ± 1% -10.75% (p=0.000 n=10+10)
This removes the old ordinary NAF implementation that is no longer used now that the implementation has been updated to use a new width-5 wNAF recoder instead. It also removes the associated tests and benchmark accordingly.
davecgh
force-pushed
the
secp256k1_wnaf
branch
from
November 20, 2026 07:06
155ec1b to
0a63318
Compare
dcr-approvals-proxy
approved these changes
Nov 20, 2026
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.
This requires #3731.
This improves the performance of the non-constant-time scalar multiplication implementation via width-5 windowed non-adjacent form (wNAF) recoding.
It introduces an optimized width-5 recoder together with precomputed odd-multiple tables for both P and φ(P) which allows the existing endomorphism-based simultaneous multiplication to replace the previous width-2 NAF representation with the sparser width-5 wNAF representation.
On average, width-5 wNAF reduces the density of non-zero digits from approximately one in three to one in six which significantly reduces the number of point additions required during scalar multiplication at the cost of a small amount of additional precomputation.
Comprehensive tests are included to verify the mathematical properties of the resulting encodings, reconstruct the original values, and cover both deterministic edge cases and randomized balanced scalars. A benchmark for the recoder is also added.
Finally, it removes the old ordinary width-2 NAF code and associated tests and benchmark since it is no longer used.
It is implemented via a series of commits to ease the review process. See the individual commit descriptions for more details.
The following benchmark shows a before and after comparison: