-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.cu
372 lines (315 loc) · 12.1 KB
/
program.cu
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <iomanip>
#include <cstring>
#define __GMP_DECLSPEC_XX
#include <gmp.h>
extern "C" {
#define HAVE_CONFIG_H
#include "libsecp256k1-config.h"
#include "secp256k1.c"
#include "ecmult_big_impl.h"
#include "secp256k1_batch_impl.h"
}
#include <cuda.h>
#include <cuda_runtime.h>
#include <device_functions.h>
#include <device_launch_parameters.h>
#include "hash.cuh"
#include "bloom.cuh"
struct gpu_t {
int device;
int threadsPerBlock;
int blocksPerGrid;
};
gpu_t getGPUConfiguration(int deviceId) {
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, deviceId);
// Determine your configuration parameters based on device properties
int maxThreadsPerBlock = deviceProp.maxThreadsPerBlock;
int maxBlocksPerGrid = deviceProp.multiProcessorCount;
// Adjust these values based on your specific requirements
int threadsPerBlock = 1024; // Choose an appropriate number based on your kernel requirements
int blocksPerGrid = maxBlocksPerGrid;
// Ensure the configuration adheres to device limits
threadsPerBlock = std::min(threadsPerBlock, maxThreadsPerBlock);
blocksPerGrid = std::min(blocksPerGrid, maxBlocksPerGrid);
gpu_t gpuConfig;
gpuConfig.device = deviceId;
gpuConfig.threadsPerBlock = threadsPerBlock;
gpuConfig.blocksPerGrid = blocksPerGrid;
return gpuConfig;
}
__device__ int KeyFound;
__global__ void pub_check_hash(const unsigned char* bloom, uint8_t *ppubKey , uint32_t *pdigest_c)
{
unsigned id = blockDim.x * blockIdx.x + threadIdx.x;
if (KeyFound != -1)
return;
hash160(ppubKey + (id * 32), 33, pdigest_c + (id * 5));
if (bloom_chk_hash160(bloom, pdigest_c)) {
// Only one thread should set KeyFound to 1.
atomicExch(&KeyFound, 1);
} else {
atomicMax(&KeyFound, id);
}
}
void increment_private_key(unsigned char* privkey) {
privkey[30] - 1;
for (int i = 31; i >= 0; i--) {
if (privkey[i] < 0xFF) {
privkey[i]++;
break;
} else {
privkey[i] = 0x00;
}
}
}
void saveKeyToFile(const unsigned char* privateKey, const uint8_t* pubKey, const uint32_t* hash) {
// Save to a file (adjust file handling as needed)
std::ofstream outFile("matching_key.txt");
if (outFile.is_open()) {
outFile << "Private Key: ";
for (int i = 0; i < 32; ++i) {
outFile << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(privateKey[i]);
}
outFile << std::dec << std::endl;
outFile << "Public Key: ";
for (int i = 0; i < 33; ++i) { // Assuming 33 bytes for compressed public key
outFile << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(pubKey[i]);
}
outFile << std::dec << std::endl;
outFile << "Hash: ";
for (int i = 0; i < 5; ++i) {
outFile << std::hex << std::setw(8) << std::setfill('0') << hash[i];
}
outFile << std::dec << std::endl;
outFile.close();
} else {
std::cerr << "Error: Failed to open output file." << std::endl;
// Handle the error as needed
}
}
void printHash(const uint32_t* hash, int length) {
printf("Hash: ");
const uint8_t* hashBytes = reinterpret_cast<const uint8_t*>(hash);
for (int i = 0; i < length * sizeof(uint32_t); ++i) {
printf("%02x", hashBytes[i]);
}
printf("\n");
}
void printPrivateKey(const unsigned char* privkey, size_t length) {
if (privkey == nullptr) {
std::cout << "Private key is null." << std::endl;
return;
}
std::cout << "Private Key: ";
for (size_t i = 0; i < length; i++) {
std::cout << std::setfill('0') << std::setw(2) << std::hex << (int)privkey[i];
}
std::cout << std::dec << std::endl;
}
void printPubKey(const unsigned char* privkey, size_t length) {
if (privkey == nullptr) {
std::cout << "Private key is null." << std::endl;
return;
}
std::cout << "Pubkey Key: ";
for (size_t i = 0; i < length; i++) {
std::cout << std::setfill('0') << std::setw(2) << std::hex << (int)privkey[i];
}
std::cout << std::dec << std::endl;
}
// Global variables
std::vector<std::string> privateKeys;
std::atomic<int> count(0);
std::mutex mutex;
std::condition_variable cv;
bool finished = false;
std::atomic<bool> keyFound(false);
uint8_t pubkeyFound[33];
unsigned char* bloom_data = nullptr;
void initializeBloomData() {
std::lock_guard<std::mutex> lock(mutex);
// Load the bloom data from a file or any other source
std::ifstream bloomFile("bloom_filter.bin", std::ios::binary);
if (bloomFile) {
bloomFile.seekg(0, std::ios::end);
size_t bloomSize = bloomFile.tellg();
bloomFile.seekg(0, std::ios::beg);
unsigned char* bloomData = new unsigned char[bloomSize];
bloomFile.read(reinterpret_cast<char*>(bloomData), bloomSize);
// Allocate memory on the CUDA device
cudaMalloc((void**)&bloom_data, bloomSize);
// Copy the data from host to device
cudaMemcpy(bloom_data, bloomData, bloomSize, cudaMemcpyHostToDevice);
delete[] bloomData;
bloomFile.close();
} else {
std::cerr << "Error: failed to open bloom filter file." << std::endl;
// Handle the error as needed
}
}
// Function to free CUDA memory for bloom_data
void cleanupBloomData() {
cudaFree(bloom_data);
}
void searchForMatchingKey(const secp256k1_context* ctx, const secp256k1_ecmult_big_context* bmul, gpu_t gpu) {
cudaSetDevice(gpu.device);
int BLOCK_SIZE = gpu.threadsPerBlock * gpu.blocksPerGrid;
uint8_t* pubKey = new uint8_t[33];
uint32_t* hash = new uint32_t[5];
uint8_t* d_publicKey;
uint32_t* d_digest_c;
cudaError_t cudaStatus = cudaMalloc((void**)&d_publicKey, BLOCK_SIZE * (sizeof(uint8_t) * 33));
if (cudaStatus != cudaSuccess) {
std::cerr << "cudaMalloc failed: " << cudaGetErrorString(cudaStatus) << std::endl;
// Handle the error as needed
}
cudaError_t cudaStatus1 = cudaMalloc((void**)&d_digest_c, BLOCK_SIZE * (sizeof(uint32_t) * 5));
if (cudaStatus1 != cudaSuccess) {
std::cerr << "cudaMalloc failed: " << cudaGetErrorString(cudaStatus) << std::endl;
// Handle the error as needed
}
dim3 blockdim(gpu.threadsPerBlock, 1, 1);
dim3 griddim(gpu.blocksPerGrid, 1, 1);
std::cout << "GPU Configuration ..";
int localCount = 0;
while (true) {
std::string privateKey;
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [] { return !privateKeys.empty() || finished; });
if (privateKeys.empty() && finished) {
break;
}
privateKey = privateKeys.back();
privateKeys.pop_back();
}
std::vector<unsigned char> privateKeyData(32, 0); // Initialize a 32-byte array
// Convert the hexadecimal string to unsigned char array
for (int i = 0; i < 32; ++i) {
std::string byteStr = privateKey.substr(2 * i, 2);
privateKeyData[i] = static_cast<unsigned char>(std::stoi(byteStr, nullptr, 16));
}
unsigned char* privateKeyBytes = privateKeyData.data();
int nKeyFound = -1;
cudaMemcpyToSymbol((const void *)&KeyFound, &nKeyFound, sizeof(int), 0, cudaMemcpyHostToDevice);
for (int i = 0; i < 0x10000; ++i) {
secp256k1_ec_pubkey_create_serialized(ctx, bmul, pubKey, privateKeyBytes, 1);
cudaMemcpy(d_publicKey, pubKey, BLOCK_SIZE * (sizeof(uint8_t) * 33) , cudaMemcpyHostToDevice);
pub_check_hash<<<griddim, blockdim>>>(bloom_data, d_publicKey, d_digest_c);
cudaDeviceSynchronize();
cudaMemcpyFromSymbol(&nKeyFound, (const void *)&KeyFound, sizeof(int), 0, cudaMemcpyDeviceToHost);
if (nKeyFound != -1) {
std::cout << "Matching key found!" << std::endl;
printPrivateKey(privateKeyBytes, 32);
printPubKey(pubKey, 32);
cudaMemcpy(hash, d_digest_c, sizeof(uint32_t) * 5, cudaMemcpyDeviceToHost);
printHash(hash, 5);
saveKeyToFile(privateKeyBytes, pubKey, hash);
cudaFree(d_publicKey);
cudaFree(d_digest_c);
keyFound = true;
// Notify other threads that this thread has finished
cv.notify_all();
break;
}
increment_private_key(privateKeyBytes);
}
cudaFree(d_publicKey);
delete[] pubKey;
++localCount;
if (localCount % 1000 == 0) {
std::unique_lock<std::mutex> lock(mutex);
std::cout << "Thread " << std::this_thread::get_id() << ": Progress: " << count << " checks" << std::endl;
printPrivateKey(privateKeyBytes, 32);
}
}
std::unique_lock<std::mutex> lock(mutex);
count -= localCount;
cv.notify_all();
cudaFree(d_publicKey);
cudaFree(d_digest_c);
}
int main() {
int ngpu;
cudaGetDeviceCount(&ngpu);
if (ngpu == 0) {
std::cerr << "No CUDA devices found." << std::endl;
return 1;
}
// Allocate an array of GPU configurations
gpu_t* gpuConfigs = new gpu_t[ngpu];
// Set up GPU configurations for each device
for (int i = 0; i < ngpu; ++i) {
gpuConfigs[i] = getGPUConfiguration(i);
}
dim3* griddims = new dim3[ngpu];
dim3* blockdims = new dim3[ngpu];
for (int i = 0; i < ngpu; ++i) {
griddims[i].x = gpuConfigs[i].blocksPerGrid;
griddims[i].y = griddims[i].z = 1;
blockdims[i].x = gpuConfigs[i].threadsPerBlock;
blockdims[i].y = blockdims[i].z = 1;
}
// Example usage: Print GPU configurations
for (int i = 0; i < ngpu; ++i) {
gpu_t gpu = getGPUConfiguration(gpuConfigs[i].device);
std::cout << "GPU " << i << " Configuration:" << std::endl;
std::cout << " Device ID: " << gpu.device << std::endl;
std::cout << " Threads per Block: " << gpu.threadsPerBlock << std::endl;
std::cout << " Blocks per Grid: " << gpu.blocksPerGrid << std::endl;
std::cout << " Grid Dimensions: " << griddims[i].x << " x " << griddims[i].y << " x " << griddims[i].z << std::endl;
std::cout << " Block Dimensions: " << blockdims[i].x << " x " << blockdims[i].y << " x " << blockdims[i].z << std::endl;
std::cout << std::endl;
}
// Get the total number of CPU cores
const int numCores = std::thread::hardware_concurrency();
// Set the number of threads to the total number of CPU cores multiplied by 2
const int numThreads = numCores * 2;
// Start the worker threads
std::vector<std::thread> threads;
initializeBloomData();
std::cout << " Bloom initialize..";
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
unsigned int bmul_size = 24;
secp256k1_ecmult_big_context* bmul = secp256k1_ecmult_big_create(ctx, bmul_size);
std::cout << " secp256k1_ecmult_big_context done!";
for (int i = 0; i < numThreads; ++i) {
threads.emplace_back(searchForMatchingKey, ctx, bmul, gpuConfigs[0]);
}
// Read private keys from standard input
std::string privateKey;
while (std::getline(std::cin, privateKey)) {
{
std::lock_guard<std::mutex> lock(mutex);
privateKeys.push_back(privateKey);
}
++count;
cv.notify_one();
}
{
std::lock_guard<std::mutex> lock(mutex);
finished = true;
}
cv.notify_all();
// Wait for all threads to finish
for (auto& thread : threads) {
thread.join();
}
cleanupBloomData();
// Free resources used by the secp256k1 library
secp256k1_context_destroy(ctx);
secp256k1_ecmult_big_destroy(bmul);
delete[] gpuConfigs;
delete[] griddims;
delete[] blockdims;
return 0;
}