From 1a927ccb44b436529c643223403943f2db0585b0 Mon Sep 17 00:00:00 2001 From: Capri Date: Sun, 13 Sep 2026 17:02:13 +0000 Subject: [PATCH] Add RandomX hashing wrapper --- src/Makefile.am | 2 ++ src/crypto/randomx_hash.cpp | 35 +++++++++++++++++++++++++++++++++++ src/crypto/randomx_hash.h | 24 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/crypto/randomx_hash.cpp create mode 100644 src/crypto/randomx_hash.h diff --git a/src/Makefile.am b/src/Makefile.am index 99c74f9..7f1c40b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -550,6 +550,8 @@ libvexta_consensus_a_SOURCES = \ crypto/jh.cpp \ crypto/keccak.cpp \ crypto/luffa.cpp \ + crypto/randomx_hash.cpp \ + crypto/randomx_hash.h \ crypto/shavite.cpp \ crypto/simd.cpp \ crypto/hashodo.h \ diff --git a/src/crypto/randomx_hash.cpp b/src/crypto/randomx_hash.cpp new file mode 100644 index 0000000..83a4e27 --- /dev/null +++ b/src/crypto/randomx_hash.cpp @@ -0,0 +1,35 @@ +#include + +#include + +bool VextaRandomXHash( + const void* input, + std::size_t input_size, + const void* key, + std::size_t key_size, + uint8_t output[VEXTA_RANDOMX_HASH_SIZE]) +{ + randomx_cache* cache = randomx_alloc_cache(RANDOMX_FLAG_DEFAULT); + if (cache == nullptr) { + return false; + } + + randomx_init_cache(cache, key, key_size); + + randomx_vm* vm = randomx_create_vm( + RANDOMX_FLAG_DEFAULT, + cache, + nullptr); + + if (vm == nullptr) { + randomx_release_cache(cache); + return false; + } + + randomx_calculate_hash(vm, input, input_size, output); + + randomx_destroy_vm(vm); + randomx_release_cache(cache); + + return true; +} diff --git a/src/crypto/randomx_hash.h b/src/crypto/randomx_hash.h new file mode 100644 index 0000000..ca2150a --- /dev/null +++ b/src/crypto/randomx_hash.h @@ -0,0 +1,24 @@ +#ifndef VEXTA_CRYPTO_RANDOMX_HASH_H +#define VEXTA_CRYPTO_RANDOMX_HASH_H + +#include +#include + +static constexpr std::size_t VEXTA_RANDOMX_HASH_SIZE = 32; + +/** + * Calculate a RandomX hash using an explicit key. + * + * This wrapper intentionally uses RANDOMX_FLAG_DEFAULT so consensus hashing + * does not depend on CPU-specific runtime feature detection. + * + * Returns false if RandomX cache or VM allocation fails. + */ +bool VextaRandomXHash( + const void* input, + std::size_t input_size, + const void* key, + std::size_t key_size, + uint8_t output[VEXTA_RANDOMX_HASH_SIZE]); + +#endif // VEXTA_CRYPTO_RANDOMX_HASH_H