Fabcoin Core  0.16.2
P2P Digital Currency
SHA3.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "SHA3.h"
23 #include <cstdint>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <cstring>
27 #include "RLP.h"
28 #include "picosha2.h"
29 using namespace std;
30 using namespace dev;
31 
32 namespace dev
33 {
34 
37 
38 namespace keccak
39 {
40 
50 #define decshake(bits) \
51  int shake##bits(uint8_t*, size_t, const uint8_t*, size_t);
52 
53 #define decsha3(bits) \
54  int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);
55 
56 decshake(128)
57 decshake(256)
58 decsha3(224)
59 decsha3(256)
60 decsha3(384)
61 decsha3(512)
62 
63 /******** The Keccak-f[1600] permutation ********/
64 
65 /*** Constants. ***/
66 static const uint8_t rho[24] = \
67  { 1, 3, 6, 10, 15, 21,
68  28, 36, 45, 55, 2, 14,
69  27, 41, 56, 8, 25, 43,
70  62, 18, 39, 61, 20, 44};
71 static const uint8_t pi[24] = \
72  {10, 7, 11, 17, 18, 3,
73  5, 16, 8, 21, 24, 4,
74  15, 23, 19, 13, 12, 2,
75  20, 14, 22, 9, 6, 1};
76 static const uint64_t RC[24] = \
77  {1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
78  0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
79  0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
80  0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
81  0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
82  0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
83 
84 /*** Helper macros to unroll the permutation. ***/
85 #define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
86 #define REPEAT6(e) e e e e e e
87 #define REPEAT24(e) REPEAT6(e e e e)
88 #define REPEAT5(e) e e e e e
89 #define FOR5(v, s, e) \
90  v = 0; \
91  REPEAT5(e; v += s;)
92 
93 /*** Keccak-f[1600] ***/
94 static inline void keccakf(void* state) {
95  uint64_t* a = (uint64_t*)state;
96  uint64_t b[5] = {0};
97  uint64_t t = 0;
98  uint8_t x, y;
99 
100  for (int i = 0; i < 24; i++) {
101  // Theta
102  FOR5(x, 1,
103  b[x] = 0;
104  FOR5(y, 5,
105  b[x] ^= a[x + y]; ))
106  FOR5(x, 1,
107  FOR5(y, 5,
108  a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1); ))
109  // Rho and pi
110  t = a[1];
111  x = 0;
112  REPEAT24(b[0] = a[pi[x]];
113  a[pi[x]] = rol(t, rho[x]);
114  t = b[0];
115  x++; )
116  // Chi
117  FOR5(y,
118  5,
119  FOR5(x, 1,
120  b[x] = a[y + x];)
121  FOR5(x, 1,
122  a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]); ))
123  // Iota
124  a[0] ^= RC[i];
125  }
126 }
127 
128 /******** The FIPS202-defined functions. ********/
129 
130 /*** Some helper macros. ***/
131 
132 #define _(S) do { S } while (0)
133 #define FOR(i, ST, L, S) \
134  _(for (size_t i = 0; i < L; i += ST) { S; })
135 #define mkapply_ds(NAME, S) \
136  static inline void NAME(uint8_t* dst, \
137  const uint8_t* src, \
138  size_t len) { \
139  FOR(i, 1, len, S); \
140  }
141 #define mkapply_sd(NAME, S) \
142  static inline void NAME(const uint8_t* src, \
143  uint8_t* dst, \
144  size_t len) { \
145  FOR(i, 1, len, S); \
146  }
147 
148 mkapply_ds(xorin, dst[i] ^= src[i]) // xorin
149 mkapply_sd(setout, dst[i] = src[i]) // setout
150 
151 #define P keccakf
152 #define Plen 200
153 
154 // Fold P*F over the full blocks of an input.
155 #define foldP(I, L, F) \
156  while (L >= rate) { \
157  F(a, I, rate); \
158  P(a); \
159  I += rate; \
160  L -= rate; \
161  }
162 
164 static inline int hash(uint8_t* out, size_t outlen,
165  const uint8_t* in, size_t inlen,
166  size_t rate, uint8_t delim) {
167  if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
168  return -1;
169  }
170  uint8_t a[Plen] = {0};
171  // Absorb input.
172  foldP(in, inlen, xorin);
173  // Xor in the DS and pad frame.
174  a[inlen] ^= delim;
175  a[rate - 1] ^= 0x80;
176  // Xor in the last block.
177  xorin(a, in, inlen);
178  // Apply P
179  P(a);
180  // Squeeze output.
181  foldP(out, outlen, setout);
182  setout(a, out, outlen);
183  memset(a, 0, 200);
184  return 0;
185 }
186 
187 /*** Helper macros to define SHA3 and SHAKE instances. ***/
188 #define defshake(bits) \
189  int shake##bits(uint8_t* out, size_t outlen, \
190  const uint8_t* in, size_t inlen) { \
191  return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \
192  }
193 #define defsha3(bits) \
194  int sha3_##bits(uint8_t* out, size_t outlen, \
195  const uint8_t* in, size_t inlen) { \
196  if (outlen > (bits/8)) { \
197  return -1; \
198  } \
199  return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \
200  }
201 
202 /*** FIPS202 SHAKE VOFs ***/
203 defshake(128)
204 defshake(256)
205 
206 /*** FIPS202 SHA3 FOFs ***/
207 defsha3(224)
208 defsha3(256)
209 defsha3(384)
210 defsha3(512)
211 
212 }
213 
214 bool sha3(bytesConstRef _input, bytesRef o_output)
215 {
216  // FIXME: What with unaligned memory?
217  if (o_output.size() != 32)
218  return false;
219  keccak::sha3_256(o_output.data(), 32, _input.data(), _input.size());
220 // keccak::keccak(ret.data(), 32, (uint64_t const*)_input.data(), _input.size());
221  return true;
222 }
223 
224 }
h256 EmptySHA3
Definition: SHA3.cpp:35
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
h256 EmptyListSHA3
Definition: SHA3.cpp:36
A modifiable reference to an existing object or vector in memory.
Definition: vector_ref.h:20
#define defshake(bits)
#define defsha3(bits)
#define REPEAT24(e)
Definition: SHA3.cpp:87
void keccak(uint8_t const *_data, uint64_t _size, uint8_t *o_hash)
Definition: Utils.cpp:178
std::hash for asio::adress
Definition: Common.h:323
#define mkapply_sd(NAME, S)
Definition: SHA3.cpp:141
#define FOR5(v, s, e)
Definition: SHA3.cpp:89
#define foldP(I, L, F)
#define Plen
#define decshake(bits)
libkeccak-tiny
Definition: SHA3.cpp:50
#define a(i)
#define x(i)
#define rol(x, s)
Definition: SHA3.cpp:85
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
#define decsha3(bits)
Definition: SHA3.cpp:53
#define b(i, j)
size_t size() const
Definition: vector_ref.h:55
bytes rlpList()
Export a list of items in RLP format, returning a byte array.
Definition: RLP.h:470
#define rho(a0, a1, a2)
Definition: 3way.cpp:62
_T * data() const
Definition: vector_ref.h:51
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
#define mkapply_ds(NAME, S)
Definition: SHA3.cpp:135
#define P