1 // Copyright (c) 2013-2016 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.
6 #include "crypto/common.h"
7 #include "crypto/hmac_sha512.h"
11 inline uint32_t ROTL32(uint32_t x
, int8_t r
)
13 return (x
<< r
) | (x
>> (32 - r
));
16 unsigned int MurmurHash3(unsigned int nHashSeed
, const std::vector
<unsigned char>& vDataToHash
)
18 // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
19 uint32_t h1
= nHashSeed
;
20 const uint32_t c1
= 0xcc9e2d51;
21 const uint32_t c2
= 0x1b873593;
23 const int nblocks
= vDataToHash
.size() / 4;
27 const uint8_t* blocks
= vDataToHash
.data();
29 for (int i
= 0; i
< nblocks
; ++i
) {
30 uint32_t k1
= ReadLE32(blocks
+ i
*4);
38 h1
= h1
* 5 + 0xe6546b64;
43 const uint8_t* tail
= vDataToHash
.data() + nblocks
* 4;
47 switch (vDataToHash
.size() & 3) {
62 h1
^= vDataToHash
.size();
72 void BIP32Hash(const ChainCode
&chainCode
, unsigned int nChild
, unsigned char header
, const unsigned char data
[32], unsigned char output
[64])
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
.begin(), chainCode
.size()).Write(&header
, 1).Write(data
, 32).Write(num
, 4).Finalize(output
);
82 #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
84 #define SIPROUND do { \
85 v0 += v1; v1 = ROTL(v1, 13); v1 ^= v0; \
87 v2 += v3; v3 = ROTL(v3, 16); v3 ^= v2; \
88 v0 += v3; v3 = ROTL(v3, 21); v3 ^= v0; \
89 v2 += v1; v1 = ROTL(v1, 17); v1 ^= v2; \
93 CSipHasher::CSipHasher(uint64_t k0
, uint64_t k1
)
95 v
[0] = 0x736f6d6570736575ULL
^ k0
;
96 v
[1] = 0x646f72616e646f6dULL
^ k1
;
97 v
[2] = 0x6c7967656e657261ULL
^ k0
;
98 v
[3] = 0x7465646279746573ULL
^ k1
;
103 CSipHasher
& CSipHasher::Write(uint64_t data
)
105 uint64_t v0
= v
[0], v1
= v
[1], v2
= v
[2], v3
= v
[3];
107 assert(count
% 8 == 0);
123 CSipHasher
& CSipHasher::Write(const unsigned char* data
, size_t size
)
125 uint64_t v0
= v
[0], v1
= v
[1], v2
= v
[2], v3
= v
[3];
130 t
|= ((uint64_t)(*(data
++))) << (8 * (c
% 8));
151 uint64_t CSipHasher::Finalize() const
153 uint64_t v0
= v
[0], v1
= v
[1], v2
= v
[2], v3
= v
[3];
155 uint64_t t
= tmp
| (((uint64_t)count
) << 56);
166 return v0
^ v1
^ v2
^ v3
;
169 uint64_t SipHashUint256(uint64_t k0
, uint64_t k1
, const uint256
& val
)
171 /* Specialized implementation for efficiency */
172 uint64_t d
= val
.GetUint64(0);
174 uint64_t v0
= 0x736f6d6570736575ULL
^ k0
;
175 uint64_t v1
= 0x646f72616e646f6dULL
^ k1
;
176 uint64_t v2
= 0x6c7967656e657261ULL
^ k0
;
177 uint64_t v3
= 0x7465646279746573ULL
^ k1
^ d
;
182 d
= val
.GetUint64(1);
187 d
= val
.GetUint64(2);
192 d
= val
.GetUint64(3);
197 v3
^= ((uint64_t)4) << 59;
200 v0
^= ((uint64_t)4) << 59;
206 return v0
^ v1
^ v2
^ v3
;
209 uint64_t SipHashUint256Extra(uint64_t k0
, uint64_t k1
, const uint256
& val
, uint32_t extra
)
211 /* Specialized implementation for efficiency */
212 uint64_t d
= val
.GetUint64(0);
214 uint64_t v0
= 0x736f6d6570736575ULL
^ k0
;
215 uint64_t v1
= 0x646f72616e646f6dULL
^ k1
;
216 uint64_t v2
= 0x6c7967656e657261ULL
^ k0
;
217 uint64_t v3
= 0x7465646279746573ULL
^ k1
^ d
;
222 d
= val
.GetUint64(1);
227 d
= val
.GetUint64(2);
232 d
= val
.GetUint64(3);
237 d
= (((uint64_t)36) << 56) | extra
;
247 return v0
^ v1
^ v2
^ v3
;