Files
yggm/sources/main.cpp
rcxpony ab6061f5bf owo
2025-08-21 13:05:05 +05:00

149 lines
5.4 KiB
C++

#include "defines.h"
#include <arpa/inet.h>
#include <atomic>
#include <immintrin.h>
#include <iostream>
#include <memory.h>
#include <sodium.h>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
struct option {
unsigned proc = 0;
std::atomic<unsigned> high = 0x14;
};
static option conf;
int parameters(std::string arg) {
if (arg.find(" ") != std::string::npos) {
const unsigned long npos = std::string::npos;
std::istringstream ss(arg.substr(arg.find(" ") + 1));
if (arg.find("--threads") != npos || arg.find("-t") != npos) {
ss >> conf.proc;
if (ss.fail())
return 1;
return 0;
}
if (arg.find("--altitude") != npos || arg.find("-a") != npos) {
unsigned tmp_high;
ss >> std::hex >> tmp_high;
if (ss.fail())
return 1;
conf.high = tmp_high;
return 0;
}
}
if (arg == "--threads" || arg == "-t" || arg == "--altitude" || arg == "-a") {
return 777;
}
return 0;
}
void displayConfig() {
unsigned processor_count = std::thread::hardware_concurrency();
if (conf.proc == 0 || conf.proc > static_cast<unsigned>(processor_count)) {
conf.proc = static_cast<unsigned>(processor_count);
}
printf("Threads: %u, high addresses (2%02x+)\n", conf.proc, conf.high.load());
}
using Address = unsigned char[16];
using Key = unsigned char[32];
inline std::string getAddress(const Address& rawAddr) {
char ipStrBuf[46];
inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46);
return std::string(ipStrBuf);
}
inline std::string KeyToString(const unsigned char* key) {
char result[65];
const char* hexDigits = "0123456789abcdef";
for (int i = 0; i < 32; i++) {
result[2 * i] = hexDigits[key[i] >> 4];
result[2 * i + 1] = hexDigits[key[i] & 0x0F];
}
result[64] = '\0';
return std::string(result);
}
typedef struct alignas(32) {
Key PublicKey;
Key PrivateKey;
} KeysBox;
void getRawAddress(unsigned lErase, Key& InvertedPublicKey, Address& rawAddr) {
lErase++;
const int bitsToShift = lErase % 8;
const int start = static_cast<int>(lErase / 8U);
if (bitsToShift != 0) {
for (int i = start; i < start + 15; i++) {
InvertedPublicKey[i] = static_cast<unsigned char>((InvertedPublicKey[i] << bitsToShift) | (InvertedPublicKey[i + 1] >> (8 - bitsToShift)));
}
}
rawAddr[0] = 0x02;
rawAddr[1] = static_cast<unsigned char>(lErase - 1);
memcpy(&rawAddr[2], &InvertedPublicKey[start], 14);
}
inline void invertKey(const unsigned char* __restrict key, Key& inverted) {
#ifdef __AVX2__
_mm256_storeu_si256(reinterpret_cast<__m256i*>(inverted), _mm256_xor_si256(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(key)), _mm256_set1_epi8(0xFF)));
#else
for (int i = 0; i < 32; ++i) {
inverted[i] = static_cast<unsigned char>(key[i] ^ 0xFF);
}
#endif
}
inline unsigned getZeros(const Key& v) {
unsigned leadZeros = 0;
for (int i = 0; i < 32; i += 8) {
unsigned long long word = (static_cast<unsigned long long>(v[i]) << 56) | (static_cast<unsigned long long>(v[i + 1]) << 48) | (static_cast<unsigned long long>(v[i + 2]) << 40) | (static_cast<unsigned long long>(v[i + 3]) << 32) | (static_cast<unsigned long long>(v[i + 4]) << 24) | (static_cast<unsigned long long>(v[i + 5]) << 16) | (static_cast<unsigned long long>(v[i + 6]) << 8) | static_cast<unsigned long long>(v[i + 7]);
if (word == 0) {
leadZeros += 64;
} else {
leadZeros += static_cast<unsigned>(__builtin_clzll(word));
break;
}
}
return leadZeros;
}
void miner_thread() {
alignas(32) thread_local Key inv;
thread_local KeysBox keys;
thread_local Address rawAddr;
while (true) {
crypto_sign_ed25519_keypair(keys.PublicKey, keys.PrivateKey);
unsigned ones = getZeros(keys.PublicKey), high = conf.high.load(std::memory_order_relaxed);
while (ones > high && !conf.high.compare_exchange_strong(high, ones, std::memory_order_relaxed)) {
high = conf.high.load(std::memory_order_relaxed);
}
if (ones > high) {
invertKey(keys.PublicKey, inv);
getRawAddress(ones, inv, rawAddr);
printf("\nIPv6:\t%s\nPK:\t%s\nSK:\t%s\n", getAddress(rawAddr).c_str(), KeyToString(keys.PublicKey).c_str(), KeyToString(keys.PrivateKey).c_str());
}
}
}
void startThreads() {
std::vector<std::thread> threads;
for (unsigned x = 0; x < conf.proc; x++) {
threads.emplace_back(miner_thread);
}
for (auto& thread : threads) {
thread.join();
}
}
int main(int argc, char* argv[]) {
std::cout << "BuildType: " << __BUILDTYPE__ << std::endl << "AVX2: " << __AVX2__ << std::endl;
if (argc < 1)
return 0;
for (int x = 0; x < argc; x++) {
if (int res = parameters(argv[x]); res == 777) {
if (++x >= argc) {
std::cerr << "Empty value for parameter \"" << argv[x - 1] << "\"" << std::endl;
return 776;
}
if (parameters(argv[x - 1] + std::string(" ") + argv[x]) != 0) {
std::cerr << "Wrong value \"" << argv[x] << "\" for parameter \"" << argv[x - 1] << "\"" << std::endl;
return res;
}
}
}
displayConfig();
startThreads();
return 0;
}