Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/uint256.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2011 The Bitcoin developers | // Copyright (c) 2011 The Bitcoin developers | ||||
// Distributed under the MIT/X11 software license, see the accompanying | // Distributed under the MIT/X11 software license, see the accompanying | ||||
// file license.txt or http://www.opensource.org/licenses/mit-license.php. | // file license.txt or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_SEEDER_UINT256_H | #ifndef BITCOIN_SEEDER_UINT256_H | ||||
#define BITCOIN_SEEDER_UINT256_H | #define BITCOIN_SEEDER_UINT256_H | ||||
#include "serialize.h" | #include "serialize.h" | ||||
#include <climits> | #include <climits> | ||||
#include <cstdint> | |||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#if defined(_MSC_VER) || defined(__BORLANDC__) | |||||
typedef __int64 int64; | |||||
typedef unsigned __int64 uint64; | |||||
#else | |||||
typedef long long int64; | |||||
typedef unsigned long long uint64; | |||||
#endif | |||||
#if defined(_MSC_VER) && _MSC_VER < 1300 | #if defined(_MSC_VER) && _MSC_VER < 1300 | ||||
#define for if (false); else for | #define for if (false); else for | ||||
#endif | #endif | ||||
inline int Testuint256AdHoc(std::vector<std::string> vArg); | inline int Testuint256AdHoc(std::vector<std::string> vArg); | ||||
// We have to keep a separate base class without constructors | // We have to keep a separate base class without constructors | ||||
// so the compiler will let us use it in a union | // so the compiler will let us use it in a union | ||||
Show All 19 Lines | public: | ||||
const base_uint operator-() const { | const base_uint operator-() const { | ||||
base_uint ret; | base_uint ret; | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
ret.pn[i] = ~pn[i]; | ret.pn[i] = ~pn[i]; | ||||
ret++; | ret++; | ||||
return ret; | return ret; | ||||
} | } | ||||
base_uint &operator=(uint64 b) { | base_uint &operator=(uint64_t b) { | ||||
pn[0] = (unsigned int)b; | pn[0] = (unsigned int)b; | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator^=(const base_uint &b) { | base_uint &operator^=(const base_uint &b) { | ||||
Show All 9 Lines | public: | ||||
} | } | ||||
base_uint &operator|=(const base_uint &b) { | base_uint &operator|=(const base_uint &b) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
pn[i] |= b.pn[i]; | pn[i] |= b.pn[i]; | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator^=(uint64 b) { | base_uint &operator^=(uint64_t b) { | ||||
pn[0] ^= (unsigned int)b; | pn[0] ^= (unsigned int)b; | ||||
pn[1] ^= (unsigned int)(b >> 32); | pn[1] ^= (unsigned int)(b >> 32); | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator&=(uint64 b) { | base_uint &operator&=(uint64_t b) { | ||||
pn[0] &= (unsigned int)b; | pn[0] &= (unsigned int)b; | ||||
pn[1] &= (unsigned int)(b >> 32); | pn[1] &= (unsigned int)(b >> 32); | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator|=(uint64 b) { | base_uint &operator|=(uint64_t b) { | ||||
pn[0] |= (unsigned int)b; | pn[0] |= (unsigned int)b; | ||||
pn[1] |= (unsigned int)(b >> 32); | pn[1] |= (unsigned int)(b >> 32); | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator<<=(unsigned int shift) { | base_uint &operator<<=(unsigned int shift) { | ||||
base_uint a(*this); | base_uint a(*this); | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
Show All 18 Lines | base_uint &operator>>=(unsigned int shift) { | ||||
if (i - k - 1 >= 0 && shift != 0) | if (i - k - 1 >= 0 && shift != 0) | ||||
pn[i - k - 1] |= (a.pn[i] << (32 - shift)); | pn[i - k - 1] |= (a.pn[i] << (32 - shift)); | ||||
if (i - k >= 0) pn[i - k] |= (a.pn[i] >> shift); | if (i - k >= 0) pn[i - k] |= (a.pn[i] >> shift); | ||||
} | } | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator+=(const base_uint &b) { | base_uint &operator+=(const base_uint &b) { | ||||
uint64 carry = 0; | uint64_t carry = 0; | ||||
for (int i = 0; i < WIDTH; i++) { | for (int i = 0; i < WIDTH; i++) { | ||||
uint64 n = carry + pn[i] + b.pn[i]; | uint64_t n = carry + pn[i] + b.pn[i]; | ||||
pn[i] = n & 0xffffffff; | pn[i] = n & 0xffffffff; | ||||
carry = n >> 32; | carry = n >> 32; | ||||
} | } | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator-=(const base_uint &b) { | base_uint &operator-=(const base_uint &b) { | ||||
*this += -b; | *this += -b; | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator+=(uint64 b64) { | base_uint &operator+=(uint64_t b64) { | ||||
base_uint b; | base_uint b; | ||||
b = b64; | b = b64; | ||||
*this += b; | *this += b; | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator-=(uint64 b64) { | base_uint &operator-=(uint64_t b64) { | ||||
base_uint b; | base_uint b; | ||||
b = b64; | b = b64; | ||||
*this += -b; | *this += -b; | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator++() { | base_uint &operator++() { | ||||
// prefix operator | // prefix operator | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | public: | ||||
} | } | ||||
friend inline bool operator==(const base_uint &a, const base_uint &b) { | friend inline bool operator==(const base_uint &a, const base_uint &b) { | ||||
for (int i = 0; i < base_uint::WIDTH; i++) | for (int i = 0; i < base_uint::WIDTH; i++) | ||||
if (a.pn[i] != b.pn[i]) return false; | if (a.pn[i] != b.pn[i]) return false; | ||||
return true; | return true; | ||||
} | } | ||||
friend inline bool operator==(const base_uint &a, uint64 b) { | friend inline bool operator==(const base_uint &a, uint64_t b) { | ||||
if (a.pn[0] != (unsigned int)b) return false; | if (a.pn[0] != (unsigned int)b) return false; | ||||
if (a.pn[1] != (unsigned int)(b >> 32)) return false; | if (a.pn[1] != (unsigned int)(b >> 32)) return false; | ||||
for (int i = 2; i < base_uint::WIDTH; i++) | for (int i = 2; i < base_uint::WIDTH; i++) | ||||
if (a.pn[i] != 0) return false; | if (a.pn[i] != 0) return false; | ||||
return true; | return true; | ||||
} | } | ||||
friend inline bool operator!=(const base_uint &a, const base_uint &b) { | friend inline bool operator!=(const base_uint &a, const base_uint &b) { | ||||
return (!(a == b)); | return (!(a == b)); | ||||
} | } | ||||
friend inline bool operator!=(const base_uint &a, uint64 b) { | friend inline bool operator!=(const base_uint &a, uint64_t b) { | ||||
return (!(a == b)); | return (!(a == b)); | ||||
} | } | ||||
std::string GetHex() const { | std::string GetHex() const { | ||||
char psz[sizeof(pn) * 2 + 1]; | char psz[sizeof(pn) * 2 + 1]; | ||||
for (int i = 0; i < sizeof(pn); i++) | for (int i = 0; i < sizeof(pn); i++) | ||||
sprintf(psz + i * 2, "%02x", ((uint8_t *)pn)[sizeof(pn) - i - 1]); | sprintf(psz + i * 2, "%02x", ((uint8_t *)pn)[sizeof(pn) - i - 1]); | ||||
return std::string(psz, psz + sizeof(pn) * 2); | return std::string(psz, psz + sizeof(pn) * 2); | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | public: | ||||
} | } | ||||
uint160 &operator=(const basetype &b) { | uint160 &operator=(const basetype &b) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
pn[i] = b.pn[i]; | pn[i] = b.pn[i]; | ||||
return *this; | return *this; | ||||
} | } | ||||
uint160(uint64 b) { | uint160(uint64_t b) { | ||||
pn[0] = (unsigned int)b; | pn[0] = (unsigned int)b; | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
} | } | ||||
uint160 &operator=(uint64 b) { | uint160 &operator=(uint64_t b) { | ||||
pn[0] = (unsigned int)b; | pn[0] = (unsigned int)b; | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
return *this; | return *this; | ||||
} | } | ||||
explicit uint160(const std::string &str) { SetHex(str); } | explicit uint160(const std::string &str) { SetHex(str); } | ||||
explicit uint160(const std::vector<uint8_t> &vch) { | explicit uint160(const std::vector<uint8_t> &vch) { | ||||
if (vch.size() == sizeof(pn)) | if (vch.size() == sizeof(pn)) | ||||
memcpy(pn, &vch[0], sizeof(pn)); | memcpy(pn, &vch[0], sizeof(pn)); | ||||
else | else | ||||
*this = 0; | *this = 0; | ||||
} | } | ||||
}; | }; | ||||
inline bool operator==(const uint160 &a, uint64 b) { | inline bool operator==(const uint160 &a, uint64_t b) { | ||||
return (base_uint160)a == b; | return (base_uint160)a == b; | ||||
} | } | ||||
inline bool operator!=(const uint160 &a, uint64 b) { | inline bool operator!=(const uint160 &a, uint64_t b) { | ||||
return (base_uint160)a != b; | return (base_uint160)a != b; | ||||
} | } | ||||
inline const uint160 operator<<(const base_uint160 &a, unsigned int shift) { | inline const uint160 operator<<(const base_uint160 &a, unsigned int shift) { | ||||
return uint160(a) <<= shift; | return uint160(a) <<= shift; | ||||
} | } | ||||
inline const uint160 operator>>(const base_uint160 &a, unsigned int shift) { | inline const uint160 operator>>(const base_uint160 &a, unsigned int shift) { | ||||
return uint160(a) >>= shift; | return uint160(a) >>= shift; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | public: | ||||
} | } | ||||
uint256 &operator=(const basetype &b) { | uint256 &operator=(const basetype &b) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
pn[i] = b.pn[i]; | pn[i] = b.pn[i]; | ||||
return *this; | return *this; | ||||
} | } | ||||
uint256(uint64 b) { | uint256(uint64_t b) { | ||||
pn[0] = (unsigned int)b; | pn[0] = (unsigned int)b; | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
} | } | ||||
uint256 &operator=(uint64 b) { | uint256 &operator=(uint64_t b) { | ||||
pn[0] = (unsigned int)b; | pn[0] = (unsigned int)b; | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
return *this; | return *this; | ||||
} | } | ||||
explicit uint256(const std::string &str) { SetHex(str); } | explicit uint256(const std::string &str) { SetHex(str); } | ||||
explicit uint256(const std::vector<uint8_t> &vch) { | explicit uint256(const std::vector<uint8_t> &vch) { | ||||
if (vch.size() == sizeof(pn)) | if (vch.size() == sizeof(pn)) | ||||
memcpy(pn, &vch[0], sizeof(pn)); | memcpy(pn, &vch[0], sizeof(pn)); | ||||
else | else | ||||
*this = 0; | *this = 0; | ||||
} | } | ||||
}; | }; | ||||
inline bool operator==(const uint256 &a, uint64 b) { | inline bool operator==(const uint256 &a, uint64_t b) { | ||||
return (base_uint256)a == b; | return (base_uint256)a == b; | ||||
} | } | ||||
inline bool operator!=(const uint256 &a, uint64 b) { | inline bool operator!=(const uint256 &a, uint64_t b) { | ||||
return (base_uint256)a != b; | return (base_uint256)a != b; | ||||
} | } | ||||
inline const uint256 operator<<(const base_uint256 &a, unsigned int shift) { | inline const uint256 operator<<(const base_uint256 &a, unsigned int shift) { | ||||
return uint256(a) <<= shift; | return uint256(a) <<= shift; | ||||
} | } | ||||
inline const uint256 operator>>(const base_uint256 &a, unsigned int shift) { | inline const uint256 operator>>(const base_uint256 &a, unsigned int shift) { | ||||
return uint256(a) >>= shift; | return uint256(a) >>= shift; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 252 Lines • Show Last 20 Lines |