#include #include //#include #include #include #include #include #include #include #include #include #include struct option { unsigned proc = 0; std::atomic high; //std::string outputfile; }; 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") return 777; else if (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(processor_count)) { conf.proc = static_cast(processor_count); } std::cout << " Threads: " << conf.proc << ", " << "high addresses (2" << std::setw(2) << std::setfill('0') << std::hex << conf.high << "+)" << std::dec << std::endl; } using Address = unsigned char[16]; using Key = unsigned char[32]; inline std::string getAddress(const Address& rawAddr) noexcept { char ipStrBuf[46]; inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46); return std::string(ipStrBuf); } inline std::string keyToString(const unsigned char* key) noexcept { char result[65]; const char* hexDigits = "0123456789abcdef"; for (size_t 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(int lErase, Key& InvertedPublicKey, Address& rawAddr) noexcept { lErase++; const int bitsToShift = lErase % 8; const int start = lErase / 8; if (bitsToShift != 0) { for (int i = start; i < start + 15; i++) { InvertedPublicKey[i] = static_cast((InvertedPublicKey[i] << bitsToShift) | (InvertedPublicKey[i + 1] >> (8 - bitsToShift))); } } rawAddr[0] = 0x02; rawAddr[1] = static_cast(lErase - 1); memcpy(&rawAddr[2], &InvertedPublicKey[start], 14); } inline void bitwiseInverse(const unsigned char* key, Key& inverted) noexcept { __m256i chunk = _mm256_loadu_si256(reinterpret_cast(key)); chunk = _mm256_xor_si256(chunk, _mm256_set1_epi8(0xFF)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(inverted), chunk); } [[nodiscard]] inline unsigned char zeroCounter(unsigned int x) noexcept { return x == 0 ? 32 : static_cast(__builtin_clz(x)); } [[nodiscard]] inline unsigned char getZeros(const Key& value) noexcept { unsigned char leadZeros = 0; for (unsigned char i = 0; i < 32; i += 4) { unsigned int word = (static_cast(value[i]) << 24) | (static_cast(value[i + 1]) << 16) | (static_cast(value[i + 2]) << 8) | (static_cast(value[i + 3])); if (word == 0) { leadZeros += 32; } else { leadZeros += zeroCounter(word); break; } } return leadZeros; } [[nodiscard]] inline long long xorshift64(unsigned long& state) noexcept { state ^= state << 21; state ^= state >> 35; state ^= state << 4; return static_cast(state * 2685821657736338717); } inline void rmbytes(unsigned char* buf, unsigned char size, unsigned long& state) noexcept { for (unsigned char x = 0; x < size / 32; x++) { _mm256_storeu_si256((__m256i*) & buf[x * 32], _mm256_set_epi64x(xorshift64(state), xorshift64(state), xorshift64(state), xorshift64(state))); } for (unsigned char x = 0; x < (size % 32); x++) { buf[(size / 32) * 32 + x] = static_cast(xorshift64(state) & 0xFF); } } inline void sign_keypair(unsigned char* pk, unsigned char* sk, const unsigned char* seed) noexcept { alignas(32) unsigned char h[64]; crypto_hash_sha512(h, seed, 32); h[31] = (h[31] & 0xF8) | (0x40 | (h[31] & 0x7F)); crypto_scalarmult_ed25519_base(pk, h); _mm256_storeu_si256(reinterpret_cast<__m256i*>(sk), _mm256_loadu_si256(reinterpret_cast(seed))); _mm256_storeu_si256(reinterpret_cast<__m256i*>(sk + 32), _mm256_loadu_si256(reinterpret_cast(pk))); } void miner_thread() noexcept { alignas(32) Key inv; alignas(32) Key seed; KeysBox keys; Address rawAddr; unsigned char ones = 0; std::random_device rd; unsigned long state = static_cast(rd()); printf("Using seed: %lu\n", state); while (true) { rmbytes(seed, sizeof(seed), state); sign_keypair(keys.PublicKey, keys.PrivateKey, seed); //crypto_sign_ed25519_seed_keypair(keys.PublicKey, keys.PrivateKey, seed); ones = getZeros(keys.PublicKey); if (ones > conf.high.load()) { conf.high.store(ones); bitwiseInverse(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() noexcept { std::vector threads; threads.reserve(conf.proc); for (unsigned char x = 0; x < conf.proc; x++) { threads.emplace_back(miner_thread); } for (auto& thread : threads) { thread.join(); } } int main(int argc, char* argv[]) noexcept { if (argc >= 2) { int res = -1; for (int i = 1;; ++i) { if (argv[i] == nullptr) break; res = parameters(std::string(argv[i])); if (res == 777) { i++; if (argv[i] == nullptr) { std::cerr << " Empty value for parameter \"" << argv[i - 1] << "\"" << std::endl; return 776; } int res2 = parameters(std::string(std::string(argv[i - 1]) + " " + std::string(argv[i]))); if (res2 != 0) { std::cerr << " Wrong value \"" << argv[i] << "\" for parameter \"" << argv[i - 1] << "\"" << std::endl; return res; } } } } if (sodium_init() < 0) return -1; displayConfig(); startThreads(); return 0; }