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