Bitcoin ABC 0.30.5
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 <span.h>
10
11#include <cassert>
12#include <cstdint>
13#include <cstring>
14#include <string>
15#include <vector>
16
18template <unsigned int BITS> class base_blob {
19protected:
20 static constexpr int WIDTH = BITS / 8;
21 uint8_t m_data[WIDTH];
22
23public:
24 /* construct 0 value by default */
25 constexpr base_blob() : m_data() {}
26
27 /* constructor for constants between 1 and 255 */
28 constexpr explicit base_blob(uint8_t v) : m_data{v} {}
29
30 explicit base_blob(const std::vector<uint8_t> &vch);
31
32 bool IsNull() const {
33 for (int i = 0; i < WIDTH; i++) {
34 if (m_data[i] != 0) {
35 return false;
36 }
37 }
38 return true;
39 }
40
41 void SetNull() { memset(m_data, 0, sizeof(m_data)); }
42
43 inline int Compare(const base_blob &other) const {
44 for (size_t i = 0; i < sizeof(m_data); i++) {
45 uint8_t a = m_data[sizeof(m_data) - 1 - i];
46 uint8_t b = other.m_data[sizeof(m_data) - 1 - i];
47 if (a > b) {
48 return 1;
49 }
50 if (a < b) {
51 return -1;
52 }
53 }
54
55 return 0;
56 }
57
58 friend inline bool operator==(const base_blob &a, const base_blob &b) {
59 return a.Compare(b) == 0;
60 }
61 friend inline bool operator!=(const base_blob &a, const base_blob &b) {
62 return a.Compare(b) != 0;
63 }
64 friend inline bool operator<(const base_blob &a, const base_blob &b) {
65 return a.Compare(b) < 0;
66 }
67 friend inline bool operator<=(const base_blob &a, const base_blob &b) {
68 return a.Compare(b) <= 0;
69 }
70 friend inline bool operator>(const base_blob &a, const base_blob &b) {
71 return a.Compare(b) > 0;
72 }
73 friend inline bool operator>=(const base_blob &a, const base_blob &b) {
74 return a.Compare(b) >= 0;
75 }
76
77 std::string GetHex() const;
78 void SetHex(const char *psz);
79 void SetHex(const std::string &str);
80 std::string ToString() const { return GetHex(); }
81
82 const uint8_t *data() const { return m_data; }
83 uint8_t *data() { return m_data; }
84
85 uint8_t *begin() { return &m_data[0]; }
86
87 uint8_t *end() { return &m_data[WIDTH]; }
88
89 const uint8_t *begin() const { return &m_data[0]; }
90
91 const uint8_t *end() const { return &m_data[WIDTH]; }
92
93 unsigned int size() const { return sizeof(m_data); }
94
95 uint64_t GetUint64(int pos) const {
96 const uint8_t *ptr = m_data + pos * 8;
97 return uint64_t(ptr[0]) | (uint64_t(ptr[1]) << 8) |
98 (uint64_t(ptr[2]) << 16) | (uint64_t(ptr[3]) << 24) |
99 (uint64_t(ptr[4]) << 32) | (uint64_t(ptr[5]) << 40) |
100 (uint64_t(ptr[6]) << 48) | (uint64_t(ptr[7]) << 56);
101 }
102
103 template <typename Stream> void Serialize(Stream &s) const {
104 s.write(MakeByteSpan(m_data));
105 }
106
107 template <typename Stream> void Unserialize(Stream &s) {
109 }
110};
111
117class uint160 : public base_blob<160> {
118public:
119 constexpr uint160() {}
120 explicit uint160(const std::vector<uint8_t> &vch) : base_blob<160>(vch) {}
121};
122
129class uint256 : public base_blob<256> {
130public:
131 constexpr uint256() {}
132 constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
133 explicit uint256(const std::vector<uint8_t> &vch) : base_blob<256>(vch) {}
134 static const uint256 ZERO;
135 static const uint256 ONE;
136};
137
143inline uint256 uint256S(const char *str) {
144 uint256 rv;
145 rv.SetHex(str);
146 return rv;
147}
148
155inline uint256 uint256S(const std::string &str) {
156 uint256 rv;
157 rv.SetHex(str);
158 return rv;
159}
160
161inline uint160 uint160S(const char *str) {
162 uint160 rv;
163 rv.SetHex(str);
164 return rv;
165}
166inline uint160 uint160S(const std::string &str) {
167 uint160 rv;
168 rv.SetHex(str);
169 return rv;
170}
171
172#endif // BITCOIN_UINT256_H
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:18
unsigned int size() const
Definition: uint256.h:93
constexpr base_blob(uint8_t v)
Definition: uint256.h:28
friend bool operator<=(const base_blob &a, const base_blob &b)
Definition: uint256.h:67
const uint8_t * end() const
Definition: uint256.h:91
uint8_t * end()
Definition: uint256.h:87
static constexpr int WIDTH
Definition: uint256.h:20
uint8_t * begin()
Definition: uint256.h:85
void SetHex(const char *psz)
Definition: uint256.cpp:24
void Unserialize(Stream &s)
Definition: uint256.h:107
int Compare(const base_blob &other) const
Definition: uint256.h:43
std::string ToString() const
Definition: uint256.h:80
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:61
friend bool operator>=(const base_blob &a, const base_blob &b)
Definition: uint256.h:73
void SetNull()
Definition: uint256.h:41
uint8_t m_data[WIDTH]
Definition: uint256.h:21
bool IsNull() const
Definition: uint256.h:32
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:58
void Serialize(Stream &s) const
Definition: uint256.h:103
const uint8_t * data() const
Definition: uint256.h:82
friend bool operator>(const base_blob &a, const base_blob &b)
Definition: uint256.h:70
std::string GetHex() const
Definition: uint256.cpp:16
const uint8_t * begin() const
Definition: uint256.h:89
uint8_t * data()
Definition: uint256.h:83
uint64_t GetUint64(int pos) const
Definition: uint256.h:95
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:64
constexpr base_blob()
Definition: uint256.h:25
160-bit opaque blob.
Definition: uint256.h:117
constexpr uint160()
Definition: uint256.h:119
uint160(const std::vector< uint8_t > &vch)
Definition: uint256.h:120
256-bit opaque blob.
Definition: uint256.h:129
static const uint256 ONE
Definition: uint256.h:135
uint256(const std::vector< uint8_t > &vch)
Definition: uint256.h:133
static const uint256 ZERO
Definition: uint256.h:134
constexpr uint256(uint8_t v)
Definition: uint256.h:132
constexpr uint256()
Definition: uint256.h:131
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:301
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:304
uint160 uint160S(const char *str)
Definition: uint256.h:161
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:143