Skip to content

Commit 1c603a2

Browse files
authored
Merge pull request #265 from tevador/pr-randomx-v2
RandomX commitments with double-hashing
2 parents e372827 + b89deae commit 1c603a2

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

src/randomx.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,15 @@ extern "C" {
417417
machine->run(machine->tempHash);
418418
machine->getFinalResult(output, RANDOMX_HASH_SIZE);
419419
}
420+
421+
void randomx_calculate_commitment(const void* input, size_t inputSize, const void* hash_in, void* com_out) {
422+
assert(inputSize == 0 || input != nullptr);
423+
assert(hash_in != nullptr);
424+
assert(com_out != nullptr);
425+
blake2b_state state;
426+
blake2b_init(&state, RANDOMX_HASH_SIZE);
427+
blake2b_update(&state, input, inputSize);
428+
blake2b_update(&state, hash_in, RANDOMX_HASH_SIZE);
429+
blake2b_final(&state, com_out, RANDOMX_HASH_SIZE);
430+
}
420431
}

src/randomx.h

+11
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ RANDOMX_EXPORT void randomx_calculate_hash_first(randomx_vm* machine, const void
260260
RANDOMX_EXPORT void randomx_calculate_hash_next(randomx_vm* machine, const void* nextInput, size_t nextInputSize, void* output);
261261
RANDOMX_EXPORT void randomx_calculate_hash_last(randomx_vm* machine, void* output);
262262

263+
/**
264+
* Calculate a RandomX commitment from a RandomX hash and its input.
265+
*
266+
* @param input is a pointer to memory that was hashed. Must not be NULL.
267+
* @param inputSize is the number of bytes in the input.
268+
* @param hash_in is the output from randomx_calculate_hash* (RANDOMX_HASH_SIZE bytes).
269+
* @param com_out is a pointer to memory where the commitment will be stored. Must not
270+
* be NULL and at least RANDOMX_HASH_SIZE bytes must be available for writing.
271+
*/
272+
RANDOMX_EXPORT void randomx_calculate_commitment(const void* input, size_t inputSize, const void* hash_in, void* com_out);
273+
263274
#if defined(__cplusplus)
264275
}
265276
#endif

src/tests/benchmark.cpp

+25-7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void printUsage(const char* executable) {
9696
std::cout << " --avx2 use optimized Argon2 for AVX2 CPUs" << std::endl;
9797
std::cout << " --auto select the best options for the current CPU" << std::endl;
9898
std::cout << " --noBatch calculate hashes one by one (default: batch)" << std::endl;
99+
std::cout << " --commit calculate commitments instead of hashes (default: hashes)" << std::endl;
99100
}
100101

