yggm/sources/main.cpp

175 lines
5.8 KiB
C++
Raw Normal View History

2025-02-28 11:37:30 +05:00
#include <sodium.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <array>
2025-03-01 01:23:03 +05:00
#include <string>
2025-02-28 11:37:30 +05:00
#include <memory.h>
#include <thread>
#include <regex>
#include <arpa/inet.h>
2025-03-01 01:23:03 +05:00
#include <immintrin.h>
using Key = std::array<unsigned char, 32>;
using Address = std::array<unsigned char, 16>;
2025-02-28 11:37:30 +05:00
struct KeysBox {
Key PublicKey;
Key PrivateKey;
};
struct option {
2025-03-01 01:23:03 +05:00
unsigned int proc = 0;
2025-03-01 04:57:59 +05:00
bool log = true;
int high = 20;
2025-02-28 11:37:30 +05:00
std::string outputfile;
};
static option conf;
2025-02-28 12:53:12 +05:00
int parameters(option& conf, std::string arg) {
if (arg.find(" ") != std::string::npos) {
const size_t npos = -1;
2025-02-28 11:37:30 +05:00
int position = arg.find(" ");
2025-03-01 01:23:03 +05:00
std::istringstream ss(arg.substr(position + 1));
2025-02-28 11:37:30 +05:00
if (arg.find("--threads") != npos || arg.find("-t") != npos) {
ss >> conf.proc;
if (ss.fail()) return 1;
return 0;
}
if (arg.find("--altitude") != npos || arg.find("-a") != npos) {
ss >> std::hex >> conf.high;
if (ss.fail()) return 1;
return 0;
}
}
2025-03-01 04:57:59 +05:00
if (arg == "--threads" || arg == "-t") return 777;
2025-02-28 11:37:30 +05:00
else if (arg == "--altitude" || arg == "-a") return 777;
return 0;
}
void displayConfig() {
2025-02-28 12:53:12 +05:00
unsigned short processor_count = std::thread::hardware_concurrency();
2025-03-01 04:57:59 +05:00
if (conf.proc == 0 || conf.proc > static_cast<unsigned int>(processor_count)) {
2025-02-28 11:37:30 +05:00
conf.proc = static_cast<unsigned int>(processor_count);
2025-03-01 04:57:59 +05:00
}
std::cout << " Threads: " << conf.proc << ", " << "high addresses (2" << std::setw(2) << std::setfill('0') << std::hex << conf.high << "+)" << std::dec << std::endl;
2025-02-28 11:37:30 +05:00
}
2025-03-01 04:57:59 +05:00
KeysBox getKeyPair() noexcept {
2025-02-28 11:37:30 +05:00
KeysBox keys;
2025-03-01 04:57:59 +05:00
crypto_sign_ed25519_keypair(keys.PublicKey.data(), keys.PrivateKey.data());
2025-02-28 11:37:30 +05:00
return keys;
}
std::string getAddress(const Address& rawAddr) {
char ipStrBuf[46];
inet_ntop(AF_INET6, rawAddr.data(), ipStrBuf, 46);
return std::string(ipStrBuf);
}
2025-03-01 01:23:03 +05:00
inline std::string keyToString(const Key& key) {
2025-02-28 12:53:12 +05:00
std::string result;
2025-03-01 04:57:59 +05:00
result.reserve(64);
2025-03-01 01:23:03 +05:00
for (int i = 0; i < 32; ++i) {
result.push_back("0123456789abcdef"[key.data()[i] >> 4]);
result.push_back("0123456789abcdef"[key.data()[i] & 0x0F]);
2025-02-28 11:37:30 +05:00
}
2025-02-28 12:53:12 +05:00
return result;
2025-02-28 11:37:30 +05:00
}
2025-03-01 04:57:59 +05:00
inline Key bitwiseInverse(const Key& key) noexcept {
2025-03-01 01:23:03 +05:00
Key inverted;
std::transform(key.begin(), key.end(), inverted.begin(), [](unsigned char byte) {
return static_cast<unsigned char>(~byte);
});
2025-02-28 11:37:30 +05:00
return inverted;
}
2025-03-01 04:57:59 +05:00
int getOnes(const Key&& value) noexcept {
2025-02-28 11:37:30 +05:00
int leadOnes = 0;
2025-02-28 12:53:12 +05:00
for (int i = 0; i < 17; ++i) {
2025-02-28 11:37:30 +05:00
if (value[i] == 0xFF) {
leadOnes += 8;
} else {
2025-03-01 01:23:03 +05:00
for (int j = 7; j >= 0; j--) {
if (value[i] & (1 << j)) {
leadOnes++;
} else {
2025-02-28 11:37:30 +05:00
return leadOnes;
2025-03-01 01:23:03 +05:00
}
2025-02-28 11:37:30 +05:00
}
}
}
return leadOnes;
}
void miner_thread() {
KeysBox keys;
2025-03-01 01:23:03 +05:00
int ones;
while (1) {
2025-02-28 11:37:30 +05:00
keys = getKeyPair();
2025-03-01 04:57:59 +05:00
if ((ones = getOnes(bitwiseInverse(keys.PublicKey))) > conf.high) {
2025-03-01 01:23:03 +05:00
conf.high = ones;
2025-03-01 04:57:59 +05:00
printf("\nAddr:\t2%x\nPK:\t%s\nSK:\t%s\n", ones, keyToString(keys.PublicKey).c_str(), keyToString(keys.PrivateKey).c_str());
2025-02-28 12:53:12 +05:00
}
2025-02-28 11:37:30 +05:00
}
}
void startThreads() {
2025-03-01 01:23:03 +05:00
std::vector<std::thread> threads;
2025-03-01 04:57:59 +05:00
for (unsigned int i = 0; i < conf.proc; i++) {
2025-03-01 01:23:03 +05:00
threads.emplace_back(miner_thread);
}
for (auto& thread : threads) {
thread.join();
2025-02-28 11:37:30 +05:00
}
}
int main(int argc, char* argv[]) {
if (argc >= 2) {
2025-02-28 12:53:12 +05:00
int res = -1;
for (int i = 1;; ++i) {
if (argv[i] == nullptr) break;
res = parameters(conf, std::string(argv[i]));
if (res == 777) {
2025-03-01 01:23:03 +05:00
i++;
2025-02-28 12:53:12 +05:00
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;
2025-02-28 11:37:30 +05:00
}
}
}
}
displayConfig();
startThreads();
2025-03-01 01:23:03 +05:00
}
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) {
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();
}
}
*/