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