Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. LLM Linter (✨ experimental)Possible places where named args for integral literals may be used (e.g.
2026-11-02 15:39:36 |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
da657fa to
1c216a0
Compare
|
Concept ACK |
1 similar comment
|
Concept ACK |
|
Finally! Concept ACK |
|
Awesome Concept ACK |
|
Splendid Concept ACK |
theStack
left a comment
There was a problem hiding this comment.
LGTM, just left two organizational comments. The MuSig and BIP352 modules could also take use of the plugged in compression function, either here or in a follow-up PR.
| if (!secp256k1_xonly_pubkey_parse(secp256k1_context_static, &pubkey, m_keydata.data())) return false; | ||
| return secp256k1_schnorrsig_verify(secp256k1_context_static, sigbytes.data(), msg.begin(), 32, &pubkey); | ||
| if (!secp256k1_xonly_pubkey_parse(GetSecp256k1VerifyContext(), &pubkey, m_keydata.data())) return false; | ||
| return secp256k1_schnorrsig_verify(GetSecp256k1VerifyContext(), sigbytes.data(), msg.begin(), 32, &pubkey); |
There was a problem hiding this comment.
minimal-diff nit: from all the secp function calls in this file, this (_schnorrsig_verify) is the only one that involves hashing and thus benefits from the SHA256 override, i.e. it would be fine if the others kept using the static context
There was a problem hiding this comment.
I would add that secp256k1_context_static aborted on any call needing ecmult_gen while this ctx doesn't anymore. So that would be another argument aside from it being "minimal-diff" but it wouldn't prevent misuse, just may be make users of the function more cautious if it's not so widely used.
| ~ECC_Context(); | ||
|
|
||
| secp256k1_context* SignContext() const { return m_sign_ctx; } | ||
| const secp256k1_context* VerifyContext() const { return m_verify_ctx; } |
There was a problem hiding this comment.
An alternative to this split-up would be to keep having only one "full" (i.e. non-static) context and use it for both signing and verifying (and any other API calls that either involve a generator point multiplication or hashing). It's true that some functions don't take use of the blinding data generated with _context_randomize, but it also doesn't slow things down or hurt in another way.
The only challenge might be to find a good name for that context, since it's not only used for signing anymore (FullContext, DynamicContext, ...?).
There was a problem hiding this comment.
An alternative to this split-up would be to keep having only one "full" (i.e. non-static) context and use it for both signing and verifying (and any other API calls that either involve a generator point multiplication or hashing). It's true that some functions don't take use of the blinding data generated with _context_randomize, but it also doesn't slow things down or hurt in another way.
What if we ever want to re-seed the context rng? (secp256k1_context_randomize docs suggests doing it). By having two contexts, we avoid adding a mutex on the verification side and keep that context immutable and thread safe after construction.
There was a problem hiding this comment.
An alternative to this split-up would be to keep having only one "full" (i.e. non-static) context and use it for both signing and verifying (and any other API calls that either involve a generator point multiplication or hashing). It's true that some functions don't take use of the blinding data generated with _context_randomize, but it also doesn't slow things down or hurt in another way.
What if we ever want to re-seed the context rng? (
secp256k1_context_randomizedocs suggests doing it). By having two contexts, we avoid adding a mutex on the verification side and keep that context immutable and thread safe after construction.
Fair point, I didn't consider that there could be potential use-cases where the context needs any further modification after its initial setup in ECC_Start. Not sure how realistic plans of implementing re-seeding in Core currently are (and what the actual gain in security would really be by doing that), but I agree it's worth to take into account. No strong objection on the split-up, but wanted to bring the simpler option for discussion.
There was a problem hiding this comment.
Not sure how realistic plans of implementing re-seeding in Core currently are (and what the actual gain in security would really be by doing that),
I suspect there's a real gain when it comes to certain types of side-channel attack (e.g., power analysis). There were many discussions in the past, and I wrote a libsecp256k1 issue a while ago. The problem is just that nobody is working on it. ;)
The simplest thing we could do is to re-randomize the context after every operation that makes an ecmult_gen (pubkey generation and signing). This would be a ~100% hit in performance for these operations because rerandomizing also needs an ecmult_gen. 100% sounds huge but I'm not sure if anyone would really care here for these operations.
If people do care, the next simplest thing is to it only after (every n operations). For example, if we set n=10, then the performance hit is only 10%.
Anyway, this discussion belongs to a separate issue. But I agree that it's worth taking into account.
There was a problem hiding this comment.
Nice issue write-up.
I think we shouldn't over-think this now. Most of the work in this PR is about structuring the code properly so that we don't continue expanding the technical debt (the transform callback plug could have been done in 2 commits max otherwise, but they would be built on top of a dirty base). Once this lands, any future decision should be simple to do and isolated.
1c216a0 to
b11ea3d
Compare
|
@theStack can you take another look here? |
theStack
left a comment
There was a problem hiding this comment.
ACK ecf779f
nit: I think the "signing/verification" context naming is a bit imprecise, as many secp function calls in other modules (in e.g. ecdh, ellswift, musig, silentpayments) involve neither of these operations but still need (or profit from) a non-static context, and it's not obvious which one should be picked. Dropping an alternative naming suggestion from my clanker that I think might work, could be picked up in a renaming follow-up if others feel strongly (enough) about this:
/** secp256k1 context for operations involving secret data (secret keys, nonces,
* secret tweaks). Randomized for side-channel protection. Requires an ECC_Context. */
secp256k1_context* GetSecp256k1SecretContext();
/** secp256k1 context for operations on public data only (parsing, serialization,
* verification, public tweaks). Falls back to secp256k1_context_static without an ECC_Context. */
const secp256k1_context* GetSecp256k1PublicContext();ecf779f to
02b7b76
Compare
|
Updated per @theStack feedback. Thanks! |
sipa
left a comment
There was a problem hiding this comment.
I think there is a simpler approach that just removes ECC_Context and uses secp contexts created on first use in static locals. Clankerbranch: https://github.com/sipa/bitcoin/commits/202611_ecc_static.
Instead of requiring an ECC_Context to exist while signing, create the secp256k1 signing context in a function-local static, the first time GetSecp256k1SignContext() is called. It is never destroyed, so it stays valid for threads still running at exit. The RNG this depends on already initializes itself on first use, so no explicit initialization order is needed. The blinding seed is obtained through GetStrongRandBytes rather than GetRandBytes: the latter draws from the deterministic RNG in tests, and consuming from it at whatever point the context is first used would make test and fuzz results depend on whether an earlier test or input happened to sign. Blinding does not affect any outputs, so real randomness is harmless there. As a side effect, BasicTestingSetup no longer consumes 32 bytes of the deterministic RNG stream. The context is never modified after creation, so return it as const. ECC_Context is kept for now, merely creating the context eagerly. It is removed in the next commit.
It is no longer needed, as the signing context is created on first use. The fuzz harness still creates the signing context up front, before the coverage counters are reset, so that its one-time setup is not counted as coverage of whichever input happens to sign first.
Low-level signature creation and validation benchmarks.
libsecp256k1 lets a context use an external SHA256 compression function. With ours, ECDSA signing gets ~15% faster on CPUs with SHA extensions, Schnorr signing ~3%. SHA256Transform dispatches to whichever implementation is selected at the time of the call, so there is no need to run SHA256AutoDetect() before creating the context. Doing so lazily would race with other threads hashing, and override any explicitly selected implementation. Also use the signing context instead of the static one for the BIP324 ECDH, which hashes inside libsecp256k1 too.
Verification uses secp256k1_context_static, which can't take our optimized SHA256 compression function. Rather than reusing the signing context, which blinds certain calculations, for no benefit to public data, verification gets its own context. Like the signing context, it is created on first use. As it needs no randomness, it lives next to the verification code in pubkey.cpp, so users of the kernel library get it without any setup. Around 1% faster Schnorr signature verification. ECDSA remains equal as hashing is done outside libsecp. Co-authored-by: Pieter Wuille <[email protected]>
Every use of secp256k1_context_static in pubkey.cpp uses public data, so all of them can use the verification context, which contains our optimized SHA256 compression plugged in. -BEGIN VERIFY SCRIPT- sed -i -e '/\/\*/! s/secp256k1_context_static/GetSecp256k1PublicContext()/g' src/pubkey.cpp -END VERIFY SCRIPT-
…contexts Temporarily install a call-counting SHA256 compression function in each context, to check that ECDSA signing goes through the signing context and Schnorr verification through the public one. Also check that both contexts are distinct from each other and from the static one, and stay the same across calls.
secp256k1_musig_partial_sign() and secp256k1_silentpayments_recipient_scan_outputs() take data that is not public, like the nonce or the scan key, so they should match the rule operations on private data should use the signing context.
So all functions make use of our hardware optimized SHA256. -BEGIN VERIFY SCRIPT- sed -i -e '/\/\*/! s/secp256k1_context_static/GetSecp256k1PublicContext()/g' src/musig.cpp sed -i -e '/\/\*/! s/secp256k1_context_static/GetSecp256k1PublicContext()/g' src/common/bip352.cpp -END VERIFY SCRIPT-
02b7b76 to
67ac809
Compare
That's hidden magic, nice. Thanks! |
|
@furszy can you update the PR description, given the latest changes? |
Sure, thanks. Done. |
theStack
left a comment
There was a problem hiding this comment.
re-ACK 67ac809
Very nice simplification! It felt a bit strange at first sight that the context cleanup part (secp256k1_context_destroy call) is now completely gone, but seems fine to rely on the OS handling that, if the object intentionally lives for the duration of the process.
fjahr
left a comment
There was a problem hiding this comment.
Looks good to me, saving the ACK for later since this seems to need a rebase because of the silent merge conflict I saw.
| * Creates the secp256k1 signing context, if it does not exist yet. This is not | ||
| * necessary anymore, as GetSecp256k1SignContext() creates it on first use. | ||
| */ | ||
| class ECC_Context |
There was a problem hiding this comment.
There appears to be a silent merge conflict from this removal. #36475 added a new call site of ECC_Context which isn't removed here yet: https://github.com/bitcoin/bitcoin/pull/36475/files#diff-6bf59f47d71af9c1c3a2dd315228db6b820594f4a6009df7668696c310097637R38
| std::vector<unsigned char> sig(64); | ||
| BOOST_REQUIRE(key.SignSchnorr(uint256::ONE, sig, /*merkle_root=*/nullptr, uint256::ZERO)); | ||
|
|
||
| // Check that both contexts use our SHA256 implementation, by temporarily wrapping it with a |
There was a problem hiding this comment.
Hm, I am not sure if this works or maybe I am understanding it but if the secp256k1_context_set_sha256_compression(ctx, SHA256Transform) lines are deleted from key.cpp or pubkey.cpp the test still passes.

Since https://github.com/bitcoin-core/secp256k1/pull//1777 , libsecp256k1 allows an externally provided SHA256 compression function. This PR plug ours in, so it runs on our SHA-NI/ARMv8/SSE4 hardware optimized implementation instead of the libsecp bare internal one.
The biggest gains are at the signing side, not at the verification side.
Benchmarks at the bench introduction commit (43c9bdb) and at the tip:
On ARM64,
arm_shaniimplementationECDSASignSchnorrSignECDSAVerifySchnorrVerifyEllSwiftCreateBIP324_ECDH