Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exact match mode with multiple keys output #42

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 89 additions & 13 deletions Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,41 @@ static std::string toHex(const uint8_t * const s, const size_t len) {
return r;
}

static void printResult(cl_ulong4 seed, cl_ulong round, result r, cl_uchar score, const std::chrono::time_point<std::chrono::steady_clock> & timeStart, const Mode & mode) {
// Time delta
const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - timeStart).count();

// Format private key
static std::string formatPrivateKey(cl_ulong4 seed, cl_ulong round, uint foundId) {
cl_ulong carry = 0;
cl_ulong4 seedRes;

seedRes.s[0] = seed.s[0] + round; carry = seedRes.s[0] < round;
seedRes.s[1] = seed.s[1] + carry; carry = !seedRes.s[1];
seedRes.s[2] = seed.s[2] + carry; carry = !seedRes.s[2];
seedRes.s[3] = seed.s[3] + carry + r.foundId;
seedRes.s[3] = seed.s[3] + carry + foundId;

std::ostringstream ss;
ss << std::hex << std::setfill('0');
ss << std::setw(16) << seedRes.s[3] << std::setw(16) << seedRes.s[2] << std::setw(16) << seedRes.s[1] << std::setw(16) << seedRes.s[0];
const std::string strPrivate = ss.str();

return ss.str();
}

static void printResult(cl_ulong4 seed, cl_ulong round, result r, cl_uchar score, const std::chrono::time_point<std::chrono::steady_clock> & timeStart, const Mode & mode) {
// Time delta
const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - timeStart).count();

// Format private key
const std::string strPrivate = formatPrivateKey(seed, round, r.foundId);

// Format public key
const std::string strPublic = toHex(r.foundHash, 20);

// Print
const std::string strVT100ClearLine = "\33[2K\r";
std::cout << strVT100ClearLine << " Time: " << std::setw(5) << seconds << "s Score: " << std::setw(2) << (int) score << " Private: 0x" << strPrivate << ' ';
std::cout << strVT100ClearLine << " Time: " << std::setw(5) << seconds << "s";

if (mode.name != "exact") {
std::cout << " Score: " << std::setw(2) << (int) score;
}

std::cout << " Private: 0x" << strPrivate << ' ';

std::cout << mode.transformName();
std::cout << ": 0x" << strPublic << std::endl;
Expand Down Expand Up @@ -140,8 +151,9 @@ cl_command_queue Dispatcher::Device::createQueue(cl_context & clContext, cl_devi
}

cl_kernel Dispatcher::Device::createKernel(cl_program & clProgram, const std::string s) {
cl_kernel ret = clCreateKernel(clProgram, s.c_str(), NULL);
return ret == NULL ? throw std::runtime_error("failed to create kernel \"" + s + "\"") : ret;
cl_int errorCode;
cl_kernel ret = clCreateKernel(clProgram, s.c_str(), &errorCode);
return ret == NULL ? throw std::runtime_error("failed to create kernel \"" + s + "\"" + ", code: " + toString(errorCode)) : ret;
}

