2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2013-2015 The Bitcoin Core developers
|
2014-11-08 21:38:11 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2013-01-10 19:16:00 +01:00
|
|
|
#include "hash.h"
|
2014-12-19 11:38:03 +01:00
|
|
|
#include "crypto/common.h"
|
2014-10-26 09:23:23 +01:00
|
|
|
#include "crypto/hmac_sha512.h"
|
2015-04-22 00:09:37 +02:00
|
|
|
#include "pubkey.h"
|
2013-01-10 19:16:00 +01:00
|
|
|
|
2014-12-19 11:38:03 +01:00
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
inline uint32_t ROTL32(uint32_t x, int8_t r)
|
2013-01-10 19:16:00 +01:00
|
|
|
{
|
|
|
|
return (x << r) | (x >> (32 - r));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash)
|
|
|
|
{
|
|
|
|
// The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
|
|
|
|
uint32_t h1 = nHashSeed;
|
2014-09-23 17:54:12 +02:00
|
|
|
if (vDataToHash.size() > 0)
|
|
|
|
{
|
|
|
|
const uint32_t c1 = 0xcc9e2d51;
|
|
|
|
const uint32_t c2 = 0x1b873593;
|
2013-01-10 19:16:00 +01:00
|
|
|
|
2014-09-23 17:54:12 +02:00
|
|
|
const int nblocks = vDataToHash.size() / 4;
|
2013-01-10 19:16:00 +01:00
|
|
|
|
2014-09-23 17:54:12 +02:00
|
|
|
//----------
|
|
|
|
// body
|
2014-12-19 11:38:03 +01:00
|
|
|
const uint8_t* blocks = &vDataToHash[0] + nblocks * 4;
|
2013-01-10 19:16:00 +01:00
|
|
|
|
2014-09-23 17:54:12 +02:00
|
|
|
for (int i = -nblocks; i; i++) {
|
2014-12-19 11:38:03 +01:00
|
|
|
uint32_t k1 = ReadLE32(blocks + i*4);
|
2013-01-10 19:16:00 +01:00
|
|
|
|
2014-09-23 17:54:12 +02:00
|
|
|
k1 *= c1;
|
|
|
|
k1 = ROTL32(k1, 15);
|
|
|
|
k1 *= c2;
|
2013-01-10 19:16:00 +01:00
|
|
|
|
2014-09-23 17:54:12 +02:00
|
|
|
h1 ^= k1;
|
|
|
|
h1 = ROTL32(h1, 13);
|
|
|
|
h1 = h1 * 5 + 0xe6546b64;
|
|
|
|
}
|
2013-01-10 19:16:00 +01:00
|
|
|
|
2014-09-23 17:54:12 +02:00
|
|
|
//----------
|
|
|
|
// tail
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|
2013-01-10 19:16:00 +01:00
|
|
|
|
|
|
|
//----------
|
|
|
|
// finalization
|
|
|
|
h1 ^= vDataToHash.size();
|
|
|
|
h1 ^= h1 >> 16;
|
|
|
|
h1 *= 0x85ebca6b;
|
|
|
|
h1 ^= h1 >> 13;
|
|
|
|
h1 *= 0xc2b2ae35;
|
|
|
|
h1 ^= h1 >> 16;
|
|
|
|
|
|
|
|
return h1;
|
|
|
|
}
|
2014-10-28 22:35:24 +01:00
|
|
|
|
2015-04-22 00:09:37 +02:00
|
|
|
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
|
2014-10-28 22:35:24 +01:00
|
|
|
{
|
|
|
|
unsigned char num[4];
|
|
|
|
num[0] = (nChild >> 24) & 0xFF;
|
|
|
|
num[1] = (nChild >> 16) & 0xFF;
|
|
|
|
num[2] = (nChild >> 8) & 0xFF;
|
|
|
|
num[3] = (nChild >> 0) & 0xFF;
|
2015-04-22 00:09:37 +02:00
|
|
|
CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
|
2014-10-28 22:35:24 +01:00
|
|
|
}
|
2016-05-06 20:41:28 +02:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
v[0] = 0x736f6d6570736575ULL ^ k0;
|
|
|
|
v[1] = 0x646f72616e646f6dULL ^ k1;
|
|
|
|
v[2] = 0x6c7967656e657261ULL ^ k0;
|
|
|
|
v[3] = 0x7465646279746573ULL ^ k1;
|
|
|
|
count = 0;
|
2016-05-22 11:06:18 +02:00
|
|
|
tmp = 0;
|
2016-05-06 20:41:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSipHasher& CSipHasher::Write(uint64_t data)
|
|
|
|
{
|
|
|
|
uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
|
|
|
|
|
2016-05-22 11:06:18 +02:00
|
|
|
assert(count % 8 == 0);
|
|
|
|
|
2016-05-06 20:41:28 +02:00
|
|
|
v3 ^= data;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
v0 ^= data;
|
|
|
|
|
|
|
|
v[0] = v0;
|
|
|
|
v[1] = v1;
|
|
|
|
v[2] = v2;
|
|
|
|
v[3] = v3;
|
|
|
|
|
2016-05-22 11:06:18 +02:00
|
|
|
count += 8;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
while (size--) {
|
|
|
|
t |= ((uint64_t)(*(data++))) << (8 * (c % 8));
|
|
|
|
c++;
|
|
|
|
if ((c & 7) == 0) {
|
|
|
|
v3 ^= t;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
v0 ^= t;
|
|
|
|
t = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v[0] = v0;
|
|
|
|
v[1] = v1;
|
|
|
|
v[2] = v2;
|
|
|
|
v[3] = v3;
|
|
|
|
count = c;
|
|
|
|
tmp = t;
|
|
|
|
|
2016-05-06 20:41:28 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t CSipHasher::Finalize() const
|
|
|
|
{
|
|
|
|
uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
|
|
|
|
|
2016-05-22 11:06:18 +02:00
|
|
|
uint64_t t = tmp | (((uint64_t)count) << 56);
|
|
|
|
|
|
|
|
v3 ^= t;
|
2016-05-06 20:41:28 +02:00
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
2016-05-22 11:06:18 +02:00
|
|
|
v0 ^= t;
|
2016-05-06 20:41:28 +02:00
|
|
|
v2 ^= 0xFF;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
return v0 ^ v1 ^ v2 ^ v3;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val)
|
|
|
|
{
|
|
|
|
/* Specialized implementation for efficiency */
|
|
|
|
uint64_t d = val.GetUint64(0);
|
|
|
|
|
|
|
|
uint64_t v0 = 0x736f6d6570736575ULL ^ k0;
|
|
|
|
uint64_t v1 = 0x646f72616e646f6dULL ^ k1;
|
|
|
|
uint64_t v2 = 0x6c7967656e657261ULL ^ k0;
|
|
|
|
uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d;
|
|
|
|
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
v0 ^= d;
|
|
|
|
d = val.GetUint64(1);
|
|
|
|
v3 ^= d;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
v0 ^= d;
|
|
|
|
d = val.GetUint64(2);
|
|
|
|
v3 ^= d;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
v0 ^= d;
|
|
|
|
d = val.GetUint64(3);
|
|
|
|
v3 ^= d;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
v0 ^= d;
|
|
|
|
v3 ^= ((uint64_t)4) << 59;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
v0 ^= ((uint64_t)4) << 59;
|
|
|
|
v2 ^= 0xFF;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
SIPROUND;
|
|
|
|
return v0 ^ v1 ^ v2 ^ v3;
|
|
|
|
}
|