avx2 optimized

This commit is contained in:
rcxpony 2025-03-07 03:35:04 +05:00
parent 8433194500
commit bbb884b834
2 changed files with 57 additions and 25 deletions

View File

@ -1,11 +1,12 @@
cmake_minimum_required(VERSION 3.31) cmake_minimum_required(VERSION 3.31)
project(yggm) project(yggm)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(OpenSSL REQUIRED) 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 -fomit-frame-pointer") set(CXX_ADDITIONAL_FLAGS "-mavx2 -fomit-frame-pointer -ftree-vectorize -ftree-slp-vectorize -fdelete-null-pointer-checks -fno-exceptions -fno-rtti -pthread")
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")
target_link_libraries(${PROJECT_NAME} pthread) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CXX_ADDITIONAL_FLAGS}")
target_link_libraries(${PROJECT_NAME} sodium) target_link_libraries(${PROJECT_NAME} pthread sodium)
#target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto)

View File

@ -1,4 +1,3 @@
#include <sodium.h>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
@ -6,16 +5,17 @@
#include <vector> #include <vector>
#include <array> #include <array>
#include <string> #include <string>
#include <random>
#include <memory.h> #include <memory.h>
#include <thread> #include <thread>
#include <regex> #include <regex>
#include <atomic> #include <sodium.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <immintrin.h> #include <immintrin.h>
struct option { struct option {
unsigned int proc = 0; unsigned int proc = 0;
bool log = true; bool log = true;
int high = 20; int high = 14;
std::string outputfile; std::string outputfile;
}; };
static option conf; static option conf;
@ -48,12 +48,12 @@ 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];
std::string getAddress(const Address& rawAddr) { [[gnu::unused]] std::string getAddress(const Address& rawAddr) noexcept {
char ipStrBuf[46]; char ipStrBuf[46];
inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46); inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46);
return std::string(ipStrBuf); return std::string(ipStrBuf);
} }
inline std::string keyToString(const unsigned char* key) { inline std::string keyToString(const unsigned char* key) noexcept {
std::string result; std::string result;
result.resize(64); result.resize(64);
const char* hexDigits = "0123456789abcdef"; const char* hexDigits = "0123456789abcdef";
@ -68,38 +68,68 @@ 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 {
[[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);
} }
*/ [[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
}
[[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 #pragma unroll
for (unsigned char i = 0; i < 6; i++) { for (unsigned char i = 0; i < 32; i += 4) {
if (value[i] == 0x00) { unsigned int word =
leadZeros += 8; (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;
} else { } else {
unsigned char byte = value[i]; leadZeros += zeroCounter(word);
while (!(byte & 0x80)) { break;
leadZeros++;
byte <<= 1;
}
return leadZeros;
} }
} }
return leadZeros; return leadZeros;
} }
inline long long xorshift64(unsigned long& state) {
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 {
#pragma unroll
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)));
}
}
void miner_thread() noexcept { void miner_thread() noexcept {
KeysBox keys; KeysBox keys;
unsigned char ones = 0; unsigned char ones = 0;
unsigned char seed[32];
std::random_device rd;
unsigned long state = static_cast<unsigned long>(rd());
while (true) { while (true) {
crypto_sign_ed25519_keypair(keys.PublicKey, keys.PrivateKey); randombytesavx2(seed, sizeof(seed) / sizeof(seed[0]), state);
crypto_sign_ed25519_seed_keypair(keys.PublicKey, keys.PrivateKey, seed);
ones = getZeros(keys.PublicKey); ones = getZeros(keys.PublicKey);
if (ones > conf.high) { if (ones > conf.high) {
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()); 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());
} }
} }
} }
@ -112,7 +142,7 @@ void startThreads() noexcept {
thread.join(); thread.join();
} }
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) noexcept {
if (argc >= 2) { if (argc >= 2) {
int res = -1; int res = -1;
for (int i = 1;; ++i) { for (int i = 1;; ++i) {
@ -132,6 +162,7 @@ int main(int argc, char* argv[]) {
} }
} }
} }
if (sodium_init() < 0) return -1;
displayConfig(); displayConfig();
startThreads(); startThreads();
return 0; return 0;