optimized

This commit is contained in:
fluttershy 2025-03-01 18:02:11 +05:00
parent 3fb7beb701
commit 193575da8a
2 changed files with 20 additions and 23 deletions

View File

@ -1,8 +1,10 @@
cmake_minimum_required(VERSION 3.31) cmake_minimum_required(VERSION 3.31)
project(yggm) project(yggm)
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(CMAKE_CXX_FLAGS_RELEASE "-march=native -O3 -ffast-math -ftree-vectorize -fdelete-null-pointer-checks -fno-exceptions -fno-rtti -finline-functions -pthread -fomit-frame-pointer -pipe -Wall -Wextra -flto=full") set(CMAKE_CXX_FLAGS_RELEASE "-march=native -O3 -ffast-math -ftree-vectorize -fdelete-null-pointer-checks -fno-exceptions -fno-rtti -finline-functions -pthread -fomit-frame-pointer -pipe -Wall -Wextra -flto=full")
target_link_libraries(${PROJECT_NAME} pthread) target_link_libraries(${PROJECT_NAME} pthread)
target_link_libraries(${PROJECT_NAME} sodium) target_link_libraries(${PROJECT_NAME} sodium)
target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto)

View File

@ -12,8 +12,10 @@
#include <atomic> #include <atomic>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <immintrin.h> #include <immintrin.h>
using Key = std::array<unsigned char, 32>; //using Key = std::array<unsigned char, 32>;
using Address = std::array<unsigned char, 16>; //using Address = std::array<unsigned char, 16>;
using Address = unsigned char[16];
using Key = unsigned char[32];
struct KeysBox { struct KeysBox {
Key PublicKey; Key PublicKey;
Key PrivateKey; Key PrivateKey;
@ -21,7 +23,7 @@ struct KeysBox {
struct option { struct option {
unsigned int proc = 0; unsigned int proc = 0;
bool log = true; bool log = true;
std::atomic<int> high{ 20 }; int high = 20;
std::string outputfile; std::string outputfile;
}; };
static option conf; static option conf;
@ -57,15 +59,15 @@ void displayConfig() {
} }
KeysBox getKeyPair() noexcept { KeysBox getKeyPair() noexcept {
KeysBox keys; KeysBox keys;
crypto_sign_ed25519_keypair(keys.PublicKey.data(), keys.PrivateKey.data()); crypto_sign_ed25519_keypair(keys.PublicKey, keys.PrivateKey);
return keys; return keys;
} }
std::string getAddress(const Address& rawAddr) { std::string getAddress(const Address& rawAddr) {
char ipStrBuf[46]; char ipStrBuf[46];
inet_ntop(AF_INET6, rawAddr.data(), ipStrBuf, 46); inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46);
return std::string(ipStrBuf); return std::string(ipStrBuf);
} }
inline std::string keyToString(const Key& key) { inline std::string keyToString(const unsigned char* key) {
std::string result; std::string result;
result.resize(64); result.resize(64);
const char* hexDigits = "0123456789abcdef"; const char* hexDigits = "0123456789abcdef";
@ -75,17 +77,14 @@ inline std::string keyToString(const Key& key) {
} }
return result; return result;
} }
inline void bitwiseInverse(const unsigned char* key, Key inverted) noexcept {
inline Key bitwiseInverse(const Key& key) noexcept { for (size_t i = 0; i < 32; i++) {
Key inverted;
for (unsigned long i = 0; i < 32; i++) {
inverted[i] = ~key[i]; inverted[i] = ~key[i];
} }
return inverted;
} }
int getOnes(const Key&& value) noexcept { int getOnes(const Key value) noexcept {
int leadOnes = 0; int leadOnes = 0;
for (unsigned char i = 0; i < 17; ++i) { for (unsigned char i = 0; i < 5; i++) {
if (value[i] == 0xFF) { if (value[i] == 0xFF) {
leadOnes += 8; leadOnes += 8;
} else { } else {
@ -101,17 +100,13 @@ int getOnes(const Key&& value) noexcept {
return leadOnes; return leadOnes;
} }
void miner_thread() { void miner_thread() {
Key inv;
while (true) { while (true) {
KeysBox keys = getKeyPair(); KeysBox keys = getKeyPair();
int ones = getOnes(bitwiseInverse(keys.PublicKey)); bitwiseInverse(keys.PublicKey, inv);
int current = conf.high.load(std::memory_order_relaxed); int ones = getOnes(inv);
if (ones > current) { if (ones > conf.high) {
while (!conf.high.compare_exchange_weak(current, ones, std::memory_order_relaxed)) { printf("\nAddr:\t2%x/%d\nPK:\t%s\nSK:\t%s\n", conf.high = ones, ones, keyToString(keys.PublicKey).c_str(), keyToString(keys.PrivateKey).c_str());
if (ones <= current) break;
}
if (ones > current) {
printf("\nAddr:\t2%x\nPK:\t%s\nSK:\t%s\n", ones, keyToString(keys.PublicKey).c_str(), keyToString(keys.PrivateKey).c_str());
}
} }
} }
} }