Bitcoin Core 28.99.0
P2P Digital Currency
uint256_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2021 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <streams.h>
7#include <uint256.h>
8#include <util/strencodings.h>
10
11#include <boost/test/unit_test.hpp>
12
13#include <iomanip>
14#include <sstream>
15#include <string>
16#include <string_view>
17#include <vector>
18
19BOOST_AUTO_TEST_SUITE(uint256_tests)
20
21const unsigned char R1Array[] =
22 "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
23 "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
24const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
25const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32));
26const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20));
27
28const unsigned char R2Array[] =
29 "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
30 "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
31const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32));
32const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20));
33
34const unsigned char ZeroArray[] =
35 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
36 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
37const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
38const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
39
40const unsigned char OneArray[] =
41 "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
42 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
43const uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32));
44const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20));
45
46const unsigned char MaxArray[] =
47 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
48 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
49const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
50const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
51
52static std::string ArrayToString(const unsigned char A[], unsigned int width)
53{
54 std::stringstream Stream;
55 Stream << std::hex;
56 for (unsigned int i = 0; i < width; ++i)
57 {
58 Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
59 }
60 return Stream.str();
61}
62
63BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
64{
65 // constructor uint256(vector<char>):
76 BOOST_CHECK_NE(OneL.ToString(), ArrayToString(ZeroArray,32));
77 BOOST_CHECK_NE(OneS.ToString(), ArrayToString(ZeroArray,20));
78
79 // == and !=
80 BOOST_CHECK_NE(R1L, R2L); BOOST_CHECK_NE(R1S, R2S);
81 BOOST_CHECK_NE(ZeroL, OneL); BOOST_CHECK_NE(ZeroS, OneS);
82 BOOST_CHECK_NE(OneL, ZeroL); BOOST_CHECK_NE(OneS, ZeroS);
83 BOOST_CHECK_NE(MaxL, ZeroL); BOOST_CHECK_NE(MaxS, ZeroS);
84
85 // String Constructor and Copy Constructor
95
101 BOOST_CHECK_EQUAL(uint160::FromHex(std::string_view{R1ArrayHex + 24, 40}).value(), R1S);
102
106}
107
108BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
109{
110 uint256 LastL;
111 for (int i = 255; i >= 0; --i) {
112 uint256 TmpL;
113 *(TmpL.begin() + (i>>3)) |= 1<<(7-(i&7));
114 BOOST_CHECK_LT(LastL, TmpL);
115 LastL = TmpL;
116 }
117
118 BOOST_CHECK_LT(ZeroL, R1L);
119 BOOST_CHECK_LT(R2L, R1L);
120 BOOST_CHECK_LT(ZeroL, OneL);
121 BOOST_CHECK_LT(OneL, MaxL);
122 BOOST_CHECK_LT(R1L, MaxL);
123 BOOST_CHECK_LT(R2L, MaxL);
124
125 uint160 LastS;
126 for (int i = 159; i >= 0; --i) {
127 uint160 TmpS;
128 *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7));
129 BOOST_CHECK_LT(LastS, TmpS);
130 LastS = TmpS;
131 }
132 BOOST_CHECK_LT(ZeroS, R1S);
133 BOOST_CHECK_LT(R2S, R1S);
134 BOOST_CHECK_LT(ZeroS, OneS);
135 BOOST_CHECK_LT(OneS, MaxS);
136 BOOST_CHECK_LT(R1S, MaxS);
137 BOOST_CHECK_LT(R2S, MaxS);
138
139 // Non-arithmetic uint256s compare from the beginning of their inner arrays:
140 BOOST_CHECK_LT(R2L, R1L);
141 // Ensure first element comparisons give the same order as above:
142 BOOST_CHECK_LT(*R2L.begin(), *R1L.begin());
143 // Ensure last element comparisons give a different result (swapped params):
144 BOOST_CHECK_LT(*(R1L.end()-1), *(R2L.end()-1));
145 // Hex strings represent reverse-encoded bytes, with lexicographic ordering:
146 BOOST_CHECK_LT(uint256{"1000000000000000000000000000000000000000000000000000000000000000"},
147 uint256{"0000000000000000000000000000000000000000000000000000000000000001"});
148}
149
150BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
151{
156 uint256 TmpL(R1L);
157 BOOST_CHECK_EQUAL(TmpL, R1L);
158 // Verify previous values don't persist when setting to truncated string.
159 TmpL.SetHexDeprecated("21");
160 BOOST_CHECK_EQUAL(TmpL.ToString(), "0000000000000000000000000000000000000000000000000000000000000021");
163
164 TmpL = uint256::FromHex(R1L.ToString()).value();
165 BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array + uint256::size());
166 BOOST_CHECK_EQUAL_COLLECTIONS(TmpL.begin(), TmpL.end(), R1Array, R1Array + uint256::size());
167 BOOST_CHECK_EQUAL_COLLECTIONS(R2L.begin(), R2L.end(), R2Array, R2Array + uint256::size());
168 BOOST_CHECK_EQUAL_COLLECTIONS(ZeroL.begin(), ZeroL.end(), ZeroArray, ZeroArray + uint256::size());
169 BOOST_CHECK_EQUAL_COLLECTIONS(OneL.begin(), OneL.end(), OneArray, OneArray + uint256::size());
170 BOOST_CHECK_EQUAL(R1L.size(), sizeof(R1L));
171 BOOST_CHECK_EQUAL(sizeof(R1L), 32);
176 BOOST_CHECK_EQUAL(R1L.begin() + 32, R1L.end());
177 BOOST_CHECK_EQUAL(R2L.begin() + 32, R2L.end());
180 BOOST_CHECK_EQUAL(TmpL.begin() + 32, TmpL.end());
183
184 DataStream ss{};
185 ss << R1L;
186 BOOST_CHECK_EQUAL(ss.str(), std::string(R1Array,R1Array+32));
187 ss >> TmpL;
188 BOOST_CHECK_EQUAL(R1L, TmpL);
189 ss.clear();
190 ss << ZeroL;
191 BOOST_CHECK_EQUAL(ss.str(), std::string(ZeroArray,ZeroArray+32));
192 ss >> TmpL;
194 ss.clear();
195 ss << MaxL;
196 BOOST_CHECK_EQUAL(ss.str(), std::string(MaxArray,MaxArray+32));
197 ss >> TmpL;
198 BOOST_CHECK_EQUAL(MaxL, TmpL);
199 ss.clear();
200
205 uint160 TmpS(R1S);
206 BOOST_CHECK_EQUAL(TmpS, R1S);
209
210 TmpS = uint160::FromHex(R1S.ToString()).value();
211 BOOST_CHECK_EQUAL_COLLECTIONS(R1S.begin(), R1S.end(), R1Array, R1Array + uint160::size());
212 BOOST_CHECK_EQUAL_COLLECTIONS(TmpS.begin(), TmpS.end(), R1Array, R1Array + uint160::size());
213 BOOST_CHECK_EQUAL_COLLECTIONS(R2S.begin(), R2S.end(), R2Array, R2Array + uint160::size());
214 BOOST_CHECK_EQUAL_COLLECTIONS(ZeroS.begin(), ZeroS.end(), ZeroArray, ZeroArray + uint160::size());
215 BOOST_CHECK_EQUAL_COLLECTIONS(OneS.begin(), OneS.end(), OneArray, OneArray + uint160::size());
216 BOOST_CHECK_EQUAL(R1S.size(), sizeof(R1S));
217 BOOST_CHECK_EQUAL(sizeof(R1S), 20);
222 BOOST_CHECK_EQUAL(R1S.begin() + 20, R1S.end());
223 BOOST_CHECK_EQUAL(R2S.begin() + 20, R2S.end());
226 BOOST_CHECK_EQUAL(TmpS.begin() + 20, TmpS.end());
229
230 ss << R1S;
231 BOOST_CHECK_EQUAL(ss.str(), std::string(R1Array,R1Array+20));
232 ss >> TmpS;
233 BOOST_CHECK_EQUAL(R1S, TmpS);
234 ss.clear();
235 ss << ZeroS;
236 BOOST_CHECK_EQUAL(ss.str(), std::string(ZeroArray,ZeroArray+20));
237 ss >> TmpS;
239 ss.clear();
240 ss << MaxS;
241 BOOST_CHECK_EQUAL(ss.str(), std::string(MaxArray,MaxArray+20));
242 ss >> TmpS;
243 BOOST_CHECK_EQUAL(MaxS, TmpS);
244 ss.clear();
245}
246
251template <typename T>
253{
254 constexpr unsigned int num_chars{T::size() * 2};
255 static_assert(num_chars <= 64); // this test needs to be modified to allow for more than 64 hex chars
256 const std::string valid_64char_input{"0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF"};
257 const auto valid_input{valid_64char_input.substr(0, num_chars)};
258 {
259 // check that lower and upper case hex characters are accepted
260 auto valid_result{T::FromHex(valid_input)};
261 BOOST_REQUIRE(valid_result);
262 BOOST_CHECK_EQUAL(valid_result->ToString(), ToLower(valid_input));
263 }
264 {
265 // check that only strings of size num_chars are accepted
267 BOOST_CHECK(!T::FromHex("0"));
268 BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars / 2)));
269 BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1)));
270 BOOST_CHECK(!T::FromHex(valid_input + "0"));
271 }
272 {
273 // check that non-hex characters are not accepted
274 std::string invalid_chars{R"( !"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`ghijklmnopqrstuvwxyz{|}~)"};
275 for (auto c : invalid_chars) {
276 BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1) + c));
277 }
278 // 0x prefixes are invalid
279 std::string invalid_prefix{"0x" + valid_input};
280 BOOST_CHECK(!T::FromHex(std::string_view(invalid_prefix.data(), num_chars)));
281 BOOST_CHECK(!T::FromHex(invalid_prefix));
282 }
283 {
284 // check that string_view length is respected
285 std::string chars_68{valid_64char_input + "0123"};
286 BOOST_CHECK_EQUAL(T::FromHex(std::string_view(chars_68.data(), num_chars)).value().ToString(), ToLower(valid_input));
287 BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars - 1))); // too short
288 BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars + 1))); // too long
289 }
290}
291
293{
294 TestFromHex<uint160>();
295 TestFromHex<uint256>();
296 TestFromHex<Txid>();
297 TestFromHex<Wtxid>();
298}
299
301{
311 const std::string valid_hex_64{"0x0123456789abcdef0123456789abcdef0123456789ABDCEF0123456789ABCDEF"};
312 BOOST_REQUIRE_EQUAL(valid_hex_64.size(), 2 + 64); // 0x prefix and 64 hex digits
313 BOOST_CHECK_EQUAL(uint256::FromUserHex(valid_hex_64.substr(2)).value().ToString(), ToLower(valid_hex_64.substr(2)));
314 BOOST_CHECK_EQUAL(uint256::FromUserHex(valid_hex_64.substr(0)).value().ToString(), ToLower(valid_hex_64.substr(2)));
315
316 BOOST_CHECK(!uint256::FromUserHex("0x0 ")); // no spaces at end,
317 BOOST_CHECK(!uint256::FromUserHex(" 0x0")); // or beginning,
318 BOOST_CHECK(!uint256::FromUserHex("0x 0")); // or middle,
319 BOOST_CHECK(!uint256::FromUserHex(" ")); // etc.
320 BOOST_CHECK(!uint256::FromUserHex("0x0ga")); // invalid character
321 BOOST_CHECK(!uint256::FromUserHex("x0")); // broken prefix
322 BOOST_CHECK(!uint256::FromUserHex("0x0x00")); // two prefixes not allowed
323 BOOST_CHECK(!uint256::FromUserHex(valid_hex_64.substr(2) + "0")); // 1 hex digit too many
324 BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + "a")); // 1 hex digit too many
325 BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + " ")); // whitespace after max length
326 BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + "z")); // invalid character after max length
327}
328
330{
331 uint256 one = uint256{"0000000000000000000000000000000000000000000000000000000000000001"};
333}
334
335BOOST_AUTO_TEST_CASE(FromHex_vs_uint256)
336{
337 auto runtime_uint{uint256::FromHex("4A5E1E4BAAB89F3A32518A88C31BC87F618f76673e2cc77ab2127b7afdeda33b")};
338 constexpr uint256 consteval_uint{ "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"};
339 BOOST_CHECK_EQUAL(consteval_uint, runtime_uint);
340}
341
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
constexpr unsigned char * end()
Definition: uint256.h:105
static constexpr unsigned int size()
Definition: uint256.h:110
std::string ToString() const
Definition: uint256.cpp:47
void SetHexDeprecated(std::string_view str)
Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
Definition: uint256.cpp:21
constexpr unsigned char * begin()
Definition: uint256.h:104
std::string GetHex() const
Definition: uint256.cpp:11
160-bit opaque blob.
Definition: uint256.h:178
static std::optional< uint160 > FromHex(std::string_view str)
Definition: uint256.h:180
256-bit opaque blob.
Definition: uint256.h:190
static std::optional< uint256 > FromUserHex(std::string_view str)
Definition: uint256.h:193
static const uint256 ONE
Definition: uint256.h:199
static const uint256 ZERO
Definition: uint256.h:198
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:192
BOOST_AUTO_TEST_SUITE_END()
std::optional< uintN_t > FromHex(std::string_view str)
Writes the hex string (in reverse byte order) into a new uintN_t object and only returns a value iff ...
Definition: uint256.h:146
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
size_t GetSerializeSize(const T &t)
Definition: serialize.h:1101
static void from_hex(unsigned char *data, int len, const char *hex)
Definition: test.c:41
const unsigned char ZeroArray[]
const unsigned char R1Array[]
BOOST_AUTO_TEST_CASE(basics)
const unsigned char OneArray[]
const uint160 OneS
const uint160 MaxS
const char R1ArrayHex[]
const uint160 ZeroS
const uint256 MaxL
const uint160 R1S
const unsigned char R2Array[]
const uint256 R2L
const uint160 R2S
const uint256 R1L
static std::string ArrayToString(const unsigned char A[], unsigned int width)
const unsigned char MaxArray[]
const uint256 ZeroL
const uint256 OneL
void TestFromHex()
Implemented as a templated function so it can be reused by other classes that have a FromHex() method...
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.