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 {
14 const DAY: f64 = 86_400.0;
15 if attempts >= 1e15 || avg_secs >= DAY * 365.0 * 10.0 {
16 PatternRisk::Impractical
17 } else if avg_secs >= DAY * 7.0 {
18 PatternRisk::Long
19 } else if avg_secs >= 3_600.0 {
20 PatternRisk::Caution
21 } else {
22 PatternRisk::None
23 }
24 }
25
26 pub fn as_str(self) -> &'static str {
27 match self {
28 PatternRisk::None => "none",
29 PatternRisk::Caution => "caution",
30 PatternRisk::Long => "long",
31 PatternRisk::Impractical => "impractical",
32 }
33 }
34}
35
36#[derive(Debug, Clone)]
37pub struct GrindEstimate {
38 pub attempts: f64,
39 pub attempts_label: String,
40 pub avg_secs: f64,
41 pub time_label: String,
42 pub difficulty: &'static str,
43 pub difficulty_bars: String,
44 pub risk: PatternRisk,
45 pub pattern_chars: usize,
46}
47
48pub fn effective_pattern_chars(pattern: &Pattern) -> usize {
49 pattern.effective_literal_chars()
50}
51
52pub fn format_attempts(n: f64) -> String {
53 if n >= 1_000_000_000_000.0 {
54 format!("{:.1}T", n / 1_000_000_000_000.0)
55 } else if n >= 1_000_000_000.0 {
56 format!("{:.1}B", n / 1_000_000_000.0)
57 } else if n >= 1_000_000.0 {
58 format!("{:.1}M", n / 1_000_000.0)
59 } else if n >= 1_000.0 {
60 format!("{:.1}K", n / 1_000.0)
61 } else {
62 format!("{:.0}", n)
63 }
64}
65
66pub fn format_duration(avg_secs: f64) -> String {
67 if avg_secs < 5.0 {
68 "a few seconds".to_string()
69 } else if avg_secs < 60.0 {
70 format!("~{:.0} seconds", avg_secs)
71 } else if avg_secs < 3_600.0 {
72 format!("~{:.0} minutes", avg_secs / 60.0)
73 } else if avg_secs < 86_400.0 {
74 format!("~{:.1} hours", avg_secs / 3_600.0)
75 } else if avg_secs < 86_400.0 * 30.0 {
76 format!("~{:.0} days", avg_secs / 86_400.0)
77 } else if avg_secs < 86_400.0 * 365.0 {
78 format!("~{:.0} months", avg_secs / (86_400.0 * 30.0))
79 } else if avg_secs < 86_400.0 * 365.0 * 100.0 {
80 format!("~{:.0} years", avg_secs / (86_400.0 * 365.0))
81 } else {
82 "centuries+ — not practical on one machine".to_string()
83 }
84}
85
86pub fn default_keys_per_sec(chain_id: &str) -> f64 {
88 match chain_id {
89 "evm" | "eth" | "ethereum" | "evm-contract" | "trx-contract" | "create2" | "aptos"
90 | "apt" | "sui" | "near" => 35_000.0,
91 _ => 80_000.0,
92 }
93}
94
95pub fn pattern_guide(estimate: &GrindEstimate, per_char: f64) -> (&'static str, String) {
98 const DAY: f64 = 86_400.0;
99 let verdict = match estimate.avg_secs {
100 s if s < 60.0 => "✓ Great — about a minute or less",
101 s if s < 3_600.0 => "✓ OK — minutes",
102 s if s < DAY => "⚠ Getting long — hours",
103 s if s < DAY * 30.0 => "⚠ Very long — days",
104 _ => "⛔ Too long for one machine — shorten the pattern",
105 };
106 let per_char = per_char.round();
107 let shorter = format_duration(estimate.avg_secs / per_char);
108 let rule = if estimate.pattern_chars > 1 {
109 format!("Each character ≈ {per_char:.0}× longer · one fewer ≈ {shorter}")
110 } else {
111 format!("Each character ≈ {per_char:.0}× longer")
112 };
113 (verdict, rule)
114}
115
116pub fn grind_estimate(attempts: f64, keys_per_sec: f64, pattern: &Pattern) -> GrindEstimate {
117 let keys_per_sec = keys_per_sec.max(1.0);
118 let avg_secs = attempts / keys_per_sec;
119 let pattern_chars = effective_pattern_chars(pattern);
120 let risk = PatternRisk::assess(attempts, pattern_chars, avg_secs);
121
122 let (difficulty, filled) = match attempts {
123 a if a < 100_000.0 => ("Easy", 2),
124 a if a < 10_000_000.0 => ("Quick", 3),
125 a if a < 1_000_000_000.0 => ("Medium", 5),
126 a if a < 1_000_000_000_000.0 => ("Hard", 7),
127 _ => ("Extreme", 10),
128 };
129
130 GrindEstimate {
131 attempts,
132 attempts_label: format_attempts(attempts),
133 avg_secs,
134 time_label: format_duration(avg_secs),
135 difficulty,
136 difficulty_bars: format!("{}{}", "█".repeat(filled), "░".repeat(10 - filled)),
137 risk,
138 pattern_chars,
139 }
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145 use crate::pattern::Pattern;
146
147 fn suffix_pattern(suffix: &str) -> Pattern {
148 Pattern {
149 prefix: String::new(),
150 suffix: suffix.to_string(),
151 contains: String::new(),
152 prefix_match: String::new(),
153 suffix_match: suffix.to_ascii_lowercase(),
154 contains_match: String::new(),
155 ignore_case: true,
156 fixed_prefix_len: 0,
157 }
158 }
159
160 #[test]
161 fn long_suffix_is_impractical() {
162 let p = suffix_pattern("akshaysingh");
163 let est = grind_estimate(1e16, 720_000.0, &p);
164 assert_eq!(est.risk, PatternRisk::Impractical);
165 }
166
167 #[test]
168 fn short_suffix_is_easy() {
169 let p = suffix_pattern("ab");
170 let est = grind_estimate(3_364.0, 720_000.0, &p);
171 assert_eq!(est.risk, PatternRisk::None);
172 }
173}