yggm/sources/main.cpp

171 lines
6.3 KiB
C++
Raw Permalink Normal View History

2025-02-28 11:37:30 +05:00
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
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>
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;
2025-03-10 21:23:06 +05:00
std::atomic<unsigned> high = 0x14;
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-10 21:23:06 +05:00
/*
if (arg.find("--invert") != npos || arg.find("-i") != npos) {
return 0;
}
*/
2025-02-28 11:37:30 +05:00
}
2025-03-13 04:09:27 +05:00
if (arg == "--threads" || arg == "-t" || arg == "--altitude" || arg == "-a") {
return 777;
}
2025-02-28 11:37:30 +05:00
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-13 19:43:54 +05:00
2025-03-01 04:57:59 +05:00
}
2025-03-10 21:23:06 +05:00
printf("Threads: %u, high addresses (2%02x+)\n", conf.proc, conf.high.load());
2025-02-28 11:37:30 +05:00
}
2025-03-02 16:41:02 +05:00
using Address = unsigned char[16];
2025-03-10 20:31:58 +05:00
using Key = unsigned char[32];
inline std::string getAddress(const Address& rawAddr) noexcept {
char ipStrBuf[46];
2025-03-01 18:02:11 +05:00
inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46);
2025-03-10 20:31:58 +05:00
return std::string(ipStrBuf);
2025-02-28 11:37:30 +05:00
}
2025-03-10 21:23:06 +05:00
inline std::string KeyToString(const unsigned char* key) noexcept {
2025-03-10 20:31:58 +05:00
char result[65];
2025-03-01 16:38:11 +05:00
const char* hexDigits = "0123456789abcdef";
2025-03-15 00:13:50 +05:00
#pragma unroll
2025-03-10 21:24:29 +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-03-10 20:31:58 +05:00
result[64] = '\0';
return std::string(result);
2025-02-28 11:37:30 +05:00
}
2025-03-10 20:31:58 +05:00
typedef struct alignas(32) {
2025-03-02 17:47:08 +05:00
Key PublicKey;
Key PrivateKey;
2025-03-10 20:31:58 +05:00
} KeysBox;
void getRawAddress(int lErase, Key& InvertedPublicKey, Address& rawAddr) noexcept {
2025-03-09 20:17:04 +05:00
lErase++;
const int bitsToShift = lErase % 8;
const int start = lErase / 8;
2025-03-10 20:31:58 +05:00
if (bitsToShift != 0) {
for (int i = start; i < start + 15; i++) {
2025-03-09 20:17:04 +05:00
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);
}
2025-03-10 21:23:06 +05:00
inline void invertKey(const unsigned char* __restrict key, Key& inverted) noexcept {
_mm256_storeu_si256(reinterpret_cast<__m256i*>(inverted), _mm256_xor_si256(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(key)), _mm256_set1_epi8(0xFF)));
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 {
2025-03-09 20:43:23 +05:00
return x == 0 ? 32 : static_cast<unsigned char>(__builtin_clz(x));
2025-03-07 03:35:04 +05:00
}
2025-03-10 21:23:06 +05:00
[[nodiscard]] inline unsigned char getZeros(const Key& v) noexcept {
2025-03-04 03:01:12 +05:00
unsigned char leadZeros = 0;
2025-03-07 03:35:04 +05:00
for (unsigned char i = 0; i < 32; i += 4) {
2025-03-10 21:23:06 +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-07 03:35:04 +05:00
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-10 20:31:58 +05:00
[[nodiscard]] inline long long xorshift64(unsigned long& state) noexcept {
2025-03-07 03:35:04 +05:00
state ^= state << 21;
state ^= state >> 35;
state ^= state << 4;
return static_cast<long long>(state * 2685821657736338717);
}
2025-03-10 21:23:06 +05:00
inline void rmbytes(unsigned char* __restrict buf, unsigned char size, unsigned long& state) noexcept {
2025-03-15 00:13:50 +05:00
for (unsigned char x = 0; x < 32; x++) {
2025-03-13 04:09:27 +05:00
_mm256_store_si256((__m256i*) & buf[x * 32], _mm256_set_epi64x(xorshift64(state), xorshift64(state), xorshift64(state), xorshift64(state)));
2025-03-07 03:35:04 +05:00
}
}
2025-03-10 21:23:06 +05:00
inline void sign_keypair(unsigned char* __restrict pk, unsigned char* __restrict sk, const unsigned char* __restrict seed) noexcept {
2025-03-10 20:31:58 +05:00
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);
2025-03-13 04:09:27 +05:00
_mm256_store_si256(reinterpret_cast<__m256i*>(sk), _mm256_load_si256(reinterpret_cast<const __m256i*>(seed)));
_mm256_store_si256(reinterpret_cast<__m256i*>(sk + 32), _mm256_load_si256(reinterpret_cast<const __m256i*>(pk)));
2025-03-10 20:31:58 +05:00
}
2025-03-02 16:41:02 +05:00
void miner_thread() noexcept {
2025-03-10 20:31:58 +05:00
alignas(32) Key inv;
alignas(32) Key seed;
2025-03-02 16:41:02 +05:00
KeysBox keys;
2025-03-09 20:17:04 +05:00
Address rawAddr;
2025-03-07 03:35:04 +05:00
std::random_device rd;
unsigned long state = static_cast<unsigned long>(rd());
2025-03-10 20:31:58 +05:00
printf("Using seed: %lu\n", state);
2025-03-01 16:38:11 +05:00
while (true) {
2025-03-10 20:31:58 +05:00
rmbytes(seed, sizeof(seed), state);
2025-03-13 04:09:27 +05:00
crypto_sign_ed25519_seed_keypair(keys.PublicKey, keys.PrivateKey, seed);
2025-03-15 01:38:00 +05:00
if (unsigned char ones = getZeros(keys.PublicKey); ones > conf.high.load()) {
2025-03-10 20:31:58 +05:00
conf.high.store(ones);
2025-03-10 21:23:06 +05:00
invertKey(keys.PublicKey, inv);
2025-03-09 20:17:04 +05:00
getRawAddress(ones, inv, rawAddr);
2025-03-10 21:23:06 +05:00
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());
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-10 20:31:58 +05:00
for (unsigned char x = 0; x < conf.proc; x++) {
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-03-13 04:09:27 +05:00
if (argc < 2) return 0;
for (int x = 1; 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;
2025-02-28 11:37:30 +05:00
}
}
}
displayConfig();
startThreads();
2025-03-04 03:01:12 +05:00
return 0;
2025-03-10 20:31:58 +05:00
}