Bitcoin ABC 0.33.12
P2P Digital Currency
uint256.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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
12#include <algorithm>
13#include <array>
14#include <cassert>
15#include <cstdint>
16#include <cstring>
17#include <string>
18
20template <unsigned int BITS> class base_blob {
21protected:
22 static constexpr int WIDTH = BITS / 8;
23 static_assert(BITS % 8 == 0,
24 "base_blob currently only supports whole bytes.");
25 std::array<uint8_t, WIDTH> m_data;
26 static_assert(WIDTH == sizeof(m_data), "Sanity check");
27
28public:
29 /* construct 0 value by default */
30 constexpr base_blob() : m_data() {}
31
32 /* constructor for constants between 1 and 255 */
33 constexpr explicit base_blob(uint8_t v) : m_data{{v}} {}
34
35 constexpr explicit base_blob(Span<const uint8_t> vch) {
36 assert(vch.size() == WIDTH);
37 std::copy(vch.begin(), vch.end(), m_data.begin());
38 }
39
40 constexpr bool IsNull() const {
41 return std::all_of(m_data.begin(), m_data.end(),
42 [](uint8_t val) { return val == 0; });
43 }
44
45 constexpr void SetNull() { std::fill(m_data.begin(), m_data.end(), 0); }
46
47 constexpr int Compare(const base_blob &other) const {
48 for (size_t i = 0; i < WIDTH; i++) {
49 uint8_t a = m_data[WIDTH - 1 - i];
50 uint8_t b = other.m_data[WIDTH - 1 - i];
51 if (a > b) {
52 return 1;
53 }
54 if (a < b) {
55 return -1;
56 }
57 }
58
59 return 0;
60 }
61
62 friend constexpr bool operator==(const base_blob &a, const base_blob &b) {
63 return a.Compare(b) == 0;
64 }
65 friend constexpr bool operator!=(const base_blob &a, const base_blob &b) {
66 return a.Compare(b) != 0;
67 }
68 friend constexpr bool operator<(const base_blob &a, const base_blob &b) {
69 return a.Compare(b) < 0;
70 }
71 friend constexpr bool operator<=(const base_blob &a, const base_blob &b) {
72 return a.Compare(b) <= 0;
73 }
74 friend constexpr bool operator>(const base_blob &a, const base_blob &b) {
75 return a.Compare(b) > 0;
76 }
77 friend constexpr bool operator>=(const base_blob &a, const base_blob &b) {
78 return a.Compare(b) >= 0;
79 }
80
81 std::string GetHex() const;
82 void SetHex(const char *psz);
83 void SetHex(const std::string &str);
84 std::string ToString() const { return GetHex(); }
85
86 constexpr const uint8_t *data() const { return m_data.data(); }
87 constexpr uint8_t *data() { return m_data.data(); }
88
89 constexpr uint8_t *begin() { return m_data.data(); }
90 constexpr uint8_t *end() { return m_data.data() + WIDTH; }
91
92 constexpr const uint8_t *begin() const { return m_data.data(); }
93 constexpr const uint8_t *end() const { return m_data.data() + WIDTH; }
94
95 static constexpr unsigned int size() { return WIDTH; }
96
97 constexpr uint64_t GetUint64(int pos) const {
98 return ReadLE64(m_data.data() + pos * 8);
99 }
100
101 template <typename Stream> void Serialize(Stream &s) const {
102 s << Span(m_data);
103 }
104
105 template <typename Stream> void Unserialize(Stream &s) {
106 s.read(MakeWritableByteSpan(m_data));
107 }
108};
109
115class uint160 : public base_blob<160> {
116public:
117 constexpr uint160() = default;
118 constexpr explicit uint160(Span<const uint8_t> vch) : base_blob<160>(vch) {}
119};
120
127class uint256 : public base_blob<256> {
128public:
129 constexpr uint256() = default;
130 constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
131 constexpr explicit uint256(Span<const uint8_t> vch) : base_blob<256>(vch) {}
132 static const uint256 ZERO;
133 static const uint256 ONE;
134};
135
141inline uint256 uint256S(const char *str) {
142 uint256 rv;
143 rv.SetHex(str);
144 return rv;
145}
146
153inline uint256 uint256S(const std::string &str) {
154 uint256 rv;
155 rv.SetHex(str);
156 return rv;
157}
158
159inline uint160 uint160S(const char *str) {
160 uint160 rv;
161 rv.SetHex(str);
162 return rv;
163}
164inline uint160 uint160S(const std::string &str) {
165 uint160 rv;
166 rv.SetHex(str);
167 return rv;
168}
169
170#endif // BITCOIN_UINT256_H
constexpr std::size_t size() const noexcept
Definition: span.h:210
constexpr C * begin() const noexcept
Definition: span.h:200
constexpr C * end() const noexcept
Definition: span.h:201
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:20
constexpr base_blob(uint8_t v)
Definition: uint256.h:33
constexpr bool IsNull() const
Definition: uint256.h:40
constexpr const uint8_t * data() const
Definition: uint256.h:86
friend constexpr bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:65
static constexpr int WIDTH
Definition: uint256.h:22
constexpr base_blob(Span< const uint8_t > vch)
Definition: uint256.h:35
static constexpr unsigned int size()
Definition: uint256.h:95
friend constexpr bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:68
void SetHex(const char *psz)
Definition: uint256.cpp:18
friend constexpr bool operator>(const base_blob &a, const base_blob &b)
Definition: uint256.h:74
constexpr uint64_t GetUint64(int pos) const
Definition: uint256.h:97
void Unserialize(Stream &s)
Definition: uint256.h:105
constexpr uint8_t * begin()
Definition: uint256.h:89
constexpr uint8_t * data()
Definition: uint256.h:87
friend constexpr bool operator<=(const base_blob &a, const base_blob &b)
Definition: uint256.h:71
std::string ToString() const
Definition: uint256.h:84
friend constexpr bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:62
constexpr const uint8_t * begin() const
Definition: uint256.h:92
constexpr uint8_t * end()
Definition: uint256.h:90
friend constexpr bool operator>=(const base_blob &a, const base_blob &b)
Definition: uint256.h:77
constexpr int Compare(const base_blob &other) const
Definition: uint256.h:47
constexpr const uint8_t * end() const
Definition: uint256.h:93
void Serialize(Stream &s) const
Definition: uint256.h:101
constexpr void SetNull()
Definition: uint256.h:45
std::array< uint8_t, WIDTH > m_data
Definition: uint256.h:25
constexpr base_blob()
Definition: uint256.h:30
160-bit opaque blob.
Definition: uint256.h:115
constexpr uint160(Span< const uint8_t > vch)
Definition: uint256.h:118
constexpr uint160()=default
256-bit opaque blob.
Definition: uint256.h:127
constexpr uint256()=default
constexpr uint256(Span< const uint8_t > vch)
Definition: uint256.h:131
static const uint256 ONE
Definition: uint256.h:133
static const uint256 ZERO
Definition: uint256.h:132
constexpr uint256(uint8_t v)
Definition: uint256.h:130
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:25
Span(T *, EndOrSize) -> Span< T >
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:305
uint160 uint160S(const char *str)
Definition: uint256.h:159
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:141
assert(!tx.IsCoinBase())