204 lines
6.3 KiB
C
204 lines
6.3 KiB
C
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
#define BATCH_SIZE 150000
|
|
#define MAX_MSG_LEN 256
|
|
#define NONCE_MAX_LEN 128
|
|
|
|
/* Global state */
|
|
static char solution_str[32];
|
|
static uint8_t hash_output[32];
|
|
static uint8_t message_buffer[MAX_MSG_LEN];
|
|
|
|
static uint32_t nonce_length = 0;
|
|
static uint32_t difficulty = 0;
|
|
static uint64_t current_solution = 0;
|
|
static uint64_t tries = 0;
|
|
static uint32_t zero_bytes = 0;
|
|
static uint32_t half_nibble = 0;
|
|
|
|
__attribute__((__import_module__("global"), __import_name__("pow_update"))) void
|
|
pow_update(uint64_t tries);
|
|
__attribute__((__import_module__("global"), __import_name__("pow_finish"))) void
|
|
pow_finish(uint64_t tries, uint64_t solution);
|
|
|
|
static const uint32_t K[64] = {
|
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
|
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
|
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
|
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
};
|
|
|
|
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
|
|
#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
|
|
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
|
#define EP0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
|
|
#define EP1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
|
|
#define SIG0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3))
|
|
#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
|
|
|
|
static inline void g_sha256_hash(uint32_t message_length) {
|
|
uint32_t h[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
|
|
|
|
const uint64_t bit_length = (uint64_t)message_length * 8;
|
|
const uint32_t padded_length = ((message_length + 9 + 63) / 64) * 64;
|
|
|
|
static uint8_t padded_msg[128];
|
|
|
|
for (uint32_t idx = 0; idx < message_length; ++idx) {
|
|
padded_msg[idx] = message_buffer[idx];
|
|
}
|
|
padded_msg[message_length] = 0x80;
|
|
for (uint32_t idx = message_length + 1; idx < padded_length - 8; ++idx) {
|
|
padded_msg[idx] = 0;
|
|
}
|
|
for (int idx = 0; idx < 8; ++idx) {
|
|
padded_msg[padded_length - 1 - idx] =
|
|
(uint8_t)(bit_length >> (8 * idx));
|
|
}
|
|
|
|
for (uint32_t chunk = 0; chunk < padded_length; chunk += 64) {
|
|
uint32_t w[64];
|
|
for (int idx = 0; idx < 16; ++idx) {
|
|
w[idx] = ((uint32_t)padded_msg[chunk + idx * 4] << 24) |
|
|
((uint32_t)padded_msg[chunk + idx * 4 + 1] << 16) |
|
|
((uint32_t)padded_msg[chunk + idx * 4 + 2] << 8) |
|
|
((uint32_t)padded_msg[chunk + idx * 4 + 3]);
|
|
}
|
|
|
|
for (int idx = 16; idx < 64; ++idx) {
|
|
w[idx] =
|
|
SIG1(w[idx - 2]) + w[idx - 7] + SIG0(w[idx - 15]) + w[idx - 16];
|
|
}
|
|
|
|
uint32_t a = h[0], b = h[1], c = h[2], d = h[3];
|
|
uint32_t e = h[4], f = h[5], g = h[6], h_val = h[7];
|
|
|
|
for (int idx = 0; idx < 64; ++idx) {
|
|
const uint32_t t1 = h_val + EP1(e) + CH(e, f, g) + K[idx] + w[idx];
|
|
const uint32_t t2 = EP0(a) + MAJ(a, b, c);
|
|
h_val = g;
|
|
g = f;
|
|
f = e;
|
|
e = d + t1;
|
|
d = c;
|
|
c = b;
|
|
b = a;
|
|
a = t1 + t2;
|
|
}
|
|
|
|
h[0] += a;
|
|
h[1] += b;
|
|
h[2] += c;
|
|
h[3] += d;
|
|
h[4] += e;
|
|
h[5] += f;
|
|
h[6] += g;
|
|
h[7] += h_val;
|
|
}
|
|
|
|
for (int idx = 0; idx < 8; ++idx) {
|
|
hash_output[idx * 4] = (h[idx] >> 24) & 0xFF;
|
|
hash_output[idx * 4 + 1] = (h[idx] >> 16) & 0xFF;
|
|
hash_output[idx * 4 + 2] = (h[idx] >> 8) & 0xFF;
|
|
hash_output[idx * 4 + 3] = h[idx] & 0xFF;
|
|
}
|
|
}
|
|
|
|
static inline int uint64_to_str(uint64_t number, char *str) {
|
|
if (number == 0) {
|
|
str[0] = '0';
|
|
str[1] = '\0';
|
|
return 1;
|
|
}
|
|
|
|
int length = 0;
|
|
uint64_t temp = number;
|
|
|
|
while (temp > 0) {
|
|
++length;
|
|
temp /= 10;
|
|
}
|
|
|
|
str[length] = '\0';
|
|
|
|
for (int idx = length - 1; idx >= 0; --idx) {
|
|
str[idx] = '0' + (number % 10);
|
|
number /= 10;
|
|
}
|
|
|
|
return length;
|
|
}
|
|
|
|
static inline int g_meets_difficulty(void) {
|
|
for (uint32_t idx = 0; idx < zero_bytes; ++idx) {
|
|
if (hash_output[idx] != 0) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (half_nibble && (hash_output[zero_bytes] & 0xF0) != 0) {
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
__attribute__((export_name("pow_init"))) void
|
|
pow_init(uintptr_t nonce_ptr, uint32_t _nonce_length, uint32_t _difficulty) {
|
|
const uint8_t *nonce = (const uint8_t *)nonce_ptr;
|
|
|
|
nonce_length =
|
|
(_nonce_length > NONCE_MAX_LEN) ? NONCE_MAX_LEN : _nonce_length;
|
|
difficulty = _difficulty;
|
|
zero_bytes = _difficulty / 2;
|
|
half_nibble = _difficulty % 2;
|
|
current_solution = 0;
|
|
tries = 0;
|
|
|
|
for (uint32_t idx = 0; idx < nonce_length; ++idx) {
|
|
message_buffer[idx] = nonce[idx];
|
|
}
|
|
}
|
|
|
|
__attribute__((export_name("pow_solve_batch"))) int pow_solve_batch(void) {
|
|
if (nonce_length == 0) {
|
|
return 0;
|
|
}
|
|
|
|
for (uint32_t batch_idx = 0; batch_idx < BATCH_SIZE; batch_idx++) {
|
|
const int sol_len = uint64_to_str(current_solution, solution_str);
|
|
const int message_length = nonce_length + sol_len;
|
|
|
|
if (message_length > MAX_MSG_LEN) {
|
|
return -1;
|
|
}
|
|
|
|
for (int idx = 0; idx < sol_len; ++idx) {
|
|
message_buffer[nonce_length + idx] = (uint8_t)solution_str[idx];
|
|
}
|
|
|
|
g_sha256_hash(message_length);
|
|
|
|
if (g_meets_difficulty()) {
|
|
pow_finish(tries + batch_idx, current_solution);
|
|
return 1;
|
|
}
|
|
|
|
++current_solution;
|
|
}
|
|
|
|
tries += BATCH_SIZE;
|
|
pow_update(tries);
|
|
|
|
return 0;
|
|
}
|