2010-07-14 17:54:31 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2023-01-12 22:26:21 +01:00
|
|
|
// Copyright (c) 2014-2022 The Dash 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
|
|
|
|
|
2022-09-16 11:26:57 +02:00
|
|
|
#include <crypto/common.h>
|
2023-02-06 19:56:32 +01:00
|
|
|
#include <span.h>
|
2022-09-16 11:26:57 +02:00
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cassert>
|
2014-09-14 12:43:56 +02:00
|
|
|
#include <cstring>
|
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
|
|
|
|
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
|
|
|
{
|
|
|
|
protected:
|
2017-11-12 00:07:23 +01:00
|
|
|
static constexpr int WIDTH = BITS / 8;
|
2023-02-06 19:56:32 +01:00
|
|
|
std::array<uint8_t, WIDTH> m_data;
|
|
|
|
static_assert(WIDTH == sizeof(m_data), "Sanity check");
|
|
|
|
|
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} {}
|
2014-04-19 23:02:47 +02:00
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr explicit base_blob(Span<const unsigned char> vch)
|
|
|
|
{
|
|
|
|
assert(vch.size() == WIDTH);
|
|
|
|
std::copy(vch.begin(), vch.end(), m_data.begin());
|
|
|
|
}
|
2014-04-19 23:02:47 +02:00
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr bool IsNull() const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2023-02-06 19:56:32 +01:00
|
|
|
return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
|
|
|
|
return val == 0;
|
|
|
|
});
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr void SetNull()
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2023-02-06 19:56:32 +01:00
|
|
|
std::fill(m_data.begin(), m_data.end(), 0);
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
|
2016-03-21 12:50:09 +01:00
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
|
|
|
|
friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
|
|
|
|
friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 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
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr const unsigned char* data() const { return m_data.data(); }
|
|
|
|
constexpr unsigned char* data() { return m_data.data(); }
|
2021-05-19 17:39:18 +02:00
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr unsigned char* begin() { return m_data.data(); }
|
|
|
|
constexpr unsigned char* end() { return m_data.data() + WIDTH; }
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr const unsigned char* begin() const { return m_data.data(); }
|
|
|
|
constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
static constexpr unsigned int size() { return WIDTH; }
|
2012-08-13 05:26:25 +02:00
|
|
|
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
|
2016-05-06 20:41:28 +02:00
|
|
|
|
2010-07-14 17:54:31 +02:00
|
|
|
template<typename Stream>
|
2016-11-09 12:32:57 +01:00
|
|
|
void Serialize(Stream& s) const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2023-02-06 19:56:32 +01:00
|
|
|
s.write((char*)m_data.data(), sizeof(m_data));
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2016-11-09 12:32:57 +01:00
|
|
|
void Unserialize(Stream& s)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
2023-02-06 19:56:32 +01:00
|
|
|
s.read((char*)m_data.data(), sizeof(m_data));
|
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:
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr uint160() = default;
|
|
|
|
constexpr explicit uint160(Span<const 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:
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr uint256() = default;
|
2022-05-07 18:20:42 +02:00
|
|
|
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
|
2023-02-06 19:56:32 +01:00
|
|
|
constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
|
2023-03-05 20:45:22 +01:00
|
|
|
static const uint256 ZERO;
|
2022-05-07 18:20:42 +02:00
|
|
|
static const uint256 ONE;
|
|
|
|
static const uint256 TWO;
|
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
|
|
|
|
2015-04-03 00:51:08 +02:00
|
|
|
/** 512-bit unsigned big integer. */
|
2016-02-02 16:28:56 +01:00
|
|
|
class uint512 : public base_blob<512> {
|
2014-11-29 12:29:14 +01:00
|
|
|
public:
|
2015-04-03 00:51:08 +02:00
|
|
|
uint512() {}
|
2016-02-02 16:28:56 +01:00
|
|
|
uint512(const base_blob<512>& b) : base_blob<512>(b) {}
|
|
|
|
explicit uint512(const std::vector<unsigned char>& vch) : base_blob<512>(vch) {}
|
2014-11-29 12:29:14 +01:00
|
|
|
|
|
|
|
uint256 trim256() const
|
|
|
|
{
|
2016-02-02 16:28:56 +01:00
|
|
|
uint256 result;
|
2023-02-06 19:56:32 +01:00
|
|
|
memcpy((void*)&result, (void*)m_data.data(), 32);
|
2016-02-02 16:28:56 +01:00
|
|
|
return result;
|
2014-11-29 12:29:14 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UINT256_H
|