#36188 crypto: plug hardware optimized SHA256 into libsecp256k1's context
https://github.com/bitcoin/bitcoin/pull/36188 · · +452/-151 in 43 files, 11 commits · labels: Utils/log/libs
Goal
- Speed up signing operations by using hardware-accelerated SHA256 in secp256k1
- Unify and clean up the lifecycle management of cryptographic contexts
This pull request integrates Bitcoin Core's hardware-accelerated SHA256 transform into libsecp256k1 contexts using the external compression hook introduced in upstream libsecp256k1 #1777. It restructures `ECC_Context` into a proper singleton managing both signing and verification contexts, moves ECC initialization into `common/ecc_init`, and updates all call sites across binaries, tests, and benchmarks.
Problem: Libsecp256k1 previously relied solely on its internal software SHA256 implementation, leaving hardware acceleration (ARM SHA-NI, x86 SHA-NI) unused during signing and verification. Furthermore, ECC context initialization was coupled to key primitives rather than managed through a unified lifecycle.
Category: Utilities (logging, arguments, libraries) (#11 of 66)
P3 · speedup
- P3 because signing is faster on CPUs with SHA extensions but verification speed is largely unaffected
- Improves context lifecycle management across the codebase without fixing any critical safety issues
Signing speeds improve significantly on CPUs with SHA extensions (ECDSASign is 13% faster and SchnorrSign is 7% faster on ARM64), though verification performance is largely unaffected. It also delivers clean RAII lifecycle management for secp256k1 contexts, though the improvement is not blocking any critical safety issues.
Membership: Refactors cryptographic primitive integration (SHA256 hook into libsecp256k1) and ECC context lifecycle utilities under src/crypto/ and src/common/.
Factors: security/stability 1, bug 0, performance 2, user value 1, leverage 1
Reviewability: Ready
- Ready to review
- Applies cleanly with passing CI and no open questions or objections
The patch applies cleanly, CI is passing, and there are no open material questions or reviewer objections.
Author status: active
Agreement: Positive
- Broad concept support across contributors and maintainers with full code review pending
- Enthusiastic concept approval (sedited)
- Concept approval without stated reasons (w0xlt, fjahr, ismaelsadeeq, theStack, hebasto)
- Notified upstream maintainer for awareness (fanquake)
Unanimous Concept ACKs from six contributors and maintainers; full code review pending.
The concept has broad enthusiasm with multiple maintainer Concept ACKs and no objections.
- w0xlt, fjahr, sedited, ismaelsadeeq, theStack, and hebasto all posted Concept ACKs
- sedited commented 'Finally! Concept ACK'
- fanquake pinged secp256k1 upstream maintainer real-or-random for awareness
Review verdicts (DrahtBot): 0
Files
255 lines under test/bench/ci.
- src/bench/ecc_sign_verify.cpp +118/-0
- src/key.cpp +14/-63
- src/test/ecc_context_tests.cpp +62/-0
- src/ecc_context.cpp +58/-0
- src/pubkey.cpp +28/-27
- src/ecc_context.h +41/-0
- src/common/ecc_init.cpp +35/-0
- src/key.h +0/-23
- src/common/ecc_init.h +21/-0
- src/bitcoin-tx.cpp +4/-4
- src/test/fuzz/script_flags.cpp +7/-1
- src/test/fuzz/script_assets_test_minimizer.cpp +5/-1
- src/bench/sign_transaction.cpp +3/-2
- src/crypto/sha256.cpp +5/-0
- src/bench/ccoins_caching.cpp +2/-2
- src/bench/checkqueue.cpp +2/-2
- src/bench/descriptors.cpp +2/-2
- src/bitcoin-wallet.cpp +2/-2
- src/node/context.h +2/-2
- src/node/interfaces.cpp +2/-2
- src/test/fuzz/secp256k1_ecdsa_signature_parse_der_lax.cpp +2/-2
- src/bench/bip324_ecdh.cpp +2/-1
- src/bench/ellswift.cpp +2/-1
- src/bench/verify_script.cpp +2/-1
- src/bitcoind.cpp +2/-1
- src/crypto/sha256.h +3/-0
- src/test/fuzz/bip324.cpp +2/-1
- src/test/fuzz/descriptor_parse.cpp +2/-1
- src/test/fuzz/key.cpp +2/-1
- src/test/fuzz/key_io.cpp +2/-1
- src/test/fuzz/message.cpp +2/-1
- src/test/fuzz/miniscript.cpp +2/-1
- src/test/fuzz/p2p_transport_serialization.cpp +2/-1
- src/test/fuzz/script_sign.cpp +2/-1
- src/test/util/setup_common.cpp +2/-1
- src/CMakeLists.txt +2/-0
- src/init.cpp +1/-1
- src/test/sanity_tests.cpp +1/-1
- src/bench/CMakeLists.txt +1/-0
- src/kernel/CMakeLists.txt +1/-0
- src/musig.cpp +1/-0
- src/node/context.cpp +0/-1
- src/test/CMakeLists.txt +1/-0
Card
PR #36188 plugs Bitcoin Core's hardware-accelerated SHA256 compression function into libsecp256k1 contexts and encapsulates ECC context initialization into a unified RAII singleton. This improves ECDSA signing performance by roughly 13% and Schnorr signing by 7% on architectures with hardware SHA extensions, while verification performance remains essentially flat. The changes clean up global ECC state handling across bitcoind and auxiliary tools without blocking critical projects. Review state is fully ready, having earned unanimous Concept ACKs from six maintainers and contributors with detailed code review still to follow.