mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:50:37 +00:00
test: speed up secp256k1 fixed-base multiplication and Schnorr signing #36327
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36327
Loading…
Reference in a new issue
No description provided.
Delete branch "2026-09-test-crypto"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR speeds up the test framework’s fixed-base multiplication and Schnorr signing by:
On my local machine (Macbook M2 Pro) the
feature_taproot.py's medium time decreased from 83.39s to 35.42s.The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.cc @theStack @real-or-random
ACK
Nice, the multiplication logic looks correct
Since this is test-only code, I believe the actual trade-off here is between simplicity of the implementation vs. running time. The current implementation is probably close to the most obvious one can write whereas the windowed one is already "optimized", though it's still simple.
Concept ACK -- And yeah saving ~40s sounds like a reasonable trade-off.
Though I wonder how much comes from the caching and how much from the optimized FastGEMul.
Without this, the
rowvariable name below may be confusing. It's not clear to the reader if the table indices are row-first or column-first.What about caching
GE.__rmul__instead, which is the actual expensive operation here? Other callers may benefit, and you could leavesign_schnorruntouched (which keeps it closer to the reference implementation in BIP340 also).Sounds good!
Done
Done
Force-pushed addressing https://github.com/bitcoin/bitcoin/pull/36327#discussion_r4108067325 and https://github.com/bitcoin/bitcoin/pull/36327#discussion_r4108374223
From my bench, I guess 70% from the FastGEMul and 30% from the caching.
Concept ACK
tACK
b6fc3cf004I checked
k * Gon this branch against a second implementation for about 10,000 random scalars plus edge cases (0, n-1, n, n+1, 2n, negative values, runs of ones, each 4-bit window position). All matched.sign_schnorrpasses the BIP340 vectors and gave the same signatures as that implementation for 2,000 random messages and aux_rand.On where the gain comes from: both changes matter, the window somewhat more. In my steadiest run of
feature_taproot.py(shared machine, so rough), the window alone took about 35% off master, the cache alone about 30%, and both together 54% (62s to 29s).Nit, not new here: no unit test multiplies G by a negative scalar. I suggested one inline.
This catches the
% GE.ORDERbeing dropped from bothGE.__rmul__andFastGEMul.mul, which the current tests miss:Base:
a829aeaac1aff6cb786aab7d2514b49eb233334bHead:
b6fc3cf0046d3106e0dfb30f320d48514eff6fb2🟡 Minor
Cached generator multiplication exposes mutable points (test/functional/test_framework/crypto/secp256k1.py:360, head)
For
3 * G, windowed multiplication selectstable[0][3]and adds infinity entries for the remaining zero digits. BecauseGE.__add__returns an operand unchanged when the other point is infinity,mul_Gcan cache and return that table object directly. SinceGE.x,GE.y, andGE.infinityare writable, a caller can mutate the returned point, corrupting the cached result and precomputed table so later multiplications can return an incorrect point. Return a freshGEfrom the cached value or cache an immutable representation. No tracked current caller mutating a returnedGEwas identified, so production impact is unconfirmed.💡 Suggestion
Add focused differential tests for windowed multiplication (test/functional/test_framework/crypto/secp256k1.py:352, head)
The new chunk extraction and table accumulation are not directly compared with the independent
GE.mulimplementation. Existing signing and BIP340-vector tests exercise multiplication indirectly, but do not systematically cover window positions or scalar-reduction cases. Add deterministic comparisons for representative nonzero nibbles at each window position and for zero, negative, and order-boundary scalars. These tests would directly check that the optimization preserves multiplication semantics alongside the existing indirect cryptographic tests.Review coverage was incomplete. The adversarial_glm review did not complete.
Full review report
ACK
b6fc3cf004Results on my arm64 machine:
1m16.030smaster vs.0m52.547sPR branchnit: could update the PR description before merging, as it still mentions the caching on
compute_xonly_pubkeynit: alternatively, applying the cache decorator directly on the
GE.__rmul__method (as suggested in https://github.com/bitcoin/bitcoin/pull/36327#discussion_r4108067325) seems to work fine and would include caching for other points (probably only the generator is relevant though), ~same speedup on my machine. no strong opinion on what to preferDone.
I considered that, but I preferred to keep the cache scoped to
G. I will leave as-is for now, but happy to change if others prefer.tACK
b6fc3cf004Tested on an x86-64 machine with
build/test/functional/feature_taproot.py --randomseed=1, mean of 5 runs:Before: 51.570s
After: 27.586s
This is indeed what I had in mind when I suggested it. I have a slight preference for that variant. It's more general and future tests may profit from it.
(But no objections against merging as-is. :) It's certainly an improvement.)