|
| 1 | +#define _BSD_SOURCE |
| 2 | +#include <stdint.h> |
| 3 | +#include <iso646.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <string.h> |
| 6 | +#include <openssl/sha.h> |
| 7 | +#include "proofofwork-private.h" |
| 8 | + |
| 9 | +uint64_t pow_sha256_count = 0; // for benchmark |
| 10 | +bool pow_sha256_mine(uint8_t const *mask, uint8_t const *target, uint8_t *buffer, uint64_t size, int32_t const *indices) { |
| 11 | + // check arguments |
| 12 | + static_assert (__BYTE_ORDER == __LITTLE_ENDIAN, ""); |
| 13 | + if (mask == NULL) return false; |
| 14 | + if (target == NULL) return false; |
| 15 | + if (buffer == NULL) return false; |
| 16 | + if (indices == NULL) return false; |
| 17 | + for (int i = 0; i < pow_indices_length; ++ i) { |
| 18 | + if (indices[i] < -1 or (int64_t)size <= indices[i]) return false; |
| 19 | + } |
| 20 | + if (indices[0] == -1) return false; |
| 21 | + if (size > pow_sha256_block_length - sizeof(uint64_t) / CHAR_BIT - 1) return false; |
| 22 | + |
| 23 | + // search |
| 24 | + bool found = false; |
| 25 | + uint64_t cnt = 0; |
| 26 | + static_assert (pow_indices_length == 8, ""); |
| 27 | + repeat (i7, alphabet_size) { if (indices[7] != -1) buffer[indices[7]] = alphabet[i7]; |
| 28 | + repeat (i6, alphabet_size) { if (indices[6] != -1) buffer[indices[6]] = alphabet[i6]; |
| 29 | + repeat (i5, alphabet_size) { if (indices[5] != -1) buffer[indices[5]] = alphabet[i5]; |
| 30 | + repeat (i4, alphabet_size) { if (indices[4] != -1) buffer[indices[4]] = alphabet[i4]; |
| 31 | + repeat (i3, alphabet_size) { if (indices[3] != -1) buffer[indices[3]] = alphabet[i3]; |
| 32 | + repeat (i2, alphabet_size) { if (indices[2] != -1) buffer[indices[2]] = alphabet[i2]; |
| 33 | + repeat (i1, alphabet_size) { if (indices[1] != -1) buffer[indices[1]] = alphabet[i1]; |
| 34 | + repeat (i0, alphabet_size) { if (indices[0] != -1) buffer[indices[0]] = alphabet[i0]; |
| 35 | + uint8_t digest[pow_sha256_digest_length]; |
| 36 | + SHA256(buffer, size, digest); |
| 37 | + ++ cnt; |
| 38 | + found = true; |
| 39 | + repeat (j, pow_sha256_digest_length) { |
| 40 | + if ((digest[j] & mask[j]) != (target[j] & mask[j])) { |
| 41 | + found = false; |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + if (found) goto done; |
| 46 | + }}}}}}}} |
| 47 | +done: ; |
| 48 | + |
| 49 | + // leave |
| 50 | + pow_sha256_count = cnt; |
| 51 | + return found; |
| 52 | +} |
0 commit comments