Bitcoin Core  24.99.0
P2P Digital Currency
arith_uint256.cpp
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 #include <arith_uint256.h>
7 
8 #include <uint256.h>
9 #include <crypto/common.h>
10 
11 
12 template <unsigned int BITS>
13 base_uint<BITS>::base_uint(const std::string& str)
14 {
15  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
16 
17  SetHex(str);
18 }
19 
20 template <unsigned int BITS>
22 {
23  base_uint<BITS> a(*this);
24  for (int i = 0; i < WIDTH; i++)
25  pn[i] = 0;
26  int k = shift / 32;
27  shift = shift % 32;
28  for (int i = 0; i < WIDTH; i++) {
29  if (i + k + 1 < WIDTH && shift != 0)
30  pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
31  if (i + k < WIDTH)
32  pn[i + k] |= (a.pn[i] << shift);
33  }
34  return *this;
35 }
36 
37 template <unsigned int BITS>
39 {
40  base_uint<BITS> a(*this);
41  for (int i = 0; i < WIDTH; i++)
42  pn[i] = 0;
43  int k = shift / 32;
44  shift = shift % 32;
45  for (int i = 0; i < WIDTH; i++) {
46  if (i - k - 1 >= 0 && shift != 0)
47  pn[i - k - 1] |= (a.pn[i] << (32 - shift));
48  if (i - k >= 0)
49  pn[i - k] |= (a.pn[i] >> shift);
50  }
51  return *this;
52 }
53 
54 template <unsigned int BITS>
56 {
57  uint64_t carry = 0;
58  for (int i = 0; i < WIDTH; i++) {
59  uint64_t n = carry + (uint64_t)b32 * pn[i];
60  pn[i] = n & 0xffffffff;
61  carry = n >> 32;
62  }
63  return *this;
64 }
65 
66 template <unsigned int BITS>
68 {
70  for (int j = 0; j < WIDTH; j++) {
71  uint64_t carry = 0;
72  for (int i = 0; i + j < WIDTH; i++) {
73  uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
74  a.pn[i + j] = n & 0xffffffff;
75  carry = n >> 32;
76  }
77  }
78  *this = a;
79  return *this;
80 }
81 
82 template <unsigned int BITS>
84 {
85  base_uint<BITS> div = b; // make a copy, so we can shift.
86  base_uint<BITS> num = *this; // make a copy, so we can subtract.
87  *this = 0; // the quotient.
88  int num_bits = num.bits();
89  int div_bits = div.bits();
90  if (div_bits == 0)
91  throw uint_error("Division by zero");
92  if (div_bits > num_bits) // the result is certainly 0.
93  return *this;
94  int shift = num_bits - div_bits;
95  div <<= shift; // shift so that div and num align.
96  while (shift >= 0) {
97  if (num >= div) {
98  num -= div;
99  pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
100  }
101  div >>= 1; // shift back.
102  shift--;
103  }
104  // num now contains the remainder of the division.
105  return *this;
106 }
107 
108 template <unsigned int BITS>
110 {
111  for (int i = WIDTH - 1; i >= 0; i--) {
112  if (pn[i] < b.pn[i])
113  return -1;
114  if (pn[i] > b.pn[i])
115  return 1;
116  }
117  return 0;
118 }
119 
120 template <unsigned int BITS>
121 bool base_uint<BITS>::EqualTo(uint64_t b) const
122 {
123  for (int i = WIDTH - 1; i >= 2; i--) {
124  if (pn[i])
125  return false;
126  }
127  if (pn[1] != (b >> 32))
128  return false;
129  if (pn[0] != (b & 0xfffffffful))
130  return false;
131  return true;
132 }
133 
134 template <unsigned int BITS>
136 {
137  double ret = 0.0;
138  double fact = 1.0;
139  for (int i = 0; i < WIDTH; i++) {
140  ret += fact * pn[i];
141  fact *= 4294967296.0;
142  }
143  return ret;
144 }
145 
146 template <unsigned int BITS>
147 std::string base_uint<BITS>::GetHex() const
148 {
149  base_blob<BITS> b;
150  for (int x = 0; x < this->WIDTH; ++x) {
151  WriteLE32(b.begin() + x*4, this->pn[x]);
152  }
153  return b.GetHex();
154 }
155 
156 template <unsigned int BITS>
157 void base_uint<BITS>::SetHex(const char* psz)
158 {
159  base_blob<BITS> b;
160  b.SetHex(psz);
161  for (int x = 0; x < this->WIDTH; ++x) {
162  this->pn[x] = ReadLE32(b.begin() + x*4);
163  }
164 }
165 
166 template <unsigned int BITS>
167 void base_uint<BITS>::SetHex(const std::string& str)
168 {
169  SetHex(str.c_str());
170 }
171 
172 template <unsigned int BITS>
173 std::string base_uint<BITS>::ToString() const
174 {
175  return GetHex();
176 }
177 
178 template <unsigned int BITS>
179 unsigned int base_uint<BITS>::bits() const
180 {
181  for (int pos = WIDTH - 1; pos >= 0; pos--) {
182  if (pn[pos]) {
183  for (int nbits = 31; nbits > 0; nbits--) {
184  if (pn[pos] & 1U << nbits)
185  return 32 * pos + nbits + 1;
186  }
187  return 32 * pos + 1;
188  }
189  }
190  return 0;
191 }
192 
193 // Explicit instantiations for base_uint<256>
194 template class base_uint<256>;
195 
196 // This implementation directly uses shifts instead of going
197 // through an intermediate MPI representation.
198 arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)
199 {
200  int nSize = nCompact >> 24;
201  uint32_t nWord = nCompact & 0x007fffff;
202  if (nSize <= 3) {
203  nWord >>= 8 * (3 - nSize);
204  *this = nWord;
205  } else {
206  *this = nWord;
207  *this <<= 8 * (nSize - 3);
208  }
209  if (pfNegative)
210  *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
211  if (pfOverflow)
212  *pfOverflow = nWord != 0 && ((nSize > 34) ||
213  (nWord > 0xff && nSize > 33) ||
214  (nWord > 0xffff && nSize > 32));
215  return *this;
216 }
217 
218 uint32_t arith_uint256::GetCompact(bool fNegative) const
219 {
220  int nSize = (bits() + 7) / 8;
221  uint32_t nCompact = 0;
222  if (nSize <= 3) {
223  nCompact = GetLow64() << 8 * (3 - nSize);
224  } else {
225  arith_uint256 bn = *this >> 8 * (nSize - 3);
226  nCompact = bn.GetLow64();
227  }
228  // The 0x00800000 bit denotes the sign.
229  // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
230  if (nCompact & 0x00800000) {
231  nCompact >>= 8;
232  nSize++;
233  }
234  assert((nCompact & ~0x007fffffU) == 0);
235  assert(nSize < 256);
236  nCompact |= nSize << 24;
237  nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
238  return nCompact;
239 }
240 
242 {
243  uint256 b;
244  for(int x=0; x<a.WIDTH; ++x)
245  WriteLE32(b.begin() + x*4, a.pn[x]);
246  return b;
247 }
249 {
250  arith_uint256 b;
251  for(int x=0; x<b.WIDTH; ++x)
252  b.pn[x] = ReadLE32(a.begin() + x*4);
253  return b;
254 }
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
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
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:22
void SetHex(const char *psz)
Definition: uint256.cpp:21
constexpr unsigned char * begin()
Definition: uint256.h:67
std::string GetHex() const
Definition: uint256.cpp:11
Template base class for unsigned big integers.
Definition: arith_uint256.h:25
uint32_t pn[WIDTH]
Definition: arith_uint256.h:29
int CompareTo(const base_uint &b) const
base_uint & operator>>=(unsigned int shift)
static constexpr int WIDTH
Definition: arith_uint256.h:28
base_uint & operator*=(uint32_t b32)
bool EqualTo(uint64_t b) const
double getdouble() const
base_uint & operator<<=(unsigned int shift)
std::string ToString() const
base_uint & operator/=(const base_uint &b)
uint64_t GetLow64() const
void SetHex(const char *psz)
std::string GetHex() const
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:105
static uint32_t ReadLE32(const unsigned char *ptr)
Definition: common.h:24
static void WriteLE32(unsigned char *ptr, uint32_t x)
Definition: common.h:44
assert(!tx.IsCoinBase())