Use the override specifier (C++11) where we expect to be overriding the virtual funct...
[bitcoinplatinum.git] / src / uint256.h
bloba92ce07f11181a7b541154602100ad2ecdb3f4ef
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.
6 #ifndef BITCOIN_UINT256_H
7 #define BITCOIN_UINT256_H
9 #include <assert.h>
10 #include <cstring>
11 #include <stdexcept>
12 #include <stdint.h>
13 #include <string>
14 #include <vector>
15 #include "crypto/common.h"
17 /** Template base class for fixed-sized opaque blobs. */
18 template<unsigned int BITS>
19 class base_blob
21 protected:
22 enum { WIDTH=BITS/8 };
23 uint8_t data[WIDTH];
24 public:
25 base_blob()
27 memset(data, 0, sizeof(data));
30 explicit base_blob(const std::vector<unsigned char>& vch);
32 bool IsNull() const
34 for (int i = 0; i < WIDTH; i++)
35 if (data[i] != 0)
36 return false;
37 return true;
40 void SetNull()
42 memset(data, 0, sizeof(data));
45 inline int Compare(const base_blob& other) const { return memcmp(data, other.data, sizeof(data)); }
47 friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
48 friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
49 friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
51 std::string GetHex() const;
52 void SetHex(const char* psz);
53 void SetHex(const std::string& str);
54 std::string ToString() const;
56 unsigned char* begin()
58 return &data[0];
61 unsigned char* end()
63 return &data[WIDTH];
66 const unsigned char* begin() const
68 return &data[0];
71 const unsigned char* end() const
73 return &data[WIDTH];
76 unsigned int size() const
78 return sizeof(data);
81 uint64_t GetUint64(int pos) const
83 const uint8_t* ptr = data + pos * 8;
84 return ((uint64_t)ptr[0]) | \
85 ((uint64_t)ptr[1]) << 8 | \
86 ((uint64_t)ptr[2]) << 16 | \
87 ((uint64_t)ptr[3]) << 24 | \
88 ((uint64_t)ptr[4]) << 32 | \
89 ((uint64_t)ptr[5]) << 40 | \
90 ((uint64_t)ptr[6]) << 48 | \
91 ((uint64_t)ptr[7]) << 56;
94 template<typename Stream>
95 void Serialize(Stream& s) const
97 s.write((char*)data, sizeof(data));
100 template<typename Stream>
101 void Unserialize(Stream& s)
103 s.read((char*)data, sizeof(data));
107 /** 160-bit opaque blob.
108 * @note This type is called uint160 for historical reasons only. It is an opaque
109 * blob of 160 bits and has no integer operations.
111 class uint160 : public base_blob<160> {
112 public:
113 uint160() {}
114 uint160(const base_blob<160>& b) : base_blob<160>(b) {}
115 explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
118 /** 256-bit opaque blob.
119 * @note This type is called uint256 for historical reasons only. It is an
120 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
121 * those are required.
123 class uint256 : public base_blob<256> {
124 public:
125 uint256() {}
126 uint256(const base_blob<256>& b) : base_blob<256>(b) {}
127 explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
129 /** A cheap hash function that just returns 64 bits from the result, it can be
130 * used when the contents are considered uniformly random. It is not appropriate
131 * when the value can easily be influenced from outside as e.g. a network adversary could
132 * provide values to trigger worst-case behavior.
134 uint64_t GetCheapHash() const
136 return ReadLE64(data);
140 /* uint256 from const char *.
141 * This is a separate function because the constructor uint256(const char*) can result
142 * in dangerously catching uint256(0).
144 inline uint256 uint256S(const char *str)
146 uint256 rv;
147 rv.SetHex(str);
148 return rv;
150 /* uint256 from std::string.
151 * This is a separate function because the constructor uint256(const std::string &str) can result
152 * in dangerously catching uint256(0) via std::string(const char*).
154 inline uint256 uint256S(const std::string& str)
156 uint256 rv;
157 rv.SetHex(str);
158 return rv;
161 #endif // BITCOIN_UINT256_H