Files
yespower_arm_algo_optimized/create_genesis.cpp
2024-12-23 10:47:22 +01:00

153 lines
4.5 KiB
C++

#include <iostream>
#include <string>
#include "yespower.h"
#include <vector>
#include <array>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <thread>
#include <atomic>
#include <mutex>
yespower_params_t yespower_params = { .version = YESPOWER_1_0, .N = 2048, .r = 16 };
using uint256 = std::array<uint8_t, 32>;
uint256 CalculateMerkleRoot(const std::string &genesisMessage, const std::string &publicKey) {
uint256 merkleRoot = {};
std::string combined = genesisMessage + publicKey;
std::hash<std::string> hasher;
size_t hash = hasher(combined);
memcpy(merkleRoot.data(), &hash, sizeof(hash));
return merkleRoot;
}
struct CBlockHeader {
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;
uint256 hashMerkleRoot;
std::string Serialize() const {
std::ostringstream ss;
ss << nTime << nBits << nNonce;
for (const auto &byte : hashMerkleRoot) {
ss << std::hex << std::setw(2) << std::setfill('0') << (int)byte;
}
return ss.str();
}
};
class arith_uint256 {
uint64_t value = 0;
public:
void SetCompact(uint32_t nBits) {
value = nBits;
}
bool operator<=(const arith_uint256 &other) const {
return value <= other.value;
}
std::string GetHex() const {
std::ostringstream ss;
ss << std::hex << value;
return ss.str();
}
};
std::atomic<bool> found(false);
std::mutex printMutex;
void MineThread(CBlockHeader genesisBlock, arith_uint256 hashTarget, int threadID, int numThreads) {
uint256 resultHash = {};
uint32_t nonce = threadID;
while (!found) {
genesisBlock.nNonce = nonce;
std::string serializedHeader = genesisBlock.Serialize();
yespower_tls(reinterpret_cast<const uint8_t *>(serializedHeader.c_str()),
serializedHeader.size(),
&yespower_params,
reinterpret_cast<yespower_binary_t *>(resultHash.data()));
arith_uint256 newhash;
memcpy(&newhash, resultHash.data(), sizeof(newhash));
if (newhash <= hashTarget) {
found = true;
std::lock_guard<std::mutex> lock(printMutex);
std::cout << "\nGenesis Block Found by Thread " << threadID << "!\n";
std::cout << "Hash: ";
for (auto byte : resultHash) {
printf("%02x", byte);
}
std::cout << "\nnTime: " << genesisBlock.nTime << "\n";
std::cout << "nNonce: " << genesisBlock.nNonce << "\n";
std::cout << "Merkle Root: ";
for (auto byte : genesisBlock.hashMerkleRoot) {
printf("%02x", byte);
}
std::cout << "\n";
break;
}
nonce += numThreads;
}
}
void MineGenesis() {
const uint32_t nBits = 0x1e00ffff;
const std::string genesisMessage = "One coin to rule them all One coin to find them One coin to bring them And in the darkness bind them";
const std::string publicKey = "043056301006072a8648ce3d020106052b8104000a034200042d23252e243d07ee9a6bcf82200b6f98748a90323b36513ea3f62165d75b12f4269cb91e3c0a01572ab64997e62615e9b25ac97413eccabb2391eebc4c633dd7";
CBlockHeader genesisBlock;
genesisBlock.nTime = std::time(0);
genesisBlock.nBits = nBits;
genesisBlock.nNonce = 0;
genesisBlock.hashMerkleRoot = CalculateMerkleRoot(genesisMessage, publicKey);
std::cout << "Genesis Block Parameters:\n";
std::cout << "nTime: " << genesisBlock.nTime << "\n";
std::cout << "nBits: " << std::hex << nBits << "\n";
std::cout << "Public Key: " << publicKey << "\n";
std::cout << "Genesis Message: " << genesisMessage << "\n";
arith_uint256 hashTarget;
hashTarget.SetCompact(nBits);
int maxThreads = std::thread::hardware_concurrency();
int numThreads = static_cast<int>(maxThreads * 0.8); // Verwende 80% der verfügbaren Threads
std::vector<std::thread> threads;
for (int i = 0; i < numThreads; ++i) {
threads.emplace_back(MineThread, genesisBlock, hashTarget, i, numThreads);
}
for (auto &t : threads) {
t.join();
}
if (!found) {
std::cout << "Genesis Block not found. Adjust parameters and retry.\n";
}
}
int main() {
MineGenesis();
return 0;
}
# create public key with my script
# compile
# g++ -o genesis create_genesis.cpp -lyespower
# start
# ./genesis