optimized

This commit is contained in:
rcxpony 2025-03-09 20:17:04 +05:00
parent bbb884b834
commit 1af1a7924e
2 changed files with 35 additions and 36 deletions

View File

@ -2,11 +2,10 @@ cmake_minimum_required(VERSION 3.31)
project(yggm) project(yggm)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(OpenSSL REQUIRED)
file(GLOB ${PROJECT_NAME}SOURCES *.cpp) file(GLOB ${PROJECT_NAME}SOURCES *.cpp)
file(GLOB ${PROJECT_NAME}HEADERS *.h) file(GLOB ${PROJECT_NAME}HEADERS *.h)
add_executable(${PROJECT_NAME} sources/main.cpp) add_executable(${PROJECT_NAME} sources/main.cpp)
set(CXX_ADDITIONAL_FLAGS "-mavx2 -fomit-frame-pointer -ftree-vectorize -ftree-slp-vectorize -fdelete-null-pointer-checks -fno-exceptions -fno-rtti -pthread") set(CXX_ADDITIONAL_FLAGS "-mavx2 -fomit-frame-pointer -ftree-vectorize -ftree-slp-vectorize -fdelete-null-pointer-checks -fno-exceptions -fno-rtti")
set(CMAKE_CXX_FLAGS_RELEASE "-march=native -O3 -ffast-math -pipe -Wall -Wextra -Wpedantic -Wconversion -Wuninitialized -Wsign-conversion -flto=full") set(CMAKE_CXX_FLAGS_RELEASE "-march=native -O3 -ffast-math -pipe -Wall -Wextra -Wpedantic -Wconversion -Wuninitialized -Wsign-conversion -flto=full")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CXX_ADDITIONAL_FLAGS}") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CXX_ADDITIONAL_FLAGS}")
target_link_libraries(${PROJECT_NAME} pthread sodium) target_link_libraries(${PROJECT_NAME} pthread sodium)

View File

