dash/src/uint256.h

172 lines
4.5 KiB
C
Raw Normal View History

2010-07-14 17:54:31 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Copyright (c) 2014-2022 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_UINT256_H
#define BITCOIN_UINT256_H
#include <assert.h>
#include <cstring>
#include <stdint.h>
2010-07-14 17:54:31 +02:00
#include <string>
#include <vector>
/** Template base class for fixed-sized opaque blobs. */
2010-07-14 17:54:31 +02:00
template<unsigned int BITS>
class base_blob
2010-07-14 17:54:31 +02:00
{
protected:
static constexpr int WIDTH = BITS / 8;
uint8_t m_data[WIDTH];
2010-07-14 17:54:31 +02:00
public:
2022-05-07 18:20:42 +02:00
/* construct 0 value by default */
constexpr base_blob() : m_data() {}
/* constructor for constants between 1 and 255 */
constexpr explicit base_blob(uint8_t v) : m_data{v} {}
explicit base_blob(const std::vector<unsigned char>& vch);
bool IsNull() const
2010-07-14 17:54:31 +02:00
{
for (int i = 0; i < WIDTH; i++)
if (m_data[i] != 0)
2010-07-14 17:54:31 +02:00
return false;
return true;
}
void SetNull()
2010-07-14 17:54:31 +02:00
{
memset(m_data, 0, sizeof(m_data));
2010-07-14 17:54:31 +02:00
}
inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); }
friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
2010-07-14 17:54:31 +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
const unsigned char* data() const { return m_data; }
unsigned char* data() { return m_data; }
2010-07-14 17:54:31 +02:00
unsigned char* begin()
{
return &m_data[0];
2010-07-14 17:54:31 +02:00
}
unsigned char* end()
{
return &m_data[WIDTH];
2010-07-14 17:54:31 +02:00
}
const unsigned char* begin() const
{
return &m_data[0];
}
const unsigned char* end() const
{
return &m_data[WIDTH];
}
unsigned int size() const
2010-07-14 17:54:31 +02:00
{
return sizeof(m_data);
}
2010-07-14 17:54:31 +02:00
2016-05-06 20:41:28 +02:00
uint64_t GetUint64(int pos) const
{
const uint8_t* ptr = m_data + pos * 8;
2016-05-06 20:41:28 +02:00
return ((uint64_t)ptr[0]) | \
((uint64_t)ptr[1]) << 8 | \
((uint64_t)ptr[2]) << 16 | \
((uint64_t)ptr[3]) << 24 | \
((uint64_t)ptr[4]) << 32 | \
((uint64_t)ptr[5]) << 40 | \
((uint64_t)ptr[6]) << 48 | \
((uint64_t)ptr[7]) << 56;
}
2010-07-14 17:54:31 +02:00
template<typename Stream>
void Serialize(Stream& s) const
2010-07-14 17:54:31 +02:00
{
s.write((char*)m_data, sizeof(m_data));
2010-07-14 17:54:31 +02:00
}
template<typename Stream>
void Unserialize(Stream& s)
2010-07-14 17:54:31 +02:00
{
s.read((char*)m_data, sizeof(m_data));
2010-07-14 17:54:31 +02: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:
2022-05-07 18:20:42 +02:00
constexpr uint160() {}
explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
2010-07-14 17:54:31 +02: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:
2022-05-07 18:20:42 +02:00
constexpr uint256() {}
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
2022-05-07 18:20:42 +02:00
static const uint256 ONE;
static const uint256 TWO;
2010-07-14 17:54:31 +02: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;
}
2015-04-03 00:51:08 +02:00
/** 512-bit unsigned big integer. */
class uint512 : public base_blob<512> {
public:
2015-04-03 00:51:08 +02:00
uint512() {}
uint512(const base_blob<512>& b) : base_blob<512>(b) {}
explicit uint512(const std::vector<unsigned char>& vch) : base_blob<512>(vch) {}
uint256 trim256() const
{
uint256 result;
memcpy((void*)&result, (void*)m_data, 32);
return result;
}
};
#endif // BITCOIN_UINT256_H