]>
Commit | Line | Data |
---|---|---|
a8a5e013 | 1 | // Copyright (c) 2013-2014 The Bitcoin developers |
2 | // Distributed under the MIT software license, see the accompanying | |
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | |
4 | ||
7ab026f4 | 5 | #include "hash.h" |
36fa4a78 | 6 | #include "crypto/hmac_sha512.h" |
7ab026f4 | 7 | |
20e01b1a | 8 | inline uint32_t ROTL32(uint32_t x, int8_t r) |
7ab026f4 MC |
9 | { |
10 | return (x << r) | (x >> (32 - r)); | |
11 | } | |
12 | ||
13 | unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash) | |
14 | { | |
15 | // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp | |
16 | uint32_t h1 = nHashSeed; | |
459a2d25 E |
17 | if (vDataToHash.size() > 0) |
18 | { | |
19 | const uint32_t c1 = 0xcc9e2d51; | |
20 | const uint32_t c2 = 0x1b873593; | |
7ab026f4 | 21 | |
459a2d25 | 22 | const int nblocks = vDataToHash.size() / 4; |
7ab026f4 | 23 | |
459a2d25 E |
24 | //---------- |
25 | // body | |
26 | const uint32_t* blocks = (const uint32_t*)(&vDataToHash[0] + nblocks * 4); | |
7ab026f4 | 27 | |
459a2d25 E |
28 | for (int i = -nblocks; i; i++) { |
29 | uint32_t k1 = blocks[i]; | |
7ab026f4 | 30 | |
459a2d25 E |
31 | k1 *= c1; |
32 | k1 = ROTL32(k1, 15); | |
33 | k1 *= c2; | |
7ab026f4 | 34 | |
459a2d25 E |
35 | h1 ^= k1; |
36 | h1 = ROTL32(h1, 13); | |
37 | h1 = h1 * 5 + 0xe6546b64; | |
38 | } | |
7ab026f4 | 39 | |
459a2d25 E |
40 | //---------- |
41 | // tail | |
42 | const uint8_t* tail = (const uint8_t*)(&vDataToHash[0] + nblocks * 4); | |
43 | ||
44 | uint32_t k1 = 0; | |
45 | ||
46 | switch (vDataToHash.size() & 3) { | |
47 | case 3: | |
48 | k1 ^= tail[2] << 16; | |
49 | case 2: | |
50 | k1 ^= tail[1] << 8; | |
51 | case 1: | |
52 | k1 ^= tail[0]; | |
53 | k1 *= c1; | |
54 | k1 = ROTL32(k1, 15); | |
55 | k1 *= c2; | |
56 | h1 ^= k1; | |
57 | }; | |
58 | } | |
7ab026f4 MC |
59 | |
60 | //---------- | |
61 | // finalization | |
62 | h1 ^= vDataToHash.size(); | |
63 | h1 ^= h1 >> 16; | |
64 | h1 *= 0x85ebca6b; | |
65 | h1 ^= h1 >> 13; | |
66 | h1 *= 0xc2b2ae35; | |
67 | h1 ^= h1 >> 16; | |
68 | ||
69 | return h1; | |
70 | } | |
78c228c6 CF |
71 | |
72 | void BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]) | |
73 | { | |
74 | unsigned char num[4]; | |
75 | num[0] = (nChild >> 24) & 0xFF; | |
76 | num[1] = (nChild >> 16) & 0xFF; | |
77 | num[2] = (nChild >> 8) & 0xFF; | |
78 | num[3] = (nChild >> 0) & 0xFF; | |
79 | CHMAC_SHA512(chainCode, 32).Write(&header, 1) | |
80 | .Write(data, 32) | |
81 | .Write(num, 4) | |
82 | .Finalize(output); | |
83 | } |