2010-07-14 15:54:31 +00:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2024-07-18 22:38:14 +02:00
|
|
|
// Copyright (c) 2009-present The Bitcoin Core developers
|
2014-10-31 08:43:19 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 22:02:28 +08:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
#ifndef BITCOIN_UINT256_H
|
|
|
|
#define BITCOIN_UINT256_H
|
|
|
|
|
2022-09-15 16:48:50 -04:00
|
|
|
#include <crypto/common.h>
|
2022-01-02 11:31:25 +01:00
|
|
|
#include <span.h>
|
2024-07-18 22:38:14 +02:00
|
|
|
#include <util/strencodings.h>
|
2022-01-02 11:31:25 +01:00
|
|
|
|
2022-10-19 15:16:04 -05:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cassert>
|
2024-07-18 22:38:14 +02:00
|
|
|
#include <cstdint>
|
2014-09-14 12:43:56 +02:00
|
|
|
#include <cstring>
|
2024-07-18 22:38:14 +02:00
|
|
|
#include <optional>
|
2010-07-14 15:54:31 +00: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 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
|
|
|
{
|
2014-07-09 17:25:09 +02:00
|
|
|
protected:
|
2017-07-05 16:49:57 +02:00
|
|
|
static constexpr int WIDTH = BITS / 8;
|
2023-06-22 01:31:06 +02:00
|
|
|
static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
|
2022-10-19 15:16:04 -05:00
|
|
|
std::array<uint8_t, WIDTH> m_data;
|
|
|
|
static_assert(WIDTH == sizeof(m_data), "Sanity check");
|
|
|
|
|
2010-07-14 15:54:31 +00:00
|
|
|
public:
|
2020-09-25 15:01:38 +10: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
|
|
|
|
2022-10-19 15:16:04 -05: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
|
|
|
|
2024-08-01 23:33:24 +02:00
|
|
|
consteval explicit base_blob(std::string_view hex_str);
|
|
|
|
|
2022-10-19 15:16:04 -05:00
|
|
|
constexpr bool IsNull() const
|
2010-07-14 15:54:31 +00:00
|
|
|
{
|
2022-10-19 15:16:04 -05:00
|
|
|
return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
|
|
|
|
return val == 0;
|
|
|
|
});
|
2010-07-14 15:54:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 15:16:04 -05:00
|
|
|
constexpr void SetNull()
|
2010-07-14 15:54:31 +00:00
|
|
|
{
|
2022-10-19 15:16:04 -05:00
|
|
|
std::fill(m_data.begin(), m_data.end(), 0);
|
2010-07-14 15:54:31 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 23:20:07 +02:00
|
|
|
/** Lexicographic ordering
|
|
|
|
* @note Does NOT match the ordering on the corresponding \ref
|
|
|
|
* base_uint::CompareTo, which starts comparing from the end.
|
|
|
|
*/
|
2022-10-19 15:16:04 -05:00
|
|
|
constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
|
2016-03-18 04:14:19 +00:00
|
|
|
|
2022-10-19 15:16:04 -05: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 15:54:31 +00:00
|
|
|
|
2024-08-01 23:20:07 +02:00
|
|
|
/** @name Hex representation
|
|
|
|
*
|
|
|
|
* The reverse-byte hex representation is a convenient way to view the blob
|
|
|
|
* as a number, because it is consistent with the way the base_uint class
|
|
|
|
* converts blobs to numbers.
|
|
|
|
*
|
|
|
|
* @note base_uint treats the blob as an array of bytes with the numerically
|
|
|
|
* least significant byte first and the most significant byte last. Because
|
|
|
|
* numbers are typically written with the most significant digit first and
|
|
|
|
* the least significant digit last, the reverse hex display of the blob
|
|
|
|
* corresponds to the same numeric value that base_uint interprets from the
|
|
|
|
* blob.
|
|
|
|
* @{*/
|
2014-06-28 17:35:22 +02:00
|
|
|
std::string GetHex() const;
|
2024-08-01 23:20:07 +02:00
|
|
|
/** Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
|
|
|
|
*
|
|
|
|
* - Hex numbers that don't specify enough bytes to fill the internal array
|
|
|
|
* will be treated as setting the beginning of it, which corresponds to
|
|
|
|
* the least significant bytes when converted to base_uint.
|
|
|
|
*
|
|
|
|
* - Hex numbers specifying too many bytes will have the numerically most
|
|
|
|
* significant bytes (the beginning of the string) narrowed away.
|
|
|
|
*
|
|
|
|
* - An odd count of hex digits will result in the high bits of the leftmost
|
|
|
|
* byte being zero.
|
|
|
|
* "0x123" => {0x23, 0x1, 0x0, ..., 0x0}
|
|
|
|
*/
|
2024-07-18 22:24:38 +02:00
|
|
|
void SetHexDeprecated(std::string_view str);
|
2014-06-28 17:35:22 +02:00
|
|
|
std::string ToString() const;
|
2024-08-01 23:20:07 +02:00
|
|
|
/**@}*/
|
2010-07-14 15:54:31 +00:00
|
|
|
|
2022-10-19 15:16:04 -05:00
|
|
|
constexpr const unsigned char* data() const { return m_data.data(); }
|
|
|
|
constexpr unsigned char* data() { return m_data.data(); }
|
2020-06-18 14:12:54 -07:00
|
|
|
|
2022-10-19 15:16:04 -05:00
|
|
|
constexpr unsigned char* begin() { return m_data.data(); }
|
|
|
|
constexpr unsigned char* end() { return m_data.data() + WIDTH; }
|
2010-07-14 15:54:31 +00:00
|
|
|
|
2022-10-19 15:16:04 -05: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 15:54:31 +00:00
|
|
|
|
2022-10-19 15:16:04 -05:00
|
|
|
static constexpr unsigned int size() { return WIDTH; }
|
2012-08-13 05:26:25 +02:00
|
|
|
|
2022-10-19 15:16:04 -05: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 15:54:31 +00:00
|
|
|
template<typename Stream>
|
2016-10-28 16:29:17 -07:00
|
|
|
void Serialize(Stream& s) const
|
2010-07-14 15:54:31 +00:00
|
|
|
{
|
2023-06-22 17:02:28 +02:00
|
|
|
s << Span(m_data);
|
2010-07-14 15:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2016-10-28 16:29:17 -07:00
|
|
|
void Unserialize(Stream& s)
|
2010-07-14 15:54:31 +00:00
|
|
|
{
|
2022-01-02 11:31:25 +01:00
|
|
|
s.read(MakeWritableByteSpan(m_data));
|
2014-12-15 10:05:51 +01:00
|
|
|
}
|
2010-07-14 15:54:31 +00:00
|
|
|
};
|
|
|
|
|
2024-08-01 23:33:24 +02:00
|
|
|
template <unsigned int BITS>
|
|
|
|
consteval base_blob<BITS>::base_blob(std::string_view hex_str)
|
|
|
|
{
|
|
|
|
// Non-lookup table version of HexDigit().
|
|
|
|
auto from_hex = [](const char c) -> int8_t {
|
|
|
|
if (c >= '0' && c <= '9') return c - '0';
|
|
|
|
if (c >= 'a' && c <= 'f') return c - 'a' + 0xA;
|
|
|
|
if (c >= 'A' && c <= 'F') return c - 'A' + 0xA;
|
|
|
|
|
|
|
|
assert(false); // Reached if ctor is called with an invalid hex digit.
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(hex_str.length() == m_data.size() * 2); // 2 hex digits per byte.
|
|
|
|
auto str_it = hex_str.rbegin();
|
|
|
|
for (auto& elem : m_data) {
|
|
|
|
auto lo = from_hex(*(str_it++));
|
|
|
|
elem = (from_hex(*(str_it++)) << 4) | lo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 22:38:14 +02:00
|
|
|
namespace detail {
|
|
|
|
/**
|
2024-08-01 23:20:07 +02:00
|
|
|
* Writes the hex string (in reverse byte order) into a new uintN_t object
|
2024-07-18 22:38:14 +02:00
|
|
|
* and only returns a value iff all of the checks pass:
|
|
|
|
* - Input length is uintN_t::size()*2
|
|
|
|
* - All characters are hex
|
|
|
|
*/
|
|
|
|
template <class uintN_t>
|
|
|
|
std::optional<uintN_t> FromHex(std::string_view str)
|
|
|
|
{
|
|
|
|
if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
|
|
|
|
uintN_t rv;
|
|
|
|
rv.SetHexDeprecated(str);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
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:
|
2024-07-18 22:38:14 +02:00
|
|
|
static std::optional<uint160> FromHex(std::string_view str) { return detail::FromHex<uint160>(str); }
|
2022-10-19 15:16:04 -05:00
|
|
|
constexpr uint160() = default;
|
|
|
|
constexpr explicit uint160(Span<const 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:
|
2024-07-18 22:38:14 +02:00
|
|
|
static std::optional<uint256> FromHex(std::string_view str) { return detail::FromHex<uint256>(str); }
|
2022-10-19 15:16:04 -05:00
|
|
|
constexpr uint256() = default;
|
2024-08-01 23:33:24 +02:00
|
|
|
consteval explicit uint256(std::string_view hex_str) : base_blob<256>(hex_str) {}
|
2020-09-25 15:01:38 +10:00
|
|
|
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
|
2022-10-19 15:16:04 -05:00
|
|
|
constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
|
2020-09-20 21:17:29 -07:00
|
|
|
static const uint256 ZERO;
|
2020-09-25 15:01:38 +10:00
|
|
|
static const uint256 ONE;
|
2010-07-14 15:54:31 +00:00
|
|
|
};
|
|
|
|
|
2024-08-01 23:20:07 +02:00
|
|
|
/* uint256 from std::string_view, containing byte-reversed hex encoding.
|
2024-07-18 22:38:14 +02:00
|
|
|
* DEPRECATED. Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
|
2014-12-15 10:22:19 +01:00
|
|
|
*/
|
2024-07-23 14:51:36 +02:00
|
|
|
inline uint256 uint256S(std::string_view str)
|
2014-12-15 10:22:19 +01:00
|
|
|
{
|
|
|
|
uint256 rv;
|
2024-07-18 22:24:38 +02:00
|
|
|
rv.SetHexDeprecated(str);
|
2014-12-15 10:22:19 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-12-15 10:05:51 +01:00
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UINT256_H
|