Skip to content

Fix wnaf_table overallocation#2437

Merged
tarcieri merged 1 commit into
RustCrypto:masterfrom
42Pupusas:fix-wnaf-table-size
Jun 6, 2026
Merged

Fix wnaf_table overallocation#2437
tarcieri merged 1 commit into
RustCrypto:masterfrom
42Pupusas:fix-wnaf-table-size

Conversation

@42Pupusas

Copy link
Copy Markdown
Contributor

For window size w, wNAF digits have max magnitude 2^(w-1) - 1. The table is indexed by |digit| / 2, so the maximum index is (2^(w-1) - 1) / 2 = 2^(w-2) - 1, requiring 2^(w-2) entries.

The previous 2^(w-1) allocation computed twice as many odd multiples as needed, wasting point additions during table setup.

Ran the k256 Schnorr signing and verifying benches and gave around 20% improvements for hot paths, however
@tarcieri please note my benchmarks were ran against this branch that does GLV decomposition + shared doublings, where the larger table + extra allocations actually hurt.

While the fix is provably correct, the current vendored wNAF path only allocates a single table, so the effects a not really measurable.

Stage wNAF tables built per verify 2^(w-2) (fix) 2^(w-1) (no fix) Fix's contribution
Pre-wNAF base 0 (constant-time lincomb) 0% (can't apply)
GLV wNAF, separate ladders 2 (only P; G uses basepoint table) 53.2 µs 57.6 µs ~7%
GLV + shared doublings 4 (G via wNAF too, fused) 50.6 µs 59.8 µs ~19%

For window size `w`, wNAF digits are odd with magnitude at most
`2^(w-1) - 1`. The table is indexed by `|digit| / 2`, so the maximum
index is `(2^(w-1) - 1) / 2 = 2^(w-2) - 1`, requiring `2^(w-2)` entries.

The previous `2^(w-1)` allocation computed twice as many odd multiples
as needed, wasting point additions during table setup. Also corrects the
module-level doc comment to reflect the `2^(w-2)` table size.
@tarcieri tarcieri merged commit 89fe92f into RustCrypto:master Jun 6, 2026
15 checks passed
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.

2 participants