AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Moderate 58 Bitcoin

Merge bitcoin/bitcoin#36895: Keep witness stacks in serialized form in CScriptWitness

Public commit record

What the developer wrote

Authored by merge-script

91/100 · Strong
Merge bitcoin/bitcoin#36895: Keep witness stacks in serialized form in CScriptWitness

56fa33eac63c37513305841ff1bd73f56ad92322 doc: add release note for the reduced memory usage of witnesses (Pieter Wuille)
150002be68e0ca307b71fc968589c45d7b44687e script: store witness stacks as their serialization (Pieter Wuille)
3fca9597f0db1c7603cf9f3ab5f8081b04f60b41 span: add SameSpan (Pieter Wuille)
519aa85b6b76b55ae388d4e2f171aa80a76ac6f0 script: remove CScriptWitness's old interface (Pieter Wuille)
77f8bac8a0eec6355e3dc8252b3dcb315c242e3a test, bench, fuzz: build witness stacks before constructing a CScriptWitness (Pieter Wuille)
5fc4db08a8f4ff5af05bffe3d588600584a42534 refactor: use CScriptWitness's interface where that needs more than a rename (Pieter Wuille)
68b0ae9d9b32d3bf9c46774bccb412ddc71499e5 policy: iterate over witness stacks directly in IsWitnessStandard (Pieter Wuille)
0f1253eb5a64874fadc73ec6fdcf57247fc9ab8f script: build the witness execution stack with ToVectors() (Pieter Wuille)
0e317e2ab5d069197bf3e4c50015c890f9b14570 script: let ExecuteWitnessScript take its initial stack by value (Pieter Wuille)
e294e2812ac0d1b6fbe46ab904ef00be8e18d3ac scripted-diff: replace witnesses as a whole instead of their stack (Pieter Wuille)
ce3c41bc3e89f4dfa0ede8b4f3722cfe8a8fd3d2 scripted-diff: serialize CScriptWitness rather than its stack (Pieter Wuille)
afc3248f6b08e18aadb14ab51d36943847a229ea scripted-diff: read and compare CScriptWitness through its interface (Pieter Wuille)
a2230b147be820753ea916a18be5f986f3c141a9 scripted-diff: use CScriptWitness's size(), empty() and back() (Pieter Wuille)
4be659599837ad23da879fbafc0f323ca532fc7b test: check CScriptWitness against the vector of vectors it represents (Pieter Wuille)
fa34694a431597258039ca5e16e7f23a85e5c576 script: give CScriptWitness an interface that hides its representation (Pieter Wuille)

Pull request description:

This is split off from #36521. This PR focuses on reducing memory usage of transactions in memory, leaving performance improvements for other PRs.

`CScriptWitness` stores a witness stack as a `std::vector<std::vector<unsigned char>>`, which costs a 24-byte vector object and an allocation for every non-empty element. This makes it possible to construct transactions that take a large amount of memory after deserialization.

This PR first introduces encapsulation for `CScriptWitness`, hiding its representation. The last commit then changes the representation to be single `std::unique_ptr<byte[]>` which for non-empty witness stacks stores their serialization prefixed by a small length header. Witnesses can no longer be modified in place; they can only be replaced in their entirety. Access to the individual witness stack elements is done by on-the-fly deserialization instead.

`CScriptWitness` shrinks from 24 to 8 bytes, and `CTxIn` from 112 to 96 bytes. Worst-case allocated size for transactions kept in memory drops from roughly 28 bytes/WU to 1.6 bytes/WU.

This worsens deserialization and validation time, adding some 50-70 ms per block to the worst case for me. A follow-up #36918 with optimizations improves the worst case deserialization time significantly instead. Other minor degradations:
* libbitcoinkernel's witness stack element accessor becomes $\mathcal{O}(n)$ in the index of the witness element; using it to iterate a whole witness may therefore become $\mathcal{O}(n^2)$.
* `CScriptWitness::back()` is $\mathcal{O}(n)$ in the length of the witness stack.

Issue reported by furszy and 0xB10C.

ACKs for top commit:
instagibbs:
reACK 56fa33eac63c37513305841ff1bd73f56ad92322
l0rinc:
reACK 56fa33eac63c37513305841ff1bd73f56ad92322
brunoerg:
reACK 56fa33eac63c37513305841ff1bd73f56ad92322
ajtowns:
ACK 56fa33eac63c37513305841ff1bd73f56ad92322
w0xlt:
ACK 56fa33eac63c37513305841ff1bd73f56ad92322

Tree-SHA512: b9786d96fe14641a8029d3cd5fcf440c6b60ee613d6cb0a19c9b9b765b43cdef8497cbd6a9a4dd8c757a621e1605b9966617649d147ad5cc67af9d2a448d44c7
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Mentions testing or verification✓ Links an issue, advisory, or supporting reference
The short version

What changed, and why it matters

This change is a defensive memory-optimization refactor for how Bitcoin Core stores transaction witness data. It replaces a structure that used many small memory allocations with a single compact serialized buffer. The primary security benefit is reducing the worst-case memory an attacker could force a node to use by sending transactions with large witness stacks, which helps mitigate denial-of-service via memory exhaustion. The change also adds a new fuzz test to verify the new witness representation behaves identically to the old one.

Recommended action

Review the new CScriptWitness implementation for iterator/span lifetime safety, ensure the O(n) indexed access is not used in hot validation loops, and monitor follow-up PR #36918 for performance improvements. The change should be included in release notes as a memory-usage improvement and potential DoS hardening.

Security signals we found

01

Memory-usage reduction for attacker-controlled transaction witnesses

02

Mitigation of resource-exhaustion DoS via large witness stacks

03

Immutable witness representation prevents in-place modification bugs

04

New fuzz target added for witness serialization equivalence

05

Extensive test updates to preserve consensus and policy behavior

06

Acknowledged performance trade-offs in deserialization and indexed access

Risk score

Why this scored 58/100

Our methodology →
Potential impact 18/30
Exploitability 8/25
Stealth signal 6/15
Affected reach 14/15
Confidence 8/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.