Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto # 2 : : // Copyright (c) 2009-2021 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 <assert.h> # 12 : : #include <cstring> # 13 : : #include <stdint.h> # 14 : : #include <string> # 15 : : #include <vector> # 16 : : # 17 : : /** Template base class for fixed-sized opaque blobs. */ # 18 : : template<unsigned int BITS> # 19 : : class base_blob # 20 : : { # 21 : : protected: # 22 : : static constexpr int WIDTH = BITS / 8; # 23 : : uint8_t m_data[WIDTH]; # 24 : : public: # 25 : : /* construct 0 value by default */ # 26 : 916494810 : constexpr base_blob() : m_data() {} # 27 : : # 28 : : /* constructor for constants between 1 and 255 */ # 29 : 94 : constexpr explicit base_blob(uint8_t v) : m_data{v} {} # 30 : : # 31 : : explicit base_blob(const std::vector<unsigned char>& vch); # 32 : : # 33 : : bool IsNull() const # 34 : 30963576 : { # 35 [ + + ][ + + ]: 136532629 : for (int i = 0; i < WIDTH; i++) # 36 [ + + ][ + + ]: 133224749 : if (m_data[i] != 0) # 37 : 27655696 : return false; # 38 : 3307880 : return true; # 39 : 30963576 : } # 40 : : # 41 : : void SetNull() # 42 : 2933242 : { # 43 : 2933242 : memset(m_data, 0, sizeof(m_data)); # 44 : 2933242 : } # 45 : : # 46 : 1621462358 : inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); } # 47 : : # 48 : 211926966 : friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; } # 49 : 804556 : friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; } # 50 : 936503209 : friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; } # 51 : : # 52 : : std::string GetHex() const; # 53 : : void SetHex(const char* psz); # 54 : : void SetHex(const std::string& str); # 55 : : std::string ToString() const; # 56 : : # 57 : 1820361 : const unsigned char* data() const { return m_data; } # 58 : 11263753 : unsigned char* data() { return m_data; } # 59 : : # 60 : : unsigned char* begin() # 61 : 66864835 : { # 62 : 66864835 : return &m_data[0]; # 63 : 66864835 : } # 64 : : # 65 : : unsigned char* end() # 66 : 74746 : { # 67 : 74746 : return &m_data[WIDTH]; # 68 : 74746 : } # 69 : : # 70 : : const unsigned char* begin() const # 71 : 182762316 : { # 72 : 182762316 : return &m_data[0]; # 73 : 182762316 : } # 74 : : # 75 : : const unsigned char* end() const # 76 : 2745877 : { # 77 : 2745877 : return &m_data[WIDTH]; # 78 : 2745877 : } # 79 : : # 80 : : static constexpr unsigned int size() # 81 : 19890090 : { # 82 : 19890090 : return sizeof(m_data); # 83 : 19890090 : } # 84 : : # 85 : : uint64_t GetUint64(int pos) const # 86 : 1035114659 : { # 87 : 1035114659 : const uint8_t* ptr = m_data + pos * 8; # 88 : 1035114659 : return ((uint64_t)ptr[0]) | \ # 89 : 1035114659 : ((uint64_t)ptr[1]) << 8 | \ # 90 : 1035114659 : ((uint64_t)ptr[2]) << 16 | \ # 91 : 1035114659 : ((uint64_t)ptr[3]) << 24 | \ # 92 : 1035114659 : ((uint64_t)ptr[4]) << 32 | \ # 93 : 1035114659 : ((uint64_t)ptr[5]) << 40 | \ # 94 : 1035114659 : ((uint64_t)ptr[6]) << 48 | \ # 95 : 1035114659 : ((uint64_t)ptr[7]) << 56; # 96 : 1035114659 : } # 97 : : # 98 : : template<typename Stream> # 99 : : void Serialize(Stream& s) const # 100 : 90173970 : { # 101 : 90173970 : s.write(MakeByteSpan(m_data)); # 102 : 90173970 : } # 103 : : # 104 : : template<typename Stream> # 105 : : void Unserialize(Stream& s) # 106 : 1448448 : { # 107 : 1448448 : s.read(MakeWritableByteSpan(m_data)); # 108 : 1448448 : } # 109 : : }; # 110 : : # 111 : : /** 160-bit opaque blob. # 112 : : * @note This type is called uint160 for historical reasons only. It is an opaque # 113 : : * blob of 160 bits and has no integer operations. # 114 : : */ # 115 : : class uint160 : public base_blob<160> { # 116 : : public: # 117 : 15033451 : constexpr uint160() {} # 118 : 2830242 : explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {} # 119 : : }; # 120 : : # 121 : : /** 256-bit opaque blob. # 122 : : * @note This type is called uint256 for historical reasons only. It is an # 123 : : * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if # 124 : : * those are required. # 125 : : */ # 126 : : class uint256 : public base_blob<256> { # 127 : : public: # 128 : 901428856 : constexpr uint256() {} # 129 : 94 : constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {} # 130 : 5205 : explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {} # 131 : : static const uint256 ZERO; # 132 : : static const uint256 ONE; # 133 : : }; # 134 : : # 135 : : /* uint256 from const char *. # 136 : : * This is a separate function because the constructor uint256(const char*) can result # 137 : : * in dangerously catching uint256(0). # 138 : : */ # 139 : : inline uint256 uint256S(const char *str) # 140 : 97014 : { # 141 : 97014 : uint256 rv; # 142 : 97014 : rv.SetHex(str); # 143 : 97014 : return rv; # 144 : 97014 : } # 145 : : /* uint256 from std::string. # 146 : : * This is a separate function because the constructor uint256(const std::string &str) can result # 147 : : * in dangerously catching uint256(0) via std::string(const char*). # 148 : : */ # 149 : : inline uint256 uint256S(const std::string& str) # 150 : 23172 : { # 151 : 23172 : uint256 rv; # 152 : 23172 : rv.SetHex(str); # 153 : 23172 : return rv; # 154 : 23172 : } # 155 : : # 156 : : #endif // BITCOIN_UINT256_H