You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arith_uint256.cpp 7.3KB

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