6 static const uint32_t blake2b_block_len = 128;
7 static const uint32_t blake2b_rounds = 12;
8 static const uint64_t blake2b_iv[8] =
10 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
11 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
12 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
13 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL,
15 static const uint8_t blake2b_sigma[12][16] =
17 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
18 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
19 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
20 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
21 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
22 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
23 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
24 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
25 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
26 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
27 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
28 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
35 uint32_t n, uint32_t k)
39 st->
h[0] = blake2b_iv[0] ^ (0x01010000 | hash_len);
40 for (uint32_t i = 1; i <= 5; i++)
41 st->
h[i] = blake2b_iv[i];
43 if( n == 200 && k == 9 )
44 st->
h[6] = blake2b_iv[6] ^ *(uint64_t *)
"ZcashPoW";
46 st->
h[6] = blake2b_iv[6] ^ *(uint64_t *)
"FABcoin_";
48 st->
h[7] = blake2b_iv[7] ^ (((uint64_t)k << 32) | n);
52 static uint64_t rotr64(uint64_t
a, uint8_t bits)
54 return (
a >> bits) | (
a << (64 - bits));
57 static void mix(uint64_t *va, uint64_t *vb, uint64_t *vc, uint64_t *vd,
58 uint64_t
x, uint64_t y)
60 *va = (*va + *vb +
x);
61 *vd = rotr64(*vd ^ *va, 32);
63 *vb = rotr64(*vb ^ *vc, 24);
64 *va = (*va + *vb + y);
65 *vd = rotr64(*vd ^ *va, 16);
67 *vb = rotr64(*vb ^ *vc, 63);
79 uint32_t msg_len, uint32_t is_final)
81 const uint64_t *m = (
const uint64_t *)_msg;
85 memcpy(v + 0, st->
h, 8 * sizeof (*v));
86 memcpy(v + 8, blake2b_iv, 8 *
sizeof (*v));
87 v[12] ^= (st->
bytes += msg_len);
88 v[14] ^= is_final ? -1 : 0;
91 const uint8_t *s = blake2b_sigma[
round];
92 mix(v + 0, v + 4, v + 8, v + 12, m[s[0]], m[s[1]]);
93 mix(v + 1, v + 5, v + 9, v + 13, m[s[2]], m[s[3]]);
94 mix(v + 2, v + 6, v + 10, v + 14, m[s[4]], m[s[5]]);
95 mix(v + 3, v + 7, v + 11, v + 15, m[s[6]], m[s[7]]);
96 mix(v + 0, v + 5, v + 10, v + 15, m[s[8]], m[s[9]]);
97 mix(v + 1, v + 6, v + 11, v + 12, m[s[10]], m[s[11]]);
98 mix(v + 2, v + 7, v + 8, v + 13, m[s[12]], m[s[13]]);
99 mix(v + 3, v + 4, v + 9, v + 14, m[s[14]], m[s[15]]);
101 for (uint32_t i = 0; i < 8; i++)
102 st->
h[i] ^= v[i] ^ v[i + 8];
void zcash_blake2b_final(blake2b_state_t *st, uint8_t *out, uint8_t outlen)
void zcash_blake2b_init(blake2b_state_t *st, uint8_t hash_len, uint32_t n, uint32_t k)
assert(len-trim+(2 *lenIndices)<=WIDTH)
void zcash_blake2b_update(blake2b_state_t *st, const uint8_t *_msg, uint32_t msg_len, uint32_t is_final)
void * memcpy(void *a, const void *b, size_t c)
#define round(a, b, c, x, mul)