Skip to content

fix: enforce BIP-352 K_max on the live receive path (SNB-540) - #18

Merged
setavenger merged 5 commits into
masterfrom
snb-540-live-kmax
Oct 30, 2026
Merged

setavenger merged 5 commits into
masterfrom
snb-540-live-kmax

Conversation

@setavenger

@setavenger setavenger commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Update 2026-10-30: merged with master after the Friglet Deslop

master now carries the Friglet Deslop cleanup (#67–#76), which conflicted with this PR in four files. Merge commit 3ae8888 resolves them as described in #75:

  • blindbit-lib/Cargo.toml, friglet/Cargo.toml: this PR's bdk-sp rev pin and comments, on top of the shorter manifest lines from SNB-670.
  • scanning.rs: BIP352_K_MAX drops its #[expect(dead_code)] (the compile-time assert now uses it), and its doc no longer refers to scan_transaction_full, which blindbit-lib: remove the unused full-transaction receive path (SNB-662) #75 removed. The edit inside that function is dropped with it.
  • bip352_vectors.rs: the full-path K_max boundary test stays removed (blindbit-lib: remove the unused full-transaction receive path (SNB-662) #75). The live K_max test is un-ignored as before, now passes the label set, and its doc no longer links the removed test.
  • Root Cargo.toml: the secp256k1-sys comment no longer says the live K_max test is ignored.

Verified locally on 3ae8888 (debug, private target dir): cargo test --workspace 254 passed, 0 ignored (master: 253 + the ignored K_max test); cargo test -p blindbit-lib --features serde bip352 8 passed in 30 s, including official_vectors_live_receive_step_enforces_k_max; cargo check --workspace --all-targets --all-features 0 warnings; clippy -D warnings on friglet/friglet-ipc/friglet-tray clean.

The original description follows. Where it mentions scan_transaction_full or official_vectors_enforce_k_max_at_the_blindbit_boundary, those are gone since #75.


Problem

The live receive path is scan_transaction_short → Scanner::apply_matched_block → apply_block_relevant on the external indexer → bdk_sp::receive::scan_txouts. That last function had no K_max cutoff. On the official "exceeding K_max" vector it stored 2324 outputs, but BIP-352 caps a recipient group at K_max = 2323.

Fix

  • The cutoff goes in scan_txouts in the fork (fix(receive): stop scan_txouts at BIP-352 K_max bdk-sp#1): a K_MAX constant bounds the matching loop. Outputs with k >= 2323 are never matched, and every k < 2323 is still found. Putting it there covers every caller: the indexer crate, Scanner::scan_txouts, and this repo's live path.
  • bdk_sp and indexer in blindbit-lib, and bdk_sp in friglet, are pinned with rev = "94b72f60…". That is fix(receive): stop scan_txouts at BIP-352 K_max bdk-sp#1's head commit. The pin is permanent, not a stopgap: bdk-sp#1 must land with a merge commit (not squash or rebase), so 94b72f6 stays in the fork's history and no pin bump is needed. The Cargo.toml comments now say this. The Cargo.lock diff is only those two source lines.
  • A compile-time check, const _: () = assert!(BIP352_K_MAX == bdk_sp::K_MAX as usize) (blindbit-lib/src/scanner/scanning.rs), fails the build if the pinned bdk_sp ever disagrees with the BIP-352 value this crate uses. I checked it goes red: with the local constant set to 2324, the build fails with evaluation panicked: bdk_sp::K_MAX differs from BIP-352 K_max.
  • official_vectors_live_receive_step_enforces_k_max is no longer ignored. It now checks the exact set, not just the count: each stored tweak matches a distinct k < 2323, and neither the unlabelled nor the labelled k = 2323 candidate is stored.
  • Updated the comments that said the live path enforces no K_max. scan_transaction_full's truncate(BIP352_K_MAX) is unchanged.

CI cost

Un-ignoring the test makes the blindbit-lib test binary slower on CI, because libsecp256k1 runs at -O0 in debug builds. That is fixed separately, for every PR, by #25 ([profile.dev.package.secp256k1-sys] opt-level = 3). I chose that over moving this test to a separate job or a release-mode run, both of which add a second compile per PR. #25 and this PR merge cleanly in either order; I checked locally.

CI run blindbit-lib test binary Test workspace step job
#17 head 4ea2e06, K_max test ignored (run) 180.5 s 353 s 8m32s
this PR f4c8030, test un-ignored (run) 246.9 s 420 s 9m45s
this PR 5e9f4c7 (current head, without #25; warm cache) (run) 214.5 s 239 s 5m54s
this PR c3c5cf9 + #25's change (run) 27.8 s 145 s 4m49s

c3c5cf9 carried #25's change before it was split out (reverted in 5e9f4c7).

Verification

  • CI on the current head 5e9f4c7: green. blindbit-lib reports official_vectors_live_receive_step_enforces_k_max ... ok and 15 passed; 0 failed; 0 ignored; finished in 214.53s. With build: optimise libsecp256k1 in dev/test builds (BIP-352 vector tests 247s -> 28s on CI) #25's change (c3c5cf9) it was also green, in 27.77 s.
  • Locally on 5e9f4c7, private target dir: cargo test -p blindbit-lib --lib gives 15 passed; 0 failed; 0 ignored; finished in 25.22s. That machine's local cargo config optimises libsecp256k1 the way build: optimise libsecp256k1 in dev/test builds (BIP-352 vector tests 247s -> 28s on CI) #25 does. With the opt-level forced to 0, the K_max test alone takes 99.4 s of CPU time; at opt-level 3 it takes 18.9 s.
  • The compile-time assertion's red demo was re-run in the private target dir with the same result.
  • Red demos, re-run on 5e9f4c7 in a private target dir: I patched a local copy of the fork at 94b72f6 via --config patch... and ran the acceptance test. Unmodified, it passes. With three wrong cutoffs:
    • no cutoff: fails left: 2324, right: 2323
    • cutoff at 2322: fails left: 2322, right: 2323
    • cutoff at 2324: fails left: 2324, right: 2323
  • cargo test --workspace and cargo clippy --no-deps -p friglet -p friglet-ipc -p friglet-tray --all-targets -- -D warnings: CI green on 5e9f4c7.

The live receive path (scan_transaction_short, then apply_block_relevant on
the external indexer) relies on bdk_sp::receive::scan_txouts, which had no
K_max cutoff: on the official "exceeding K_max" vector it stored 2324
outputs where BIP-352 caps a recipient group at 2323.

The cutoff now lives in scan_txouts itself (setavenger/bdk-sp PR #1). Pin
bdk_sp and indexer, in blindbit-lib and friglet, to that PR's commit; the
pin must move to the fork's merged commit once the PR lands.

Un-ignore official_vectors_live_receive_step_enforces_k_max and make it
check exactness: every stored output is the match for a distinct k < 2323
and the k = 2323 candidate is never stored. Update the comments that said
the live path has no K_max.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
setavenger and others added 2 commits October 1, 2026 13:15
…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>
Un-ignoring the live K_max vector test added ~66s to the blindbit-lib test
binary on CI (180.5s -> 246.9s), on top of two other tests that already
scan the same 2324-output vector for ~3 minutes. Almost all of that time is
libsecp256k1, which secp256k1-sys compiles at -O0 in a debug profile.

Set `opt-level = 3` for secp256k1-sys in the dev profile (the test profile
inherits it). Locally the live K_max test drops from 219s to 41s of CPU
time; nothing else changes, and only one C library is rebuilt. Optimising
the secp256k1 and bdk_sp Rust crates as well gained <5% more, so they are
left alone. Debug builds of the daemon scan faster as a side effect.

This keeps the K_max coverage in the main test job instead of moving it to
a separate job or a release-mode run, both of which would add a second
full compile per PR.

SNB-540

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The secp256k1-sys `opt-level = 3` override speeds up every PR's test run,
not just this one, so it now lands separately as #25
against integration. Revert it here, with the one test comment that
referred to it, so this PR only carries the K_max change. Until #25 is in,
this branch's CI pays the ~66s the un-ignored K_max test costs at -O0.

SNB-540

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings #18 up to date with master after the Friglet Deslop cleanup (#67-#76).
Conflicts resolved as described in #75:

- blindbit-lib/Cargo.toml, friglet/Cargo.toml: keep #18's bdk-sp rev pin and
  comments on top of the reduced manifest lines from SNB-670.
- scanning.rs: BIP352_K_MAX loses its #[expect(dead_code)] (the compile-time
  assert against bdk_sp::K_MAX now uses it); its doc no longer mentions the
  removed scan_transaction_full, which stays deleted.
- bip352_vectors.rs: the full-path K_max boundary test stays deleted; the live
  K_max test takes the label set and imports BIP352_K_MAX; its doc no longer
  links the deleted test.
- Cargo.toml: the secp256k1-sys comment no longer says the live K_max test is
  ignored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@setavenger
setavenger merged commit 4ad8840 into master Oct 30, 2026
1 check passed
@setavenger
setavenger deleted the snb-540-live-kmax branch October 30, 2026 07:06
setavenger added a commit that referenced this pull request Oct 30, 2026
Brings #22 up to date with master (Friglet Deslop #67-#76, #18, #80, #81).
No textual conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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