2014-06-28 17:35:22 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-06-28 17:35:22 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "uint256.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
|
|
|
#include "utilstrencodings.h"
|
2014-06-28 17:35:22 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
assert(vch.size() == sizeof(data));
|
|
|
|
memcpy(data, &vch[0], sizeof(data));
|
2014-06-28 17:35:22 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
std::string base_blob<BITS>::GetHex() const
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
char psz[sizeof(data) * 2 + 1];
|
|
|
|
for (unsigned int i = 0; i < sizeof(data); i++)
|
|
|
|
sprintf(psz + i * 2, "%02x", data[sizeof(data) - i - 1]);
|
|
|
|
return std::string(psz, psz + sizeof(data) * 2);
|
2014-06-28 17:35:22 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
void base_blob<BITS>::SetHex(const char* psz)
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
memset(data, 0, sizeof(data));
|
2014-06-28 17:35:22 +02:00
|
|
|
|
|
|
|
// skip leading spaces
|
|
|
|
while (isspace(*psz))
|
|
|
|
psz++;
|
|
|
|
|
|
|
|
// skip 0x
|
|
|
|
if (psz[0] == '0' && tolower(psz[1]) == 'x')
|
|
|
|
psz += 2;
|
|
|
|
|
|
|
|
// hex string to uint
|
|
|
|
const char* pbegin = psz;
|
|
|
|
while (::HexDigit(*psz) != -1)
|
|
|
|
psz++;
|
|
|
|
psz--;
|
2014-12-15 10:22:19 +01:00
|
|
|
unsigned char* p1 = (unsigned char*)data;
|
2014-06-28 17:35:22 +02:00
|
|
|
unsigned char* pend = p1 + WIDTH * 4;
|
|
|
|
while (psz >= pbegin && p1 < pend) {
|
|
|
|
*p1 = ::HexDigit(*psz--);
|
|
|
|
if (psz >= pbegin) {
|
|
|
|
*p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
void base_blob<BITS>::SetHex(const std::string& str)
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
|
|
|
SetHex(str.c_str());
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
std::string base_blob<BITS>::ToString() const
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
|
|
|
return (GetHex());
|
|
|
|
}
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
// Explicit instantiations for base_blob<160>
|
|
|
|
template base_blob<160>::base_blob(const std::vector<unsigned char>&);
|
|
|
|
template std::string base_blob<160>::GetHex() const;
|
|
|
|
template std::string base_blob<160>::ToString() const;
|
|
|
|
template void base_blob<160>::SetHex(const char*);
|
|
|
|
template void base_blob<160>::SetHex(const std::string&);
|
2014-06-28 17:35:22 +02:00
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
// Explicit instantiations for base_blob<256>
|
|
|
|
template base_blob<256>::base_blob(const std::vector<unsigned char>&);
|
|
|
|
template std::string base_blob<256>::GetHex() const;
|
|
|
|
template std::string base_blob<256>::ToString() const;
|
|
|
|
template void base_blob<256>::SetHex(const char*);
|
|
|
|
template void base_blob<256>::SetHex(const std::string&);
|
2014-07-09 17:25:09 +02:00
|
|
|
|
|
|
|
static void inline HashMix(uint32_t& a, uint32_t& b, uint32_t& c)
|
|
|
|
{
|
|
|
|
// Taken from lookup3, by Bob Jenkins.
|
2014-09-19 19:21:46 +02:00
|
|
|
a -= c;
|
|
|
|
a ^= ((c << 4) | (c >> 28));
|
|
|
|
c += b;
|
|
|
|
b -= a;
|
|
|
|
b ^= ((a << 6) | (a >> 26));
|
|
|
|
a += c;
|
|
|
|
c -= b;
|
|
|
|
c ^= ((b << 8) | (b >> 24));
|
|
|
|
b += a;
|
|
|
|
a -= c;
|
|
|
|
a ^= ((c << 16) | (c >> 16));
|
|
|
|
c += b;
|
|
|
|
b -= a;
|
|
|
|
b ^= ((a << 19) | (a >> 13));
|
|
|
|
a += c;
|
|
|
|
c -= b;
|
|
|
|
c ^= ((b << 4) | (b >> 28));
|
|
|
|
b += a;
|
2014-07-09 17:25:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void inline HashFinal(uint32_t& a, uint32_t& b, uint32_t& c)
|
|
|
|
{
|
|
|
|
// Taken from lookup3, by Bob Jenkins.
|
2014-09-19 19:21:46 +02:00
|
|
|
c ^= b;
|
|
|
|
c -= ((b << 14) | (b >> 18));
|
|
|
|
a ^= c;
|
|
|
|
a -= ((c << 11) | (c >> 21));
|
|
|
|
b ^= a;
|
|
|
|
b -= ((a << 25) | (a >> 7));
|
|
|
|
c ^= b;
|
|
|
|
c -= ((b << 16) | (b >> 16));
|
|
|
|
a ^= c;
|
|
|
|
a -= ((c << 4) | (c >> 28));
|
|
|
|
b ^= a;
|
|
|
|
b -= ((a << 14) | (a >> 18));
|
|
|
|
c ^= b;
|
|
|
|
c -= ((b << 24) | (b >> 8));
|
2014-07-09 17:25:09 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
uint64_t uint256::GetHash(const uint256& salt) const
|
2014-07-09 17:25:09 +02:00
|
|
|
{
|
|
|
|
uint32_t a, b, c;
|
2014-12-15 10:22:19 +01:00
|
|
|
const uint32_t *pn = (const uint32_t*)data;
|
|
|
|
const uint32_t *salt_pn = (const uint32_t*)salt.data;
|
2014-07-09 17:25:09 +02:00
|
|
|
a = b = c = 0xdeadbeef + (WIDTH << 2);
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
a += pn[0] ^ salt_pn[0];
|
|
|
|
b += pn[1] ^ salt_pn[1];
|
|
|
|
c += pn[2] ^ salt_pn[2];
|
2014-07-09 17:25:09 +02:00
|
|
|
HashMix(a, b, c);
|
2014-12-15 10:22:19 +01:00
|
|
|
a += pn[3] ^ salt_pn[3];
|
|
|
|
b += pn[4] ^ salt_pn[4];
|
|
|
|
c += pn[5] ^ salt_pn[5];
|
2014-07-09 17:25:09 +02:00
|
|
|
HashMix(a, b, c);
|
2014-12-15 10:22:19 +01:00
|
|
|
a += pn[6] ^ salt_pn[6];
|
|
|
|
b += pn[7] ^ salt_pn[7];
|
2014-07-09 17:25:09 +02:00
|
|
|
HashFinal(a, b, c);
|
|
|
|
|
|
|
|
return ((((uint64_t)b) << 32) | c);
|
|
|
|
}
|