Concept · A1131

COVERAGE EVALUATION +
GREEDY CELF SELECTION

Set-Cover Optimization Lazy Greedy (CELF) Submodular Marginal Gain

How rulest's own selection stage turns a huge pool of candidate rules into a small ruleset that actually maximizes distinct password recovery — instead of just summing individual rule scores — and why this consistently beats plain frequency ranking at every budget size.

2 phases
GPU upper-bound pass · lazy CELF heap
1 objective
| union of recovered target words |
0 coverage matrices materialized
GPU-resident target table only, nothing streamed to disk
1 GPU
CELF always runs on exactly one device, even under --device all
--select-mode greedy
the CLI flag that turns this on
The Core Idea

Sorting candidate rules by raw GPU frequency and taking the top-N is the obvious first approach, but it optimizes the wrong quantity. Two high-frequency rules can crack almost the same subset of the target list — keeping both wastes budget that a lower-frequency, but orthogonal, rule would have spent far better. What actually matters for a ruleset of size k is not the sum of individual scores, but the size of the union of everything those k rules recover.

That's a classic set-cover problem: each rule r defines a set C(r) of the distinct target words it actually cracks, and the goal is to choose a budget-sized collection of rules whose combined C(r) sets cover as much of the target universe as possible. Set cover is NP-hard to solve exactly, but the coverage function is submodular — each additional rule can only add as much or less than it would have earlier, never more — which is exactly the property that makes a simple greedy strategy provably close to optimal (within a factor of 1 − 1/e, roughly 63%, of the true best selection).

Phase 1 — GPU Target Table + Upper-Bound Pass

rulest/selection.py no longer builds a {rule: hits} coverage matrix at all — bloom-filter prefiltering and an on-disk CoverageStore were the original design, but the current _GpuExactScorer is memory-light by construction: it keeps one GPU-resident target table and never materializes per-rule coverage rows anywhere, in RAM or on disk.

01

Memory-bounded target dedupe

The target wordlist is deduplicated by sorting the list in place and compacting out adjacent duplicates in a single pass, instead of building a Python set(). At millions of mostly-unique strings this avoids the hash-table and boxed-pointer overhead of a set-plus-list approach while the original list is still live, and target IDs are assigned only after sorting (ordering carries no meaning for CELF).

02

Build a GPU-resident target hash table

celf_initialize_target_table() uploads every deduplicated target word into a GPU hash table addressed by a 64-bit FNV-1a fingerprint and a SplitMix64 slot mixer, sized to keep load under 50%. From here on, "does this candidate's output match a target" is a GPU table lookup, not a comparison against a Python structure.

03

Upload the candidate pool once

celf_prepare_candidate_pool() uploads the full, depth-filtered candidate rule pool and every candidate's parsed function sequence to the GPU a single time, so every later batch — the upper-bound pass, the lazy heap's rescoring rounds, the final winner scan — reuses it instead of re-encoding rules per round.

04

Fast admissible upper bound per rule

One GPU pass per rule batch (record_hits=False) counts raw matching base-word outputs against the target table — fast, but duplicate outputs can inflate the count. That's fine: it can only overcount, never undercount, so it's a safe upper bound for every candidate's maximum possible marginal gain, computed once before the heap ever starts popping.

Phase 2 — Lazy CELF Heap with Batched Exact Rescoring
05

Naive greedy: correct, but too slow at scale

The textbook greedy algorithm repeatedly scans every remaining candidate, recomputes its marginal gain against the current covered set, and picks the best one — then repeats from scratch. That's O(rules × budget) full GPU scans, which is fine at a few thousand candidates but becomes the bottleneck well before 100k on the large candidate pools this pipeline routinely evaluates.

06

Pop the top bound, try to win cheaply first

Each round pops the candidate with the strongest outstanding upper bound off the heap and gets a cheap exact-gain-only score for it — a GPU kernel mode that deduplicates matching target IDs into a count but never transfers or records them. Most popped candidates turn out to have a losing or zero exact gain and stop right there, without ever paying for the far more expensive hit-recording scan.

