yggm/sources/main.cu

122 lines
4.4 KiB
Plaintext
Raw Normal View History

2025-03-15 04:42:31 +05:00
#include <stdio.h>
2025-03-13 04:09:27 +05:00
#include <cuda_runtime.h>
#include <curand_kernel.h>
2025-03-13 19:48:51 +05:00
#include <sha512.cuh>
#include <ed25519.cuh>
#include <edsign.cuh>
2025-03-14 22:03:46 +05:00
#include <string.cuh>
2025-03-15 01:38:00 +05:00
#include <keymanip.cuh>
2025-03-15 00:13:50 +05:00
__device__ unsigned d_high = 0x10;
2025-03-15 01:38:00 +05:00
__device__ int parameters(const char* arg) noexcept {
2025-03-14 22:03:46 +05:00
if ((cstring_find(arg, "--altitude") == 0 && cstring_length(arg) == 10) || (cstring_find(arg, "-a") == 0 && cstring_length(arg) == 2)) {
return 777;
}
2025-03-15 01:38:00 +05:00
int space_index = cstring_find(arg, " ");
if (space_index == -1) return 0;
const int substr_start = space_index + 1;
char sub_arg[256];
extract_substring(arg, substr_start, sub_arg, 256);
if (cstring_find(arg, "--altitude") != -1 || cstring_find(arg, "-a") != -1) {
unsigned tmp_high;
if (cstring_to_ull(sub_arg, &tmp_high) != 0)
return 1;
d_high = tmp_high;
}
2025-03-14 22:03:46 +05:00
return 0;
}
__global__ void args(char** argv, int argc, int* result) {
int err = 0;
for (int x = 1; x < argc; x++) {
int res = parameters(argv[x]);
if (res == 777) {
if (++x >= argc) {
err = 776;
break;
}
char combined[512];
concat(argv[x - 1], argv[x], combined, 512);
if (parameters(combined) != 0) {
err = res;
break;
}
}
}
result[0] = err;
}
2025-03-15 01:38:00 +05:00
__device__ __forceinline__ unsigned char zeroCounter(unsigned int x) noexcept {
2025-03-14 22:03:46 +05:00
return x ? static_cast<unsigned char>(__clz(x)) : 32;
2025-03-13 04:09:27 +05:00
}
2025-03-15 01:38:00 +05:00
__device__ __forceinline__ unsigned char getZeros(const unsigned char* v) noexcept {
2025-03-13 04:09:27 +05:00
unsigned char leadZeros = 0;
2025-03-14 22:03:46 +05:00
#pragma unroll
2025-03-13 04:09:27 +05:00
for (int i = 0; i < 32; i += 4) {
2025-03-13 19:43:54 +05:00
unsigned word = (static_cast<unsigned>(v[i]) << 24) | (static_cast<unsigned>(v[i + 1]) << 16) | (static_cast<unsigned>(v[i + 2]) << 8) | (static_cast<unsigned>(v[i + 3]));
2025-03-14 22:03:46 +05:00
if (word == 0)
2025-03-13 04:09:27 +05:00
leadZeros += 32;
2025-03-14 22:03:46 +05:00
else {
2025-03-13 04:09:27 +05:00
leadZeros += zeroCounter(word);
break;
}
}
return leadZeros;
}
2025-03-15 01:38:00 +05:00
__global__ void initRand(curandState* rs) {
2025-03-13 04:09:27 +05:00
int id = blockIdx.x * blockDim.x + threadIdx.x;
2025-03-15 01:38:00 +05:00
curand_init(clock64() + id * 7919ULL, id, 0, &rs[id]);
#pragma unroll 10
for (int i = 0; i < 10; i++) {
curand(&rs[id]);
}
2025-03-13 04:09:27 +05:00
}
2025-03-15 02:32:15 +05:00
__device__ __forceinline__ void rmbytes(unsigned char* buf, curandState* state) {
2025-03-15 00:13:50 +05:00
#pragma unroll 32
2025-03-15 02:32:15 +05:00
for (unsigned long i = 0; i < 32; i++) {
buf[i] = curand(state) & 0xFF;
}
2025-03-13 04:09:27 +05:00
}
2025-03-14 22:52:59 +05:00
__global__ void KeyGen(curandState* randStates) {
2025-03-14 22:03:46 +05:00
curandState localState = randStates[blockIdx.x * blockDim.x + threadIdx.x];
2025-03-13 04:09:27 +05:00
while (true) {
2025-03-15 02:32:15 +05:00
KeysBox32 keys;
Key32 seed;
rmbytes(seed, &localState);
2025-03-13 04:09:27 +05:00
ed25519_keygen(keys.PrivateKey, keys.PublicKey, seed);
2025-03-15 00:13:50 +05:00
if (unsigned zeros = getZeros(keys.PublicKey); zeros > atomicMax((unsigned*)&d_high, zeros)) {
2025-03-15 04:42:31 +05:00
Addr16 raw;
2025-03-15 01:38:00 +05:00
Key32 inv;
2025-03-14 22:03:46 +05:00
invertKey(keys.PublicKey, inv);
getRawAddress(zeros, inv, raw);
2025-03-15 00:13:50 +05:00
printf("\nIPv6:\t%s\nPK:\t%s\nSK:\t%s\n", getAddr(raw).data, ktos(keys.PublicKey).data, ktos(keys.PrivateKey).data);
2025-03-13 04:09:27 +05:00
}
}
}
2025-03-14 22:03:46 +05:00
int main(int argc, char* argv[]) {
2025-03-15 04:42:31 +05:00
const int thPerBlock = 256;
int* d_result, mBpSM, h_high;
2025-03-14 22:03:46 +05:00
char** d_argv;
2025-03-15 04:42:31 +05:00
cudaDeviceProp prop;
curandState* rst;
cudaMalloc((void**)&d_result, sizeof(int));
2025-03-14 22:03:46 +05:00
cudaMalloc((void**)&d_argv, argc * sizeof(char*));
for (int i = 0; i < argc; i++) {
2025-03-14 22:52:59 +05:00
unsigned long len = strlen(argv[i]) + 1;
2025-03-14 22:03:46 +05:00
char* d_str;
cudaMalloc((void**)&d_str, len);
cudaMemcpy(d_str, argv[i], len, cudaMemcpyHostToDevice);
cudaMemcpy(&d_argv[i], &d_str, sizeof(char*), cudaMemcpyHostToDevice);
}
2025-03-15 04:42:31 +05:00
args<<<1, 1>>>(d_argv, argc, d_result);
cudaDeviceSynchronize();
2025-03-15 00:13:50 +05:00
cudaMemcpyFromSymbol(&h_high, d_high, sizeof(unsigned));
2025-03-14 22:03:46 +05:00
cudaGetDeviceProperties(&prop, 0);
2025-03-15 04:42:31 +05:00
cudaOccupancyMaxActiveBlocksPerMultiprocessor(&mBpSM, KeyGen, thPerBlock, 0);
const int totalTh = mBpSM * prop.multiProcessorCount * thPerBlock;
printf("High addrs: 2%02x+\nSMs: %d\nMaxBlocksPerSM: %d\nTotalTh: %d\nBlocksThreads: %d:%d\n", h_high, prop.multiProcessorCount, mBpSM, totalTh, totalTh / thPerBlock, thPerBlock);
cudaMalloc(&rst, totalTh * sizeof(curandState));
initRand<<<totalTh / thPerBlock, thPerBlock>>>(rst);
2025-03-13 04:09:27 +05:00
cudaDeviceSynchronize();
2025-03-15 04:42:31 +05:00
KeyGen<<<totalTh / thPerBlock, thPerBlock>>>(rst);
2025-03-13 04:09:27 +05:00
cudaDeviceSynchronize();
2025-03-14 19:55:09 +05:00
cudaFree(rst);
2025-03-13 04:09:27 +05:00
return 0;
2025-03-14 22:03:46 +05:00
}