Bitcoin Core 29.99.0
P2P Digital Currency
arith_uint256.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2022 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_ARITH_UINT256_H
7#define BITCOIN_ARITH_UINT256_H
8
9#include <compare>
10#include <cstdint>
11#include <cstring>
12#include <limits>
13#include <stdexcept>
14#include <string>
15
16class uint256;
17
18class uint_error : public std::runtime_error {
19public:
20 explicit uint_error(const std::string& str) : std::runtime_error(str) {}
21};
22
24template<unsigned int BITS>
26{
27protected:
28 static_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32.");
29 static constexpr int WIDTH = BITS / 32;
31 uint32_t pn[WIDTH];
32public:
33
35 {
36 for (int i = 0; i < WIDTH; i++)
37 pn[i] = 0;
38 }
39
41 {
42 for (int i = 0; i < WIDTH; i++)
43 pn[i] = b.pn[i];
44 }
45
47 {
48 if (this != &b) {
49 for (int i = 0; i < WIDTH; i++)
50 pn[i] = b.pn[i];
51 }
52 return *this;
53 }
54
55 base_uint(uint64_t b)
56 {
57 pn[0] = (unsigned int)b;
58 pn[1] = (unsigned int)(b >> 32);
59 for (int i = 2; i < WIDTH; i++)
60 pn[i] = 0;
61 }
62
64 {
66 for (int i = 0; i < WIDTH; i++)
67 ret.pn[i] = ~pn[i];
68 return ret;
69 }
70
72 {
74 for (int i = 0; i < WIDTH; i++)
75 ret.pn[i] = ~pn[i];
76 ++ret;
77 return ret;
78 }
79
80 double getdouble() const;
81
82 base_uint& operator=(uint64_t b)
83 {
84 pn[0] = (unsigned int)b;
85 pn[1] = (unsigned int)(b >> 32);
86 for (int i = 2; i < WIDTH; i++)
87 pn[i] = 0;
88 return *this;
89 }
90
92 {
93 for (int i = 0; i < WIDTH; i++)
94 pn[i] ^= b.pn[i];
95 return *this;
96 }
97
99 {
100 for (int i = 0; i < WIDTH; i++)
101 pn[i] &= b.pn[i];
102 return *this;
103 }
104
106 {
107 for (int i = 0; i < WIDTH; i++)
108 pn[i] |= b.pn[i];
109 return *this;
110 }
111
113 {
114 pn[0] ^= (unsigned int)b;
115 pn[1] ^= (unsigned int)(b >> 32);
116 return *this;
117 }
118
120 {
121 pn[0] |= (unsigned int)b;
122 pn[1] |= (unsigned int)(b >> 32);
123 return *this;
124 }
125
126 base_uint& operator<<=(unsigned int shift);
127 base_uint& operator>>=(unsigned int shift);
128
130 {
131 uint64_t carry = 0;
132 for (int i = 0; i < WIDTH; i++)
133 {
134 uint64_t n = carry + pn[i] + b.pn[i];
135 pn[i] = n & 0xffffffff;
136 carry = n >> 32;
137 }
138 return *this;
139 }
140
142 {
143 *this += -b;
144 return *this;
145 }
146
147 base_uint& operator+=(uint64_t b64)
148 {
149 base_uint b;
150 b = b64;
151 *this += b;
152 return *this;
153 }
154
155 base_uint& operator-=(uint64_t b64)
156 {
157 base_uint b;
158 b = b64;
159 *this += -b;
160 return *this;
161 }
162
163 base_uint& operator*=(uint32_t b32);
164 base_uint& operator*=(const base_uint& b);
165 base_uint& operator/=(const base_uint& b);
166
168 {
169 // prefix operator
170 int i = 0;
171 while (i < WIDTH && ++pn[i] == 0)
172 i++;
173 return *this;
174 }
175
177 {
178 // postfix operator
179 const base_uint ret = *this;
180 ++(*this);
181 return ret;
182 }
183
185 {
186 // prefix operator
187 int i = 0;
188 while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
189 i++;
190 return *this;
191 }
192
194 {
195 // postfix operator
196 const base_uint ret = *this;
197 --(*this);
198 return ret;
199 }
200
202 int CompareTo(const base_uint& b) const;
203 bool EqualTo(uint64_t b) const;
204
205 friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
206 friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
207 friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
208 friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
209 friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
210 friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
211 friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
212 friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
213 friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
214 friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
215 friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
216 friend inline std::strong_ordering operator<=>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <=> 0; }
217 friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
218
220 std::string GetHex() const;
221 std::string ToString() const;
222
223 unsigned int size() const
224 {
225 return sizeof(pn);
226 }
227
232 unsigned int bits() const;
233
234 uint64_t GetLow64() const
235 {
236 static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
237 return pn[0] | (uint64_t)pn[1] << 32;
238 }
239};
240
242class arith_uint256 : public base_uint<256> {
243public:
244 arith_uint256() = default;
246 arith_uint256(uint64_t b) : base_uint<256>(b) {}
247
268 arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
269 uint32_t GetCompact(bool fNegative = false) const;
270
271 friend uint256 ArithToUint256(const arith_uint256 &);
272 friend arith_uint256 UintToArith256(const uint256 &);
273};
274
277
278extern template class base_uint<256>;
279
280#endif // BITCOIN_ARITH_UINT256_H
arith_uint256 UintToArith256(const uint256 &)
uint256 ArithToUint256(const arith_uint256 &)
int ret
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
uint32_t GetCompact(bool fNegative=false) const
arith_uint256()=default
arith_uint256(uint64_t b)
arith_uint256(const base_uint< 256 > &b)
friend arith_uint256 UintToArith256(const uint256 &)
friend uint256 ArithToUint256(const arith_uint256 &)
Template base class for unsigned big integers.
Definition: arith_uint256.h:26
base_uint & operator/=(const base_uint &b)
base_uint operator~() const
Definition: arith_uint256.h:63
uint32_t pn[WIDTH]
Big integer represented with 32-bit digits, least-significant first.
Definition: arith_uint256.h:31
int CompareTo(const base_uint &b) const
Numeric ordering (unlike base_blob::Compare)
base_uint operator--(int)
unsigned int size() const
base_uint(uint64_t b)
Definition: arith_uint256.h:55
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:91
base_uint & operator|=(uint64_t b)
base_uint & operator+=(uint64_t b64)
base_uint & operator>>=(unsigned int shift)
base_uint & operator++()
base_uint & operator--()
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:82
friend base_uint operator-(const base_uint &a, const base_uint &b)
base_uint(const base_uint &b)
Definition: arith_uint256.h:40
friend base_uint operator*(const base_uint &a, const base_uint &b)
static constexpr int WIDTH
Definition: arith_uint256.h:29
friend base_uint operator<<(const base_uint &a, int shift)
friend std::strong_ordering operator<=>(const base_uint &a, const base_uint &b)
base_uint & operator|=(const base_uint &b)
friend base_uint operator&(const base_uint &a, const base_uint &b)
base_uint & operator-=(uint64_t b64)
friend bool operator==(const base_uint &a, uint64_t b)
base_uint & operator^=(uint64_t b)
base_uint operator-() const
Definition: arith_uint256.h:71
friend base_uint operator>>(const base_uint &a, int shift)
base_uint & operator*=(uint32_t b32)
friend base_uint operator|(const base_uint &a, const base_uint &b)
bool EqualTo(uint64_t b) const
friend base_uint operator*(const base_uint &a, uint32_t b)
base_uint & operator&=(const base_uint &b)
Definition: arith_uint256.h:98
friend bool operator==(const base_uint &a, const base_uint &b)
friend base_uint operator/(const base_uint &a, const base_uint &b)
friend base_uint operator^(const base_uint &a, const base_uint &b)
double getdouble() const
base_uint & operator<<=(unsigned int shift)
std::string ToString() const
base_uint operator++(int)
base_uint & operator+=(const base_uint &b)
uint64_t GetLow64() const
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:46
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).
base_uint & operator-=(const base_uint &b)
friend base_uint operator+(const base_uint &a, const base_uint &b)
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
256-bit opaque blob.
Definition: uint256.h:196
uint_error(const std::string &str)
Definition: arith_uint256.h:20