Review report

core: PR #36521

Provider refusals

write_verifier: Provider declined the request because of a cybersecurity restriction. Experiments remain incomplete; static verification can continue.

1 major finding · 1 minor finding · 2 suggestions

Earlier concerns neither republished nor resolved: 1 major finding · 1 minor finding · 2 suggestions. See the Run archive for their assessments.

Findings

Link

[major] · Witness enumeration through the kernel API becomes quadratic

Enumerating a large witness through Items() or get_item_at now takes about n²/2 element decodes; this could affect kernel consumers processing untrusted blocks or transactions, but does not affect bitcoind.

Location: src/kernel/bitcoinkernel.cpp:892 (head).

Details and suggested fix

btck_witness_stack_get_item_at now uses CScriptWitness::operator[], which walks forward from the beginning for each requested element except the last. The C API exposes only count and indexed access, and the C++ Items() range calls GetItem(m_idx) on every dereference. Enumerating an n-element witness therefore takes about n²/2 CompactSize decodes, compared with vector lookups before this change. A consensus-valid block can contain one input with roughly 4 million empty witness elements, such as an input spending an unknown witness-version output. Enumerating that witness would require about 8×10¹² iterator steps. btck_transaction_create also accepts such a witness from caller-supplied bytes. The change makes random access O(n), but leaves the kernel API’s only enumeration path quadratic. Provide sequential access, such as a cursor or callback, and build Items() on it; alternatively, keep an offset table in btck_WitnessStack to preserve constant-time indexed access. No checked-in caller enumerates a large witness, so production exposure depends on kernel consumers walking witness items from untrusted blocks or transactions.


Link

[minor] · Script verification copies the witness before determining whether it is needed

For the proposed worst-case block shape, validation temporarily rebuilds about the memory removed from the deserialized block; the footprint is roughly back to base’s level, not above it.

Location: src/script/interpreter.cpp:1933 (head).

Details and suggested fix

VerifyWitnessProgram calls witness.ToVectors() before checking the witness version. This reserves one 24-byte vector object per element and allocates each non-empty element. Unknown witness versions and P2A then return without using the copy; block validation does not set SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM. Taproot key-path spends, P2WPKH spends with the wrong item count, and bad control-block sizes also return without needing an owned stack. Base used a std::span and copied only inside ExecuteWitnessScript. For a mined block with one input carrying millions of tiny witness elements and spending an unknown witness-version output, validation now rebuilds roughly the memory removed from the deserialized CBlock. Every Taproot key-path check also makes two small allocations that base did not. Dispatch using size() and the front()/back() spans, and build the owned vector only immediately before each ExecuteWitnessScript call. Consensus ordering remains unchanged because both the OP_SUCCESSx scan and MAX_STACK_SIZE check remain inside ExecuteWitnessScript. Triggering the large case requires getting a nonstandard spend mined; no such block was identified.


Optional improvements

Link

[suggestion] · Deserialization benchmarks omit the weight-budgeted P2P path

The benchmarks do not measure the per-input, per-output, and per-witness weight charging used by transaction and block P2P messages.

Location: src/bench/block_deserialize.cpp:111 (head).

Details and suggested fix

Every timed run deserializes with TX_WITH_WITNESS, but tx, block, cmpctblock, and blocktxn messages now use TxWithWitnessP2P(weight_left). With a budget set, UnserializeTransaction also calls GetSerializeSize for each input, output, and witness, and rejects empty transactions. Add a DeserializeBlock variant to cover this path, resetting weight_left to the block transaction-weight limit (MAX_BLOCK_WEIGHT minus the header and count) inside each timed iteration. Keep the current runs as the disk/RPC baseline; the synthetic blocks do not need to change.


Link

[suggestion] · SpanReader::Remaining() has no use beyond its test

The method was added for witness element lookup, but that use was replaced with DecodeCompactSize, leaving only a test as a caller.

Location: src/streams.h:104 (head).

Details and suggested fix

SpanReader::Remaining() was added so CScriptWitness could find its elements through SpanReader. A later commit in this change switched element lookup to the inline DecodeCompactSize. At head, the only caller is the streams_span_reader_remaining test. The Remaining() calls in httpserver.cpp and util_string_tests.cpp belong to LineReader, which returns a size. Remove the method and its test, or identify the follow-up that will use it.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.