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 pub fn as_str(self) -> &'static str {
25 match self {
26 PatternRisk::None => "none",
27 PatternRisk::Caution => "caution",
28 PatternRisk::Long => "long",
29 PatternRisk::Impractical => "impractical",
30 }
31 }
32}
33
34#[derive(Debug, Clone)]
35pub struct GrindEstimate {
36 pub attempts: f64,
37 pub attempts_label: String,
38 pub avg_secs: f64,
39 pub time_label: String,
40 pub difficulty: &'static str,
41 pub difficulty_bars: String,
42 pub risk: PatternRisk,
43 pub pattern_chars: usize,
44}
45
46pub fn effective_pattern_chars(pattern: &Pattern) -> usize {
47 let prefix = pattern
48 .prefix_match
49 .strip_prefix("0x")
50 .unwrap_or(&pattern.prefix_match);
51 prefix.len() + pattern.suffix_match.len()
52}
53
54pub fn format_attempts(n: f64) -> String {
55 if n >= 1_000_000_000_000.0 {
56 format!("{:.1}T", n / 1_000_000_000_000.0)
57 } else if n >= 1_000_000_000.0 {
58 format!("{:.1}B", n / 1_000_000_000.0)
59 } else if n >= 1_000_000.0 {
60 format!("{:.1}M", n / 1_000_000.0)
61 } else if n >= 1_000.0 {
62 format!("{:.1}K", n / 1_000.0)
63 } else {
64 format!("{:.0}", n)
65 }
66}
67
68pub fn format_duration(avg_secs: f64) -> String {
69 if avg_secs < 5.0 {
70 "a few seconds".to_string()
71 } else if avg_secs < 60.0 {
72 format!("~{:.0} seconds", avg_secs)
73 } else if avg_secs < 3_600.0 {
74 format!("~{:.0} minutes", avg_secs / 60.0)
75 } else if avg_secs < 86_400.0 {
76 format!("~{:.1} hours", avg_secs / 3_600.0)
77 } else if avg_secs < 86_400.0 * 30.0 {
78 format!("~{:.0} days", avg_secs / 86_400.0)
79 } else if avg_secs < 86_400.0 * 365.0 {
80 format!("~{:.0} months", avg_secs / (86_400.0 * 30.0))
81 } else if avg_secs < 86_400.0 * 365.0 * 100.0 {
82 format!("~{:.0} years", avg_secs / (86_400.0 * 365.0))
83 } else {
84 "centuries+ — not practical on one machine".to_string()
85 }
86}
87
88pub fn default_keys_per_sec(chain_id: &str) -> f64 {
90 match chain_id {
91 "evm" | "eth" | "ethereum" | "aptos" | "apt" | "sui" | "near" => 35_000.0,
92 _ => 80_000.0,
93 }
94}
95
96pub fn grind_estimate(attempts: f64, keys_per_sec: f64, pattern: &Pattern) -> GrindEstimate {
97 let keys_per_sec = keys_per_sec.max(1.0);
98 let avg_secs = attempts / keys_per_sec;
99 let pattern_chars = effective_pattern_chars(pattern);
100 let risk = PatternRisk::assess(attempts, pattern_chars, avg_secs);
101
102 let (difficulty, filled) = match attempts {
103 a if a < 100_000.0 => ("Easy", 2),
104 a if a < 10_000_000.0 => ("Quick", 3),
105 a if a < 1_000_000_000.0 => ("Medium", 5),
106 a if a < 1_000_000_000_000.0 => ("Hard", 7),
107 _ => ("Extreme", 10),
108 };
109
110 GrindEstimate {
111 attempts,
112 attempts_label: format_attempts(attempts),
113 avg_secs,
114 time_label: format_duration(avg_secs),
115 difficulty,
116 difficulty_bars: format!("{}{}", "█".repeat(filled), "░".repeat(10 - filled)),
117 risk,
118 pattern_chars,
119 }
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125 use crate::pattern::Pattern;
126
127 fn suffix_pattern(suffix: &str) -> Pattern {
128 Pattern {
129 prefix: String::new(),
130 suffix: suffix.to_string(),
131 prefix_match: String::new(),
132 suffix_match: suffix.to_ascii_lowercase(),
133 ignore_case: true,
134 }
135 }
136
137 #[test]
138 fn long_suffix_is_impractical() {
139 let p = suffix_pattern("akshaysingh");
140 let est = grind_estimate(1e16, 720_000.0, &p);
141 assert_eq!(est.risk, PatternRisk::Impractical);
142 }
143
144 #[test]
145 fn short_suffix_is_easy() {
146 let p = suffix_pattern("ab");
147 let est = grind_estimate(3_364.0, 720_000.0, &p);
148 assert_eq!(est.risk, PatternRisk::None);
149 }
150}