optimized

This commit is contained in:
rcxpony 2025-03-10 20:31:58 +05:00
parent 4458b8b368
commit 26bd90f022

View File

@ -1,20 +1,18 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <fstream> //#include <fstream>
#include <iomanip> #include <iomanip>
#include <vector> #include <vector>
#include <array>
#include <string> #include <string>
#include <random> #include <random>
#include <memory.h> #include <memory.h>
#include <thread> #include <thread>
#include <regex>
#include <sodium.h> #include <sodium.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <immintrin.h> #include <immintrin.h>
struct option { struct option {
unsigned proc = 0; unsigned proc = 0;
unsigned high = 10; std::atomic<unsigned int> high;
//std::string outputfile; //std::string outputfile;
}; };
static option conf; static option conf;
@ -47,35 +45,32 @@ 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];
const char* getAddress(const Address& rawAddr) noexcept { using Key = unsigned char[32];
char* ipStrBuf = new char[46]; inline std::string getAddress(const Address& rawAddr) noexcept {
char ipStrBuf[46];
inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46); inet_ntop(AF_INET6, rawAddr, ipStrBuf, 46);
return ipStrBuf; return std::string(ipStrBuf);
} }
inline std::string keyToString(const unsigned char* key) noexcept { inline std::string keyToString(const unsigned char* key) noexcept {
std::string result; char result[65];
result.resize(64);
const char* hexDigits = "0123456789abcdef"; const char* hexDigits = "0123456789abcdef";
for (unsigned char i = 0; i < 32; i++) { for (size_t i = 0; i < 32; ++i) {
result[2 * i] = hexDigits[key[i] >> 4]; result[2 * i] = hexDigits[key[i] >> 4];
result[2 * i + 1] = hexDigits[key[i] & 0x0F]; result[2 * i + 1] = hexDigits[key[i] & 0x0F];
} }
return result; result[64] = '\0';
return std::string(result);
} }
using Key = unsigned char[32]; typedef struct alignas(32) {
struct KeysBox {
Key PublicKey; Key PublicKey;
Key PrivateKey; Key PrivateKey;
}; } KeysBox;
void getRawAddress(int lErase, Key& InvertedPublicKey, Address& rawAddr) { void getRawAddress(int lErase, Key& InvertedPublicKey, Address& rawAddr) noexcept {
lErase++; lErase++;
const int bitsToShift = lErase % 8; const int bitsToShift = lErase % 8;
const int start = lErase / 8; const int start = lErase / 8;
if (bitsToShift == 0) { if (bitsToShift != 0) {
for (int i = start; i < start + 15; ++i) 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))); InvertedPublicKey[i] = static_cast<unsigned char>((InvertedPublicKey[i] << bitsToShift) | (InvertedPublicKey[i + 1] >> (8 - bitsToShift)));
} }
} }
@ -108,41 +103,54 @@ inline void bitwiseInverse(const unsigned char* key, Key& inverted) noexcept {
} }
return leadZeros; return leadZeros;
} }
[[nodiscard]] inline long long xorshift64(unsigned long& state) { [[nodiscard]] inline long long xorshift64(unsigned long& state) noexcept {
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 rmbytes(unsigned char* buf, unsigned char size, unsigned long& state) noexcept {
for (unsigned char x = 0; x < size; x += 32) { for (unsigned char x = 0; x < size / 32; x++) {
_mm256_storeu_si256((__m256i*) & buf[x], _mm256_set_epi64x(xorshift64(state), xorshift64(state), xorshift64(state), xorshift64(state))); _mm256_storeu_si256((__m256i*) & buf[x * 32], _mm256_set_epi64x(xorshift64(state), xorshift64(state), xorshift64(state), xorshift64(state)));
}
for (unsigned char x = 0; x < (size % 32); x++) {
buf[(size / 32) * 32 + x] = static_cast<unsigned char>(xorshift64(state) & 0xFF);
} }
} }
inline void sign_keypair(unsigned char* pk, unsigned char* sk, const unsigned char* seed) noexcept {
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);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(sk), _mm256_loadu_si256(reinterpret_cast<const __m256i*>(seed)));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(sk + 32), _mm256_loadu_si256(reinterpret_cast<const __m256i*>(pk)));
}
void miner_thread() noexcept { void miner_thread() noexcept {
Key inv; alignas(32) Key inv;
alignas(32) Key seed;
KeysBox keys; KeysBox keys;
Address rawAddr; Address rawAddr;
unsigned char ones = 0; unsigned char ones = 0;
unsigned char seed[32];
std::random_device rd; std::random_device rd;
unsigned long state = static_cast<unsigned long>(rd()); unsigned long state = static_cast<unsigned long>(rd());
printf("Using seed: %lu\n", state);
while (true) { while (true) {
randombytesavx2(seed, sizeof(seed) / sizeof(seed[0]), state); rmbytes(seed, sizeof(seed), state);
crypto_sign_ed25519_seed_keypair(keys.PublicKey, keys.PrivateKey, seed); sign_keypair(keys.PublicKey, keys.PrivateKey, seed);
//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.load()) {
conf.high = ones; conf.high.store(ones);
bitwiseInverse(keys.PublicKey, inv); bitwiseInverse(keys.PublicKey, inv);
getRawAddress(ones, inv, rawAddr); getRawAddress(ones, inv, rawAddr);
printf("\nIPv6:\t%s\nPK:\t%s\nSK:\t%s\n", getAddress(rawAddr), keyToString(keys.PublicKey).c_str(), keyToString(keys.PrivateKey).c_str()); 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());
} }
} }
} }
void startThreads() noexcept { void startThreads() noexcept {
std::vector<std::thread> threads; std::vector<std::thread> threads;
threads.reserve(conf.proc); threads.reserve(conf.proc);
for (unsigned char i = 0; i < conf.proc; i++) { for (unsigned char x = 0; x < conf.proc; x++) {
threads.emplace_back(miner_thread); threads.emplace_back(miner_thread);
} }
for (auto& thread : threads) { for (auto& thread : threads) {
@ -174,21 +182,3 @@ int main(int argc, char* argv[]) noexcept {
startThreads(); startThreads();
return 0; return 0;
} }
/*
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();
}
}
*/