07

Batch-tighten, then batch-exact-rescore real threats

Only candidates still at the top of the heap whose bound could still beat the current best get touched. Their bounds are tightened first using the cheap batched upper-bound kernel; any that could still win are then exact-rescored together, in one GPU launch per batch, instead of one GPU synchronization per candidate — this is what keeps a round's "am I beaten yet" question affordable at scale.

08

One recording scan, only for the actual winner

Once nothing left in the heap can beat the round's best candidate, exactly one full hit-recording GPU scan runs — for that winner only — to materialize its real target IDs. A built-in safety net cross-checks the recorded gain against the batched exact gain that won the round: on any mismatch, the partial selection is discarded, the GPU's active-target bitmap is reset, and the entire run is retried with batched exact rescoring disabled (falling back to always-singleton exact scoring), rather than ever risk silently wrong coverage.

09

Stop at budget, or at saturation

Selection continues until either the requested rule budget is reached, or no remaining candidate adds any new coverage at all (saturation). An optional cost term (gain / depth^α, --select-cost-alpha) can bias the search toward shorter, cheaper rule chains among candidates with similar marginal gain — pure marginal-coverage greedy is the α = 0 default. There is currently no post-selection local-search / swap pass — the lazy heap's own pick is the final one.

How rulest Runs This

CELF is one stage inside the main rulest pipeline (rulest/selection.py), run right after candidate generation (token-strip / genetic / chain-building) and after minimization-by-signature, via --select-mode greedy (frequency, which just sorts by raw GPU hit count, remains the default). select_rules_by_marginal_coverage() dedupes the target list, builds a _GpuExactScorer — which owns the GPU target hash table and the uploaded candidate pool — runs the upper-bound pass, then hands bound/scoring callbacks to the CELF heap loop in _celf_select_from_upper_bounds(). CELF always runs on a single GPU (_resolve_single_engine()), even when extraction itself used --device all; the rest of that device pool just stays idle for this stage.

rulest
$ python3 run_rulest.py base.txt target.txt \ --select-mode greedy --select-budget 150000 \ --select-budgets "100,1000,10000,150000" \ --select-cost-alpha 0.0 --exact-recovery

--select-budgets cuts several prefix files from one greedy ordering without re-running the upper-bound pass. --select-cost-alpha biases ties toward shorter chains; --exact-recovery just adds a reporting line to the output header, since greedy coverage is already exact — no second GPU pass. There is no --local-search flag in the current CLI: the earlier 1-swap local-search pass has been removed, and the heap's own pick is the final ruleset.

Why This Beats Frequency Ranking

Frequency ranking asks "how many hits does this rule get on its own?" Greedy CELF asks "how many new passwords does this rule recover that nothing already selected recovers?" Those are different questions, and at every budget size the second one is the one that actually determines how many unique passwords a ruleset of size k will crack. The practical result is a ruleset that reaches a given coverage percentage with dramatically fewer rules — or reaches a much higher coverage percentage at the same rule count — than a frequency-sorted list ever can, because frequency ranking has no mechanism to notice or penalize redundancy between rules.

The submodularity guarantee: because marginal gains only ever shrink as more gets covered, CELF's lazy re-validation is not an approximation shortcut — it produces the exact same selection as naive greedy, just without the wasted rescans. The speed comes for free; nothing is traded away for it.
The Heap

Python's built-in heapq only knows how to be a min-heap — it always hands you back the smallest item first. CELF wants the opposite: the rule with the biggest possible remaining gain. The trick is to store every candidate's priority as a negative number, (-bound/cost, rule_index, bound). The biggest priority becomes the most negative number, which is exactly what a min-heap pops first — so a min-heap storing negated scores behaves like a max-heap, without needing a separate max-heap implementation. rule_index is the tie-breaker (lower index wins); when --select-cost-alpha is 0 (the default), cost is a constant 1.0 for every rule, so priority reduces to the raw bound.

