diff --git a/components/equihash/tromp/blake2b.h b/components/equihash/tromp/blake2b.h index 6a0927182..23a7409b7 100644 --- a/components/equihash/tromp/blake2b.h +++ b/components/equihash/tromp/blake2b.h @@ -16,7 +16,7 @@ typedef struct BLAKE2bState BLAKE2bState; /// `personalization` MUST be a pointer to a 16-byte array. /// /// Please free this with `blake2b_free` when you are done. -BLAKE2bState* blake2b_init( +typedef BLAKE2bState* (*blake2b_init)( size_t output_len, const unsigned char* personalization); @@ -24,13 +24,13 @@ BLAKE2bState* blake2b_init( /// /// Both states need to be separately freed with `blake2b_free` when you are /// done. -BLAKE2bState* blake2b_clone(const BLAKE2bState* state); +typedef BLAKE2bState* (*blake2b_clone)(const BLAKE2bState* state); /// Frees a BLAKE2b state returned by `blake2b_init`. -void blake2b_free(BLAKE2bState* state); +typedef void (*blake2b_free)(BLAKE2bState* state); /// Adds input to the hash. You can call this any number of times. -void blake2b_update( +typedef void (*blake2b_update)( BLAKE2bState* state, const unsigned char* input, size_t input_len); @@ -43,7 +43,7 @@ void blake2b_update( /// This method is idempotent, and calling it multiple times will give the same /// result. It's also possible to call `blake2b_update` with more input in /// between. -void blake2b_finalize( +typedef void (*blake2b_finalize)( BLAKE2bState* state, unsigned char* output, size_t output_len); diff --git a/components/equihash/tromp/equi.h b/components/equihash/tromp/equi.h index 932896c58..0d159df39 100644 --- a/components/equihash/tromp/equi.h +++ b/components/equihash/tromp/equi.h @@ -43,63 +43,9 @@ typedef u32 proof[PROOFSIZE]; enum verify_code { POW_OK, POW_DUPLICATE, POW_OUT_OF_ORDER, POW_NONZERO_XOR }; const char *errstr[] = { "OK", "duplicate index", "indices out of order", "nonzero xor" }; -void genhash(const BLAKE2bState *ctx, u32 idx, uchar *hash) { - BLAKE2bState* state = blake2b_clone(ctx); - u32 leb = htole32(idx / HASHESPERBLAKE); - blake2b_update(state, (uchar *)&leb, sizeof(u32)); - uchar blakehash[HASHOUT]; - blake2b_finalize(state, blakehash, HASHOUT); - blake2b_free(state); - memcpy(hash, blakehash + (idx % HASHESPERBLAKE) * WN/8, WN/8); -} - -int verifyrec(const BLAKE2bState *ctx, u32 *indices, uchar *hash, int r) { - if (r == 0) { - genhash(ctx, *indices, hash); - return POW_OK; - } - u32 *indices1 = indices + (1 << (r-1)); - if (*indices >= *indices1) - return POW_OUT_OF_ORDER; - uchar hash0[WN/8], hash1[WN/8]; - int vrf0 = verifyrec(ctx, indices, hash0, r-1); - if (vrf0 != POW_OK) - return vrf0; - int vrf1 = verifyrec(ctx, indices1, hash1, r-1); - if (vrf1 != POW_OK) - return vrf1; - for (int i=0; i < WN/8; i++) - hash[i] = hash0[i] ^ hash1[i]; - int i, b = r * DIGITBITS; - for (i = 0; i < b/8; i++) - if (hash[i]) - return POW_NONZERO_XOR; - if ((b%8) && hash[i] >> (8-(b%8))) - return POW_NONZERO_XOR; - return POW_OK; -} - int compu32(const void *pa, const void *pb) { u32 a = *(u32 *)pa, b = *(u32 *)pb; return anthreads = n_threads; + eq->blake2b_clone = blake2b_clone; + eq->blake2b_free = blake2b_free; + eq->blake2b_update = blake2b_update; + eq->blake2b_finalize = blake2b_finalize; const int err = pthread_barrier_init(&eq->barry, NULL, eq->nthreads); assert(!err); alloctrees(eq->hta); @@ -246,11 +260,11 @@ typedef struct equi equi; dealloctrees(eq->hta); free(eq->nslots); free(eq->sols); - blake2b_free(eq->blake_ctx); + eq->blake2b_free(eq->blake_ctx); free(eq); } void equi_setstate(equi *eq, const BLAKE2bState *ctx) { - eq->blake_ctx = blake2b_clone(ctx); + eq->blake_ctx = eq->blake2b_clone(ctx); memset(eq->nslots, 0, NBUCKETS * sizeof(au32)); // only nslots[0] needs zeroing eq->nsols = 0; } @@ -464,11 +478,11 @@ typedef struct equi equi; htlayout htl = htlayout_new(eq, 0); const u32 hashbytes = hashsize(0); for (u32 block = id; block < NBLOCKS; block += eq->nthreads) { - state = blake2b_clone(eq->blake_ctx); + state = eq->blake2b_clone(eq->blake_ctx); u32 leb = htole32(block); - blake2b_update(state, (uchar *)&leb, sizeof(u32)); - blake2b_finalize(state, hash, HASHOUT); - blake2b_free(state); + eq->blake2b_update(state, (uchar *)&leb, sizeof(u32)); + eq->blake2b_finalize(state, hash, HASHOUT); + eq->blake2b_free(state); for (u32 i = 0; i