Repository navigation
Conversation
Eager materialization of m-of-n combinations during policy extraction in Satisfaction::finalize can lead to exponential time and memory consumption for large threshold descriptors (such as Taproot multi_a). Because Satisfaction::PartialComplete publicly exposes a concrete FoldedConditionMap, replacing the map with a lazy iterator alone is insufficient without breaking changes to the public API. This commit introduces a checked upper bound (MAX_POLICY_COMBINATIONS = 200,000) on the number of materialized threshold combinations and Cartesian condition expansions before allocation. This bound accommodates all conventional 20-key multisig thresholds (including 10-of-20 with 184,756 combinations) while preventing unconstrained allocation and long hangs on larger thresholds. When the combination budget or condition expansion is exceeded, policy extraction returns PolicyError::ThresholdCombinationsExceeded rather than hanging or allocating unbounded memory. Accepted policies retain their exact conditions and deterministic behavior.
Intermediate DP states in checked_expanded_combinations_count could overflow usize::MAX on large n policies with high thresholds near n (such as 67-of-68 where C(68, 67) = 68, or 35-of-36 on 32-bit systems) because intermediate combinations around n / 2 grow combinatorially large before descending near n. This caused false rejections even when the true candidate count was well within the enumeration budget. This commit resolves intermediate overflow by: - Representing DP states as u128. - Using saturating multiplication and addition for intermediate states. - Clamping each DP state to (budget + 1), which prevents intermediate overflow while preserving exact values for any final total at or below the budget. - Safely handling m = 0, m = items.len(), and zero/missing condition items. Added regression tests covering 67-of-68, 68-of-68, 1-of-68, 35-of-36, 10-of-20, budget boundary rejections (8-of-21, 12-of-24), Cartesian condition alternatives, zero/missing condition items, large n/m values, Taproot multi_a descriptors near n, and FoldedConditionMap combination preservation.
j-kon
marked this pull request as ready for review
October 17, 2026 02:57
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Policy extraction in$m$ -of-$n$ combinations and expands Cartesian condition alternatives via
Satisfaction::finalizeeagerly materializes allmix(). For large threshold descriptors (such as Taprootmulti_a), this causes combinatorial explosion in memory and CPU execution time (e.g. 12-of-24 requires ~2.7 million combinations and takes seconds/gigabytes).Because
Satisfaction::PartialCompletepublicly exposes a concreteFoldedConditionMap, replacing the map with a lazy iterator is not possible without breaking changes to the public API representation.This PR introduces an explicit safeguard:
MAX_POLICY_COMBINATIONS = 200_000candidates per threshold node.u128with saturating operations clamped tocap = budget + 1.budget + 1prevents false rejection while guaranteeing mathematically exact answers whenever the final combination count is at or below the budget.PolicyError::ThresholdCombinationsExceeded.Limitations & Design Notes
Satisfaction::Partial), not globally across the entire descriptor tree. For descriptors with multiple independent or nested threshold sub-policies, each node is capped at 200,000 candidate combinations.PolicyError::ThresholdCombinationsExceededtoPolicyError. SincePolicyErroris currently not marked#[non_exhaustive], downstream code exhaustively matching without a wildcard could require updating.Testing
Comprehensive regression tests cover:
Some(68)), 68-of-68 (Some(1)), 1-of-68 (Some(68)).Some(36)).Some(184756)), 8-of-21 (Noneat 200k), 12-of-24 (Noneat 200k).usize::MAX).multi_a(20, ...)with 21 keys (Satisfaction::PartialCompletewith 21 folded combinations).FoldedConditionMapverification for all combinations in accepted policies.cargo +nightly fmt --all -- --checkcargo test --lib(debug and release)RUSTFLAGS="-D warnings --cfg bdk_wallet_unstable" cargo clippy --all-features --all-targetsRUSTFLAGS="--cfg bdk_wallet_unstable" cargo check --all-targets --no-default-features --features miniscript/no-std,bdk_chain/hashbrownRUSTDOCFLAGS="-D warnings --cfg bdk_wallet_unstable" cargo doc --workspace --all-features --no-depsCloses #555.