cl_ulong4 Dispatcher::Device::createSeed() {
Expand Down Expand Up @@ -192,7 +204,8 @@ Dispatcher::Device::Device(Dispatcher & parent, cl_context & clContext, cl_progr
m_round(0),
m_speed(PROFANITY_SPEEDSAMPLES),
m_sizeInitialized(0),
m_eventFinished(NULL)
m_eventFinished(NULL),
m_lastCursorIndex(1)
{

}
Expand Down Expand Up @@ -235,6 +248,21 @@ void Dispatcher::run() {
const auto timeInitialization = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - timeStart).count();
std::cout << "Initialization time: " << timeInitialization << " seconds" << std::endl;

if (m_mode.name == "exact" || m_mode.name == "matching") {
auto mask = toHex(m_mode.data1, 20); // FF0...
auto pattern = toHex(m_mode.data2, 20); // AE53424...

for (size_t i = 0; i < mask.size(); i++) {
if (mask[i] == '0') {
pattern[i] = '_';
}
}

std::cout << "Hex string mask: " << pattern << std::endl;
}

std::cout << std::endl;

m_quit = false;
m_countRunning = m_vDevices.size();

Expand Down Expand Up @@ -353,7 +381,7 @@ void Dispatcher::initContinue(Device & d) {
// the commands are not required to begin execution until the queue is flushed. In standard usage, blocking enqueue calls serve this role by implicitly
// flushing the queue. Since blocking calls are not permitted in callbacks, those callbacks that enqueue commands on a command queue should either call
// clFlush on the queue before returning or arrange for clFlush to be called later on another thread.
clFlush(d.m_clQueue);
clFlush(d.m_clQueue);

std::lock_guard<std::mutex> lock(m_mutex);
d.m_sizeInitialized += sizeRun;
Expand Down Expand Up @@ -425,15 +453,63 @@ void Dispatcher::dispatch(Device & d) {
// We're actually not allowed to call clFinish here because this function is ultimately asynchronously called by OpenCL.
// However, this happens to work on my computer and it's not really intended for release, just something to aid me in
// optimizations.
clFinish(d.m_clQueue);
clFinish(d.m_clQueue);
std::cout << "Timing: profanity_inverse = " << getKernelExecutionTimeMicros(eventInverse) << "us, profanity_iterate = " << getKernelExecutionTimeMicros(eventIterate) << "us" << std::endl;
#endif

const auto res = clSetEventCallback(event, CL_COMPLETE, staticCallback, &d);
OpenCLException::throwIfError("failed to set custom callback", res);
}


void Dispatcher::handleExactResult(Device & d) {
uint cursorLocation = d.m_memResult[0].found;
uint currentCursorIndex = (cursorLocation % PROFANITY_MAX_SCORE) + 1;

// 3 cases:

// d.m_lastCursorIndex = currentCursorIndex -> no new results
if (currentCursorIndex == d.m_lastCursorIndex) {
return;
}

std::vector<size_t> range;

if (currentCursorIndex > d.m_lastCursorIndex) {
// d.m_lastCursorIndex > currentCursorIndex -> new results, read from last to current
for (size_t resultIndex = d.m_lastCursorIndex; resultIndex < currentCursorIndex; ++resultIndex) {
range.push_back(resultIndex);
}
} else {
// d.m_lastCursorIndex < currentCursorIndex -> buffer rollover:

// read from last to PROFANITY_MAX_SCORE
for (size_t resultIndex = d.m_lastCursorIndex; resultIndex < PROFANITY_MAX_SCORE; ++resultIndex) {
range.push_back(resultIndex);
}

// read from 0 to currentCursorIndex
for (size_t resultIndex = 0; resultIndex < currentCursorIndex; ++resultIndex) {
range.push_back(resultIndex);
}
}

std::lock_guard<std::mutex> lock(m_mutex);

for (auto resultIndex : range) {
printResult(d.m_clSeed, d.m_round, d.m_memResult[resultIndex], 0, timeStart, m_mode);
}

d.m_lastCursorIndex = currentCursorIndex;
}

void Dispatcher::handleResult(Device & d) {
if (m_mode.name == "exact") {
handleExactResult(d);

return;
}

for (auto i = PROFANITY_MAX_SCORE; i > m_clScoreMax; --i) {
result & r = d.m_memResult[i];

Expand Down
3 changes: 3 additions & 0 deletions Dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class Dispatcher {
// Initialization
size_t m_sizeInitialized;
cl_event m_eventFinished;

size_t m_lastCursorIndex;
};

public:
Expand All @@ -96,6 +98,7 @@ class Dispatcher {
void enqueueKernelDevice(Device & d, cl_kernel & clKernel, size_t worksizeGlobal, cl_event * pEvent);

void handleResult(Device & d);
void handleExactResult(Device & d);
void randomizeSeed(Device & d);

void onEvent(cl_event event, cl_int status, Device & d);
Expand Down
26 changes: 25 additions & 1 deletion Mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ static std::string::size_type hexValue(char c) {
return ret;
}

//
// Pattern:
// 1337________________________________C0DE
// 10000______________________________00001
//
// | # | Chr | iHi | iLo | mHi | mLo | d1[i] | d2[i] |
// |-----------|------|------|------|------|-------|-------|
// | 0 | d d | 13 | 13 | 0xF0 | 0x0F | 0xFF | 0xDD |
// | 1 | 0 b | 0 | 11 | 0xF0 | 0x0F | 0xFF | 0x0B |
// | 2 | b _ | 11 | npos | 0xF0 | 0x00 | 0xF0 | 0x00 |
// | 3-9 | _ _ | npos | npos | 0x00 | 0x00 | 0x00 | 0x00 |
// | 10 | d d | 13 | 13 | 0xF0 | 0x0F | 0xFF | 0xDD |
// | 11 | 0 b | 0 | 11 | 0xF0 | 0x0F | 0xFF | 0x0B |

Mode Mode::matching(const std::string strHex) {
Mode r;
r.name = "matching";
Expand All @@ -46,7 +60,7 @@ Mode Mode::matching(const std::string strHex) {
std::fill( r.data2, r.data2 + sizeof(r.data2), cl_uchar(0) );

auto index = 0;

for( size_t i = 0; i < strHex.size(); i += 2 ) {
const auto indexHi = hexValueNoException(strHex[i]);
const auto indexLo = i + 1 < strHex.size() ? hexValueNoException(strHex[i+1]) : std::string::npos;
Expand All @@ -66,6 +80,16 @@ Mode Mode::matching(const std::string strHex) {
return r;
}

Mode Mode::exact(const std::string strHex) {
Mode r = matching(strHex);

r.name = "exact";
r.kernel = "profanity_exact_match";

return r;
}


Mode Mode::leading(const char charLeading) {

Mode r;
Expand Down
1 change: 1 addition & 0 deletions Mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Mode {

public:
static Mode matching(const std::string strHex);
static Mode exact(const std::string strHex);
static Mode range(const cl_uchar min, const cl_uchar max);
static Mode leading(const char charLeading);
static Mode leadingRange(const cl_uchar min, const cl_uchar max);
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ usage: ./profanity2 [OPTIONS]

Modes with arguments:
--leading <single hex> Score on hashes leading with given hex character.
--matching <hex string> Score on hashes matching given hex string.
--matching <hex mask> Score on hashes matching given hex string.
-e, --exact <hex mask> Score on all hashes exactly matching given mask.

Advanced modes:
--contract Instead of account address, score the contract
Expand Down Expand Up @@ -88,8 +89,9 @@ usage: ./profanity2 [OPTIONS]

Examples:
./profanity2 --leading f -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --matching dead -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --matching badXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbad -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --matching beef -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --matching 1337________________________________C0DE -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --exact 1337________________________________C0DE -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --leading-range -m 0 -M 1 -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --leading-range -m 10 -M 12 -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --range -m 0 -M 1 -z HEX_PUBLIC_KEY_128_CHARS_LONG
Expand Down
9 changes: 6 additions & 3 deletions help.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ usage: ./profanity2 [OPTIONS]

Modes with arguments:
--leading <single hex> Score on hashes leading with given hex character.
--matching <hex string> Score on hashes matching given hex string.
--matching <hex mask> Score on hashes matching given hex string.
-e, --exact <hex mask> Score on all hashes exactly matching given mask.

Advanced modes:
--contract Instead of account address, score the contract
Expand Down Expand Up @@ -49,12 +50,14 @@ usage: ./profanity2 [OPTIONS]

Examples:
./profanity2 --leading f -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --matching dead -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --matching badXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbad -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --matching beef -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --matching 1337________________________________C0DE -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --exact 1337________________________________C0DE -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --leading-range -m 0 -M 1 -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --leading-range -m 10 -M 12 -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --range -m 0 -M 1 -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --contract --leading 0 -z HEX_PUBLIC_KEY_128_CHARS_LONG
./profanity2 --contract --zero-bytes -z HEX_PUBLIC_KEY_128_CHARS_LONG

About:
profanity2 is a vanity address generator for Ethereum that utilizes
Expand Down
Loading