Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
blake2.cpp
Go to the documentation of this file.
1/*
2 BLAKE2 reference source code package - reference C implementations
3
4 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5 terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6 your option. The terms of these licenses can be found at:
7
8 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9 - OpenSSL license : https://www.openssl.org/source/license.html
10 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11
12 More information about the BLAKE2 hash function can be found at
13 https://blake2.net.
14*/
15
16#include <cstdint>
17#include <cstring>
18#include <limits>
19#include <fc/crypto/blake2.hpp>
20
21namespace fc {
22
23static const uint64_t blake2b_IV[8] = {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL,
24 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
25 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
26
27static const uint8_t blake2b_sigma[12][16] = {
28 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
29 {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4}, {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
30 {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13}, {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
31 {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11}, {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
32 {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5}, {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
33 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}};
34
35static inline uint64_t load64(const void *src) {
36 uint64_t w;
37 memcpy(&w, src, sizeof w);
38 return w;
39}
40
41static inline uint64_t rotr64(const uint64_t w, const unsigned c) { return (w >> c) | (w << (64 - c)); }
42
43inline void blake2b_wrapper::G(uint8_t r, uint8_t i, uint64_t& a, uint64_t& b, uint64_t& c, uint64_t& d) noexcept
44{
45 a = a + b + m[blake2b_sigma[r][2 * i + 0]];
46 d = rotr64(d ^ a, 32);
47 c = c + d;
48 b = rotr64(b ^ c, 24);
49 a = a + b + m[blake2b_sigma[r][2 * i + 1]];
50 d = rotr64(d ^ a, 16);
51 c = c + d;
52 b = rotr64(b ^ c, 63);
53}
54
55inline void blake2b_wrapper::ROUND(uint8_t r) noexcept
56{
57 G(r, 0, v[0], v[4], v[8], v[12]);
58 G(r, 1, v[1], v[5], v[9], v[13]);
59 G(r, 2, v[2], v[6], v[10], v[14]);
60 G(r, 3, v[3], v[7], v[11], v[15]);
61 G(r, 4, v[0], v[5], v[10], v[15]);
62 G(r, 5, v[1], v[6], v[11], v[12]);
63 G(r, 6, v[2], v[7], v[8], v[13]);
64 G(r, 7, v[3], v[4], v[9], v[14]);
65}
66
67void blake2b_wrapper::blake2b_compress(blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES], size_t r, const yield_function_t& yield) {
68 blake2b_compress_init(S, block, r);
69
70 for (i = 0; i < r; ++i) {
71 ROUND(i % 10);
72 if (i % 100) {
73 yield();
74 }
75 }
76
77 blake2b_compress_end(S);
78}
79
80void blake2b_wrapper::blake2b_compress_init(blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES], size_t r) {
81 for (i = 0; i < 16; ++i) {
82 m[i] = load64(block + i * sizeof(m[i]));
83 }
84
85 for (i = 0; i < 8; ++i) {
86 v[i] = S->h[i];
87 }
88
89 v[8] = blake2b_IV[0];
90 v[9] = blake2b_IV[1];
91 v[10] = blake2b_IV[2];
92 v[11] = blake2b_IV[3];
93 v[12] = blake2b_IV[4] ^ S->t[0];
94 v[13] = blake2b_IV[5] ^ S->t[1];
95 v[14] = blake2b_IV[6] ^ S->f[0];
96 v[15] = blake2b_IV[7];
97}
98
99void blake2b_wrapper::blake2b_compress_end(blake2b_state *S) {
100 for (i = 0; i < 8; ++i) {
101 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
102 }
103}
104
105std::variant<blake2b_error, bytes> blake2b(uint32_t _rounds, const bytes& _h, const bytes& _m, const bytes& _t0_offset, const bytes& _t1_offset, bool _f, const yield_function_t& yield) {
106
107 // EIP-152 [4 bytes for rounds][64 bytes for h][128 bytes for m][8 bytes for t_0][8 bytes for t_1][1 byte for f] : 213
108 // [------------------][64 bytes for h][128 bytes for m][8 bytes for t_0][8 bytes for t_1][------------] : 208
109 // * rounds and final indicator flag are not serialized
110 if (_h.size() != 64 || _m.size() != blake2b_wrapper::BLAKE2B_BLOCKBYTES || _t0_offset.size() != 8 || _t1_offset.size() != 8) {
112 }
113
114 blake2b_wrapper b2wrapper;
116
117 memcpy(state.h, _h.data(), 64);
118
119 // final indicator flag set words to 1's if true
120 state.f[0] = _f ? std::numeric_limits<uint64_t>::max() : 0;
121
122 memcpy(&state.t[0], _t0_offset.data(), 8);
123 memcpy(&state.t[1], _t1_offset.data(), 8);
124
127
128 b2wrapper.blake2b_compress(&state, block, _rounds, yield);
129
130 bytes out(sizeof(state.h), 0);
131 memcpy(&out[0], &state.h[0], out.size());
132 return out;
133}
134
135}
136#undef G
137#undef ROUND
const mie::Vuint & r
Definition bn.cpp:28
void blake2b_compress(blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES], size_t r, const yield_function_t &yield)
Definition blake2.cpp:67
namespace sysio::chain
Definition authority.cpp:3
std::vector< char > bytes
Definition alt_bn128.hpp:10
std::variant< blake2b_error, bytes > blake2b(uint32_t _rounds, const bytes &_h, const bytes &_m, const bytes &_t0_offset, const bytes &_t1_offset, bool _f, const yield_function_t &yield)
Definition blake2.cpp:105
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
void f()
CK_ULONG d
memcpy((char *) pInfo->slotDescription, s, l)