]> Git Repo - VerusCoin.git/blame - src/uint256.h
Auto merge of #1260 - str4d:1175-byte-array-manipulation, r=ebfull
[VerusCoin.git] / src / uint256.h
CommitLineData
8bd66202 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2014 The Bitcoin Core developers
fa94b9d5 3// Distributed under the MIT software license, see the accompanying
3a25a2b9 4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
51ed9ec9 5
223b6f1b
WL
6#ifndef BITCOIN_UINT256_H
7#define BITCOIN_UINT256_H
8
4d480c8a 9#include <assert.h>
611116d4 10#include <cstring>
4d480c8a 11#include <stdexcept>
51ed9ec9 12#include <stdint.h>
8bd66202 13#include <string>
223b6f1b
WL
14#include <vector>
15
bfc60703 16/** Template base class for fixed-sized opaque blobs. */
8bd66202 17template<unsigned int BITS>
bfc60703 18class base_blob
8bd66202 19{
bc42503f 20protected:
bfc60703 21 enum { WIDTH=BITS/8 };
c4cbee43 22 alignas(uint32_t) uint8_t data[WIDTH];
8bd66202 23public:
bfc60703 24 base_blob()
eb2cbd75 25 {
bfc60703 26 memset(data, 0, sizeof(data));
eb2cbd75
PW
27 }
28
bfc60703 29 explicit base_blob(const std::vector<unsigned char>& vch);
eb2cbd75 30
bfc60703 31 bool IsNull() const
8bd66202
GA
32 {
33 for (int i = 0; i < WIDTH; i++)
bfc60703 34 if (data[i] != 0)
8bd66202
GA
35 return false;
36 return true;
37 }
38
bfc60703 39 void SetNull()
8bd66202 40 {
bfc60703 41 memset(data, 0, sizeof(data));
8bd66202
GA
42 }
43
bfc60703
WL
44 friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; }
45 friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; }
46 friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; }
8bd66202 47
de79aaa7
PW
48 std::string GetHex() const;
49 void SetHex(const char* psz);
50 void SetHex(const std::string& str);
51 std::string ToString() const;
8bd66202
GA
52
53 unsigned char* begin()
54 {
bfc60703 55 return &data[0];
8bd66202
GA
56 }
57
58 unsigned char* end()
59 {
bfc60703 60 return &data[WIDTH];
8bd66202
GA
61 }
62
68feac96
MC
63 const unsigned char* begin() const
64 {
bfc60703 65 return &data[0];
68feac96
MC
66 }
67
68 const unsigned char* end() const
69 {
bfc60703 70 return &data[WIDTH];
68feac96
MC
71 }
72
73 unsigned int size() const
8bd66202 74 {
bfc60703 75 return sizeof(data);
5fee401f 76 }
8bd66202 77
6b6aaa16 78 unsigned int GetSerializeSize(int nType, int nVersion) const
8bd66202 79 {
bfc60703 80 return sizeof(data);
8bd66202
GA
81 }
82
83 template<typename Stream>
6b6aaa16 84 void Serialize(Stream& s, int nType, int nVersion) const
8bd66202 85 {
bfc60703 86 s.write((char*)data, sizeof(data));
8bd66202
GA
87 }
88
89 template<typename Stream>
6b6aaa16 90 void Unserialize(Stream& s, int nType, int nVersion)
8bd66202 91 {
bfc60703 92 s.read((char*)data, sizeof(data));
5d3064bc 93 }
8bd66202
GA
94};
95
bfc60703
WL
96/** 160-bit opaque blob.
97 * @note This type is called uint160 for historical reasons only. It is an opaque
98 * blob of 160 bits and has no integer operations.
99 */
100class uint160 : public base_blob<160> {
8bd66202 101public:
eb2cbd75 102 uint160() {}
bfc60703
WL
103 uint160(const base_blob<160>& b) : base_blob<160>(b) {}
104 explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
8bd66202
GA
105};
106
bfc60703
WL
107/** 256-bit opaque blob.
108 * @note This type is called uint256 for historical reasons only. It is an
109 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
110 * those are required.
111 */
112class uint256 : public base_blob<256> {
8bd66202 113public:
eb2cbd75 114 uint256() {}
bfc60703
WL
115 uint256(const base_blob<256>& b) : base_blob<256>(b) {}
116 explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
117
118 /** A cheap hash function that just returns 64 bits from the result, it can be
119 * used when the contents are considered uniformly random. It is not appropriate
120 * when the value can easily be influenced from outside as e.g. a network adversary could
121 * provide values to trigger worst-case behavior.
122 * @note The result of this function is not stable between little and big endian.
fa94b9d5 123 */
bfc60703
WL
124 uint64_t GetCheapHash() const
125 {
126 uint64_t result;
127 memcpy((void*)&result, (void*)data, 8);
128 return result;
129 }
bc42503f 130
bfc60703
WL
131 /** A more secure, salted hash function.
132 * @note This hash is not stable between little and big endian.
133 */
bc42503f 134 uint64_t GetHash(const uint256& salt) const;
8bd66202
GA
135};
136
bfc60703
WL
137/* uint256 from const char *.
138 * This is a separate function because the constructor uint256(const char*) can result
139 * in dangerously catching uint256(0).
140 */
141inline uint256 uint256S(const char *str)
142{
143 uint256 rv;
144 rv.SetHex(str);
145 return rv;
146}
147/* uint256 from std::string.
148 * This is a separate function because the constructor uint256(const std::string &str) can result
149 * in dangerously catching uint256(0) via std::string(const char*).
150 */
151inline uint256 uint256S(const std::string& str)
152{
153 uint256 rv;
154 rv.SetHex(str);
155 return rv;
156}
5d3064bc 157
093303a8 158#endif // BITCOIN_UINT256_H
This page took 0.216905 seconds and 4 git commands to generate.