Fabcoin Core  0.16.2
P2P Digital Currency
Hash.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 "Hash.h"
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 #include "picosha2.h"
27 using namespace std;
28 using namespace dev;
29 
30 namespace dev
31 {
32 
34 {
35  h256 ret;
36  picosha2::hash256(_input.begin(), _input.end(), ret.data(), ret.data() + 32);
37  return ret;
38 }
39 
40 namespace rmd160
41 {
42 
43 /********************************************************************\
44  *
45  * FILE: rmd160.h
46  * FILE: rmd160.c
47  *
48  * CONTENTS: Header file for a sample C-implementation of the
49  * RIPEMD-160 hash-function.
50  * TARGET: any computer with an ANSI C compiler
51  *
52  * AUTHOR: Antoon Bosselaers, ESAT-COSIC
53  * DATE: 1 March 1996
54  * VERSION: 1.0
55  *
56  * Copyright (c) Katholieke Universiteit Leuven
57  * 1996, All Rights Reserved
58  *
59  \********************************************************************/
60 
61 // Adapted into "header-only" format by Gav Wood.
62 
63 /* macro definitions */
64 
65 #define RMDsize 160
66 
67 /* collect four bytes into one word: */
68 #define BYTES_TO_DWORD(strptr) \
69 (((uint32_t) *((strptr)+3) << 24) | \
70 ((uint32_t) *((strptr)+2) << 16) | \
71 ((uint32_t) *((strptr)+1) << 8) | \
72 ((uint32_t) *(strptr)))
73 
74 /* ROL(x, n) cyclically rotates x over n bits to the left */
75 /* x must be of an unsigned 32 bits type and 0 <= n < 32. */
76 #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
77 
78 /* the five basic functions F(), G() and H() */
79 #define F(x, y, z) ((x) ^ (y) ^ (z))
80 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
81 #define H(x, y, z) (((x) | ~(y)) ^ (z))
82 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
83 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
84 
85 /* the ten basic operations FF() through III() */
86 #define FF(a, b, c, d, e, x, s) {\
87 (a) += F((b), (c), (d)) + (x);\
88 (a) = ROL((a), (s)) + (e);\
89 (c) = ROL((c), 10);\
90 }
91 #define GG(a, b, c, d, e, x, s) {\
92 (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
93 (a) = ROL((a), (s)) + (e);\
94 (c) = ROL((c), 10);\
95 }
96 #define HH(a, b, c, d, e, x, s) {\
97 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
98 (a) = ROL((a), (s)) + (e);\
99 (c) = ROL((c), 10);\
100 }
101 #define II(a, b, c, d, e, x, s) {\
102 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
103 (a) = ROL((a), (s)) + (e);\
104 (c) = ROL((c), 10);\
105 }
106 #define JJ(a, b, c, d, e, x, s) {\
107 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
108 (a) = ROL((a), (s)) + (e);\
109 (c) = ROL((c), 10);\
110 }
111 #define FFF(a, b, c, d, e, x, s) {\
112 (a) += F((b), (c), (d)) + (x);\
113 (a) = ROL((a), (s)) + (e);\
114 (c) = ROL((c), 10);\
115 }
116 #define GGG(a, b, c, d, e, x, s) {\
117 (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
118 (a) = ROL((a), (s)) + (e);\
119 (c) = ROL((c), 10);\
120 }
121 #define HHH(a, b, c, d, e, x, s) {\
122 (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
123 (a) = ROL((a), (s)) + (e);\
124 (c) = ROL((c), 10);\
125 }
126 #define III(a, b, c, d, e, x, s) {\
127 (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
128 (a) = ROL((a), (s)) + (e);\
129 (c) = ROL((c), 10);\
130 }
131 #define JJJ(a, b, c, d, e, x, s) {\
132 (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
133 (a) = ROL((a), (s)) + (e);\
134 (c) = ROL((c), 10);\
135 }
136 
137 void MDinit(uint32_t *MDbuf)
138 {
139  MDbuf[0] = 0x67452301UL;
140  MDbuf[1] = 0xefcdab89UL;
141  MDbuf[2] = 0x98badcfeUL;
142  MDbuf[3] = 0x10325476UL;
143  MDbuf[4] = 0xc3d2e1f0UL;
144 
145  return;
146 }
147 
148 /********************************************************************/
149 
150 void MDcompress(uint32_t *MDbuf, uint32_t *X)
151 {
152  uint32_t aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2],
153  dd = MDbuf[3], ee = MDbuf[4];
154  uint32_t aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2],
155  ddd = MDbuf[3], eee = MDbuf[4];
156 
157  /* round 1 */
158  FF(aa, bb, cc, dd, ee, X[ 0], 11);
159  FF(ee, aa, bb, cc, dd, X[ 1], 14);
160  FF(dd, ee, aa, bb, cc, X[ 2], 15);
161  FF(cc, dd, ee, aa, bb, X[ 3], 12);
162  FF(bb, cc, dd, ee, aa, X[ 4], 5);
163  FF(aa, bb, cc, dd, ee, X[ 5], 8);
164  FF(ee, aa, bb, cc, dd, X[ 6], 7);
165  FF(dd, ee, aa, bb, cc, X[ 7], 9);
166  FF(cc, dd, ee, aa, bb, X[ 8], 11);
167  FF(bb, cc, dd, ee, aa, X[ 9], 13);
168  FF(aa, bb, cc, dd, ee, X[10], 14);
169  FF(ee, aa, bb, cc, dd, X[11], 15);
170  FF(dd, ee, aa, bb, cc, X[12], 6);
171  FF(cc, dd, ee, aa, bb, X[13], 7);
172  FF(bb, cc, dd, ee, aa, X[14], 9);
173  FF(aa, bb, cc, dd, ee, X[15], 8);
174 
175  /* round 2 */
176  GG(ee, aa, bb, cc, dd, X[ 7], 7);
177  GG(dd, ee, aa, bb, cc, X[ 4], 6);
178  GG(cc, dd, ee, aa, bb, X[13], 8);
179  GG(bb, cc, dd, ee, aa, X[ 1], 13);
180  GG(aa, bb, cc, dd, ee, X[10], 11);
181  GG(ee, aa, bb, cc, dd, X[ 6], 9);
182  GG(dd, ee, aa, bb, cc, X[15], 7);
183  GG(cc, dd, ee, aa, bb, X[ 3], 15);
184  GG(bb, cc, dd, ee, aa, X[12], 7);
185  GG(aa, bb, cc, dd, ee, X[ 0], 12);
186  GG(ee, aa, bb, cc, dd, X[ 9], 15);
187  GG(dd, ee, aa, bb, cc, X[ 5], 9);
188  GG(cc, dd, ee, aa, bb, X[ 2], 11);
189  GG(bb, cc, dd, ee, aa, X[14], 7);
190  GG(aa, bb, cc, dd, ee, X[11], 13);
191  GG(ee, aa, bb, cc, dd, X[ 8], 12);
192 
193  /* round 3 */
194  HH(dd, ee, aa, bb, cc, X[ 3], 11);
195  HH(cc, dd, ee, aa, bb, X[10], 13);
196  HH(bb, cc, dd, ee, aa, X[14], 6);
197  HH(aa, bb, cc, dd, ee, X[ 4], 7);
198  HH(ee, aa, bb, cc, dd, X[ 9], 14);
199  HH(dd, ee, aa, bb, cc, X[15], 9);
200  HH(cc, dd, ee, aa, bb, X[ 8], 13);
201  HH(bb, cc, dd, ee, aa, X[ 1], 15);
202  HH(aa, bb, cc, dd, ee, X[ 2], 14);
203  HH(ee, aa, bb, cc, dd, X[ 7], 8);
204  HH(dd, ee, aa, bb, cc, X[ 0], 13);
205  HH(cc, dd, ee, aa, bb, X[ 6], 6);
206  HH(bb, cc, dd, ee, aa, X[13], 5);
207  HH(aa, bb, cc, dd, ee, X[11], 12);
208  HH(ee, aa, bb, cc, dd, X[ 5], 7);
209  HH(dd, ee, aa, bb, cc, X[12], 5);
210 
211  /* round 4 */
212  II(cc, dd, ee, aa, bb, X[ 1], 11);
213  II(bb, cc, dd, ee, aa, X[ 9], 12);
214  II(aa, bb, cc, dd, ee, X[11], 14);
215  II(ee, aa, bb, cc, dd, X[10], 15);
216  II(dd, ee, aa, bb, cc, X[ 0], 14);
217  II(cc, dd, ee, aa, bb, X[ 8], 15);
218  II(bb, cc, dd, ee, aa, X[12], 9);
219  II(aa, bb, cc, dd, ee, X[ 4], 8);
220  II(ee, aa, bb, cc, dd, X[13], 9);
221  II(dd, ee, aa, bb, cc, X[ 3], 14);
222  II(cc, dd, ee, aa, bb, X[ 7], 5);
223  II(bb, cc, dd, ee, aa, X[15], 6);
224  II(aa, bb, cc, dd, ee, X[14], 8);
225  II(ee, aa, bb, cc, dd, X[ 5], 6);
226  II(dd, ee, aa, bb, cc, X[ 6], 5);
227  II(cc, dd, ee, aa, bb, X[ 2], 12);
228 
229  /* round 5 */
230  JJ(bb, cc, dd, ee, aa, X[ 4], 9);
231  JJ(aa, bb, cc, dd, ee, X[ 0], 15);
232  JJ(ee, aa, bb, cc, dd, X[ 5], 5);
233  JJ(dd, ee, aa, bb, cc, X[ 9], 11);
234  JJ(cc, dd, ee, aa, bb, X[ 7], 6);
235  JJ(bb, cc, dd, ee, aa, X[12], 8);
236  JJ(aa, bb, cc, dd, ee, X[ 2], 13);
237  JJ(ee, aa, bb, cc, dd, X[10], 12);
238  JJ(dd, ee, aa, bb, cc, X[14], 5);
239  JJ(cc, dd, ee, aa, bb, X[ 1], 12);
240  JJ(bb, cc, dd, ee, aa, X[ 3], 13);
241  JJ(aa, bb, cc, dd, ee, X[ 8], 14);
242  JJ(ee, aa, bb, cc, dd, X[11], 11);
243  JJ(dd, ee, aa, bb, cc, X[ 6], 8);
244  JJ(cc, dd, ee, aa, bb, X[15], 5);
245  JJ(bb, cc, dd, ee, aa, X[13], 6);
246 
247  /* parallel round 1 */
248  JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
249  JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
250  JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
251  JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
252  JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
253  JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
254  JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
255  JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
256  JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
257  JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
258  JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
259  JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
260  JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
261  JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
262  JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
263  JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
264 
265  /* parallel round 2 */
266  III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
267  III(ddd, eee, aaa, bbb, ccc, X[11], 13);
268  III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
269  III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
270  III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
271  III(eee, aaa, bbb, ccc, ddd, X[13], 8);
272  III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
273  III(ccc, ddd, eee, aaa, bbb, X[10], 11);
274  III(bbb, ccc, ddd, eee, aaa, X[14], 7);
275  III(aaa, bbb, ccc, ddd, eee, X[15], 7);
276  III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
277  III(ddd, eee, aaa, bbb, ccc, X[12], 7);
278  III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
279  III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
280  III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
281  III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
282 
283  /* parallel round 3 */
284  HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
285  HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
286  HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
287  HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
288  HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
289  HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
290  HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
291  HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
292  HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
293  HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
294  HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
295  HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
296  HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
297  HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
298  HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
299  HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
300 
301  /* parallel round 4 */
302  GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
303  GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
304  GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
305  GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
306  GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
307  GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
308  GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
309  GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
310  GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
311  GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
312  GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
313  GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
314  GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
315  GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
316  GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
317  GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
318 
319  /* parallel round 5 */
320  FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
321  FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
322  FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
323  FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
324  FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
325  FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
326  FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
327  FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
328  FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
329  FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
330  FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
331  FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
332  FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
333  FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
334  FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
335  FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
336 
337  /* combine results */
338  ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */
339  MDbuf[1] = MDbuf[2] + dd + eee;
340  MDbuf[2] = MDbuf[3] + ee + aaa;
341  MDbuf[3] = MDbuf[4] + aa + bbb;
342  MDbuf[4] = MDbuf[0] + bb + ccc;
343  MDbuf[0] = ddd;
344 
345  return;
346 }
347 
348 void MDfinish(uint32_t *MDbuf, byte const *strptr, uint32_t lswlen, uint32_t mswlen)
349 {
350  unsigned int i; /* counter */
351  uint32_t X[16]; /* message words */
352 
353  memset(X, 0, 16*sizeof(uint32_t));
354 
355  /* put bytes from strptr into X */
356  for (i=0; i<(lswlen&63); i++) {
357  /* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */
358  X[i>>2] ^= (uint32_t) *strptr++ << (8 * (i&3));
359  }
360 
361  /* append the bit m_n == 1 */
362  X[(lswlen>>2)&15] ^= (uint32_t)1 << (8*(lswlen&3) + 7);
363 
364  if ((lswlen & 63) > 55) {
365  /* length goes to next block */
366  MDcompress(MDbuf, X);
367  memset(X, 0, 16*sizeof(uint32_t));
368  }
369 
370  /* append length in bits*/
371  X[14] = lswlen << 3;
372  X[15] = (lswlen >> 29) | (mswlen << 3);
373  MDcompress(MDbuf, X);
374 
375  return;
376 }
377 
378 #undef ROL
379 #undef F
380 #undef G
381 #undef H
382 #undef I
383 #undef J
384 #undef FF
385 #undef GG
386 #undef HH
387 #undef II
388 #undef JJ
389 #undef FFF
390 #undef GGG
391 #undef HHH
392 #undef III
393 #undef JJJ
394 
395 }
396 
397 /*
398  * @returns RMD(_input)
399  */
401 {
402  h160 hashcode;
403  uint32_t buffer[RMDsize / 32]; // contains (A, B, C, D(, E))
404  uint32_t current[16]; // current 16-word chunk
405 
406  // initialize
407  rmd160::MDinit(buffer);
408  byte const* message = _input.data();
409  uint32_t remaining = _input.size(); // # of bytes not yet processed
410 
411  // process message in 16x 4-byte chunks
412  for (; remaining >= 64; remaining -= 64)
413  {
414  for (unsigned i = 0; i < 16; i++)
415  {
416  current[i] = BYTES_TO_DWORD(message);
417  message += 4;
418  }
419  rmd160::MDcompress(buffer, current);
420  }
421  // length mod 64 bytes left
422 
423  // finish:
424  rmd160::MDfinish(buffer, message, _input.size(), 0);
425 
426  for (unsigned i = 0; i < RMDsize / 8; i += 4)
427  {
428  hashcode[i] = buffer[i >> 2]; // implicit cast to byte
429  hashcode[i + 1] = (buffer[i >> 2] >> 8); //extracts the 8 least
430  hashcode[i + 2] = (buffer[i >> 2] >> 16); // significant bits.
431  hashcode[i + 3] = (buffer[i >> 2] >> 24);
432  }
433 
434  return hashcode;
435 }
436 
437 #undef BYTES_TO_DWORD
438 #undef RMDsize
439 
440 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
#define II(a, b, c, d, e, x, s)
Definition: Hash.cpp:101
uint8_t byte
Definition: Common.h:57
byte * data()
Definition: FixedHash.h:139
void MDcompress(uint32_t *MDbuf, uint32_t *X)
Definition: Hash.cpp:150
void hash256(RaIter first, RaIter last, OutIter first2, OutIter last2)
Definition: picosha2.h:304
#define HHH(a, b, c, d, e, x, s)
Definition: Hash.cpp:121
#define JJJ(a, b, c, d, e, x, s)
Definition: Hash.cpp:131
std::hash for asio::adress
Definition: Common.h:323
void MDfinish(uint32_t *MDbuf, byte const *strptr, uint32_t lswlen, uint32_t mswlen)
Definition: Hash.cpp:348
#define HH(a, b, c, d, e, x, s)
Definition: Hash.cpp:96
#define RMDsize
Definition: Hash.cpp:65
#define FFF(a, b, c, d, e, x, s)
Definition: Hash.cpp:111
#define GG(a, b, c, d, e, x, s)
Definition: Hash.cpp:91
#define FF(a, b, c, d, e, x, s)
Definition: Hash.cpp:86
#define GGG(a, b, c, d, e, x, s)
Definition: Hash.cpp:116
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
#define III(a, b, c, d, e, x, s)
Definition: Hash.cpp:126
size_t size() const
Definition: vector_ref.h:55
#define X(name)
Definition: net.cpp:642
void MDinit(uint32_t *MDbuf)
Definition: Hash.cpp:137
#define UL(i)
_T * data() const
Definition: vector_ref.h:51
h160 ripemd160(bytesConstRef _input)
Definition: Hash.cpp:400
h256 sha256(bytesConstRef _input)
Definition: Hash.cpp:33
#define JJ(a, b, c, d, e, x, s)
Definition: Hash.cpp:106
#define BYTES_TO_DWORD(strptr)
Definition: Hash.cpp:68