diff --git a/src/hash.cpp b/src/hash.cpp --- a/src/hash.cpp +++ b/src/hash.cpp @@ -7,18 +7,16 @@ #include "crypto/hmac_sha512.h" #include "pubkey.h" - -inline uint32_t ROTL32(uint32_t x, int8_t r) -{ +inline uint32_t ROTL32(uint32_t x, int8_t r) { return (x << r) | (x >> (32 - r)); } -unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector& vDataToHash) -{ - // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp +unsigned int MurmurHash3(unsigned int nHashSeed, + const std::vector &vDataToHash) { + // The following is MurmurHash3 (x86_32), see + // http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp uint32_t h1 = nHashSeed; - if (vDataToHash.size() > 0) - { + if (vDataToHash.size() > 0) { const uint32_t c1 = 0xcc9e2d51; const uint32_t c2 = 0x1b873593; @@ -26,10 +24,10 @@ //---------- // body - const uint8_t* blocks = &vDataToHash[0] + nblocks * 4; + const uint8_t *blocks = &vDataToHash[0] + nblocks * 4; for (int i = -nblocks; i; i++) { - uint32_t k1 = ReadLE32(blocks + i*4); + uint32_t k1 = ReadLE32(blocks + i * 4); k1 *= c1; k1 = ROTL32(k1, 15); @@ -42,21 +40,21 @@ //---------- // tail - const uint8_t* tail = (const uint8_t*)(&vDataToHash[0] + nblocks * 4); + const uint8_t *tail = (const uint8_t *)(&vDataToHash[0] + nblocks * 4); uint32_t k1 = 0; switch (vDataToHash.size() & 3) { - case 3: - k1 ^= tail[2] << 16; - case 2: - k1 ^= tail[1] << 8; - case 1: - k1 ^= tail[0]; - k1 *= c1; - k1 = ROTL32(k1, 15); - k1 *= c2; - h1 ^= k1; + case 3: + k1 ^= tail[2] << 16; + case 2: + k1 ^= tail[1] << 8; + case 1: + k1 ^= tail[0]; + k1 *= c1; + k1 = ROTL32(k1, 15); + k1 *= c2; + h1 ^= k1; } } @@ -72,29 +70,42 @@ return h1; } -void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]) -{ +void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, + unsigned char header, const unsigned char data[32], + unsigned char output[64]) { unsigned char num[4]; num[0] = (nChild >> 24) & 0xFF; num[1] = (nChild >> 16) & 0xFF; - num[2] = (nChild >> 8) & 0xFF; - num[3] = (nChild >> 0) & 0xFF; - CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output); + num[2] = (nChild >> 8) & 0xFF; + num[3] = (nChild >> 0) & 0xFF; + CHMAC_SHA512(chainCode.begin(), chainCode.size()) + .Write(&header, 1) + .Write(data, 32) + .Write(num, 4) + .Finalize(output); } #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) -#define SIPROUND do { \ - v0 += v1; v1 = ROTL(v1, 13); v1 ^= v0; \ - v0 = ROTL(v0, 32); \ - v2 += v3; v3 = ROTL(v3, 16); v3 ^= v2; \ - v0 += v3; v3 = ROTL(v3, 21); v3 ^= v0; \ - v2 += v1; v1 = ROTL(v1, 17); v1 ^= v2; \ - v2 = ROTL(v2, 32); \ -} while (0) - -CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) -{ +#define SIPROUND \ + do { \ + v0 += v1; \ + v1 = ROTL(v1, 13); \ + v1 ^= v0; \ + v0 = ROTL(v0, 32); \ + v2 += v3; \ + v3 = ROTL(v3, 16); \ + v3 ^= v2; \ + v0 += v3; \ + v3 = ROTL(v3, 21); \ + v3 ^= v0; \ + v2 += v1; \ + v1 = ROTL(v1, 17); \ + v1 ^= v2; \ + v2 = ROTL(v2, 32); \ + } while (0) + +CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) { v[0] = 0x736f6d6570736575ULL ^ k0; v[1] = 0x646f72616e646f6dULL ^ k1; v[2] = 0x6c7967656e657261ULL ^ k0; @@ -103,8 +114,7 @@ tmp = 0; } -CSipHasher& CSipHasher::Write(uint64_t data) -{ +CSipHasher &CSipHasher::Write(uint64_t data) { uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; assert(count % 8 == 0); @@ -123,8 +133,7 @@ return *this; } -CSipHasher& CSipHasher::Write(const unsigned char* data, size_t size) -{ +CSipHasher &CSipHasher::Write(const unsigned char *data, size_t size) { uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; uint64_t t = tmp; int c = count; @@ -151,8 +160,7 @@ return *this; } -uint64_t CSipHasher::Finalize() const -{ +uint64_t CSipHasher::Finalize() const { uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; uint64_t t = tmp | (((uint64_t)count) << 56); @@ -169,8 +177,7 @@ return v0 ^ v1 ^ v2 ^ v3; } -uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val) -{ +uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val) { /* Specialized implementation for efficiency */ uint64_t d = val.GetUint64(0);