diff options
Diffstat (limited to 'src/uint256.cpp')
-rw-r--r-- | src/uint256.cpp | 133 |
1 files changed, 77 insertions, 56 deletions
diff --git a/src/uint256.cpp b/src/uint256.cpp index feda0ca5a9..79406f2475 100644 --- a/src/uint256.cpp +++ b/src/uint256.cpp @@ -10,13 +10,13 @@ #include <stdio.h> #include <string.h> -template<unsigned int BITS> +template <unsigned int BITS> base_uint<BITS>::base_uint(const std::string& str) { SetHex(str); } -template<unsigned int BITS> +template <unsigned int BITS> base_uint<BITS>::base_uint(const std::vector<unsigned char>& vch) { if (vch.size() != sizeof(pn)) @@ -24,7 +24,7 @@ base_uint<BITS>::base_uint(const std::vector<unsigned char>& vch) memcpy(pn, &vch[0], sizeof(pn)); } -template<unsigned int BITS> +template <unsigned int BITS> base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift) { base_uint<BITS> a(*this); @@ -33,15 +33,15 @@ base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift) int k = shift / 32; shift = shift % 32; for (int i = 0; i < WIDTH; i++) { - if (i+k+1 < WIDTH && shift != 0) - pn[i+k+1] |= (a.pn[i] >> (32-shift)); - if (i+k < WIDTH) - pn[i+k] |= (a.pn[i] << shift); + if (i + k + 1 < WIDTH && shift != 0) + pn[i + k + 1] |= (a.pn[i] >> (32 - shift)); + if (i + k < WIDTH) + pn[i + k] |= (a.pn[i] << shift); } return *this; } -template<unsigned int BITS> +template <unsigned int BITS> base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift) { base_uint<BITS> a(*this); @@ -50,15 +50,15 @@ base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift) int k = shift / 32; shift = shift % 32; for (int i = 0; i < WIDTH; i++) { - if (i-k-1 >= 0 && shift != 0) - pn[i-k-1] |= (a.pn[i] << (32-shift)); - if (i-k >= 0) - pn[i-k] |= (a.pn[i] >> shift); + if (i - k - 1 >= 0 && shift != 0) + pn[i - k - 1] |= (a.pn[i] << (32 - shift)); + if (i - k >= 0) + pn[i - k] |= (a.pn[i] >> shift); } return *this; } -template<unsigned int BITS> +template <unsigned int BITS> base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32) { uint64_t carry = 0; @@ -70,7 +70,7 @@ base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32) return *this; } -template<unsigned int BITS> +template <unsigned int BITS> base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b) { base_uint<BITS> a = *this; @@ -86,12 +86,12 @@ base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b) return *this; } -template<unsigned int BITS> +template <unsigned int BITS> base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b) { - base_uint<BITS> div = b; // make a copy, so we can shift. + base_uint<BITS> div = b; // make a copy, so we can shift. base_uint<BITS> num = *this; // make a copy, so we can subtract. - *this = 0; // the quotient. + *this = 0; // the quotient. int num_bits = num.bits(); int div_bits = div.bits(); if (div_bits == 0) @@ -112,9 +112,10 @@ base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b) return *this; } -template<unsigned int BITS> -int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const { - for (int i = WIDTH-1; i >= 0; i--) { +template <unsigned int BITS> +int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const +{ + for (int i = WIDTH - 1; i >= 0; i--) { if (pn[i] < b.pn[i]) return -1; if (pn[i] > b.pn[i]) @@ -123,9 +124,10 @@ int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const { return 0; } -template<unsigned int BITS> -bool base_uint<BITS>::EqualTo(uint64_t b) const { - for (int i = WIDTH-1; i >= 2; i--) { +template <unsigned int BITS> +bool base_uint<BITS>::EqualTo(uint64_t b) const +{ + for (int i = WIDTH - 1; i >= 2; i--) { if (pn[i]) return false; } @@ -136,7 +138,7 @@ bool base_uint<BITS>::EqualTo(uint64_t b) const { return true; } -template<unsigned int BITS> +template <unsigned int BITS> double base_uint<BITS>::getdouble() const { double ret = 0.0; @@ -148,19 +150,19 @@ double base_uint<BITS>::getdouble() const return ret; } -template<unsigned int BITS> +template <unsigned int BITS> std::string base_uint<BITS>::GetHex() const { - char psz[sizeof(pn)*2 + 1]; + char psz[sizeof(pn) * 2 + 1]; for (unsigned int i = 0; i < sizeof(pn); i++) - sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]); - return std::string(psz, psz + sizeof(pn)*2); + sprintf(psz + i * 2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]); + return std::string(psz, psz + sizeof(pn) * 2); } -template<unsigned int BITS> +template <unsigned int BITS> void base_uint<BITS>::SetHex(const char* psz) { - memset(pn,0,sizeof(pn)); + memset(pn, 0, sizeof(pn)); // skip leading spaces while (isspace(*psz)) @@ -186,28 +188,28 @@ void base_uint<BITS>::SetHex(const char* psz) } } -template<unsigned int BITS> +template <unsigned int BITS> void base_uint<BITS>::SetHex(const std::string& str) { SetHex(str.c_str()); } -template<unsigned int BITS> +template <unsigned int BITS> std::string base_uint<BITS>::ToString() const { return (GetHex()); } -template<unsigned int BITS> +template <unsigned int BITS> unsigned int base_uint<BITS>::bits() const { - for (int pos = WIDTH-1; pos >= 0; pos--) { + for (int pos = WIDTH - 1; pos >= 0; pos--) { if (pn[pos]) { for (int bits = 31; bits > 0; bits--) { - if (pn[pos] & 1<<bits) - return 32*pos + bits + 1; + if (pn[pos] & 1 << bits) + return 32 * pos + bits + 1; } - return 32*pos + 1; + return 32 * pos + 1; } } return 0; @@ -249,16 +251,16 @@ template unsigned int base_uint<256>::bits() const; // This implementation directly uses shifts instead of going // through an intermediate MPI representation. -uint256& uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow) +uint256& uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow) { int nSize = nCompact >> 24; uint32_t nWord = nCompact & 0x007fffff; if (nSize <= 3) { - nWord >>= 8*(3-nSize); + nWord >>= 8 * (3 - nSize); *this = nWord; } else { *this = nWord; - *this <<= 8*(nSize-3); + *this <<= 8 * (nSize - 3); } if (pfNegative) *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0; @@ -274,9 +276,9 @@ uint32_t uint256::GetCompact(bool fNegative) const int nSize = (bits() + 7) / 8; uint32_t nCompact = 0; if (nSize <= 3) { - nCompact = GetLow64() << 8*(3-nSize); + nCompact = GetLow64() << 8 * (3 - nSize); } else { - uint256 bn = *this >> 8*(nSize-3); + uint256 bn = *this >> 8 * (nSize - 3); nCompact = bn.GetLow64(); } // The 0x00800000 bit denotes the sign. @@ -295,27 +297,46 @@ uint32_t uint256::GetCompact(bool fNegative) const static void inline HashMix(uint32_t& a, uint32_t& b, uint32_t& c) { // Taken from lookup3, by Bob Jenkins. - a -= c; a ^= ((c << 4) | (c >> 28)); c += b; - b -= a; b ^= ((a << 6) | (a >> 26)); a += c; - c -= b; c ^= ((b << 8) | (b >> 24)); b += a; - a -= c; a ^= ((c << 16) | (c >> 16)); c += b; - b -= a; b ^= ((a << 19) | (a >> 13)); a += c; - c -= b; c ^= ((b << 4) | (b >> 28)); b += a; + a -= c; + a ^= ((c << 4) | (c >> 28)); + c += b; + b -= a; + b ^= ((a << 6) | (a >> 26)); + a += c; + c -= b; + c ^= ((b << 8) | (b >> 24)); + b += a; + a -= c; + a ^= ((c << 16) | (c >> 16)); + c += b; + b -= a; + b ^= ((a << 19) | (a >> 13)); + a += c; + c -= b; + c ^= ((b << 4) | (b >> 28)); + b += a; } static void inline HashFinal(uint32_t& a, uint32_t& b, uint32_t& c) { // Taken from lookup3, by Bob Jenkins. - c ^= b; c -= ((b << 14) | (b >> 18)); - a ^= c; a -= ((c << 11) | (c >> 21)); - b ^= a; b -= ((a << 25) | (a >> 7)); - c ^= b; c -= ((b << 16) | (b >> 16)); - a ^= c; a -= ((c << 4) | (c >> 28)); - b ^= a; b -= ((a << 14) | (a >> 18)); - c ^= b; c -= ((b << 24) | (b >> 8)); + c ^= b; + c -= ((b << 14) | (b >> 18)); + a ^= c; + a -= ((c << 11) | (c >> 21)); + b ^= a; + b -= ((a << 25) | (a >> 7)); + c ^= b; + c -= ((b << 16) | (b >> 16)); + a ^= c; + a -= ((c << 4) | (c >> 28)); + b ^= a; + b -= ((a << 14) | (a >> 18)); + c ^= b; + c -= ((b << 24) | (b >> 8)); } -uint64_t uint256::GetHash(const uint256 &salt) const +uint64_t uint256::GetHash(const uint256& salt) const { uint32_t a, b, c; a = b = c = 0xdeadbeef + (WIDTH << 2); |