Every candidate's first number isn't a guess — it's an admissible upper bound from the Phase 1 GPU scan: a raw count of matching base-word outputs against the whole target table, computed once. Duplicate outputs can make that count too high, but never too low. Because coverage is also submodular (a rule can only recover fewer new words later, never more, once other rules have already covered part of the list), the bound is guaranteed to be the most that rule could ever still be worth — a safe ceiling from two independent directions, not a real-time estimate.

Picking each rule then works in layers, from cheapest to most expensive: pop the top of the heap and get its exact gain with a cheap GPU mode that counts distinct new targets but doesn't transfer their IDs. If nothing else in the heap could possibly beat that number, it's the pick — full stop. If some other candidate's bound could still win, its bound gets tightened first with a cheap batched rescan, and only candidates that survive that get batch-exact-rescored together. Only once a round's true winner is settled does one real hit-recording GPU scan run, to pull back the actual target IDs that get marked covered. Most of the heap never leaves the "bound" stage in a given round — that's what makes this fast: it skips the expensive scan for every candidate it can already prove loses, while a built-in cross-check between the batched gain and the recorded gain guards against ever acting on a silently wrong number.

Benchmark Comparison — rulest (Same Corpus)

Single-run results on base = hashmob.mini, target = hashmob.medium. Each pair below holds ruleset size fixed and compares greedy CELF selection against plain frequency ranking.

Ruleset Cover Size Efficiency
rulest.greedy.150000.rule36.12%150,0000.2408
rulest.freq.150000.rule28.02%150,0000.1868
rulest.greedy.50000.rule29.78%50,0000.5956
rulest.freq.50000.rule20.25%50,0000.4050
rulest.greedy.25000.rule24.91%25,0000.9964
rulest.freq.25000.rule15.99%25,0000.6396
rulest.greedy.10000.rule19.71%10,0001.971
rulest.freq.10000.rule11.44%10,0001.144
rulest.greedy.1500.rule10.45%1,5006.9667
rulest.freq.1500.rule4.61%1,5003.0733
rulest.greedy.250.rule5.06%25020.24
rulest.freq.250.rule1.73%2506.92
rulest.greedy.64.rule2.10%6432.8125
rulest.freq.64.rule0.88%6413.75
greedy (CELF marginal coverage) freq (raw GPU-frequency ranking)
Reading the table: at every fixed size, greedy roughly matches or exceeds 1.4–2.4× the coverage of frequency ranking with the same rule budget — and the gap actually widens at smaller sizes (64 rules: 2.10% vs 0.88%, nearly 2.4×), which is exactly what redundancy elimination predicts: the fewer rules you can afford, the more it costs to waste any of them on overlap.
Practical Notes
No coverage matrix, in memory or on disk

Selection used to stream a {rule: hits} coverage matrix into an in-memory dict below a threshold and a disk-backed CoverageStore above it. That matrix is gone: the current scorer keeps one GPU-resident target table and computes bounds/gains on demand, so the selection log reports coverage matrix: none regardless of pool size. The SELECTION_DISK_THRESHOLD constant still exists in rulest/common.py but is dead — nothing in selection.py reads it any more.

Budget vs saturation

A fixed budget stops selection at exactly k rules. Leaving it unset runs to saturation — every rule that can still add coverage gets added, useful for finding the practical ceiling of a candidate pool before picking a production budget.

Cost-adjusted gain (α)

Raising cost_alpha above 0 divides marginal gain by chain depth raised to that power, nudging ties toward shorter, cheaper rules — useful when hashcat runtime per candidate matters as much as raw coverage.

Tie-breaking

Ties in priority are broken by rule index — whichever candidate was uploaded to the GPU pool first wins — not by comparing rule strings. This is handled directly by the second element of each heap tuple, so it's deterministic across runs with no separate pre-sort step needed.

Takeaway: greedy CELF selection isn't a heuristic trick layered on top of frequency ranking — it optimizes a fundamentally different, and more correct, objective (union coverage instead of summed score), and CELF's lazy heap is what makes running that correct objective affordable at candidate pools from a few thousand up to millions of rules.