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