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.
--device all
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).
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.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
--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.
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.
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.
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.rule | 36.12% | 150,000 | 0.2408 |
| rulest.freq.150000.rule | 28.02% | 150,000 | 0.1868 |
| rulest.greedy.50000.rule | 29.78% | 50,000 | 0.5956 |
| rulest.freq.50000.rule | 20.25% | 50,000 | 0.4050 |
| rulest.greedy.25000.rule | 24.91% | 25,000 | 0.9964 |
| rulest.freq.25000.rule | 15.99% | 25,000 | 0.6396 |
| rulest.greedy.10000.rule | 19.71% | 10,000 | 1.971 |
| rulest.freq.10000.rule | 11.44% | 10,000 | 1.144 |
| rulest.greedy.1500.rule | 10.45% | 1,500 | 6.9667 |
| rulest.freq.1500.rule | 4.61% | 1,500 | 3.0733 |
| rulest.greedy.250.rule | 5.06% | 250 | 20.24 |
| rulest.freq.250.rule | 1.73% | 250 | 6.92 |
| rulest.greedy.64.rule | 2.10% | 64 | 32.8125 |
| rulest.freq.64.rule | 0.88% | 64 | 13.75 |
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.
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.
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.
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.