-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathripplegen.cpp
212 lines (182 loc) · 7 KB
/
ripplegen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
///////////////////////////////////////////////////////////////////////////////////
//
// ripplegen.cpp
//
// Copyright (c) 2013 Eric Lombrozo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// Portions of the source were taken from the ripple and bitcoin reference
// implementations. Their licensing terms and conditions must be respected.
// Please see accompanying LICENSE file for details.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "RippleAddress.h"
#include <iostream>
#include <stdint.h>
#include <boost/thread.hpp>
#include <openssl/rand.h>
#define UPDATE_ITERATIONS 1000
using namespace std;
boost::mutex mutex;
bool fDone = false;
uint64_t start_time;
uint64_t total_searched;
const char* ALPHABET = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";
char charHex(int iDigit)
{
return iDigit < 10 ? '0' + iDigit : 'A' - 10 + iDigit;
}
void getRand(unsigned char *buf, int num)
{
if (RAND_bytes(buf, num) != 1)
{
assert(false);
throw std::runtime_error("Entropy pool not seeded");
}
}
void LoopThread(unsigned int n, uint64_t eta50, string* ppattern,
string* pmaster_seed, string* pmaster_seed_hex, string* paccount_id)
{
RippleAddress naSeed;
RippleAddress naAccount;
string pattern = *ppattern;
string account_id;
uint128 key;
getRand(key.begin(), key.size());
uint64_t count = 0;
uint64_t last_count = 0;
do {
naSeed.setSeed(key);
RippleAddress naGenerator = createGeneratorPublic(naSeed);
naAccount.setAccountPublic(naGenerator.getAccountPublic(), 0);
account_id = naAccount.humanAccountID();
count++;
if (count % UPDATE_ITERATIONS == 0) {
boost::unique_lock<boost::mutex> lock(mutex);
total_searched += count - last_count;
last_count = count;
uint64_t nSecs = time(NULL) - start_time;
double speed = (1.0 * total_searched)/nSecs;
const char* unit = "seconds";
double eta50f = eta50/speed;
if (eta50f > 100) {
unit = "minutes";
eta50f /= 60;
if (eta50f > 100) {
unit = "hours";
eta50f /= 60;
if (eta50f > 48) {
unit = "days";
eta50f /= 24;
}
}
}
cout << "# Thread " << n << ": " << count << " seeds." << endl
<< "#" << endl
<< "# Total Speed: " << speed << " seeds/second" << endl
<< "# Total Searched: " << total_searched << endl
<< "# Total Time: " << nSecs << " seconds" << endl
<< "# ETA 50%: " << eta50f << " " << unit << endl
<< "# Last: " << account_id << endl
<< "# Pattern: " << pattern << endl
<< "#" << endl;
}
key++;
boost::this_thread::yield();
} while ((account_id.substr(0, pattern.size()) != pattern) && !fDone);
boost::unique_lock<boost::mutex> lock(mutex);
if (fDone) return;
fDone = true;
cout << "# *** Found by thread " << n << ". ***" << endl
<< "#" << endl;
*pmaster_seed = naSeed.humanSeed();
*pmaster_seed_hex = naSeed.getSeed().ToString();
*paccount_id = account_id;
}
// isPatternValid can be changed depending on encoding being used.
bool isPatternValid(const string& pattern, string& msg)
{
// Check for valid ripple account id.
// TODO: Implement it correctly
if (pattern.size() == 0) {
msg = "Pattern cannot be empty.";
return false;
}
if (pattern[0] != 'r') {
msg = "Pattern must begin with an 'r'.";
return false;
}
return true;
}
// eta50 = ceiling(1/log_2(n/(n-1))) where n = 58^length
// the minimum number of iterations such that there's at
// least a 50% chance of finding a match.
uint64_t getEta50(const string& pattern)
{
const uint64_t eta50[] = { 0, 0, 40, 2332, 135241, 7843997, 454951843, 26387206905ull,
1530458000460ull, 8876654026661ull, 5148460713546319ull,
298610721385686486ull, 17319421840369816160ull };
unsigned int len = pattern.size();
if (len > 12) return 0xffffffffffffffffull;
return eta50[len];
}
int main(int argc, char* argv[])
{
if (argc < 2) {
cout << "# Usage: " << argv[0] << " [pattern] [threads=cpus available]" << endl
<< "#" << endl;
return 0;
}
string pattern = argv[1];
string msg;
if (!isPatternValid(pattern, msg)) {
cout << "# " << msg << endl
<< "#" << endl;
return -2;
}
unsigned int cpus = boost::thread::hardware_concurrency();
unsigned int threads = (argc >= 3) ? strtoul(argv[2], NULL, 0) : cpus;
if (threads == 0) {
cout << "# You must run at least one thread." << endl
<< "#" << endl;
return -1;
}
cout << "# CPUs detected: " << cpus << endl
<< "#" << endl
<< "# Running " << threads << " thread" << (threads == 1 ? "" : "s") << "." << endl
<< "#" << endl
<< "# Generating seed for pattern \"" << pattern << "\"..." << endl
<< "#" << endl;
uint64_t eta50 = getEta50(pattern);
start_time = time(NULL);
string master_seed, master_seed_hex, account_id;
vector<boost::thread*> vpThreads;
for (unsigned int i = 0; i < threads; i++)
vpThreads.push_back(new boost::thread(LoopThread, i, eta50, &pattern, &master_seed, &master_seed_hex, &account_id));
for (unsigned int i = 0; i < threads; i++)
vpThreads[i]->join();
for (unsigned int i = 0; i < threads; i++)
delete vpThreads[i];
cout << "# master seed: " << master_seed << endl
<< "# master seed hex: " << master_seed_hex << endl
<< "# account id: " << account_id << endl
<< "#" << endl;
return 0;
}