@ -13,13 +13,12 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <immintrin.h> #include <immintrin.h>
struct option { struct option {
unsigned int proc = 0; unsigned proc = 0;
bool log = true; unsigned high = 10;
int high = 14; //std::string outputfile;
std::string outputfile;
}; };
static option conf; static option conf;
int parameters(option& conf, std::string arg) { int parameters(std::string arg) {
if (arg.find(" ") != std::string::npos) { if (arg.find(" ") != std::string::npos) {
const unsigned long npos = std::string::npos; const unsigned long npos = std::string::npos;
std::istringstream ss(arg.substr(arg.find(" ") + 1)); std::istringstream ss(arg.substr(arg.find(" ") + 1));
@ -29,7 +28,7 @@ int parameters(option& conf, std::string arg) {
return 0; return 0;
} }
if (arg.find("--altitude") != npos || arg.find("-a") != npos) { if (arg.find("--altitude") != npos || arg.find("-a") != npos) {
int tmp_high; unsigned tmp_high;
ss >> std::hex >> tmp_high; ss >> std::hex >> tmp_high;
if (ss.fail()) return 1; if (ss.fail()) return 1;
conf.high = tmp_high; conf.high = tmp_high;
@ -48,10 +47,10 @@ void displayConfig() {
std::cout << " Threads: " << conf.proc << ", " << "high addresses (2" << std::setw(2) << std::setfill('0') << std::hex << conf.high << "+)" << std::dec << std::endl; 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 Address = unsigned char[16];
[[gnu::unused]] std::string getAddress(const Address& rawAddr) noexcept { const char* getAddress(const Address& rawAddr) noexcept {
char ipStrBuf[46]; char* ipStrBuf = new char[46];
inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46); inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46);
return std::string(ipStrBuf); return ipStrBuf;
} }
inline std::string keyToString(const unsigned char* key) noexcept { inline std::string keyToString(const unsigned char* key) noexcept {
std::string result; std::string result;
@ -68,7 +67,23 @@ struct KeysBox {
Key PublicKey; Key PublicKey;
Key PrivateKey; Key PrivateKey;
}; };
[[gnu::always_inline]] [[gnu::unused]] inline void bitwiseInverse(const unsigned char* key, Key& inverted) noexcept { 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 {
__m256i chunk = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(key)); __m256i chunk = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(key));
chunk = _mm256_xor_si256(chunk, _mm256_set1_epi8(0xFF)); chunk = _mm256_xor_si256(chunk, _mm256_set1_epi8(0xFF));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(inverted), chunk); _mm256_storeu_si256(reinterpret_cast<__m256i*>(inverted), chunk);
@ -89,7 +104,6 @@ struct KeysBox {
} }
[[nodiscard]] inline unsigned char getZeros(const Key& value) noexcept { [[nodiscard]] inline unsigned char getZeros(const Key& value) noexcept {
unsigned char leadZeros = 0; unsigned char leadZeros = 0;
#pragma unroll
for (unsigned char i = 0; i < 32; i += 4) { for (unsigned char i = 0; i < 32; i += 4) {
unsigned int word = unsigned int word =
(static_cast<unsigned int>(value[i]) << 24) | (static_cast<unsigned int>(value[i]) << 24) |
@ -105,20 +119,21 @@ struct KeysBox {
} }
return leadZeros; return leadZeros;
} }
inline long long xorshift64(unsigned long& state) { [[nodiscard]] inline long long xorshift64(unsigned long& state) {
state ^= state << 21; state ^= state << 21;
state ^= state >> 35; state ^= state >> 35;
state ^= state << 4; state ^= state << 4;
return static_cast<long long>(state * 2685821657736338717); return static_cast<long long>(state * 2685821657736338717);
} }
inline void randombytesavx2(unsigned char* buf, unsigned char size, unsigned long& state) noexcept { inline void randombytesavx2(unsigned char* buf, unsigned char size, unsigned long& state) noexcept {
#pragma unroll
for (unsigned char x = 0; x < size; x += 32) { 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))); _mm256_storeu_si256((__m256i*) & buf[x], _mm256_set_epi64x(xorshift64(state), xorshift64(state), xorshift64(state), xorshift64(state)));
} }
} }
void miner_thread() noexcept { void miner_thread() noexcept {
Key inv;
KeysBox keys; KeysBox keys;
Address rawAddr;
unsigned char ones = 0; unsigned char ones = 0;
unsigned char seed[32]; unsigned char seed[32];
std::random_device rd; std::random_device rd;
@ -129,12 +144,15 @@ void miner_thread() noexcept {
ones = getZeros(keys.PublicKey); ones = getZeros(keys.PublicKey);
if (ones > conf.high) { if (ones > conf.high) {
conf.high = ones; conf.high = ones;
printf("\nAddr:\t2%x/%d\nPK:\t%s\nSK:\t%s\n", ones, ones, keyToString(keys.PublicKey).c_str(), keyToString(keys.PrivateKey).c_str()); 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());
} }
} }
} }
void startThreads() noexcept { void startThreads() noexcept {
std::vector<std::thread> threads; std::vector<std::thread> threads;
threads.reserve(conf.proc);
for (unsigned char i = 0; i < conf.proc; i++) { for (unsigned char i = 0; i < conf.proc; i++) {
threads.emplace_back(miner_thread); threads.emplace_back(miner_thread);
} }
@ -147,14 +165,14 @@ int main(int argc, char* argv[]) noexcept {
int res = -1; int res = -1;
for (int i = 1;; ++i) { for (int i = 1;; ++i) {
if (argv[i] == nullptr) break; if (argv[i] == nullptr) break;
res = parameters(conf, std::string(argv[i])); res = parameters(std::string(argv[i]));
if (res == 777) { if (res == 777) {
i++; i++;
if (argv[i] == nullptr) { if (argv[i] == nullptr) {
std::cerr << " Empty value for parameter \"" << argv[i - 1] << "\"" << std::endl; std::cerr << " Empty value for parameter \"" << argv[i - 1] << "\"" << std::endl;
return 776; return 776;
} }
int res2 = parameters(conf, std::string(std::string(argv[i - 1]) + " " + std::string(argv[i]))); int res2 = parameters(std::string(std::string(argv[i - 1]) + " " + std::string(argv[i])));
if (res2 != 0) { if (res2 != 0) {
std::cerr << " Wrong value \"" << argv[i] << "\" for parameter \"" << argv[i - 1] << "\"" << std::endl; std::cerr << " Wrong value \"" << argv[i] << "\" for parameter \"" << argv[i - 1] << "\"" << std::endl;
return res; return res;
@ -168,24 +186,6 @@ int main(int argc, char* argv[]) noexcept {
return 0; return 0;
} }
/* /*
void getRawAddress(int lErase, Key& InvertedPublicKey, Address& rawAddr) {
lErase++;
const int bitsToShift = lErase % 8;
const int start = lErase / 8;
const int invSize = start + 15;
if (bitsToShift == 0) {
for (int i = start; i < invSize; ++i)
InvertedPublicKey[i] = InvertedPublicKey[i];
} else {
const int complementary = 8 - bitsToShift;
for (int i = start; i < start + 15; ++i) {
InvertedPublicKey[i] = static_cast<unsigned char>((InvertedPublicKey[i] << bitsToShift) | (InvertedPublicKey[i + 1] >> complementary));
}
}
rawAddr[0] = 0x02;
rawAddr[1] = static_cast<unsigned char>(lErase - 1);
memcpy(&rawAddr[2], &InvertedPublicKey[start], 14);
}
void logKeys(const Address& raw, const KeysBox& keys) { void logKeys(const Address& raw, const KeysBox& keys) {
std::cout << std::endl; std::cout << std::endl;
std::cout << " Address: " << getAddress(raw) << std::endl; std::cout << " Address: " << getAddress(raw) << std::endl;