yggm/sources/main.cpp

205 lines
7.2 KiB
C++
Raw Normal View History

2025-02-28 11:37:30 +05:00
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <array>
2025-03-01 01:23:03 +05:00
#include <string>
2025-03-07 03:35:04 +05:00
#include <random>
2025-02-28 11:37:30 +05:00
#include <memory.h>
#include <thread>
#include <regex>
2025-03-07 03:35:04 +05:00
#include <sodium.h>
2025-02-28 11:37:30 +05:00
#include <arpa/inet.h>
2025-03-01 01:23:03 +05:00
#include <immintrin.h>
2025-02-28 11:37:30 +05:00
struct option {
2025-03-09 20:17:04 +05:00
unsigned proc = 0;
unsigned high = 10;
//std::string outputfile;
2025-02-28 11:37:30 +05:00
};
static option conf;
2025-03-09 20:17:04 +05:00
int parameters(std::string arg) {
2025-02-28 12:53:12 +05:00
if (arg.find(" ") != std::string::npos) {
2025-03-01 16:38:11 +05:00
const unsigned long npos = std::string::npos;
2025-03-04 03:01:12 +05:00
std::istringstream ss(arg.substr(arg.find(" ") + 1));
2025-02-28 11:37:30 +05:00
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) {
2025-03-09 20:17:04 +05:00
unsigned tmp_high;
2025-03-01 16:38:11 +05:00
ss >> std::hex >> tmp_high;
2025-02-28 11:37:30 +05:00
if (ss.fail()) return 1;
2025-03-01 16:38:11 +05:00
conf.high = tmp_high;
2025-02-28 11:37:30 +05:00
return 0;
}
}
2025-03-01 04:57:59 +05:00
if (arg == "--threads" || arg == "-t") return 777;
2025-02-28 11:37:30 +05:00
else if (arg == "--altitude" || arg == "-a") return 777;
return 0;
}
void displayConfig() {
2025-03-04 03:01:12 +05:00
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);
2025-03-01 04:57:59 +05:00
}
std::cout << " Threads: " << conf.proc << ", " << "high addresses (2" << std::setw(2) << std::setfill('0') << std::hex << conf.high << "+)" << std::dec << std::endl;
2025-02-28 11:37:30 +05:00
}
2025-03-02 16:41:02 +05:00
using Address = unsigned char[16];
2025-03-09 20:17:04 +05:00
const char* getAddress(const Address& rawAddr) noexcept {
char* ipStrBuf = new char[46];
2025-03-01 18:02:11 +05:00
inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46);
2025-03-09 20:17:04 +05:00
return ipStrBuf;
2025-02-28 11:37:30 +05:00
}
2025-03-07 03:35:04 +05:00
inline std::string keyToString(const unsigned char* key) noexcept {
2025-02-28 12:53:12 +05:00
std::string result;
2025-03-01 16:38:11 +05:00
result.resize(64);
const char* hexDigits = "0123456789abcdef";
2025-03-02 16:41:02 +05:00
for (unsigned char i = 0; i < 32; i++) {
2025-03-01 16:38:11 +05:00
result[2 * i] = hexDigits[key[i] >> 4];
result[2 * i + 1] = hexDigits[key[i] & 0x0F];
2025-02-28 11:37:30 +05:00
}
2025-02-28 12:53:12 +05:00
return result;
2025-02-28 11:37:30 +05:00
}
2025-03-02 17:47:08 +05:00
using Key = unsigned char[32];
struct KeysBox {
Key PublicKey;
Key PrivateKey;
};
2025-03-09 20:17:04 +05:00
void getRawAddress(int lErase, Key& InvertedPublicKey, Address& rawAddr) {
lErase++;
const int bitsToShift = lErase % 8;
const int start = lErase / 8;
if (bitsToShift == 0) {
for (int i = start; i < start + 15; ++i)
InvertedPublicKey[i] = InvertedPublicKey[i];
} else {
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);
}
[[gnu::always_inline]] inline void bitwiseInverse(const unsigned char* key, Key& inverted) noexcept {
2025-03-02 16:41:02 +05:00
__m256i chunk = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(key));
chunk = _mm256_xor_si256(chunk, _mm256_set1_epi8(0xFF));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(inverted), chunk);
2025-02-28 11:37:30 +05:00
}
2025-03-07 03:35:04 +05:00
[[nodiscard]] inline unsigned char zeroCounter(unsigned int x) noexcept {
#if defined(__GNUC__) || defined(__clang__)
if (x == 0)
return 32;
return static_cast<unsigned char>(__builtin_clz(x));
#else
unsigned char zeros = 0;
for (uint32_t mask = 0x80000000; mask; mask >>= 1) {
if (x & mask) break;
zeros++;
}
return zeros;
#endif
}
2025-03-04 03:01:12 +05:00
[[nodiscard]] inline unsigned char getZeros(const Key& value) noexcept {
unsigned char leadZeros = 0;
2025-03-07 03:35:04 +05:00
for (unsigned char i = 0; i < 32; i += 4) {
unsigned int word =
(static_cast<unsigned int>(value[i]) << 24) |
(static_cast<unsigned int>(value[i + 1]) << 16) |
(static_cast<unsigned int>(value[i + 2]) << 8) |
(static_cast<unsigned int>(value[i + 3]));
if (word == 0) {
leadZeros += 32;
2025-02-28 11:37:30 +05:00
} else {
2025-03-07 03:35:04 +05:00
leadZeros += zeroCounter(word);
break;
2025-02-28 11:37:30 +05:00
}
}
2025-03-04 03:01:12 +05:00
return leadZeros;
2025-02-28 11:37:30 +05:00
}
2025-03-09 20:17:04 +05:00
[[nodiscard]] inline long long xorshift64(unsigned long& state) {
2025-03-07 03:35:04 +05:00
state ^= state << 21;
state ^= state >> 35;
state ^= state << 4;
return static_cast<long long>(state * 2685821657736338717);
}
inline void randombytesavx2(unsigned char* buf, unsigned char size, unsigned long& state) noexcept {
for (unsigned char x = 0; x < size; x += 32) {
_mm256_storeu_si256((__m256i*) & buf[x], _mm256_set_epi64x(xorshift64(state), xorshift64(state), xorshift64(state), xorshift64(state)));
}
}
2025-03-02 16:41:02 +05:00
void miner_thread() noexcept {
2025-03-09 20:17:04 +05:00
Key inv;
2025-03-02 16:41:02 +05:00
KeysBox keys;
2025-03-09 20:17:04 +05:00
Address rawAddr;
2025-03-04 03:01:12 +05:00
unsigned char ones = 0;
2025-03-07 03:35:04 +05:00
unsigned char seed[32];
std::random_device rd;
unsigned long state = static_cast<unsigned long>(rd());
2025-03-01 16:38:11 +05:00
while (true) {
2025-03-07 03:35:04 +05:00
randombytesavx2(seed, sizeof(seed) / sizeof(seed[0]), state);
crypto_sign_ed25519_seed_keypair(keys.PublicKey, keys.PrivateKey, seed);
2025-03-04 03:01:12 +05:00
ones = getZeros(keys.PublicKey);
2025-03-01 18:02:11 +05:00
if (ones > conf.high) {
2025-03-07 03:35:04 +05:00
conf.high = ones;
2025-03-09 20:17:04 +05:00
bitwiseInverse(keys.PublicKey, inv);
getRawAddress(ones, inv, rawAddr);
printf("\nIPv6:\t%s/%d\nPK:\t%s\nSK:\t%s\n", getAddress(rawAddr), ones, keyToString(keys.PublicKey).c_str(), keyToString(keys.PrivateKey).c_str());
2025-02-28 12:53:12 +05:00
}
2025-02-28 11:37:30 +05:00
}
}
2025-03-02 16:41:02 +05:00
void startThreads() noexcept {
2025-03-01 01:23:03 +05:00
std::vector<std::thread> threads;
2025-03-09 20:17:04 +05:00
threads.reserve(conf.proc);
2025-03-02 16:41:02 +05:00
for (unsigned char i = 0; i < conf.proc; i++) {
2025-03-01 01:23:03 +05:00
threads.emplace_back(miner_thread);
}
for (auto& thread : threads) {
thread.join();
2025-02-28 11:37:30 +05:00
}
}
2025-03-07 03:35:04 +05:00
int main(int argc, char* argv[]) noexcept {
2025-02-28 11:37:30 +05:00
if (argc >= 2) {
2025-02-28 12:53:12 +05:00
int res = -1;
for (int i = 1;; ++i) {
if (argv[i] == nullptr) break;
2025-03-09 20:17:04 +05:00
res = parameters(std::string(argv[i]));
2025-02-28 12:53:12 +05:00
if (res == 777) {
2025-03-01 01:23:03 +05:00
i++;
2025-02-28 12:53:12 +05:00
if (argv[i] == nullptr) {
std::cerr << " Empty value for parameter \"" << argv[i - 1] << "\"" << std::endl;
return 776;
}
2025-03-09 20:17:04 +05:00
int res2 = parameters(std::string(std::string(argv[i - 1]) + " " + std::string(argv[i])));
2025-02-28 12:53:12 +05:00
if (res2 != 0) {
std::cerr << " Wrong value \"" << argv[i] << "\" for parameter \"" << argv[i - 1] << "\"" << std::endl;
return res;
2025-02-28 11:37:30 +05:00
}
}
}
}
2025-03-07 03:35:04 +05:00
if (sodium_init() < 0) return -1;
2025-02-28 11:37:30 +05:00
displayConfig();
startThreads();
2025-03-04 03:01:12 +05:00
return 0;
2025-03-01 01:23:03 +05:00
}
2025-03-01 16:38:11 +05:00
/*
2025-03-01 01:23:03 +05:00
void logKeys(const Address& raw, const KeysBox& keys) {
std::cout << std::endl;
std::cout << " Address: " << getAddress(raw) << std::endl;
std::cout << " PublicKey: " << keyToString(keys.PublicKey) << std::endl;
std::cout << " PrivateKey: " << keyToString(keys.PrivateKey);
if (!conf.log || conf.fullkeys) std::cout << keyToString(keys.PublicKey);
std::cout << std::endl << std::endl;
if (conf.log) {
std::ofstream output(conf.outputfile, std::ios::app);
output << std::endl;
output << "Address: " << getAddress(raw) << std::endl;
output << "PublicKey: " << keyToString(keys.PublicKey) << std::endl;
output << "PrivateKey: " << keyToString(keys.PrivateKey) << keyToString(keys.PublicKey) << std::endl;
output.close();
}
}
2025-03-07 03:35:04 +05:00
*/