Fabcoin Core  0.16.2
P2P Digital Currency
blake.cpp
Go to the documentation of this file.
1 #include <stdint.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "blake.h"
5 
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] =
9 {
10  0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
11  0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
12  0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
13  0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL,
14 };
15 static const uint8_t blake2b_sigma[12][16] =
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 },
29 };
30 
31 /*
32 ** Init the state according to Zcash parameters.
33 */
34 void zcash_blake2b_init(blake2b_state_t *st, uint8_t hash_len,
35  uint32_t n, uint32_t k)
36 {
37  assert(n > k);
38  assert(hash_len <= 64);
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];
42  st->h[6] = blake2b_iv[6] ^ *(uint64_t *)"ZcashPoW";
43  st->h[7] = blake2b_iv[7] ^ (((uint64_t)k << 32) | n);
44  st->bytes = 0;
45 }
46 
47 static uint64_t rotr64(uint64_t a, uint8_t bits)
48 {
49  return (a >> bits) | (a << (64 - bits));
50 }
51 
52 static void mix(uint64_t *va, uint64_t *vb, uint64_t *vc, uint64_t *vd,
53  uint64_t x, uint64_t y)
54 {
55  *va = (*va + *vb + x);
56  *vd = rotr64(*vd ^ *va, 32);
57  *vc = (*vc + *vd);
58  *vb = rotr64(*vb ^ *vc, 24);
59  *va = (*va + *vb + y);
60  *vd = rotr64(*vd ^ *va, 16);
61  *vc = (*vc + *vd);
62  *vb = rotr64(*vb ^ *vc, 63);
63 }
64 
65 /*
66 ** Process either a full message block or the final partial block.
67 ** Note that v[13] is not XOR'd because st->bytes is assumed to never overflow.
68 **
69 ** _msg pointer to message (must be zero-padded to 128 bytes if final block)
70 ** msg_len must be 128 (<= 128 allowed only for final partial block)
71 ** is_final indicate if this is the final block
72 */
73 void zcash_blake2b_update(blake2b_state_t *st, const uint8_t *_msg,
74  uint32_t msg_len, uint32_t is_final)
75 {
76  const uint64_t *m = (const uint64_t *)_msg;
77  uint64_t v[16];
78  assert(msg_len <= 128);
79  assert(st->bytes <= UINT64_MAX - msg_len);
80  memcpy(v + 0, st->h, 8 * sizeof (*v));
81  memcpy(v + 8, blake2b_iv, 8 * sizeof (*v));
82  v[12] ^= (st->bytes += msg_len);
83  v[14] ^= is_final ? -1 : 0;
84  for (uint32_t round = 0; round < blake2b_rounds; round++)
85  {
86  const uint8_t *s = blake2b_sigma[round];
87  mix(v + 0, v + 4, v + 8, v + 12, m[s[0]], m[s[1]]);
88  mix(v + 1, v + 5, v + 9, v + 13, m[s[2]], m[s[3]]);
89  mix(v + 2, v + 6, v + 10, v + 14, m[s[4]], m[s[5]]);
90  mix(v + 3, v + 7, v + 11, v + 15, m[s[6]], m[s[7]]);
91  mix(v + 0, v + 5, v + 10, v + 15, m[s[8]], m[s[9]]);
92  mix(v + 1, v + 6, v + 11, v + 12, m[s[10]], m[s[11]]);
93  mix(v + 2, v + 7, v + 8, v + 13, m[s[12]], m[s[13]]);
94  mix(v + 3, v + 4, v + 9, v + 14, m[s[14]], m[s[15]]);
95  }
96  for (uint32_t i = 0; i < 8; i++)
97  st->h[i] ^= v[i] ^ v[i + 8];
98 }
99 
100 void zcash_blake2b_final(blake2b_state_t *st, uint8_t *out, uint8_t outlen)
101 {
102  assert(outlen <= 64);
103  memcpy(out, st->h, outlen);
104 }
void zcash_blake2b_final(blake2b_state_t *st, uint8_t *out, uint8_t outlen)
Definition: blake.cpp:105
void zcash_blake2b_init(blake2b_state_t *st, uint8_t hash_len, uint32_t n, uint32_t k)
Definition: blake.cpp:34
assert(len-trim+(2 *lenIndices)<=WIDTH)
uint64_t bytes
Definition: blake.h:4
#define a(i)
#define x(i)
void zcash_blake2b_update(blake2b_state_t *st, const uint8_t *_msg, uint32_t msg_len, uint32_t is_final)
Definition: blake.cpp:78
uint64_t h[8]
Definition: blake.h:3
void * memcpy(void *a, const void *b, size_t c)
#define round(a, b, c, x, mul)