mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 14:11:39 +00:00
script: clarify implicit signature-cache input requirements #36395
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!36395
Loading…
Reference in a new issue
No description provided.
Delete branch "l0rinc/sigcache-input-invariants"
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?
Problem: Signature-cache hashing relies on input requirements that are implicit at its public entry methods.
Inspired by the Elements rangeproof-cache vulnerability behind the recent Liquid incident and the subsequent cache-key fix, this change clarifies an easy-to-misread hashing pattern:
Write(data, size)hashes the data bytes without encoding the size.Bitcoin Core's cache keys already include the signature hash, public key, and signature in separate ECDSA and Schnorr domains.
Bitcoin Core's signature-cache encoding is unambiguous for inputs accepted by its current script checker: a valid ECDSA public key's prefix determines its length, while a Schnorr public key is always 32 bytes, leaving the signature as the remaining suffix.
Fix: Use native
HashWriterserialization in the typedComputeEntry*methods so ECDSA public keys and signature vectors include their CompactSize lengths.Schnorr retains its fixed-size public-key encoding and raw signature span.
Return the digest by value from the public
ComputeEntry*methods so callers explicitly initialize the cache entry, where the previous chainedFinalize(entry.begin())obscured the output assignment.Keep the prepared salt prefixes private and const, and copy them locally for each entry.
Retain the ECDSA salt padding and drop the Schnorr salt padding.
Add a nonce-taking constructor so tests can reproduce the encoding deterministically.
The process-local cache-entry bytes change, while script validity rules are unchanged.
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.Conflicts
Reviewers, 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.
Earlier concerns neither republished nor resolved: 2 suggestions. See the Run archive linked from the Review report for their assessments.
🔵 Suggestion · ECDSA second pass does not check cache insertion
The new ECDSA caching passes would still pass if successful signatures were never inserted, so the second pass does not establish that the cache is populated.
Location:
src/test/script_tests.cpp:163(head).🔵 Suggestion · Fuzz assertions miss different-tuple cache hits
The new
script_sigcacheassertions retain a pre-existing limitation: each input repeats one tuple against a fresh cache, so they cannot detect a cached success returned for a different sighash, key or signature.Location:
src/test/fuzz/script_sigcache.cpp:56(head).Review coverage was incomplete.
Reviewed:
a772594831f7· Review report · Previous reportspermalink
🚧 At least one of the CI tasks failed.
Task
lint: https://github.com/bitcoin/bitcoin/actions/runs/36786811940/job/110130078415LLM reason (✨ experimental): CI failed due to lint-includes.py detecting a newly introduced Boost include (
boost/test/tools/context.hpp) insrc/test/key_tests.cpp.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
concept ACK on the tests, concept NACKy on the refactors due to blast radius. Will review the tests closer if split out
I ran the two script_tests.json rows this adds or changes, at
c8d6a97afb, through btclib's script interpreter, and both give the expected result. I haven't reviewed the sigcache changes.The refactor is the meat of this change, the original error would still be possible even if we only add tests, and I want to make sure we learn from those mistakes and refactor to make such misunderstandings harder.
If we're afraid of touching a part of the code, it means it's not ours yet, and it's probably where the ugly bugs are buried.
Concept ACK
The new tests catch five signature-cache bugs that the current unit tests miss. I made seven one-line breaking changes to
sigcache.cppand ran the unit tests onc8d6a97afband on its base,d4b0e1e4b0.On the base, the unit tests still pass with six of the seven changes. Only storing failed Schnorr checks is caught, by
txvalidationcache_tests. On this PR,script_testscatches five of those six: dropping the public key or the sighash from the ECDSA or the Schnorr entry, and storing failed ECDSA checks. The cached passes inDoTestcatch the ECDSA ones, and the new BIP341 vector checks catch the Schnorr ones.The sixth change gives Schnorr entries the ECDSA salt, and no unit test catches it on either commit. The separate salts are all that keeps the two domains apart: with one salt, the ECDSA entry for a 33-byte key and a 63-byte signature equals the Schnorr entry for the key's first 32 bytes and a signature made of the key's last byte followed by those 63 bytes. A test that builds both entries with
ComputeEntryECDSAandComputeEntrySchnorrand checks that they differ passes on this PR and fails with that change. It could go in this PR; I can share it, and the seven changes.🐙 This pull request conflicts with the target branch and needs rebase.
Rebased and replaced the hashing helper and cache-entry assertions with native serialization (with explicit size commitment), following the Liquid fix and @instagibbs's suggestion.
Made the salt prefixes const, removed the padding, and added tests that check the encoded bytes and make sure failed signature checks aren't cached.
The padding in master was there to fill a 64-byte chunk so the compression ran once at startup. With 33 bytes, the constructor leaves 33 bytes in the buffer and defers that transform into
ComputeEntry.For 65-byte uncompressed keys this also crosses the 192-byte mark, needing 4 transforms instead of 3.
Padding 31 zero bytes keeps the
HashWriterchanges and the precomputed chunk:Indeed, while Schnorr measured slightly faster without it (must be some alignment magic), there is an extra cost for uncompressed ECDSA keys: I restored the padding for ECDSA and added you as coauthor, thanks.
🐙 This pull request conflicts with the target branch and needs rebase.