optimized

This commit is contained in:
fluttershy 2025-03-01 01:23:03 +05:00
parent 120d761c80
commit f6474b9d9c
2 changed files with 95 additions and 121 deletions

5
.gitignore vendored
View File

@ -1,2 +1,5 @@
build/
.vscode/
.vscode/
libs/
*.o
*.cu

View File

@ -1,11 +1,3 @@
/*
* Address miner for Yggdrsail Network 0.4.x and higher.
*
* developers: Vort, acetone, R4SAS, lialh4, filarius, orignal
* developers team, 2021 (c) GPLv3
*
*/
#include <sodium.h>
#include <iostream>
#include <sstream>
@ -13,23 +5,21 @@
#include <iomanip>
#include <vector>
#include <array>
#include <string.h>
#include <string>
#include <memory.h>
#include <thread>
#include <regex>
#include <arpa/inet.h>
#include <cppcodec/base32_rfc4648.hpp>
#include <immintrin.h>
int countsize = 0;
uint64_t totalcount = 0;
bool newline = false;
typedef std::array<unsigned char, 32> Key;
typedef std::array<unsigned char, 16> Address;
using Key = std::array<unsigned char, 32>;
using Address = std::array<unsigned char, 16>;
struct KeysBox {
Key PublicKey;
Key PrivateKey;
};
struct option {
unsigned int proc = 0; // количество потоков
unsigned int proc = 0;
int mode = 1; // режим майнинга
bool log = true; // логгирование
int high = 20; // начальная высота при майнинге: dec(20) == hex(14)
@ -37,17 +27,16 @@ struct option {
bool mesh = false; // отображение meshname-доменов
bool fullkeys = true; // отображение секретного ключа в консоли в полном формате
std::string str = "aaaa";
std::string outputfile;
int sbt_size = 7; // 64b/8 = 8B, нумерация с нуля
bool sbt_alarm = false; // для симпатичного вывода предупреждения
int sbt_size = 7;
bool sbt_alarm = false;
};
static option conf;
int parameters(option& conf, std::string arg) {
if (arg.find(" ") != std::string::npos) {
const size_t npos = -1;
int position = arg.find(" ");
std::istringstream ss(arg.substr(position + 1)); // Поток нужен для проверки корректности и конвертации
std::istringstream ss(arg.substr(position + 1));
if (arg.find("--threads") != npos || arg.find("-t") != npos) {
ss >> conf.proc;
if (ss.fail()) return 1;
@ -129,17 +118,91 @@ std::string getAddress(const Address& rawAddr) {
inet_ntop(AF_INET6, rawAddr.data(), ipStrBuf, 46);
return std::string(ipStrBuf);
}
inline std::string hexArrayToString(const unsigned char* bytes, int length) {
inline std::string keyToString(const Key& key) {
std::string result;
result.reserve(length * 2);
for (int i = 0; i < length; ++i) {
result.push_back("0123456789abcdef"[bytes[i] >> 4]);
result.push_back("0123456789abcdef"[bytes[i] & 0x0F]);
for (int i = 0; i < 32; ++i) {
result.push_back("0123456789abcdef"[key.data()[i] >> 4]);
result.push_back("0123456789abcdef"[key.data()[i] & 0x0F]);
}
return result;
}
inline std::string keyToString(const Key& key) {
return hexArrayToString(key.data(), 32);
inline Key bitwiseInverse(const Key& key) {
Key inverted;
std::transform(key.begin(), key.end(), inverted.begin(), [](unsigned char byte) {
return static_cast<unsigned char>(~byte);
});
return inverted;
}
int getOnes(const Key& value) {
int leadOnes = 0;
for (int i = 0; i < 17; ++i) {
if (value[i] == 0xFF) {
leadOnes += 8;
} else {
for (int j = 7; j >= 0; j--) {
if (value[i] & (1 << j)) {
leadOnes++;
} else {
return leadOnes;
}
}
}
}
return leadOnes;
}
void printKeys(int ones, const KeysBox& keys) {
char buffer[256];
int len = snprintf(buffer, sizeof(buffer), "\nAddr:\t%x\nPK:\t%s\nSK:\t%s\n", ones, keyToString(keys.PublicKey).c_str(), keyToString(keys.PrivateKey).c_str());
if (len > 0 && len < static_cast<int>(sizeof(buffer))) {
printf("%s", buffer);
} else {
fprintf(stderr, "Buffer overflow or error in snprintf\n");
}
}
void miner_thread() {
KeysBox keys;
Key inv;
int ones;
while (1) {
keys = getKeyPair();
inv = bitwiseInverse(keys.PublicKey);
if ((ones = getOnes(inv)) > conf.high) {
conf.high = ones;
printKeys(ones, keys);
}
}
}
void startThreads() {
std::vector<std::thread> threads;
for (unsigned int i = 0; i < std::thread::hardware_concurrency(); ++i) {
threads.emplace_back(miner_thread);
}
for (auto& thread : threads) {
thread.join();
}
}
int main(int argc, char* argv[]) {
if (argc >= 2) {
int res = -1;
for (int i = 1;; ++i) {
if (argv[i] == nullptr) break;
res = parameters(conf, std::string(argv[i]));
if (res == 777) {
i++;
if (argv[i] == nullptr) {
std::cerr << " Empty value for parameter \"" << argv[i - 1] << "\"" << std::endl;
return 776;
}
int res2 = parameters(conf, std::string(std::string(argv[i - 1]) + " " + std::string(argv[i])));
if (res2 != 0) {
std::cerr << " Wrong value \"" << argv[i] << "\" for parameter \"" << argv[i - 1] << "\"" << std::endl;
return res;
}
}
}
}
displayConfig();
startThreads();
}
void getRawAddress(int lErase, Key& InvertedPublicKey, Address& rawAddr) {
lErase++;
@ -159,27 +222,7 @@ void getRawAddress(int lErase, Key& InvertedPublicKey, Address& rawAddr) {
rawAddr[1] = static_cast<unsigned char>(lErase - 1);
memcpy(&rawAddr[2], &InvertedPublicKey[start], 14);
}
inline Key bitwiseInverse(const Key& key) {
Key inverted{};
for (unsigned long i = 0; i < key.size(); i++) inverted[i] = static_cast<unsigned char>(~key[i]);
return inverted;
}
int getOnes(const Key& value) {
int leadOnes = 0;
for (int i = 0; i < 17; ++i) {
if (value[i] == 0xFF) {
leadOnes += 8;
} else {
for (int j = 7; j >= 0; --j) {
if (value[i] & (1 << j))
++leadOnes;
else
return leadOnes;
}
}
}
return leadOnes;
}
/*
void logKeys(const Address& raw, const KeysBox& keys) {
std::cout << std::endl;
std::cout << " Address: " << getAddress(raw) << std::endl;
@ -196,76 +239,4 @@ void logKeys(const Address& raw, const KeysBox& keys) {
output.close();
}
}
void process_fortune_key(const KeysBox& keys) {
Key invKey = bitwiseInverse(keys.PublicKey);
Address rawAddr;
getRawAddress(getOnes(invKey), invKey, rawAddr);
logKeys(rawAddr, keys);
}
template <int T>
void miner_thread() {
Address rawAddr;
KeysBox keys;
Key invKey;
while (true) {
keys = getKeyPair();
invKey = bitwiseInverse(keys.PublicKey);
int ones = getOnes(invKey);
getRawAddress(ones, invKey, rawAddr);
bool shouldProcess = false;
if constexpr (T == 0) {
shouldProcess = (getAddress(rawAddr).find(conf.str) != std::string::npos);
} else if constexpr (T == 1) {
if (ones > conf.high) {
if (conf.letsup != 0) {
conf.high = ones;
}
shouldProcess = true;
}
}
if (shouldProcess) {
process_fortune_key(keys);
}
}
}
void startThreads() {
for (unsigned int i = 0; i < conf.proc; ++i) {
std::thread* thread = new std::thread(
conf.mode == 0 ? miner_thread<0> : miner_thread<1>
);
if (i + 1 < conf.proc) thread->detach();
else thread->join();
}
}
void error(int code) {
std::cerr << std::endl << "\
+--------------------------------------------------------------------------+\n\
| Incorrect input, my dear friend. Use --help for usage information. |\n\
+--------------------------------------------------------------------------+\n\
Error code: " << code << std::endl;
}
int main(int argc, char* argv[]) {
if (argc >= 2) {
int res = -1;
for (int i = 1;; ++i) {
if (argv[i] == nullptr) break;
res = parameters(conf, std::string(argv[i]));
if (res == 777) {
++i;
if (argv[i] == nullptr) {
error(776);
std::cerr << " Empty value for parameter \"" << argv[i - 1] << "\"" << std::endl;
return 776;
}
int res2 = parameters(conf, std::string(std::string(argv[i - 1]) + " " + std::string(argv[i])));
if (res2 != 0) {
error(res);
std::cerr << " Wrong value \"" << argv[i] << "\" for parameter \"" << argv[i - 1] << "\"" << std::endl;
return res;
}
}
}
}
displayConfig();
startThreads();
}
*/