yggm/sources/main.cu

202 lines
7.2 KiB
Plaintext
Raw Normal View History

2025-03-13 04:09:27 +05:00
#include <cuda_runtime.h>
#include <curand_kernel.h>
2025-03-14 22:52:59 +05:00
#include <sstream>
#include <iostream>
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-13 04:09:27 +05:00
using Address = unsigned char[16];
using Key = unsigned char[32];
struct KeysBox {
Key PublicKey;
Key PrivateKey;
};
2025-03-14 22:03:46 +05:00
__device__ unsigned high = 0x10;
__device__ int parameters(const char* arg) {
int space_index = cstring_find(arg, " ");
if (space_index != -1) {
int substr_start = space_index + 1;
int arg_len = cstring_length(arg);
int substr_len = arg_len - substr_start + 1;
char sub_arg[256];
if (substr_len > 256) substr_len = 256;
extract_substring(arg, substr_start, sub_arg, substr_len);
if (cstring_find(arg, "--altitude") != -1 || cstring_find(arg, "-a") != -1) {
unsigned tmp_high;
int ret = cstring_to_ull(sub_arg, &tmp_high);
if (ret != 0) return 1;
high = tmp_high;
return 0;
}
}
if ((cstring_find(arg, "--altitude") == 0 && cstring_length(arg) == 10) || (cstring_find(arg, "-a") == 0 && cstring_length(arg) == 2)) {
return 777;
}
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-13 04:09:27 +05:00
struct ds64 {
char data[65];
};
struct ds46 {
char data[46];
};
2025-03-14 22:03:46 +05:00
__device__ ds64 ktos(const unsigned char* key) noexcept {
2025-03-13 04:09:27 +05:00
ds64 str;
2025-03-14 19:55:09 +05:00
const char* hexDigits = "0123456789abcdef";
2025-03-14 22:03:46 +05:00
#pragma unroll
2025-03-13 04:09:27 +05:00
for (unsigned char i = 0; i < 32; i++) {
str.data[2 * i] = hexDigits[key[i] >> 4];
str.data[2 * i + 1] = hexDigits[key[i] & 0x0F];
}
str.data[64] = '\0';
return str;
}
2025-03-14 22:03:46 +05:00
__device__ ds46 getAddr(const unsigned char rawAddr[16]) noexcept {
2025-03-13 04:09:27 +05:00
ds46 addrStr;
const char* hexDigits = "0123456789abcdef";
2025-03-14 19:55:09 +05:00
unsigned pos = 0;
2025-03-14 22:03:46 +05:00
#pragma unroll
2025-03-14 19:55:09 +05:00
for (unsigned char group = 0; group < 8; group++) {
2025-03-13 04:09:27 +05:00
int idx = group * 2;
addrStr.data[pos++] = hexDigits[rawAddr[idx] >> 4];
addrStr.data[pos++] = hexDigits[rawAddr[idx] & 0x0F];
addrStr.data[pos++] = hexDigits[rawAddr[idx + 1] >> 4];
addrStr.data[pos++] = hexDigits[rawAddr[idx + 1] & 0x0F];
2025-03-14 22:03:46 +05:00
if (group < 7) { addrStr.data[pos++] = ':'; }
2025-03-13 04:09:27 +05:00
}
addrStr.data[pos] = '\0';
return addrStr;
}
2025-03-14 22:03:46 +05:00
__device__ __forceinline__ void getRawAddress(int lErase, Key& InvertedPublicKey, Address& rawAddr) noexcept {
2025-03-13 04:09:27 +05:00
lErase++;
2025-03-14 22:03:46 +05:00
const int bitsToShift = lErase & 7;
const int start = lErase >> 3;
if (bitsToShift) {
#pragma unroll
2025-03-13 04:09:27 +05:00
for (int i = start; i < start + 15; i++) {
2025-03-13 19:43:54 +05:00
InvertedPublicKey[i] = static_cast<unsigned char>((InvertedPublicKey[i] << bitsToShift) | (InvertedPublicKey[i + 1] >> (8 - bitsToShift)));
2025-03-13 04:09:27 +05:00
}
}
rawAddr[0] = 0x02;
rawAddr[1] = static_cast<unsigned char>(lErase - 1);
memcpy(&rawAddr[2], &InvertedPublicKey[start], 14);
}
2025-03-14 22:03:46 +05:00
__device__ __forceinline__ unsigned char zeroCounter(unsigned int x) {
return x ? static_cast<unsigned char>(__clz(x)) : 32;
2025-03-13 04:09:27 +05:00
}
2025-03-14 22:03:46 +05:00
__device__ __forceinline__ unsigned char getZeros(const unsigned char* v) {
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-14 19:55:09 +05:00
__global__ void initRand(curandState* randStates) {
2025-03-13 04:09:27 +05:00
int id = blockIdx.x * blockDim.x + threadIdx.x;
2025-03-14 22:03:46 +05:00
curand_init(static_cast<unsigned long long>(clock64()) + id, id, 0, &randStates[id]);
2025-03-13 04:09:27 +05:00
}
2025-03-14 22:03:46 +05:00
__device__ __forceinline__ unsigned long long xorshift128plus(unsigned long long* state) noexcept {
2025-03-14 19:55:09 +05:00
unsigned long long x = state[0];
const unsigned long long y = state[1];
state[0] = y;
x ^= x << 23;
x ^= x >> 17;
x ^= y ^ (y >> 26);
state[1] = x;
return x + y;
}
2025-03-14 22:03:46 +05:00
__device__ __forceinline__ void rmbytes(unsigned char* buf, unsigned long size, unsigned long long* state) {
#pragma unroll
2025-03-13 19:43:54 +05:00
for (unsigned long i = 0; i < size; i++) {
2025-03-14 22:03:46 +05:00
buf[i] = static_cast<unsigned char>(xorshift128plus(state) & 0xFF);
2025-03-13 04:09:27 +05:00
}
}
2025-03-14 22:03:46 +05:00
__device__ __forceinline__ void invertKey(const unsigned char* key, unsigned char* inverted) {
#pragma unroll
2025-03-14 22:52:59 +05:00
for (unsigned char i = 0; i < 32; i++) inverted[i] = key[i] ^ 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-14 19:55:09 +05:00
unsigned long long xorshiftState[2];
xorshiftState[0] = curand(&localState);
xorshiftState[1] = curand(&localState);
2025-03-13 19:43:54 +05:00
Key seed;
2025-03-13 04:09:27 +05:00
while (true) {
2025-03-14 22:03:46 +05:00
rmbytes(seed, sizeof(seed), xorshiftState);
2025-03-13 04:09:27 +05:00
KeysBox keys;
ed25519_keygen(keys.PrivateKey, keys.PublicKey, seed);
2025-03-14 22:03:46 +05:00
unsigned zeros = getZeros(keys.PublicKey);
if (zeros > atomicMax((unsigned*)&high, zeros)) {
Address raw;
Key inv;
invertKey(keys.PublicKey, inv);
getRawAddress(zeros, inv, raw);
printf("\nIPv6:\t%s\nPK:\t%s\nSK:\t%s\nFK:\t%s%s\n", getAddr(raw).data, ktos(keys.PublicKey).data, ktos(keys.PrivateKey).data, ktos(keys.PrivateKey).data, ktos(keys.PublicKey).data);
2025-03-13 04:09:27 +05:00
}
}
}
2025-03-14 22:03:46 +05:00
int main(int argc, char* argv[]) {
int* d_result;
cudaMalloc((void**)&d_result, sizeof(int));
char** d_argv;
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);
}
args<<<1, 1 >>>(d_argv, argc, d_result);
unsigned h_high;
cudaMemcpyFromSymbol(&h_high, high, sizeof(unsigned));
printf("High addresses (2%02x+)\n", h_high);
2025-03-14 19:55:09 +05:00
const int threadsPerBlock = 256;
cudaDeviceProp prop;
2025-03-14 22:03:46 +05:00
cudaGetDeviceProperties(&prop, 0);
2025-03-14 19:55:09 +05:00
int mBpSM;
2025-03-14 22:52:59 +05:00
cudaOccupancyMaxActiveBlocksPerMultiprocessor(&mBpSM, KeyGen, threadsPerBlock, 0);
2025-03-14 19:55:09 +05:00
int SMs = prop.multiProcessorCount;
int maxBlocks = mBpSM * SMs;
const int totalThreads = maxBlocks * threadsPerBlock;
printf("SMs: %d\n", SMs);
printf("maxBlocks: %d\n", maxBlocks);
printf("totalThreads: %d\n", totalThreads);
printf("MaxBlocksPerSM: %d\n", mBpSM);
2025-03-14 22:52:59 +05:00
printf("BlocksThreads: %d:%d\n", totalThreads / threadsPerBlock, threadsPerBlock);
2025-03-14 19:55:09 +05:00
curandState* rst;
cudaMalloc(&rst, totalThreads * sizeof(curandState));
2025-03-14 22:03:46 +05:00
initRand<<<totalThreads / threadsPerBlock, threadsPerBlock>>>(rst);
2025-03-13 04:09:27 +05:00
cudaDeviceSynchronize();
2025-03-14 22:52:59 +05:00
KeyGen<<<totalThreads / threadsPerBlock, threadsPerBlock>>>(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
}