2010-07-14 17:54:31 +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-10-31 01:43:19 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
#ifndef BITCOIN_UINT256_H
|
|
|
|
#define BITCOIN_UINT256_H
|
|
|
|
|
2014-04-19 23:25:44 +02:00
|
|
|
#include <assert.h>
|
2014-09-14 12:43:56 +02:00
|
|
|
#include <cstring>
|
2014-04-19 23:25:44 +02:00
|
|
|
#include <stdexcept>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
2010-07-14 17:54:31 +02:00
|
|
|
#include <string>
|
2011-05-15 09:11:04 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
/** Template base class for fixed-sized opaque blobs. */
|
2010-07-14 17:54:31 +02:00
|
|
|
template<unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
class base_blob
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2014-07-09 17:25:09 +02:00
|
|
|
protected:
|
2014-12-15 10:22:19 +01:00
|
|
|
enum { WIDTH=BITS/8 };
|
|
|
|
uint8_t data[WIDTH];
|
2010-07-14 17:54:31 +02:00
|
|
|
public:
|
2014-12-15 10:22:19 +01:00
|
|
|
base_blob()
|
2014-04-19 23:02:47 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
memset(data, 0, sizeof(data));
|
2014-04-19 23:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
explicit base_blob(const std::vector<unsigned char>& vch);
|
2014-04-19 23:02:47 +02:00
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
bool IsNull() const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
2014-12-15 10:22:19 +01:00
|
|
|
if (data[i] != 0)
|
2010-07-14 17:54:31 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
void SetNull()
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
memset(data, 0, sizeof(data));
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; }
|
|
|
|
friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; }
|
|
|
|
friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; }
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2014-06-28 17:35:22 +02:00
|
|
|
std::string GetHex() const;
|
|
|
|
void SetHex(const char* psz);
|
|
|
|
void SetHex(const std::string& str);
|
|
|
|
std::string ToString() const;
|
2010-07-14 17:54:31 +02:00
|
|
|
|
|
|
|
unsigned char* begin()
|
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
return &data[0];
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* end()
|
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
return &data[WIDTH];
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
2012-08-13 05:26:25 +02:00
|
|
|
const unsigned char* begin() const
|
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
return &data[0];
|
2012-08-13 05:26:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* end() const
|
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
return &data[WIDTH];
|
2012-08-13 05:26:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int size() const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
return sizeof(data);
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2012-04-16 14:56:45 +02:00
|
|
|
unsigned int GetSerializeSize(int nType, int nVersion) const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
return sizeof(data);
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2012-04-16 14:56:45 +02:00
|
|
|
void Serialize(Stream& s, int nType, int nVersion) const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
s.write((char*)data, sizeof(data));
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2012-04-16 14:56:45 +02:00
|
|
|
void Unserialize(Stream& s, int nType, int nVersion)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
s.read((char*)data, sizeof(data));
|
2014-12-15 10:05:51 +01:00
|
|
|
}
|
2010-07-14 17:54:31 +02:00
|
|
|
};
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
/** 160-bit opaque blob.
|
|
|
|
* @note This type is called uint160 for historical reasons only. It is an opaque
|
|
|
|
* blob of 160 bits and has no integer operations.
|
|
|
|
*/
|
|
|
|
class uint160 : public base_blob<160> {
|
2010-07-14 17:54:31 +02:00
|
|
|
public:
|
2014-04-19 23:02:47 +02:00
|
|
|
uint160() {}
|
2014-12-15 10:22:19 +01:00
|
|
|
uint160(const base_blob<160>& b) : base_blob<160>(b) {}
|
|
|
|
explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
|
2010-07-14 17:54:31 +02:00
|
|
|
};
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
/** 256-bit opaque blob.
|
|
|
|
* @note This type is called uint256 for historical reasons only. It is an
|
|
|
|
* opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
|
|
|
|
* those are required.
|
|
|
|
*/
|
|
|
|
class uint256 : public base_blob<256> {
|
2010-07-14 17:54:31 +02:00
|
|
|
public:
|
2014-04-19 23:02:47 +02:00
|
|
|
uint256() {}
|
2014-12-15 10:22:19 +01:00
|
|
|
uint256(const base_blob<256>& b) : base_blob<256>(b) {}
|
|
|
|
explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
|
|
|
|
|
|
|
|
/** A cheap hash function that just returns 64 bits from the result, it can be
|
|
|
|
* used when the contents are considered uniformly random. It is not appropriate
|
|
|
|
* when the value can easily be influenced from outside as e.g. a network adversary could
|
|
|
|
* provide values to trigger worst-case behavior.
|
|
|
|
* @note The result of this function is not stable between little and big endian.
|
2014-10-31 01:43:19 +01:00
|
|
|
*/
|
2014-12-15 10:22:19 +01:00
|
|
|
uint64_t GetCheapHash() const
|
|
|
|
{
|
|
|
|
uint64_t result;
|
|
|
|
memcpy((void*)&result, (void*)data, 8);
|
|
|
|
return result;
|
|
|
|
}
|
2014-07-09 17:25:09 +02:00
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
/** A more secure, salted hash function.
|
|
|
|
* @note This hash is not stable between little and big endian.
|
|
|
|
*/
|
2014-07-09 17:25:09 +02:00
|
|
|
uint64_t GetHash(const uint256& salt) const;
|
2010-07-14 17:54:31 +02:00
|
|
|
};
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
/* uint256 from const char *.
|
|
|
|
* This is a separate function because the constructor uint256(const char*) can result
|
|
|
|
* in dangerously catching uint256(0).
|
|
|
|
*/
|
|
|
|
inline uint256 uint256S(const char *str)
|
|
|
|
{
|
|
|
|
uint256 rv;
|
|
|
|
rv.SetHex(str);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
/* uint256 from std::string.
|
|
|
|
* This is a separate function because the constructor uint256(const std::string &str) can result
|
|
|
|
* in dangerously catching uint256(0) via std::string(const char*).
|
|
|
|
*/
|
|
|
|
inline uint256 uint256S(const std::string& str)
|
|
|
|
{
|
|
|
|
uint256 rv;
|
|
|
|
rv.SetHex(str);
|
|
|
|
return rv;
|
|
|
|
}
|
2014-12-15 10:05:51 +01:00
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UINT256_H
|