101102
struct MemoryException : public std::exception {
@@ -113,7 +114,7 @@ struct DatasetAllocException : public MemoryException {
113114

114115
using MineFunc = void(randomx_vm * vm, std::atomic<uint32_t> & atomicNonce, AtomicHash & result, uint32_t noncesCount, int thread, int cpuid);
115116

116-
template<bool batch>
117+
template<bool batch, bool commit>
117118
void mine(randomx_vm* vm, std::atomic<uint32_t>& atomicNonce, AtomicHash& result, uint32_t noncesCount, int thread, int cpuid = -1) {
118119
if (cpuid >= 0) {
119120
int rc = set_thread_affinity(cpuid);
@@ -138,6 +139,9 @@ void mine(randomx_vm* vm, std::atomic<uint32_t>& atomicNonce, AtomicHash& result
138139
}
139140
store32(noncePtr, nonce);
140141
(batch ? randomx_calculate_hash_next : randomx_calculate_hash)(vm, blockTemplate, sizeof(blockTemplate), &hash);
142+
if (commit) {
143+
randomx_calculate_commitment(blockTemplate, sizeof(blockTemplate), &hash, &hash);
144+
}
141145
result.xorWith(hash);
142146
if (!batch) {
143147
nonce = atomicNonce.fetch_add(1);
@@ -146,7 +150,7 @@ void mine(randomx_vm* vm, std::atomic<uint32_t>& atomicNonce, AtomicHash& result
146150
}
147151

148152
int main(int argc, char** argv) {
149-
bool softAes, miningMode, verificationMode, help, largePages, jit, secure;
153+
bool softAes, miningMode, verificationMode, help, largePages, jit, secure, commit;
150154
bool ssse3, avx2, autoFlags, noBatch;
151155
int noncesCount, threadCount, initThreadCount;
152156
uint64_t threadAffinity;
@@ -172,10 +176,11 @@ int main(int argc, char** argv) {
172176
readOption("--avx2", argc, argv, avx2);
173177
readOption("--auto", argc, argv, autoFlags);
174178
readOption("--noBatch", argc, argv, noBatch);
179+
readOption("--commit", argc, argv, commit);
175180

176181
store32(&seed, seedValue);
177182

178-
std::cout << "RandomX benchmark v1.1.11" << std::endl;
183+
std::cout << "RandomX benchmark v1.1.12" << std::endl;
179184

180185
if (help) {
181186
printUsage(argv[0]);
@@ -280,11 +285,24 @@ int main(int argc, char** argv) {
280285
MineFunc* func;
281286

282287
if (noBatch) {
283-
func = &mine<false>;
288+
if (commit) {
289+
std::cout << " - hash commitments" << std::endl;
290+
func = &mine<false, true>;
291+
}
292+
else {
293+
func = &mine<false, false>;
294+
}
284295
}
285296
else {
286-
func = &mine<true>;
287-
std::cout << " - batch mode" << std::endl;
297+
if (commit) {
298+
//TODO: support batch mode with commitments
299+
std::cout << " - hash commitments" << std::endl;
300+
func = &mine<false, true>;
301+
}
302+
else {
303+
std::cout << " - batch mode" << std::endl;
304+
func = &mine<true, false>;
305+
}
288306
}
289307

290308
std::cout << "Initializing";
@@ -376,7 +394,7 @@ int main(int argc, char** argv) {
376394
randomx_release_cache(cache);
377395
std::cout << "Calculated result: ";
378396
result.print(std::cout);
379-
if (noncesCount == 1000 && seedValue == 0)
397+
if (noncesCount == 1000 && seedValue == 0 && !commit)
380398
std::cout << "Reference result: 10b649a3f15c7c7f88277812f2e74b337a0f20ce909af09199cccb960771cfa1" << std::endl;
381399
if (!miningMode) {
382400
std::cout << "Performance: " << 1000 * elapsed / noncesCount << " ms per hash" << std::endl;

src/tests/tests.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ void calcStringHash(const char(&key)[K], const char(&input)[H], void* output) {
3434
randomx_calculate_hash(vm, input, H - 1, output);
3535
}
3636

37+
template<size_t K, size_t H>
38+
void calcStringCommitment(const char(&key)[K], const char(&input)[H], void* output) {
39+
initCache(key);
40+
assert(vm != nullptr);
41+
randomx_calculate_hash(vm, input, H - 1, output);
42+
randomx_calculate_commitment(input, H - 1, output, output);
43+
}
44+
3745
template<size_t K, size_t H>
3846
void calcHexHash(const char(&key)[K], const char(&hex)[H], void* output) {
3947
initCache(key);
@@ -1082,6 +1090,22 @@ int main() {
10821090
assert(rx_get_rounding_mode() == RoundToNearest);
10831091
});
10841092

1093+
if (RANDOMX_HAVE_COMPILER) {
1094+
randomx_destroy_vm(vm);
1095+
vm = nullptr;
1096+
#ifdef RANDOMX_FORCE_SECURE
1097+
vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT | RANDOMX_FLAG_SECURE, cache, nullptr);
1098+
#else
1099+
vm = randomx_create_vm(RANDOMX_FLAG_DEFAULT, cache, nullptr);
1100+
#endif
1101+
}
1102+
1103+
runTest("Commitment test", stringsEqual(RANDOMX_ARGON_SALT, "RandomX\x03"), []() {
1104+
char hash[RANDOMX_HASH_SIZE];
1105+
calcStringCommitment("test key 000", "This is a test", &hash);
1106+
assert(equalsHex(hash, "d53ccf348b75291b7be76f0a7ac8208bbced734b912f6fca60539ab6f86be919"));
1107+
});
1108+
10851109
randomx_destroy_vm(vm);
10861110
vm = nullptr;
10871111

0 commit comments

Comments
 (0)