git / moonbite-core / src/crypto/randomx_hasher.cpp · raw
// Copyright (c) 2026 The MoonBite Core developers// Distributed under the MIT software license, see the accompanying// file COPYING or http://www.opensource.org/licenses/mit-license.php.#include <crypto/randomx_hasher.h>#include <crypto/randomx/randomx.h>#include <cstring>#include <mutex>// Nothing-up-my-sleeve consensus seed (ADR-001, revised for the standard-miner// launch). RandomX keys its cache from this value; a value distinct from// Monero's live seed means rented Monero hashpower cannot point at MoonBite// without first recomputing the dataset for this key.//// It is exactly 32 bytes so it matches the Monero/XMRig job "seed_hash" format:// the pool advertises these 32 bytes, stock XMRig calls randomx_init_cache with// them, and this node keys its cache identically, so every party computes the// same hash. The bytes are SHA-256("MBITE-RX-1") - derived from the original// 10-byte salt, so the provenance is verifiable and nothing is hidden:// python3 -c 'import hashlib;print(hashlib.sha256(b"MBITE-RX-1").hexdigest())'// -> 00f960e6d9e1d7255dc2ae70d8cd44e6ceb69d9a924ffc34e2c94d46bf496953const unsigned char MBITE_RX_SEED[MBITE_RX_SEED_LEN] = { 0x00,0xf9,0x60,0xe6,0xd9,0xe1,0xd7,0x25,0x5d,0xc2,0xae,0x70,0xd8,0xcd,0x44,0xe6, 0xce,0xb6,0x9d,0x9a,0x92,0x4f,0xfc,0x34,0xe2,0xc9,0x4d,0x46,0xbf,0x49,0x69,0x53,};namespace {std::mutex g_mutex;randomx_cache* g_cache = nullptr;randomx_vm* g_vm = nullptr;// Initialise the shared light-mode VM once. Light mode (cache only, no ~2 GB// dataset) keeps validation RAM near ~256 MB; miners may opt into fast mode// separately. The produced hash is identical in either mode.void EnsureInit(){ if (g_vm) return; randomx_flags flags = randomx_get_flags(); // JIT/AES autodetect; hash is flag-independent g_cache = randomx_alloc_cache(flags); if (g_cache == nullptr) { // JIT/large-page allocation can fail on locked-down hosts; fall back to // the portable interpreter path so validation never wedges. flags = RANDOMX_FLAG_DEFAULT; g_cache = randomx_alloc_cache(flags); } randomx_init_cache(g_cache, MBITE_RX_SEED, MBITE_RX_SEED_LEN); g_vm = randomx_create_vm(flags, g_cache, nullptr); // light mode}} // namespacevoid RandomXHashBytes(const void* input, size_t inputSize, unsigned char output[32]){ std::lock_guard<std::mutex> lock(g_mutex); EnsureInit(); randomx_calculate_hash(g_vm, input, inputSize, output);}