5#include "hashx_thread.h"
6#include "hashx_endian.h"
25static hashx_thread_retval worker(
void* args) {
27 job->total_hashes = 0;
28 job->best_hash = UINT64_MAX;
29 for (
int seed = job->start; seed < job->end; seed += job->step) {
31 hashx_result result = hashx_make(job->ctx, &seed,
sizeof(seed));
32 if (result == HASHX_FAIL_SEED) {
35 if (result == HASHX_FAIL_COMPILE) {
36 printf(
"Error: not supported. Try with --interpret\n");
38 assert(result == HASHX_OK);
39 if (result != HASHX_OK)
42 for (
int nonce = 0; nonce < job->nonces; ++nonce) {
43 uint8_t hash[HASHX_SIZE] = { 0 };
45#ifndef HASHX_BLOCK_MODE
46 hashx_result result = hashx_exec(job->ctx, nonce, hash);
48 hashx_result result = hashx_exec(job->ctx,
49 &nonce,
sizeof(nonce), hash);
51 assert(result == HASHX_OK);
52 if (result != HASHX_OK)
55 uint64_t hashval = load64(hash);
56 if (hashval < job->best_hash) {
57 job->best_hash = hashval;
59 if (hashval < job->threshold) {
60 printf(
"[thread %2i] Hash (%5i, %5i) below threshold:"
61 " ...%02x%02x%02x%02x%02x%02x%02x%02x\n",
75 job->total_hashes += job->nonces;
77 return HASHX_THREAD_SUCCESS;
80int main(
int argc,
char** argv) {
81 int nonces, seeds, start, diff, threads;
83 read_int_option(
"--diff", argc, argv, &diff, INT_MAX);
84 read_int_option(
"--start", argc, argv, &start, 0);
85 read_int_option(
"--seeds", argc, argv, &seeds, 500);
86 read_int_option(
"--nonces", argc, argv, &nonces, 65536);
87 read_int_option(
"--threads", argc, argv, &threads, 1);
88 read_option(
"--interpret", argc, argv, &interpret);
89 hashx_type ctx_type = HASHX_TYPE_INTERPRETED;
91 ctx_type = HASHX_TYPE_COMPILED;
93 uint64_t best_hash = UINT64_MAX;
94 uint64_t diff_ex = (uint64_t)diff * 1000ULL;
95 uint64_t threshold = UINT64_MAX / diff_ex;
96 int seeds_end = seeds + start;
97 int64_t total_hashes = 0;
98 printf(
"Interpret: %i, Target diff.: %" PRIu64
", Threads: %i\n", interpret, diff_ex, threads);
99 printf(
"Testing seeds %i-%i with %i nonces each ...\n", start, seeds_end - 1, nonces);
100 double time_start, time_end;
103 printf(
"Error: memory allocation failure\n");
106 for (
int thd = 0; thd < threads; ++thd) {
107 jobs[thd].ctx = hashx_alloc(ctx_type);
108 if (jobs[thd].ctx == NULL) {
109 printf(
"Error: memory allocation failure\n");
113 jobs[thd].start = start + thd;
114 jobs[thd].step = threads;
115 jobs[thd].end = seeds_end;
116 jobs[thd].nonces = nonces;
117 jobs[thd].threshold = threshold;
119 time_start = hashx_time();
121 for (
int thd = 0; thd < threads; ++thd) {
122 jobs[thd].thread = hashx_thread_create(&worker, &jobs[thd]);
124 for (
int thd = 0; thd < threads; ++thd) {
125 hashx_thread_join(jobs[thd].thread);
131 time_end = hashx_time();
132 for (
int thd = 0; thd < threads; ++thd) {
133 total_hashes += jobs[thd].total_hashes;
134 if (jobs[thd].best_hash < best_hash) {
135 best_hash = jobs[thd].best_hash;
138 double elapsed = time_end - time_start;
139 printf(
"Total hashes: %" PRIi64
"\n", total_hashes);
140 printf(
"%f hashes/sec.\n", total_hashes / elapsed);
141 printf(
"%f seeds/sec.\n", seeds / elapsed);
142 printf(
"Best hash: ...");
143 output_hex((
char*)&best_hash,
sizeof(best_hash));
144 printf(
" (diff: %" PRIu64
")\n", UINT64_MAX / best_hash);
int main(int argc, char *argv[])