Bitcoin Core 30.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
40 base_uint(const base_uint& b) = default;
41 base_uint& operator=(const base_uint& b) = default;
42
43 base_uint(uint64_t b)
44 {
45 pn[0] = (unsigned int)b;
46 pn[1] = (unsigned int)(b >> 32);
47 for (int i = 2; i < WIDTH; i++)
48 pn[i] = 0;
49 }
50
52 {
54 for (int i = 0; i < WIDTH; i++)
55 ret.pn[i] = ~pn[i];
56 return ret;
57 }
58
60 {
62 for (int i = 0; i < WIDTH; i++)
63 ret.pn[i] = ~pn[i];
64 ++ret;
65 return ret;
66 }
67
68 double getdouble() const;
69
70 base_uint& operator=(uint64_t b)
71 {
72 pn[0] = (unsigned int)b;
73 pn[1] = (unsigned int)(b >> 32);
74 for (int i = 2; i < WIDTH; i++)
75 pn[i] = 0;
76 return *this;
77 }
78
80 {
81 for (int i = 0; i < WIDTH; i++)
82 pn[i] ^= b.pn[i];
83 return *this;
84 }
85
87 {
88 for (int i = 0; i < WIDTH; i++)
89 pn[i] &= b.pn[i];
90 return *this;
91 }
92
94 {
95 for (int i = 0; i < WIDTH; i++)
96 pn[i] |= b.pn[i];
97 return *this;
98 }
99
101 {
102 pn[0] ^= (unsigned int)b;
103 pn[1] ^= (unsigned int)(b >> 32);
104 return *this;
105 }
106
108 {
109 pn[0] |= (unsigned int)b;
110 pn[1] |= (unsigned int)(b >> 32);
111 return *this;
112 }
113
114 base_uint& operator<<=(unsigned int shift);
115 base_uint& operator>>=(unsigned int shift);
116
118 {
119 uint64_t carry = 0;
120 for (int i = 0; i < WIDTH; i++)
121 {
122 uint64_t n = carry + pn[i] + b.pn[i];
123 pn[i] = n & 0xffffffff;
124 carry = n >> 32;
125 }
126 return *this;
127 }
128
130 {
131 *this += -b;
132 return *this;
133 }
134
135 base_uint& operator+=(uint64_t b64)
136 {
137 base_uint b;
138 b = b64;
139 *this += b;
140 return *this;
141 }
142
143 base_uint& operator-=(uint64_t b64)
144 {
145 base_uint b;
146 b = b64;
147 *this += -b;
148 return *this;
149 }
150
151 base_uint& operator*=(uint32_t b32);
152 base_uint& operator*=(const base_uint& b);
153 base_uint& operator/=(const base_uint& b);
154
156 {
157 // prefix operator
158 int i = 0;
159 while (i < WIDTH && ++pn[i] == 0)
160 i++;
161 return *this;
162 }
163
165 {
166 // postfix operator
167 const base_uint ret = *this;
168 ++(*this);
169 return ret;
170 }
171
173 {
174 // prefix operator
175 int i = 0;
176 while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
177 i++;
178 return *this;
179 }
180
182 {
183 // postfix operator
184 const base_uint ret = *this;
185 --(*this);
186 return ret;
187 }
188
190 int CompareTo(const base_uint& b) const;
191 bool EqualTo(uint64_t b) const;
192
193 friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
194 friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
195 friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
196 friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
197 friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
198 friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
199 friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
200 friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
201 friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
202 friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
203 friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
204 friend inline std::strong_ordering operator<=>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <=> 0; }
205 friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
206
208 std::string GetHex() const;
209 std::string ToString() const;
210
211 unsigned int size() const
212 {
213 return sizeof(pn);
214 }
215
220 unsigned int bits() const;
221
222 uint64_t GetLow64() const
223 {
224 static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
225 return pn[0] | (uint64_t)pn[1] << 32;
226 }
227};
228
230class arith_uint256 : public base_uint<256> {
231public:
232 arith_uint256() = default;
234 arith_uint256(uint64_t b) : base_uint<256>(b) {}
235
256 arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
257 uint32_t GetCompact(bool fNegative = false) const;
258
259 friend uint256 ArithToUint256(const arith_uint256 &);
260 friend arith_uint256 UintToArith256(const uint256 &);
261};
262
263// Keeping the trivially copyable property is beneficial for performance
264static_assert(std::is_trivially_copyable_v<arith_uint256>);
265
268
269extern template class base_uint<256>;
270
271#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:51
base_uint & operator=(const base_uint &b)=default
uint32_t pn[WIDTH]
Big integer represented with 32-bit digits, least-significant first.
Definition: arith_uint256.h:31
base_uint(const base_uint &b)=default
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:43
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:79
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:70
friend base_uint operator-(const base_uint &a, const base_uint &b)
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)
Definition: arith_uint256.h:93
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:59
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:86
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
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