Skip to content

Commit

Permalink
Migration process: uint256 main methods removed, using arith_uint256 …
Browse files Browse the repository at this point in the history
…for now.

Once all of the invalidly instantiated fields using uint256 as a number get migrated to the arith_uint256, the uint256 file will be replaced with the blob_uint256 file.
  • Loading branch information
furszy committed Mar 15, 2020
1 parent d32999f commit 1ae7342
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 629 deletions.
49 changes: 18 additions & 31 deletions src/arith_uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ std::string base_uint<BITS>::ToString() const
return (GetHex());
}

template <unsigned int BITS>
std::string base_uint<BITS>::ToStringReverseEndian() const
{
char psz[sizeof(pn) * 2 + 1];
for (unsigned int i = 0; i < sizeof(pn); i++)
sprintf(psz + i * 2, "%02x", ((unsigned char*)pn)[i]);
return std::string(psz, psz + sizeof(pn) * 2);
}

template <unsigned int BITS>
unsigned int base_uint<BITS>::bits() const
{
Expand Down Expand Up @@ -249,6 +258,15 @@ 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;
template std::string base_uint<256>::ToStringReverseEndian() const;

// Explicit instantiations for base_uint<512>
template base_uint<512>::base_uint(const std::string&);
template base_uint<512>& base_uint<512>::operator<<=(unsigned int);
template base_uint<512>& base_uint<512>::operator>>=(unsigned int);
template std::string base_uint<512>::GetHex() const;
template std::string base_uint<512>::ToString() const;
template std::string base_uint<512>::ToStringReverseEndian() const;

// This implementation directly uses shifts instead of going
// through an intermediate MPI representation.
Expand Down Expand Up @@ -355,35 +373,4 @@ uint64_t arith_uint256::GetHash(const arith_uint256& salt) const
HashFinal(a, b, c);

return ((((uint64_t)b) << 32) | c);
}

blob_uint256 ArithToUint256(const arith_uint256 &a)
{
blob_uint256 b;
for(int x=0; x<a.WIDTH; ++x)
WriteLE32(b.begin() + x*4, a.pn[x]);
return b;
}
arith_uint256 UintToArith256(const blob_uint256 &a)
{
arith_uint256 b;
for(int x=0; x<b.WIDTH; ++x)
b.pn[x] = ReadLE32(a.begin() + x*4);
return b;
}

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

arith_uint512 UintToArith512(const blob_uint512 &a)
{
arith_uint512 b;
for(int x=0; x<b.WIDTH; ++x)
b.pn[x] = ReadLE32(a.begin() + x*4);
return b;
}
31 changes: 25 additions & 6 deletions src/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

class blob_uint512;
class blob_uint256;
class uint256;
class uint512;

class uint_error : public std::runtime_error {
public:
Expand Down Expand Up @@ -233,6 +235,7 @@ class base_uint
void SetHex(const char* psz);
void SetHex(const std::string& str);
std::string ToString() const;
std::string ToStringReverseEndian() const;

unsigned char* begin()
{
Expand All @@ -259,6 +262,15 @@ class base_uint
return sizeof(pn);
}

uint64_t Get64(int n = 0) const
{
return pn[2 * n] | (uint64_t)pn[2 * n + 1] << 32;
}

uint32_t Get32(int n = 0) const
{
return pn[2 * n];
}
/**
* Returns the position of the highest bit set plus one, or zero if the
* value is zero.
Expand Down Expand Up @@ -304,6 +316,14 @@ class base_uint
return false;
return true;
}

friend class uint160;
friend class uint256;
friend class uint512;

friend class arith_uint160;
friend class arith_uint256;
friend class arith_uint512;
};

/** 160-bit unsigned big integer. */
Expand Down Expand Up @@ -364,15 +384,14 @@ class arith_uint512 : public base_uint<512> {

uint64_t GetHash(const arith_uint256& salt) const;

friend arith_uint512 UintToArith512(const blob_uint512 &a);
friend blob_uint512 ArithToUint512(const arith_uint512 &a);
//friend arith_uint512 UintToArith512(const blob_uint512 &a);
//friend blob_uint512 ArithToUint512(const arith_uint512 &a);

};

blob_uint256 ArithToUint256(const arith_uint256 &);
arith_uint256 UintToArith256(const blob_uint256 &);
blob_uint512 ArithToUint512(const arith_uint512 &);
arith_uint512 UintToArith512(const blob_uint512 &);
/** Old classes definitions */

/** End classes definitions */

const arith_uint256 ARITH_UINT256_ZERO = arith_uint256();

Expand Down
Loading

0 comments on commit 1ae7342

Please sign in to comment.