Skip to content

Commit

Permalink
Remove CBigNum
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Jul 11, 2019
1 parent e6dd4ff commit 83292d8
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 750 deletions.
78 changes: 64 additions & 14 deletions src/arith_uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,44 @@
#include <stdio.h>
#include <string.h>

template <>
std::string base_uint<256>::GetHex() const
{
return ArithToUint256(*this).GetHex();
}

template <>
void base_uint<256>::SetHex(const char* psz)
{
*this = UintToArith256(uint256S(psz));
}

template <>
std::string base_uint<512>::GetHex() const
{
return ArithToUint512(*this).GetHex();
}

template <>
void base_uint<512>::SetHex(const char* psz)
{
*this = UintToArith512(uint512S(psz));
}

template <unsigned int BITS>
base_uint<BITS>::base_uint(const std::string& str)
{
SetHex(str);
}

template <unsigned int BITS>
base_uint<BITS>::base_uint(const std::vector<unsigned char>& vch)
{
if (vch.size() != sizeof(pn))
throw uint_error("Converting vector of wrong size to base_uint");
memcpy(pn, &vch[0], sizeof(pn));
}

template <unsigned int BITS>
base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift)
{
Expand Down Expand Up @@ -144,18 +176,6 @@ double base_uint<BITS>::getdouble() const
return ret;
}

template <unsigned int BITS>
std::string base_uint<BITS>::GetHex() const
{
return ArithToUint256(*this).GetHex();
}

template <unsigned int BITS>
void base_uint<BITS>::SetHex(const char* psz)
{
*this = UintToArith256(uint256S(psz));
}

template <unsigned int BITS>
void base_uint<BITS>::SetHex(const std::string& str)
{
Expand Down Expand Up @@ -185,6 +205,7 @@ unsigned int base_uint<BITS>::bits() const

// Explicit instantiations for base_uint<256>
template base_uint<256>::base_uint(const std::string&);
template base_uint<256>::base_uint(const std::vector<unsigned char>&);
template base_uint<256>& base_uint<256>::operator<<=(unsigned int);
template base_uint<256>& base_uint<256>::operator>>=(unsigned int);
template base_uint<256>& base_uint<256>::operator*=(uint32_t b32);
Expand All @@ -193,12 +214,25 @@ template base_uint<256>& base_uint<256>::operator/=(const base_uint<256>& b);
template int base_uint<256>::CompareTo(const base_uint<256>&) const;
template bool base_uint<256>::EqualTo(uint64_t) const;
template double base_uint<256>::getdouble() const;
template std::string base_uint<256>::GetHex() const;
template std::string base_uint<256>::ToString() const;
template void base_uint<256>::SetHex(const char*);
template void base_uint<256>::SetHex(const std::string&);
template unsigned int base_uint<256>::bits() const;

// Explicit instantiations for base_uint<512>
template base_uint<512>::base_uint(const std::string&);
template base_uint<512>::base_uint(const std::vector<unsigned char>&);
template base_uint<512>& base_uint<512>::operator<<=(unsigned int);
template base_uint<512>& base_uint<512>::operator>>=(unsigned int);
template base_uint<512>& base_uint<512>::operator*=(uint32_t b32);
template base_uint<512>& base_uint<512>::operator*=(const base_uint<512>& b);
template base_uint<512>& base_uint<512>::operator/=(const base_uint<512>& b);
template int base_uint<512>::CompareTo(const base_uint<512>&) const;
template bool base_uint<512>::EqualTo(uint64_t) const;
template double base_uint<512>::getdouble() const;
template std::string base_uint<512>::ToString() const;
template void base_uint<512>::SetHex(const std::string&);
template unsigned int base_uint<512>::bits() const;

// This implementation directly uses shifts instead of going
// through an intermediate MPI representation.
arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)
Expand Down Expand Up @@ -258,3 +292,19 @@ arith_uint256 UintToArith256(const uint256 &a)
b.pn[x] = ReadLE32(a.begin() + x*4);
return b;
}

uint512 ArithToUint512(const arith_uint512& a)
{
uint512 b;
for (int x = 0; x < a.WIDTH; ++x)
WriteLE32(b.begin() + x * 4, a.pn[x]);
return b;
}

arith_uint512 UintToArith512(const uint512& a)
{
arith_uint512 b;
for (int x = 0; x < b.WIDTH; ++x)
b.pn[x] = ReadLE32(a.begin() + x * 4);
return b;
}
51 changes: 45 additions & 6 deletions src/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string>
#include <vector>

class uint512;
class uint256;

class uint_error : public std::runtime_error {
Expand Down Expand Up @@ -57,6 +58,7 @@ class base_uint
}

explicit base_uint(const std::string& str);
explicit base_uint(const std::vector<unsigned char>& vch);

bool operator!() const
{
Expand Down Expand Up @@ -231,6 +233,26 @@ class base_uint
void SetHex(const std::string& str);
std::string ToString() const;

unsigned char* begin()
{
return (unsigned char*)&pn[0];
}

unsigned char* end()
{
return (unsigned char*)&pn[WIDTH];
}

const unsigned char* begin() const
{
return (unsigned char*)&pn[0];
}

const unsigned char* end() const
{
return (unsigned char*)&pn[WIDTH];
}

unsigned int size() const
{
return sizeof(pn);
Expand Down Expand Up @@ -273,6 +295,7 @@ class arith_uint256 : public base_uint<256> {
arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
arith_uint256(uint64_t b) : base_uint<256>(b) {}
explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
explicit arith_uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}

/**
* The "compact" format is a representation of a whole
Expand All @@ -297,14 +320,30 @@ class arith_uint256 : public base_uint<256> {
arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
uint32_t GetCompact(bool fNegative = false) const;

void SetHex(const char* psz){};
void SetHex(const std::string& str){};
friend uint256 ArithToUint256(const arith_uint256&);
friend arith_uint256 UintToArith256(const uint256&);
uint64_t GetHash(const arith_uint256& salt) const;
};

/** 512-bit unsigned big integer. */
class arith_uint512 : public base_uint<512>
{
public:
arith_uint512() {}
arith_uint512(const base_uint<512>& b) : base_uint<512>(b) {}
arith_uint512(uint64_t b) : base_uint<512>(b) {}
explicit arith_uint512(const std::string& str) : base_uint<512>(str) {}
explicit arith_uint512(const std::vector<unsigned char>& vch) : base_uint<512>(vch) {}

uint64_t GetHash(const arith_uint256& salt) const;

friend uint256 ArithToUint256(const arith_uint256 &);
friend arith_uint256 UintToArith256(const uint256 &);
friend arith_uint512 UintToArith512(const uint512& a);
friend uint512 ArithToUint512(const arith_uint512& a);
};

uint256 ArithToUint256(const arith_uint256 &);
arith_uint256 UintToArith256(const uint256 &);
uint256 ArithToUint256(const arith_uint256&);
arith_uint256 UintToArith256(const uint256&);
uint512 ArithToUint512(const arith_uint512&);
arith_uint512 UintToArith512(const uint512&);

#endif // BITCOIN_ARITH_UINT256_H
Loading

0 comments on commit 83292d8

Please sign in to comment.