Files
thehub/libs/utils/uint256.h
T

208 lines
5.6 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2009-2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
2021-02-03 16:31:46 +01:00
* Copyright (C) 2019-2021 Tom Zander <tom@flowee.org>
2017-11-09 19:34:51 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-04-13 00:13:08 -05:00
2018-01-16 10:47:52 +00:00
#ifndef FLOWEE_UINT256_H
#define FLOWEE_UINT256_H
2011-05-15 09:11:04 +02:00
2017-05-11 15:27:38 +02:00
#include "Logger.h"
2010-07-14 15:54:31 +00:00
#include <string>
2011-05-15 09:11:04 +02:00
#include <vector>
2017-05-11 15:27:38 +02:00
2014-12-15 10:22:19 +01:00
/** Template base class for fixed-sized opaque blobs. */
2010-07-14 15:54:31 +00:00
template<unsigned int BITS>
2014-12-15 10:22:19 +01:00
class base_blob
2010-07-14 15:54:31 +00:00
{
protected:
2014-12-15 10:22:19 +01:00
enum { WIDTH=BITS/8 };
uint8_t data[WIDTH];
2010-07-14 15:54:31 +00:00
public:
2014-12-15 10:22:19 +01:00
base_blob()
{
2014-12-15 10:22:19 +01:00
memset(data, 0, sizeof(data));
}
2014-12-15 10:22:19 +01:00
explicit base_blob(const std::vector<unsigned char>& vch);
explicit base_blob(const char *_data)
{
memcpy(data, _data, WIDTH);
}
2014-12-15 10:22:19 +01:00
bool IsNull() const
{
for (int i = 0; i < WIDTH; i++)
2014-12-15 10:22:19 +01:00
if (data[i] != 0)
2010-07-14 15:54:31 +00:00
return false;
return true;
}
2014-12-15 10:22:19 +01:00
void SetNull()
2010-07-14 15:54:31 +00:00
{
2014-12-15 10:22:19 +01:00
memset(data, 0, sizeof(data));
2010-07-14 15:54:31 +00:00
}
2018-11-13 14:59:31 +01:00
inline int Compare(const base_blob &other) const {
for (size_t i = sizeof(data); i > 0; --i) {
const uint8_t a = data[i - 1];
const uint8_t b = other.data[i - 1];
if (a > b)
2018-11-13 14:59:31 +01:00
return 1;
if (a < b)
2018-11-13 14:59:31 +01:00
return -1;
}
return 0;
}
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 15:54:31 +00:00
std::string GetHex() const;
void SetHex(const char* psz);
void SetHex(const std::string& str);
std::string ToString() const;
2010-07-14 15:54:31 +00:00
unsigned char* begin()
{
2014-12-15 10:22:19 +01:00
return &data[0];
2010-07-14 15:54:31 +00:00
}
unsigned char* end()
{
2014-12-15 10:22:19 +01:00
return &data[WIDTH];
2010-07-14 15:54:31 +00:00
}
const unsigned char* begin() const
{
2014-12-15 10:22:19 +01:00
return &data[0];
}
const unsigned char* end() const
{
2014-12-15 10:22:19 +01:00
return &data[WIDTH];
}
unsigned int size() const
2010-07-14 15:54:31 +00:00
{
2014-12-15 10:22:19 +01:00
return sizeof(data);
2012-01-04 23:39:45 +01:00
}
2010-07-14 15:54:31 +00:00
2012-04-16 14:56:45 +02:00
unsigned int GetSerializeSize(int nType, int nVersion) const
2010-07-14 15:54:31 +00:00
{
2014-12-15 10:22:19 +01:00
return sizeof(data);
2010-07-14 15:54:31 +00:00
}
template<typename Stream>
2012-04-16 14:56:45 +02:00
void Serialize(Stream& s, int nType, int nVersion) const
2010-07-14 15:54:31 +00:00
{
2014-12-15 10:22:19 +01:00
s.write((char*)data, sizeof(data));
2010-07-14 15:54:31 +00:00
}
template<typename Stream>
2012-04-16 14:56:45 +02:00
void Unserialize(Stream& s, int nType, int nVersion)
2010-07-14 15:54:31 +00:00
{
2014-12-15 10:22:19 +01:00
s.read((char*)data, sizeof(data));
}
2010-07-14 15:54:31 +00: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 15:54:31 +00:00
public:
uint160() {}
2014-12-15 10:22:19 +01:00
uint160(const base_blob<160>& b) : base_blob<160>(b) {}
explicit uint160(const char *d) : base_blob<160>(d) {}
2014-12-15 10:22:19 +01:00
explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
2010-07-14 15:54:31 +00: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 15:54:31 +00:00
public:
uint256() {}
uint256(const char *data) : base_blob<256>(data) {}
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) {}
2014-12-15 10:22:19 +01:00
/** 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.
*/
2021-01-05 22:05:25 +01:00
uint64_t GetCheapHash() const;
2010-07-14 15:54:31 +00: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;
}
2019-04-01 15:34:13 +02:00
/**
* Useful structure to use for maps.
* For instance:
*
* typedef boost::unordered_map<uint256, int, HashShortener> MyMap;
*/
struct HashShortener {
inline size_t operator()(const uint256& hash) const {
return hash.GetCheapHash();
}
};
2017-05-11 15:27:38 +02:00
2021-02-03 16:31:46 +01:00
struct HashComparison {
inline bool operator()(const uint256 &a, const uint256 &b) const {
return a == b;
}
};
2017-05-11 15:27:38 +02:00
template<unsigned int BITS>
inline Log::Item operator<<(Log::Item item, const base_blob<BITS> &data) {
if (item.isEnabled())
2017-06-19 13:31:08 +02:00
item << data.ToString().c_str();
return item;
2017-05-11 15:27:38 +02:00
}
template<unsigned int BITS>
2021-01-05 22:05:25 +01:00
inline Log::SilentItem operator<<(Log::SilentItem item, const base_blob<BITS>&) {
2017-05-11 15:27:38 +02:00
return item;
}
2018-01-16 10:47:52 +00:00
#endif