Skip to main content

vanity_core/
estimate.rs

1use crate::pattern::Pattern;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum PatternRisk {
5    None,
6    Caution,
7    Long,
8    Impractical,
9}
10
11impl PatternRisk {
12    pub fn assess(attempts: f64, pattern_chars: usize, avg_secs: f64) -> Self {
13        if attempts >= 1e15 || pattern_chars >= 10 || avg_secs >= 86400.0 * 365.0 * 10.0 {
14            PatternRisk::Impractical
15        } else if attempts >= 1e12 || pattern_chars >= 8 || avg_secs >= 86400.0 * 30.0 {
16            PatternRisk::Long
17        } else if attempts >= 1e9 || pattern_chars >= 6 || avg_secs >= 86400.0 {
18            PatternRisk::Caution
19        } else {
20            PatternRisk::None
21        }
22    }
23}
24
25#[derive(Debug, Clone)]
26pub struct GrindEstimate {
27    pub attempts: f64,
28    pub attempts_label: String,
29    pub avg_secs: f64,
30    pub time_label: String,
31    pub difficulty: &'static str,
32    pub difficulty_bars: String,
33    pub risk: PatternRisk,
34    pub pattern_chars: usize,
35}
36
37pub fn effective_pattern_chars(pattern: &Pattern) -> usize {
38    let prefix = pattern
39        .prefix_match
40        .strip_prefix("0x")
41        .unwrap_or(&pattern.prefix_match);
42    prefix.len() + pattern.suffix_match.len()
43}
44
45pub fn format_attempts(n: f64) -> String {
46    if n >= 1_000_000_000_000.0 {
47        format!("{:.1}T", n / 1_000_000_000_000.0)
48    } else if n >= 1_000_000_000.0 {
49        format!("{:.1}B", n / 1_000_000_000.0)
50    } else if n >= 1_000_000.0 {
51        format!("{:.1}M", n / 1_000_000.0)
52    } else if n >= 1_000.0 {
53        format!("{:.1}K", n / 1_000.0)
54    } else {
55        format!("{:.0}", n)
56    }
57}
58
59pub fn format_duration(avg_secs: f64) -> String {
60    if avg_secs < 5.0 {
61        "a few seconds".to_string()
62    } else if avg_secs < 60.0 {
63        format!("~{:.0} seconds", avg_secs)
64    } else if avg_secs < 3_600.0 {
65        format!("~{:.0} minutes", avg_secs / 60.0)
66    } else if avg_secs < 86_400.0 {
67        format!("~{:.1} hours", avg_secs / 3_600.0)
68    } else if avg_secs < 86_400.0 * 30.0 {
69        format!("~{:.0} days", avg_secs / 86_400.0)
70    } else if avg_secs < 86_400.0 * 365.0 {
71        format!("~{:.0} months", avg_secs / (86_400.0 * 30.0))
72    } else if avg_secs < 86_400.0 * 365.0 * 100.0 {
73        format!("~{:.0} years", avg_secs / (86_400.0 * 365.0))
74    } else {
75        "centuries+ — not practical on one machine".to_string()
76    }
77}
78
79pub fn grind_estimate(attempts: f64, keys_per_sec: f64, pattern: &Pattern) -> GrindEstimate {
80    let keys_per_sec = keys_per_sec.max(1.0);
81    let avg_secs = attempts / keys_per_sec;
82    let pattern_chars = effective_pattern_chars(pattern);
83    let risk = PatternRisk::assess(attempts, pattern_chars, avg_secs);
84
85    let (difficulty, filled) = match attempts {
86        a if a < 100_000.0 => ("Easy", 2),
87        a if a < 10_000_000.0 => ("Quick", 3),
88        a if a < 1_000_000_000.0 => ("Medium", 5),
89        a if a < 1_000_000_000_000.0 => ("Hard", 7),
90        _ => ("Extreme", 10),
91    };
92
93    GrindEstimate {
94        attempts,
95        attempts_label: format_attempts(attempts),
96        avg_secs,
97        time_label: format_duration(avg_secs),
98        difficulty,
99        difficulty_bars: format!("{}{}", "█".repeat(filled), "░".repeat(10 - filled)),
100        risk,
101        pattern_chars,
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108    use crate::pattern::Pattern;
109
110    fn suffix_pattern(suffix: &str) -> Pattern {
111        Pattern {
112            prefix: String::new(),
113            suffix: suffix.to_string(),
114            prefix_match: String::new(),
115            suffix_match: suffix.to_ascii_lowercase(),
116            ignore_case: true,
117        }
118    }
119
120    #[test]
121    fn long_suffix_is_impractical() {
122        let p = suffix_pattern("akshaysingh");
123        let est = grind_estimate(1e16, 720_000.0, &p);
124        assert_eq!(est.risk, PatternRisk::Impractical);
125    }
126
127    #[test]
128    fn short_suffix_is_easy() {
129        let p = suffix_pattern("ab");
130        let est = grind_estimate(3_364.0, 720_000.0, &p);
131        assert_eq!(est.risk, PatternRisk::None);
132    }
133}