Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

uint256.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "uint256.h"
  6. #include "utilstrencodings.h"
  7. #include <stdio.h>
  8. #include <string.h>
  9. template <unsigned int BITS>
  10. base_uint<BITS>::base_uint(const std::string& str)
  11. {
  12. SetHex(str);
  13. }
  14. template <unsigned int BITS>
  15. base_uint<BITS>::base_uint(const std::vector<unsigned char>& vch)
  16. {
  17. if (vch.size() != sizeof(pn))
  18. throw uint_error("Converting vector of wrong size to base_uint");
  19. memcpy(pn, &vch[0], sizeof(pn));
  20. }
  21. template <unsigned int BITS>
  22. base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift)
  23. {
  24. base_uint<BITS> a(*this);
  25. for (int i = 0; i < WIDTH; i++)
  26. pn[i] = 0;
  27. int k = shift / 32;
  28. shift = shift % 32;
  29. for (int i = 0; i < WIDTH; i++) {
  30. if (i + k + 1 < WIDTH && shift != 0)
  31. pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
  32. if (i + k < WIDTH)
  33. pn[i + k] |= (a.pn[i] << shift);
  34. }
  35. return *this;
  36. }
  37. template <unsigned int BITS>
  38. base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift)
  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. template <unsigned int BITS>
  54. base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
  55. {
  56. uint64_t carry = 0;
  57. for (int i = 0; i < WIDTH; i++) {
  58. uint64_t n = carry + (uint64_t)b32 * pn[i];
  59. pn[i] = n & 0xffffffff;
  60. carry = n >> 32;
  61. }
  62. return *this;
  63. }
  64. template <unsigned int BITS>
  65. base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
  66. {
  67. base_uint<BITS> a = *this;
  68. *this = 0;
  69. for (int j = 0; j < WIDTH; j++) {
  70. uint64_t carry = 0;
  71. for (int i = 0; i + j < WIDTH; i++) {
  72. uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
  73. pn[i + j] = n & 0xffffffff;
  74. carry = n >> 32;
  75. }
  76. }
  77. return *this;
  78. }
  79. template <unsigned int BITS>
  80. base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b)
  81. {
  82. base_uint<BITS> div = b; // make a copy, so we can shift.
  83. base_uint<BITS> num = *this; // make a copy, so we can subtract.
  84. *this = 0; // the quotient.
  85. int num_bits = num.bits();
  86. int div_bits = div.bits();
  87. if (div_bits == 0)
  88. throw uint_error("Division by zero");
  89. if (div_bits > num_bits) // the result is certainly 0.
  90. return *this;
  91. int shift = num_bits - div_bits;
  92. div <<= shift; // shift so that div and num align.
  93. while (shift >= 0) {
  94. if (num >= div) {
  95. num -= div;
  96. pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
  97. }
  98. div >>= 1; // shift back.
  99. shift--;
  100. }
  101. // num now contains the remainder of the division.
  102. return *this;
  103. }
  104. template <unsigned int BITS>
  105. int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const
  106. {
  107. for (int i = WIDTH - 1; i >= 0; i--) {
  108. if (pn[i] < b.pn[i])
  109. return -1;
  110. if (pn[i] > b.pn[i])
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. template <unsigned int BITS>
  116. bool base_uint<BITS>::EqualTo(uint64_t b) const
  117. {
  118. for (int i = WIDTH - 1; i >= 2; i--) {
  119. if (pn[i])
  120. return false;
  121. }
  122. if (pn[1] != (b >> 32))
  123. return false;
  124. if (pn[0] != (b & 0xfffffffful))
  125. return false;
  126. return true;
  127. }
  128. template <unsigned int BITS>
  129. double base_uint<BITS>::getdouble() const
  130. {
  131. double ret = 0.0;
  132. double fact = 1.0;
  133. for (int i = 0; i < WIDTH; i++) {
  134. ret += fact * pn[i];
  135. fact *= 4294967296.0;
  136. }
  137. return ret;
  138. }
  139. template <unsigned int BITS>
  140. std::string base_uint<BITS>::GetHex() const
  141. {
  142. char psz[sizeof(pn) * 2 + 1];
  143. for (unsigned int i = 0; i < sizeof(pn); i++)
  144. sprintf(psz + i * 2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
  145. return std::string(psz, psz + sizeof(pn) * 2);
  146. }
  147. template <unsigned int BITS>
  148. void base_uint<BITS>::SetHex(const char* psz)
  149. {
  150. memset(pn, 0, sizeof(pn));
  151. // skip leading spaces
  152. while (isspace(*psz))
  153. psz++;
  154. // skip 0x
  155. if (psz[0] == '0' && tolower(psz[1]) == 'x')
  156. psz += 2;
  157. // hex string to uint
  158. const char* pbegin = psz;
  159. while (::HexDigit(*psz) != -1)
  160. psz++;
  161. psz--;
  162. unsigned char* p1 = (unsigned char*)pn;
  163. unsigned char* pend = p1 + WIDTH * 4;
  164. while (psz >= pbegin && p1 < pend) {
  165. *p1 = ::HexDigit(*psz--);
  166. if (psz >= pbegin) {
  167. *p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
  168. p1++;
  169. }
  170. }
  171. }
  172. template <unsigned int BITS>
  173. void base_uint<BITS>::SetHex(const std::string& str)
  174. {
  175. SetHex(str.c_str());
  176. }
  177. template <unsigned int BITS>
  178. std::string base_uint<BITS>::ToString() const
  179. {
  180. return (GetHex());
  181. }
  182. template <unsigned int BITS>
  183. unsigned int base_uint<BITS>::bits() const
  184. {
  185. for (int pos = WIDTH - 1; pos >= 0; pos--) {
  186. if (pn[pos]) {
  187. for (int bits = 31; bits > 0; bits--) {
  188. if (pn[pos] & 1 << bits)
  189. return 32 * pos + bits + 1;
  190. }
  191. return 32 * pos + 1;
  192. }
  193. }
  194. return 0;
  195. }
  196. // Explicit instantiations for base_uint<160>
  197. template base_uint<160>::base_uint(const std::string&);
  198. template base_uint<160>::base_uint(const std::vector<unsigned char>&);
  199. template base_uint<160>& base_uint<160>::operator<<=(unsigned int);
  200. template base_uint<160>& base_uint<160>::operator>>=(unsigned int);
  201. template base_uint<160>& base_uint<160>::operator*=(uint32_t b32);
  202. template base_uint<160>& base_uint<160>::operator*=(const base_uint<160>& b);
  203. template base_uint<160>& base_uint<160>::operator/=(const base_uint<160>& b);
  204. template int base_uint<160>::CompareTo(const base_uint<160>&) const;
  205. template bool base_uint<160>::EqualTo(uint64_t) const;
  206. template double base_uint<160>::getdouble() const;
  207. template std::string base_uint<160>::GetHex() const;
  208. template std::string base_uint<160>::ToString() const;
  209. template void base_uint<160>::SetHex(const char*);
  210. template void base_uint<160>::SetHex(const std::string&);
  211. template unsigned int base_uint<160>::bits() const;
  212. // Explicit instantiations for base_uint<256>
  213. template base_uint<256>::base_uint(const std::string&);
  214. template base_uint<256>::base_uint(const std::vector<unsigned char>&);
  215. template base_uint<256>& base_uint<256>::operator<<=(unsigned int);
  216. template base_uint<256>& base_uint<256>::operator>>=(unsigned int);
  217. template base_uint<256>& base_uint<256>::operator*=(uint32_t b32);
  218. template base_uint<256>& base_uint<256>::operator*=(const base_uint<256>& b);
  219. template base_uint<256>& base_uint<256>::operator/=(const base_uint<256>& b);
  220. template int base_uint<256>::CompareTo(const base_uint<256>&) const;
  221. template bool base_uint<256>::EqualTo(uint64_t) const;
  222. template double base_uint<256>::getdouble() const;
  223. template std::string base_uint<256>::GetHex() const;
  224. template std::string base_uint<256>::ToString() const;
  225. template void base_uint<256>::SetHex(const char*);
  226. template void base_uint<256>::SetHex(const std::string&);
  227. template unsigned int base_uint<256>::bits() const;
  228. // This implementation directly uses shifts instead of going
  229. // through an intermediate MPI representation.
  230. uint256& uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)
  231. {
  232. int nSize = nCompact >> 24;
  233. uint32_t nWord = nCompact & 0x007fffff;
  234. if (nSize <= 3) {
  235. nWord >>= 8 * (3 - nSize);
  236. *this = nWord;
  237. } else {
  238. *this = nWord;
  239. *this <<= 8 * (nSize - 3);
  240. }
  241. if (pfNegative)
  242. *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
  243. if (pfOverflow)
  244. *pfOverflow = nWord != 0 && ((nSize > 34) ||
  245. (nWord > 0xff && nSize > 33) ||
  246. (nWord > 0xffff && nSize > 32));
  247. return *this;
  248. }
  249. uint32_t uint256::GetCompact(bool fNegative) const
  250. {
  251. int nSize = (bits() + 7) / 8;
  252. uint32_t nCompact = 0;
  253. if (nSize <= 3) {
  254. nCompact = GetLow64() << 8 * (3 - nSize);
  255. } else {
  256. uint256 bn = *this >> 8 * (nSize - 3);
  257. nCompact = bn.GetLow64();
  258. }
  259. // The 0x00800000 bit denotes the sign.
  260. // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
  261. if (nCompact & 0x00800000) {
  262. nCompact >>= 8;
  263. nSize++;
  264. }
  265. assert((nCompact & ~0x007fffff) == 0);
  266. assert(nSize < 256);
  267. nCompact |= nSize << 24;
  268. nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
  269. return nCompact;
  270. }
  271. static void inline HashMix(uint32_t& a, uint32_t& b, uint32_t& c)
  272. {
  273. // Taken from lookup3, by Bob Jenkins.
  274. a -= c;
  275. a ^= ((c << 4) | (c >> 28));
  276. c += b;
  277. b -= a;
  278. b ^= ((a << 6) | (a >> 26));
  279. a += c;
  280. c -= b;
  281. c ^= ((b << 8) | (b >> 24));
  282. b += a;
  283. a -= c;
  284. a ^= ((c << 16) | (c >> 16));
  285. c += b;
  286. b -= a;
  287. b ^= ((a << 19) | (a >> 13));
  288. a += c;
  289. c -= b;
  290. c ^= ((b << 4) | (b >> 28));
  291. b += a;
  292. }
  293. static void inline HashFinal(uint32_t& a, uint32_t& b, uint32_t& c)
  294. {
  295. // Taken from lookup3, by Bob Jenkins.
  296. c ^= b;
  297. c -= ((b << 14) | (b >> 18));
  298. a ^= c;
  299. a -= ((c << 11) | (c >> 21));
  300. b ^= a;
  301. b -= ((a << 25) | (a >> 7));
  302. c ^= b;
  303. c -= ((b << 16) | (b >> 16));
  304. a ^= c;
  305. a -= ((c << 4) | (c >> 28));
  306. b ^= a;
  307. b -= ((a << 14) | (a >> 18));
  308. c ^= b;
  309. c -= ((b << 24) | (b >> 8));
  310. }
  311. uint64_t uint256::GetHash(const uint256& salt) const
  312. {
  313. uint32_t a, b, c;
  314. a = b = c = 0xdeadbeef + (WIDTH << 2);
  315. a += pn[0] ^ salt.pn[0];
  316. b += pn[1] ^ salt.pn[1];
  317. c += pn[2] ^ salt.pn[2];
  318. HashMix(a, b, c);
  319. a += pn[3] ^ salt.pn[3];
  320. b += pn[4] ^ salt.pn[4];
  321. c += pn[5] ^ salt.pn[5];
  322. HashMix(a, b, c);
  323. a += pn[6] ^ salt.pn[6];
  324. b += pn[7] ^ salt.pn[7];
  325. HashFinal(a, b, c);
  326. return ((((uint64_t)b) << 32) | c);
  327. }