Skip to content

fix(receive): stop scan_txouts at BIP-352 K_max - #1

Merged
setavenger merged 1 commit into
integrationfrom
snb-540-scan-k-max
Oct 30, 2026
Merged

setavenger merged 1 commit into
integrationfrom
snb-540-scan-k-max

Conversation

@setavenger

@setavenger setavenger commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Important

Merge with "Create a merge commit" (or fast-forward integration to 94b72f6). Do not squash or rebase.
setavenger/blindbit-rs#18 pins bdk_sp and indexer to rev = "94b72f60b0015793e4db67355bdab8d240eecc00", this PR's head. Squash or rebase would give the change a new SHA; once this branch is deleted, 94b72f6 would no longer be reachable in this repo, and blindbit-rs builds would break when GitHub garbage-collects it. A merge commit keeps it in integration's history, so the pin stays valid and needs no bump.

Summary

BIP-352 caps a recipient group at K_max = 2323 outputs per transaction. The receiver's scanning loop says: "If k == K_max (=2323), stop scanning." receive::scan_txouts looped until no further output matched, so with K_max + 1 outputs to one recipient it reported all 2324. The official vector "Maximum per-group recipient limit K_max is exceeded (2324 matches): sending fails, receiver doesn't scan beyond limit" expects n_outputs = 2323.

  • Adds pub const K_MAX: u32 = 2323 (silentpayments/src/lib.rs).
  • Bounds the scan_txouts matching loop: while matched_tweaks < K_MAX. Every output with k < K_MAX is still found. k >= K_MAX is never reported, and the transaction is still processed.
  • Every receive path in this repo goes through this function, so they all get the cutoff: receive::scan::Scanner::scan_txouts, indexer (lib.rs via Scanner, and v2).
  • Out of scope: the sender side (send::create_silentpayment_scriptpubkeys) does not yet fail when a group exceeds K_max, which BIP-352 also requires. That is a separate change.

Tracked as SNB-540 (BlindBit).

Differs from upstream

bitcoindevkit/bdk-sp fixed K_max separately in its bitcoindevkit#75 (d257a22). Its version returns Err(SpReceiveError::ScanLimitExceeded(2323)) on the 2324th match. That throws away the 2323 outputs already found, and indexer v2 then skips the whole transaction without reporting anything. Upstream's vector test still passes because its receive harness accepts any error when a vector has no outputs key, and it never checks n_outputs.

This PR follows the BIP text and the vector: stop at k == K_max and keep the 2323 outputs found. A later sync with upstream (the fork is 54 commits behind) has no textual conflicts. It does have two semantic ones: upstream's receive::K_MAX collides with this PR's crate-root K_MAX (E0255), and upstream's error branch has to be dropped. These semantics should win there. Details are in SNB-540.

Tests

  • receive::tests::scan_txouts::stops_at_k_max: a tx with K_MAX + 1 outputs to one recipient. Exactly k = 0..K_MAX are found. Outputs are ordered so the test is linear (under 1s in debug).
  • receive::tests::scan_txouts::finds_all_outputs_below_k_max: the ordinary case.
  • Boundary mutants, re-run at 94b72f6: <= K_MAX fails with left: 2324, right: 2323, < K_MAX - 1 fails with left: 2322, right: 2323, and no cap fails with left: 2324, right: 2323.
  • Official vector: this repo's vector copy predates the K_max case. The upstream vector is exercised through this exact commit in fix: enforce BIP-352 K_max on the live receive path (SNB-540) blindbit-rs#18, where official_vectors_live_receive_step_enforces_k_max asserts that all 2323 matches for k < 2323 are stored and that the k = 2323 candidate (labelled or not) is not.
  • cargo test --no-fail-fast --all-features (default members, rustc 1.98.1) at 94b72f6: all pass. That is bdk_sp 104 unit + 52 functional + 2 integration, dleq 26, and 8 doctests.
  • cargo fmt --all -- --check: clean.
  • cargo clippy --all-targets --all-features -- -Dwarnings fails only on the existing clippy::iter_kv_map at silentpayments/tests/functional_tests/bip352_vectors/send.rs:28, which also fails on master. This PR doesn't touch that file.

CI

This fork has never run a workflow (0 runs, no checks on this PR). The Actions permission is "all actions allowed", but GitHub keeps a fork's inherited workflows off until the owner clicks "I understand my workflows, go ahead and enable them" on the fork's Actions tab. If they are enabled, expect:

  • CI / fmt-clippy to fail on the existing clippy lint above.
  • Code Coverage to need Coveralls set up for the fork.
  • Test mutants to run cargo-mutants on the diff.

🤖 Generated with Claude Code

BIP-352 caps the number of outputs a single recipient group can receive in
one transaction at K_max = 2323: the receiver stops scanning once k reaches
K_max. scan_txouts had no such cutoff, so a transaction paying K_max + 1
outputs to one recipient had all of them reported and stored (the official
vector "Receiving: exceeding K_max" expects 2323).

Add the K_MAX constant and bound the matching loop with it. Unit tests
cover the boundary (K_MAX + 1 outputs -> exactly k = 0..K_MAX found) and
the ordinary case below the limit.

SNB-540

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@setavenger
setavenger changed the base branch from master to integration September 24, 2026 11:09
setavenger added a commit to setavenger/blindbit-rs that referenced this pull request Oct 1, 2026
…ents

The live receive path is only BIP-352 conformant while the pinned bdk_sp
stops scan_txouts at the same K_max this crate uses, so make a mismatch a
build error: `const _: () = assert!(BIP352_K_MAX == bdk_sp::K_MAX as usize)`.

The Cargo.toml comments said to move the pin once setavenger/bdk-sp#1
lands. That is not the plan: the fork PR is merged with a merge commit, so
commit 94b72f6 stays in the fork's history and the `rev =` pin stays valid.
Say that instead, and that friglet's bdk_sp rev must match blindbit-lib's.

SNB-540

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@setavenger
setavenger merged commit 2b04619 into integration Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant