Skip to content

secp256k1: Faster scalar mult via width-5 wNAF. - #3740

Merged
davecgh merged 5 commits into
decred:masterfrom
davecgh:secp256k1_wnaf
Nov 20, 2026
Merged

davecgh merged 5 commits into
decred:masterfrom
davecgh:secp256k1_wnaf

Conversation

@davecgh

@davecgh davecgh commented Jul 19, 2026 •

Copy link
Copy Markdown
Member

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:

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)

@davecgh davecgh added this to the 2.2.0 milestone Jul 19, 2026
Comment thread dcrec/secp256k1/curve.go Resolved
Comment thread dcrec/secp256k1/curve.go Resolved
Comment thread dcrec/secp256k1/curve.go Resolved
@davecgh
davecgh force-pushed the secp256k1_wnaf branch 8 times, most recently from a72d768 to 561688b Compare July 27, 2026 07:01
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
davecgh merged commit 0a63318 into decred:master Nov 20, 2026
32 checks passed
@davecgh
davecgh deleted the secp256k1_wnaf branch November 20, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants