Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_UINT256_H
7 : : #define BITCOIN_UINT256_H
8 : :
9 : : #include <crypto/common.h>
10 : : #include <span.h>
11 : : #include <util/strencodings.h>
12 : : #include <util/string.h>
13 : :
14 : : #include <algorithm>
15 : : #include <array>
16 : : #include <cassert>
17 : : #include <cstdint>
18 : : #include <cstring>
19 : : #include <optional>
20 : : #include <string>
21 : : #include <string_view>
22 : :
23 : : /** Template base class for fixed-sized opaque blobs. */
24 : : template<unsigned int BITS>
25 : : class base_blob
26 : : {
27 : : protected:
28 : : static constexpr int WIDTH = BITS / 8;
29 : : static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
30 : : std::array<uint8_t, WIDTH> m_data;
31 : : static_assert(WIDTH == sizeof(m_data), "Sanity check");
32 : :
33 : : public:
34 : : /* construct 0 value by default */
35 : 1848434658 : constexpr base_blob() : m_data() {}
36 : :
37 : : /* constructor for constants between 1 and 255 */
38 : 4992 : constexpr explicit base_blob(uint8_t v) : m_data{v} {}
39 : :
40 [ - + ]: 6082003 : constexpr explicit base_blob(std::span<const unsigned char> vch)
41 : : {
42 [ - + ]: 6082003 : assert(vch.size() == WIDTH);
43 : 6082003 : std::copy(vch.begin(), vch.end(), m_data.begin());
44 : 6082003 : }
45 : :
46 : : consteval explicit base_blob(std::string_view hex_str);
47 : :
48 : 25598289 : constexpr bool IsNull() const
49 : : {
50 [ + + ][ + - : 50816834 : return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
+ + + + ]
[ - - - +
+ - - - ]
[ - - - -
- - - - -
- - + - +
+ + + + +
+ + + + +
- - - - ]
[ + + - + ]
[ + + + +
+ + + + +
+ ][ + + +
+ + + + -
- + - - +
- + - # #
# # # # ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ][ +
- + - - -
- - - - -
- ]
51 : : return val == 0;
52 : : });
53 : : }
54 : :
55 : 15189540 : constexpr void SetNull()
56 : : {
57 : 15189540 : std::fill(m_data.begin(), m_data.end(), 0);
58 : 15189540 : }
59 : :
60 : : /** Lexicographic ordering
61 : : * @note Does NOT match the ordering on the corresponding \ref
62 : : * base_uint::CompareTo, which starts comparing from the end.
63 : : */
64 [ + + + + ]: 1667206587 : constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
[ - + - +
- - - - -
- - - - -
- - - - +
+ - - - -
- - - - -
- - - - -
- - + + -
- - - - -
- - - - -
- - - + -
+ + - - -
+ + + + +
- - - - -
- ][ - - -
+ - - - -
+ + - - +
- + - + -
- - - - +
+ + - - -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - +
- - - - +
- + - + -
+ - - - -
- + + + +
+ + + + +
+ + + ][ -
- - - - -
- - - - -
+ + + # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# ][ - - -
- - - - -
- - - - -
- - - - -
+ + - - ]
[ + + + +
+ - + + +
+ - - ][ #
# # # # #
# # ][ - -
- - - - -
- - - - -
+ + - - -
- - - - -
- - - - -
- - - - -
- - ][ + +
# # # # #
# # # # #
# # # # #
# # # ][ +
- - + + -
+ - + - -
- - - + +
+ - - + +
- + - + -
- - - - -
+ + - + -
+ - - - -
- - + - -
- - - - -
- + + + +
+ + + + +
+ + + + +
- - - - -
- + + + +
- - - - +
- + - + -
- - - - -
- - - - -
- - - - +
+ - + - -
+ + + + -
- + - - -
+ - + - +
+ - - -
- ][ + - +
+ + + + +
+ + + + +
+ + + + +
+ + - + +
+ + + + +
- - + + #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - +
+ + + + +
+ + + - +
+ + + + +
+ + + + +
+ + - + +
+ + + + +
+ - - - -
- - - - +
+ ][ + + +
+ + + # #
# # ][ + +
+ + - - -
- - + ][ +
+ + + + -
+ + + + +
- - + + -
- - - - -
- - - - -
+ + + + +
+ + + - -
- - - - -
- - - - -
- - - - -
+ - - - +
+ + + + +
+ + + - +
+ - + + -
+ + + + +
+ + - - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + - -
+ - - - -
- - - - -
- - - + +
+ + - - -
- - - - -
- - - - -
- - - + +
- - + - -
+ + - - -
- - + - -
+ ]
65 : :
66 [ + + ][ + + : 20456162 : friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
+ + + + +
+ + + + +
+ + + + ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ - + + +
- + - + -
+ # # # #
# # ][ - +
- + + + +
+ + + ][ +
+ + + # #
# # # # #
# ][ + + +
+ + + +
+ ][ - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ + + - +
+ - - - -
+ - - - +
- + + - +
+ - + - -
- + - + +
+ - + - -
- - ][ + +
- + - + #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- + + + +
+ + + + +
+ - - - -
- + ][ - +
- + - + -
+ - + - +
- + - + -
+ - + ]
67 [ - + - - : 209301797 : friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
- - + + +
- + - + -
- - - - +
+ - - - -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + + +
+ + + + +
- - + + +
+ - - # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # ][ + +
+ + + + -
- + + + +
+ + + + -
+ + + + -
+ - - - -
- + + - +
+ + + - +
- - - - -
- + + + +
- + - - -
- - - + +
+ + - + -
- - - - +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + - + -
+ - + - +
+ + + ]
[ # # # # ]
[ + + - +
- + + - +
- + - - -
- - + + +
+ - - - -
+ + + + +
+ - + - +
+ - ][ - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- ][ - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - -
- - - + +
- - - - +
+ - + + -
- - - - -
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- - - - -
- - - - -
- - + + -
- - - - +
- + - - +
+ + - - +
+ + + + -
- + + + +
- - - - -
- - - ][ -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + +
+ + - - ]
[ + + + -
+ - + - -
- - - + +
+ + + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - - -
+ + + + +
+ + + + +
+ + + + +
+ - - - +
- - - - +
+ + + - -
- - - - -
- - - - -
- - - + +
- + - + -
- - - - +
+ - - + -
+ - + - -
- - - - -
- - + - +
- + - - -
- - - - -
- - + - +
- - - - -
- - - - -
- - - - -
- - - - -
+ + - + +
+ - - - -
+ + + + +
+ + + + +
- + ][ - -
- - - - -
- - - - -
+ + + + +
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
+ - - - -
- - - - +
+ + + + +
+ + - - -
- - - - -
+ - - - -
- - - ][ -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
- + + + +
- - - - -
- - - -
- ][ - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + + + +
- - - - -
- - - - -
+ + - - -
- - - - -
- - + + -
- - - - -
- - - - -
- - - + +
+ - + - +
- - - - -
+ + + + -
- - - - -
- - - - +
+ + - - -
- - + + +
+ - - - -
- + + - -
- - - ]
68 : :
69 : : /** @name Hex representation
70 : : *
71 : : * The hex representation used by GetHex(), ToString(), and FromHex()
72 : : * is unusual, since it shows bytes of the base_blob in reverse order.
73 : : * For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is represented
74 : : * as "78563412" instead of the more typical "12345678" representation
75 : : * that would be shown in a hex editor or used by typical
76 : : * byte-array / hex conversion functions like python's bytes.hex() and
77 : : * bytes.fromhex().
78 : : *
79 : : * The nice thing about the reverse-byte representation, even though it is
80 : : * unusual, is that if a blob contains an arithmetic number in little endian
81 : : * format (with least significant bytes first, and most significant bytes
82 : : * last), the GetHex() output will match the way the number would normally
83 : : * be written in base-16 (with most significant digits first and least
84 : : * significant digits last).
85 : : *
86 : : * This means, for example, that ArithToUint256(num).GetHex() can be used to
87 : : * display an arith_uint256 num value as a number, because
88 : : * ArithToUint256() converts the number to a blob in little-endian format,
89 : : * so the arith_uint256 class doesn't need to have its own number parsing
90 : : * and formatting functions.
91 : : *
92 : : * @{*/
93 : : std::string GetHex() const;
94 : : std::string ToString() const;
95 : : /**@}*/
96 : :
97 [ + - + - : 36011651 : constexpr const unsigned char* data() const { return m_data.data(); }
+ - + - +
- + - + -
+ - + - ]
[ - - - -
- - - - +
- # # # #
# # # # ]
[ - - - -
- - - - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ - - -
- - - +
- ]
[ + - + - ]
[ + - ]
98 [ + - + - ]: 76657942 : constexpr unsigned char* data() { return m_data.data(); }
[ + - ][ - -
- - + - ]
99 : :
100 [ + - + + : 305125623 : constexpr unsigned char* begin() { return m_data.data(); }
+ - ][ + + ]
[ + + + + ]
[ + - + +
+ - + - ]
101 : 17079 : constexpr unsigned char* end() { return m_data.data() + WIDTH; }
102 : :
103 [ + + ][ + - : 27941726 : constexpr const unsigned char* begin() const { return m_data.data(); }
+ - + - +
- + - ]
104 [ + - + - : 7570849 : constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
+ - + - +
- ]
105 : :
106 : : static constexpr unsigned int size() { return WIDTH; }
107 : :
108 [ - + ]: 664487835 : constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
109 : :
110 : : template<typename Stream>
111 : 11301760278 : void Serialize(Stream& s) const
112 : : {
113 : 11301760278 : s << std::span(m_data);
114 : : }
115 : :
116 : : template<typename Stream>
117 : 20758740 : void Unserialize(Stream& s)
118 : : {
119 : 20758740 : s.read(MakeWritableByteSpan(m_data));
120 : 20559624 : }
121 : : };
122 : :
123 : : template <unsigned int BITS>
124 : : consteval base_blob<BITS>::base_blob(std::string_view hex_str)
125 : : {
126 : : if (hex_str.length() != m_data.size() * 2) throw "Hex string must fit exactly";
127 : : auto str_it = hex_str.rbegin();
128 : : for (auto& elem : m_data) {
129 : : auto lo = util::ConstevalHexDigit(*(str_it++));
130 : : elem = (util::ConstevalHexDigit(*(str_it++)) << 4) | lo;
131 : : }
132 : : }
133 : :
134 : : namespace detail {
135 : : /**
136 : : * Writes the hex string (in reverse byte order) into a new uintN_t object
137 : : * and only returns a value iff all of the checks pass:
138 : : * - Input length is uintN_t::size()*2
139 : : * - All characters are hex
140 : : */
141 : : template <class uintN_t>
142 [ + + ]: 7970 : std::optional<uintN_t> FromHex(std::string_view str)
143 : : {
144 [ + + + + ]: 7970 : if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
145 : 666 : uintN_t rv;
146 : 666 : unsigned char* p1 = rv.begin();
147 : 666 : unsigned char* pend = rv.end();
148 : 666 : size_t digits = str.size();
149 [ + + ]: 22644 : while (digits > 0 && p1 < pend) {
150 : 21312 : *p1 = ::HexDigit(str[--digits]);
151 [ - + ]: 21312 : if (digits > 0) {
152 : 21312 : *p1 |= ((unsigned char)::HexDigit(str[--digits]) << 4);
153 : 21312 : p1++;
154 : : }
155 : : }
156 : 666 : return rv;
157 : : }
158 : : /**
159 : : * @brief Like FromHex(std::string_view str), but allows an "0x" prefix
160 : : * and pads the input with leading zeroes if it is shorter than
161 : : * the expected length of uintN_t::size()*2.
162 : : *
163 : : * Designed to be used when dealing with user input.
164 : : */
165 : : template <class uintN_t>
166 : 400 : std::optional<uintN_t> FromUserHex(std::string_view input)
167 : : {
168 : 400 : input = util::RemovePrefixView(input, "0x");
169 [ + + ]: 400 : constexpr auto expected_size{uintN_t::size() * 2};
170 [ + + ]: 400 : if (input.size() < expected_size) {
171 : 71 : auto padded = std::string(expected_size, '0');
172 [ - + ]: 71 : std::copy(input.begin(), input.end(), padded.begin() + expected_size - input.size());
173 [ + - ]: 71 : return FromHex<uintN_t>(padded);
174 : 71 : }
175 : 329 : return FromHex<uintN_t>(input);
176 : : }
177 : : } // namespace detail
178 : :
179 : : /** 160-bit opaque blob.
180 : : * @note This type is called uint160 for historical reasons only. It is an opaque
181 : : * blob of 160 bits and has no integer operations.
182 : : */
183 : : class uint160 : public base_blob<160> {
184 : : public:
185 : : static std::optional<uint160> FromHex(std::string_view str) { return detail::FromHex<uint160>(str); }
186 [ + + ][ - - : 9422219 : constexpr uint160() = default;
- - - -
- ]
187 : 940121 : constexpr explicit uint160(std::span<const unsigned char> vch) : base_blob<160>(vch) {}
188 : : };
189 : :
190 : : /** 256-bit opaque blob.
191 : : * @note This type is called uint256 for historical reasons only. It is an
192 : : * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
193 : : * those are required.
194 : : */
195 : : class uint256 : public base_blob<256> {
196 : : public:
197 [ # # # # : 7570 : static std::optional<uint256> FromHex(std::string_view str) { return detail::FromHex<uint256>(str); }
# # # # #
# # # ]
[ + - ]
[ + - + - ]
198 [ + - + - ]: 400 : static std::optional<uint256> FromUserHex(std::string_view str) { return detail::FromUserHex<uint256>(str); }
199 [ + + + - : 1806756863 : constexpr uint256() = default;
+ + ][ + + ]
[ + - + + ]
[ + - + -
+ - + - -
+ + - + +
+ - + - +
+ + - ]
200 : : consteval explicit uint256(std::string_view hex_str) : base_blob<256>(hex_str) {}
201 [ + - + - ]: 4992 : constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
202 : 5113503 : constexpr explicit uint256(std::span<const unsigned char> vch) : base_blob<256>(vch) {}
203 : : static const uint256 ZERO;
204 : : static const uint256 ONE;
205 : : };
206 : :
207 : : #endif // BITCOIN_UINT256_H
|