Skip to content

fix(send): enforce BIP352 K_MAX per-scan-key recipient limit - #80

Open
debojyoti10CC wants to merge 4 commits into
bitcoindevkit:masterfrom
debojyoti10CC:fix/sender-kmax-limit
Open

debojyoti10CC wants to merge 4 commits into
bitcoindevkit:masterfrom
debojyoti10CC:fix/sender-kmax-limit

Conversation

@debojyoti10CC

Copy link
Copy Markdown

Description

BIP352 says a sender must fail if any group of recipients sharing one B_scan has more than K_MAX (2323) members. create_silentpayment_scriptpubkeys never checked this, so it happily derived outputs for arbitrarily large groups, even though scan_txouts on the receive side already rejects them (#79 asks for the sender-side check).

The fix reuses receive::K_MAX. The function now returns Result<_, SpSendError> and returns the new SpSendError::GroupLimitExceeded(K_MAX) as soon as a (K_MAX + 1)-th recipient for the same scan key shows up. The bip32, bip352 and PSBT senders already returned SpSendError, so they just propagate it.

Notes to the reviewers

This is a breaking change: create_silentpayment_scriptpubkeys is public and its return type changed from HashMap<..> to Result<HashMap<..>, SpSendError>. I couldn't find a way to enforce the limit at the shared derivation point without it. SpSendError also gets a new variant.

Tests: exactly K_MAX recipients for one scan key succeeds, K_MAX + 1 returns GroupLimitExceeded. Existing tests were updated for the new return type.

I could not run the tests locally: Windows Application Control blocks cargo build scripts on my machine (os error 4551). cargo fmt --check passes; tests and clippy still need a run in CI or elsewhere.

Changelog notice

  • Fixed: sender now enforces the BIP352 K_MAX per-scan-key recipient limit; create_silentpayment_scriptpubkeys now returns a Result (breaking).

Checklists

  • I've signed all my commits
  • I followed the conventional commit guidelines
  • I ran just p (fmt, clippy and test) before committing (only fmt)
  • I've added tests to reproduce the issue (not yet run, see above)
  • This pull request breaks the existing API
  • I'm linking the issue being fixed by this PR

AI use: I used Claude Code to help write this change.

Fixes #79

@debojyoti10CC
debojyoti10CC marked this pull request as ready for review November 17, 2026 14:15
@AdityaKaushal17

Copy link
Copy Markdown

Tested c7dca79 on macOS arm64, cargo 1.100.0.

The checklist says the reproducing tests haven’t been run yet. I ran them: the unit tests for exactly K_MAX and K_MAX + 1 pass.

cargo test: 204 passed, 0 failed, 3 ignored (9 test binaries).
cargo test bip352_vectors::send::maximum_per_group -- --ignored: fails at send.rs:41 with assertion failed: case.expected.outputs.contains(&results).

I think the cause is the test harness, not the new check. Vector 27 has one recipient entry with "count": 2324. The recipient deserializer in serialization.rs doesn’t read count, so the test passes one recipient and the new check never runs. The unit tests cover the boundary, but the official vector doesn’t yet.

Two suggestions:

  1. Expand each recipient by its count in the deserializer, then drop the #[ignore] on the send test so the official vector runs.
    check_cases treats any Err as a pass for vectors with empty expected outputs. For this vector, asserting SpSendError::GroupLimitExceeded(K_MAX) would confirm the error is the one we expect.

  2. I also checked the bip32, bip352 and PSBT callers, and they all propagate the new Result correctly. The breaking change to create_silentpayment_scriptpubkeys is clearly flagged in the description.

I can test a branch with the deserializer change if that helps.

@debojyoti10CC

Copy link
Copy Markdown
Author

Thanks for testing this and for tracking down the vector issue, that's a great catch. I pushed 310a5c5: the test deserializer now expands each recipient by its count, the maximum_per_group vector is no longer ignored, and it asserts GroupLimitExceeded(K_MAX) specifically. I can't run the Rust tests on my machine right now, so if you're able to re-run it that would really help.

BIP352 requires senders to fail if a group of recipients sharing the same
B_scan has more than K_MAX members. Only the receive side checked this.

create_silentpayment_scriptpubkeys now returns a Result and fails with
SpSendError::GroupLimitExceeded. Breaking: public return type changed.

Fixes bitcoindevkit#79
The test deserializer ignored the recipient "count" field, so vector 27 (one recipient with count 2324) only passed a single recipient and never reached the K_MAX check. Expand each recipient by its count, un-ignore the test, and assert the exact GroupLimitExceeded(K_MAX) error.
@AdityaKaushal17

Copy link
Copy Markdown

Re-ran on 6539394 (macOS arm64, cargo 1.100.0). The diff from 310a5c5 is empty, so this is the same code with new signatures.

  • The official K_MAX send vector now runs and passes, and it asserts GroupLimitExceeded(K_MAX). Thanks for the fix.
  • Full suite: unit 114 passed; functional 54 passed, 2 ignored (receive-side slow K_MAX, by design; send-side intermediate-sum, tracked in send: false bad tweak when intermediate key sum hits zero #55); integration and doc tests pass.
  • The two K_MAX unit tests (exactly 2323 succeeds, 2324 fails) pass.

@debojyoti10CC

Copy link
Copy Markdown
Author

Thanks for re-running it, @AdityaKaushal17, and for the thorough check across the full suite. Really appreciate the help getting the vector test working.

With the official K_MAX vector passing and the full suite green, everything looks good from my side. This should be ready for review whenever a maintainer has time.

@debojyoti10CC

Copy link
Copy Markdown
Author

Thanks @AdityaKaushal17 for re-running everything on macOS and confirming the vector passes. Much appreciated.

@nymius nymius left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cACK 5ddf75d

Comment thread silentpayments/src/send/mod.rs Outdated

// BIP352: fail if a group (same B_scan) has more than K_MAX recipients
if k >= K_MAX {
return Err(SpSendError::GroupLimitExceeded(K_MAX));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why composing this as a parameterize error when it's going to be always used with K_MAX, the max group size?

Comment on lines +166 to +168
fn default_recipient_count() -> usize {
1
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default for serde has to be a function?

The error is only ever raised for K_MAX, so carry no payload and print the constant in Display. In the test deserializer, read the recipient count as Option<usize> instead of a serde default function.
@debojyoti10CC

Copy link
Copy Markdown
Author

Thanks for the review, @nymius! Both points made sense, so I pushed 0b7773c:

  • GroupLimitExceeded no longer carries a parameter, since it's only ever raised for K_MAX. The Display message prints K_MAX from receive, and the tests now match the plain variant.
  • In the test deserializer, count is now an Option<usize> with .unwrap_or(1), so there's no separate default function. (A bare #[serde(default)] would have given 0, which is why I'd used the function.)

I can't build Rust locally right now, so CI will be the compile check. rustfmt --check passes on the changed files.

@nymius nymius left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count: usize,
}

fn default_recipient_count() -> usize {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked with my clanker, and it seems this approach is sound, sorry, I did it after my original comment because I was expecting a different solution than yours. Let's roll back this.

Reviewer request on bitcoindevkit#80: a plain `count: Option<usize>` with unwrap_or
was not wanted; the default-function approach is the sound one.

Co-Authored-By: Claude Haiku 5.5 <[email protected]>
@debojyoti10CC

Copy link
Copy Markdown
Author

Thanks for checking the serde part again, @nymius. I've rolled that back in 38a9147: the test deserializer uses the default function again (#[serde(default = "default_recipient_count")]) and count stays a plain usize. The GroupLimitExceeded change from 0b7773c is still in place.nnI can't compile Rust here, so CI is the check.

@nymius nymius left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message is irrelevant for future readers, squash it in the most related commit.

@nymius

nymius commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for checking the serde part again, @nymius. I've rolled that back in 38a9147: the test deserializer uses the default function again (#[serde(default = "default_recipient_count")]) and count stays a plain usize. The GroupLimitExceeded change from 0b7773c is still in place.nnI can't compile Rust here, so CI is the check.

Can you clarify me what "can't compile Rust here" means? What's here? You're programming in rust without a rust compiler?

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.

enforce the BIP352 per-group recipient limit

3 participants