--- /dev/null
+// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2009-2012 The Bitcoin developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+
+#include "uint256.h"
+
+#include <openssl/rand.h>
+
+#include <cassert>
+#include <cstdio>
+#include <cstring>
+#include <stdexcept>
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// uint160
+//
+
+uint160::uint160()
+{
+ for (int i = 0; i < WIDTH; i++)
+ pn[i] = 0;
+}
+
+uint160::uint160(const basetype& b)
+{
+ for (int i = 0; i < WIDTH; i++)
+ pn[i] = b.pn[i];
+}
+
+uint160& uint160::operator=(const basetype& b)
+{
+ for (int i = 0; i < WIDTH; i++)
+ pn[i] = b.pn[i];
+ return *this;
+}
+
+uint160::uint160(uint64_t b)
+{
+ pn[0] = (uint32_t)b;
+ pn[1] = (uint32_t)(b >> 32);
+ for (int i = 2; i < WIDTH; i++)
+ pn[i] = 0;
+}
+
+uint160& uint160::operator=(uint64_t b)
+{
+ pn[0] = (uint32_t)b;
+ pn[1] = (uint32_t)(b >> 32);
+ for (int i = 2; i < WIDTH; i++)
+ pn[i] = 0;
+ return *this;
+}
+
+uint160::uint160(const std::string& str)
+{
+ SetHex(str);
+}
+
+uint160::uint160(const std::vector<unsigned char>& vch)
+{
+ if (vch.size() == sizeof(pn))
+ memcpy(pn, &vch[0], sizeof(pn));
+ else
+ *this = 0;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// uint256
+//
+
+uint256::uint256()
+{
+ for (int i = 0; i < WIDTH; i++)
+ pn[i] = 0;
+}
+
+uint256::uint256(const basetype& b)
+{
+ for (int i = 0; i < WIDTH; i++)
+ pn[i] = b.pn[i];
+}
+
+uint256& uint256::operator=(const basetype& b)
+{
+ for (int i = 0; i < WIDTH; i++)
+ pn[i] = b.pn[i];
+ return *this;
+}
+
+uint256::uint256(uint64_t b)
+{
+ pn[0] = (uint32_t)b;
+ pn[1] = (uint32_t)(b >> 32);
+ for (int i = 2; i < WIDTH; i++)
+ pn[i] = 0;
+}
+
+uint256& uint256::operator=(uint64_t b)
+{
+ pn[0] = (uint32_t)b;
+ pn[1] = (uint32_t)(b >> 32);
+ for (int i = 2; i < WIDTH; i++)
+ pn[i] = 0;
+ return *this;
+}
+
+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);
+ *this = nWord;
+ } else {
+ *this = nWord;
+ *this <<= 8*(nSize-3);
+ }
+ if (pfNegative)
+ *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
+ if (pfOverflow)
+ *pfOverflow = nWord != 0 && ((nSize > 34) ||
+ (nWord > 0xff && nSize > 33) ||
+ (nWord > 0xffff && nSize > 32));
+ return *this;
+}
+
+uint32_t uint256::GetCompact(bool fNegative) const
+{
+ int nSize = (bits() + 7) / 8;
+ uint32_t nCompact = 0;
+ if (nSize <= 3) {
+ nCompact = Get64(0) << 8*(3-nSize);
+ } else {
+ uint256 n = *this;
+ uint256 bn = n >> 8*(nSize-3);
+ nCompact = bn.Get64(0);
+ }
+ // The 0x00800000 bit denotes the sign.
+ // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
+ if (nCompact & 0x00800000) {
+ nCompact >>= 8;
+ nSize++;
+ }
+ assert((nCompact & ~0x007fffff) == 0);
+ assert(nSize < 256);
+ nCompact |= nSize << 24;
+ nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
+ return nCompact;
+}
+
+uint256& uint256::operator*=(uint32_t b32)
+{
+ uint64_t carry = 0;
+ for (int i = 0; i < WIDTH; i++) {
+ uint64_t n = carry + (uint64_t)b32 * pn[i];
+ pn[i] = n & 0xffffffff;
+ carry = n >> 32;
+ }
+ return *this;
+}
+
+uint256& uint256::operator*=(const uint256& b)
+{
+ uint256 a;
+ for (int j = 0; j < WIDTH; j++) {
+ uint64_t carry = 0;
+ for (int i = 0; i + j < WIDTH; i++) {
+ uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
+ pn[i + j] = n & 0xffffffff;
+ carry = n >> 32;
+ }
+ }
+ *this = a;
+ return *this;
+}
+
+uint256& uint256::operator/=(const uint256& b)
+{
+ uint256 div = b; // make a copy, so we can shift.
+ uint256 num = *this; // make a copy, so we can subtract.
+ *this = 0; // the quotient.
+ int num_bits = num.bits();
+ int div_bits = div.bits();
+ if (div_bits == 0)
+ throw uint256_error("Division by zero");
+ if (div_bits > num_bits) // the result is certainly 0.
+ return *this;
+ int shift = num_bits - div_bits;
+ div <<= shift; // shift so that div and num align.
+ while (shift >= 0) {
+ if (num >= div) {
+ num -= div;
+ pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
+ }
+ div >>= 1; // shift back.
+ shift--;
+ }
+ // num now contains the remainder of the division.
+ return *this;
+}
+
+uint256::uint256(const std::string& str)
+{
+ SetHex(str);
+}
+
+uint256::uint256(const std::vector<unsigned char>& vch)
+{
+ if (vch.size() == sizeof(pn))
+ memcpy(pn, &vch[0], sizeof(pn));
+ else
+ *this = 0;
+}
+
+uint256 GetRandHash()
+{
+ uint256 hash;
+ RAND_bytes(hash.begin(), hash.size());
+ return hash;
+}
#ifndef BITCOIN_UINT256_H
#define BITCOIN_UINT256_H
-#include <limits.h>
-#include <stdio.h>
-#include <string.h>
+#include <limits>
#include <string>
#include <vector>
-#include <stdint.h>
-
-inline int Testuint256AdHoc(std::vector<std::string> vArg);
-
+#include <iomanip>
+#include <sstream>
/** Base class without constructors for uint256 and uint160.
uint32_t pn[WIDTH];
public:
+ const base_uint max()
+ {
+ base_uint ret;
+ for (int i = 0; i < WIDTH; i++)
+ ret.pn[i] = std::numeric_limits<uint32_t>::max();
+ return ret;
+ }
+
bool operator!() const
{
for (int i = 0; i < WIDTH; i++)
std::string GetHex() const
{
- 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);
+ std::stringstream ss;
+ size_t pn_size = sizeof(pn) / sizeof(pn[0]);
+ for (size_t i = 1; i <= pn_size; i++)
+ ss << std::setfill('0') << std::setw(8) << std::setbase(16) << pn[pn_size-i];
+ return ss.str();
}
void SetHex(const char* psz)
return (GetHex());
}
- unsigned char* begin()
+ unsigned char* begin() const
{
return (unsigned char*)&pn[0];
}
- unsigned char* end()
+ unsigned char* end() const
{
return (unsigned char*)&pn[WIDTH];
}
return std::vector<unsigned char>(begin(), end());
}
- size_t size()
+ size_t size() const
{
return sizeof(pn);
}
return sizeof(pn);
}
+ unsigned int bits() const
+ {
+ 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;
+ }
+ return 32 * pos + 1;
+ }
+ }
+ return 0;
+ }
+
template<typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const
{
friend class uint160;
friend class uint256;
- friend inline int Testuint256AdHoc(std::vector<std::string> vArg);
};
typedef base_uint<160> base_uint160;
public:
typedef base_uint160 basetype;
- uint160()
- {
- for (int i = 0; i < WIDTH; i++)
- pn[i] = 0;
- }
-
- uint160(const basetype& b)
- {
- for (int i = 0; i < WIDTH; i++)
- pn[i] = b.pn[i];
- }
-
- uint160& operator=(const basetype& b)
- {
- for (int i = 0; i < WIDTH; i++)
- pn[i] = b.pn[i];
- return *this;
- }
-
- uint160(uint64_t b)
- {
- pn[0] = (uint32_t)b;
- pn[1] = (uint32_t)(b >> 32);
- for (int i = 2; i < WIDTH; i++)
- pn[i] = 0;
- }
-
- uint160& operator=(uint64_t b)
- {
- pn[0] = (uint32_t)b;
- pn[1] = (uint32_t)(b >> 32);
- for (int i = 2; i < WIDTH; i++)
- pn[i] = 0;
- return *this;
- }
-
- explicit uint160(const std::string& str)
- {
- SetHex(str);
- }
-
- explicit uint160(const std::vector<unsigned char>& vch)
- {
- if (vch.size() == sizeof(pn))
- memcpy(pn, &vch[0], sizeof(pn));
- else
- *this = 0;
- }
+ uint160();
+ uint160(const basetype& b);
+ uint160& operator=(const basetype& b);
+ uint160(uint64_t b);
+ uint160& operator=(uint64_t b);
+ explicit uint160(const std::string& str);
+ explicit uint160(const std::vector<unsigned char>& vch);
};
inline bool operator==(const uint160& a, uint64_t b) { return (base_uint160)a == b; }
// uint256
//
+
+/** Errors thrown by the uint256 class */
+class uint256_error : public std::exception
+{
+public:
+ explicit uint256_error(const std::string& error_message) : what_(error_message) {}
+
+ virtual const char* what() const throw () { return what_.c_str(); }
+ virtual ~uint256_error() throw () {}
+private:
+ std::string what_;
+};
+
/** 256-bit unsigned integer */
class uint256 : public base_uint256
{
public:
typedef base_uint256 basetype;
- uint256()
- {
- for (int i = 0; i < WIDTH; i++)
- pn[i] = 0;
- }
-
- uint256(const basetype& b)
- {
- for (int i = 0; i < WIDTH; i++)
- pn[i] = b.pn[i];
- }
-
- uint256& operator=(const basetype& b)
- {
- for (int i = 0; i < WIDTH; i++)
- pn[i] = b.pn[i];
- return *this;
- }
-
- uint256(uint64_t b)
- {
- pn[0] = (uint32_t)b;
- pn[1] = (uint32_t)(b >> 32);
- for (int i = 2; i < WIDTH; i++)
- pn[i] = 0;
- }
-
- uint256& operator=(uint64_t b)
- {
- pn[0] = (uint32_t)b;
- pn[1] = (uint32_t)(b >> 32);
- for (int i = 2; i < WIDTH; i++)
- pn[i] = 0;
- return *this;
- }
-
- explicit uint256(const std::string& str)
- {
- SetHex(str);
- }
-
- explicit uint256(const std::vector<unsigned char>& vch)
- {
- if (vch.size() == sizeof(pn))
- memcpy(pn, &vch[0], sizeof(pn));
- else
- *this = 0;
- }
+ uint256();
+ uint256(const basetype& b);
+ uint256& operator=(const basetype& b);
+ uint256(uint64_t b);
+ uint256& operator=(uint64_t b);
+ uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
+ uint32_t GetCompact(bool fNegative = false) const;
+ uint256& operator*=(uint32_t b32);
+ uint256& operator*=(const uint256& b);
+ uint256& operator/=(const uint256& b);
+ explicit uint256(const std::string& str);
+ explicit uint256(const std::vector<unsigned char>& vch);
};
inline bool operator==(const uint256& a, uint64_t b) { return (base_uint256)a == b; }
inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
+inline const uint256 operator*(const base_uint256& a, const base_uint256& b) { return uint256(a) *= b; }
+inline const uint256 operator/(const base_uint256& a, const base_uint256& b) { return uint256(a) /= b; }
inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
+inline const uint256 operator*(const base_uint256& a, const uint256& b) { return (base_uint256)a * (base_uint256)b; }
+inline const uint256 operator/(const base_uint256& a, const uint256& b) { return (base_uint256)a / (base_uint256)b; }
inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; }
inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
+uint256 GetRandHash();
+
#endif