Skip to content

Commit

Permalink
Merge branch 'master' into feature/loan_payback_dfi
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl authored Jan 24, 2022
2 parents 03c8e1c + 4fefd57 commit 2d59e5c
Show file tree
Hide file tree
Showing 25 changed files with 877 additions and 223 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ jobs:
TRAVIS_BUILD_DIR: ${{ github.workspace }}
TRAVIS_COMMIT: ${{ github.sha }}
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Checkout base branch and/or merge
if: github.event_name != 'pull_request'
uses: actions/checkout@v2

- name: Checkout pull request head commit
if: github.event_name == 'pull_request'
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}

# This script has been modified to pipe variables to GITHUB_ENV, which allows them to be
# used by subsequent steps.
Expand Down
56 changes: 39 additions & 17 deletions src/arith_uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

#include <arith_uint256.h>

#include <util/strencodings.h>
#include <uint256.h>
#include <crypto/common.h>

#include <stdio.h>
#include <string.h>

template<unsigned int BITS>
constexpr int base_uint<BITS>::WIDTH;

template <unsigned int BITS>
base_uint<BITS>::base_uint(const std::string& str)
{
Expand Down Expand Up @@ -65,6 +69,27 @@ base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
return *this;
}

template <unsigned int BITS>
base_uint<BITS>& base_uint<BITS>::operator*=(int32_t b32)
{
(*this) *= uint32_t(b32);
return *this;
}

template <unsigned int BITS>
base_uint<BITS>& base_uint<BITS>::operator*=(int64_t b64)
{
(*this) *= base_uint(b64);
return *this;
}

template <unsigned int BITS>
base_uint<BITS>& base_uint<BITS>::operator*=(uint64_t b64)
{
(*this) *= base_uint(b64);
return *this;
}

template <unsigned int BITS>
base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
{
Expand Down Expand Up @@ -148,13 +173,21 @@ double base_uint<BITS>::getdouble() const
template <unsigned int BITS>
std::string base_uint<BITS>::GetHex() const
{
return ArithToUint256(*this).GetHex();
base_blob<BITS> b;

for(int x=0; x<WIDTH; ++x)
WriteLE32(b.begin() + x*4, pn[x]);
return b.GetHex();
}

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

a.SetHex(psz);
for(int x=0; x<WIDTH; ++x)
pn[x] = ReadLE32(a.begin() + x*4);
}

template <unsigned int BITS>
Expand Down Expand Up @@ -207,22 +240,11 @@ base_uint<BITS> base_uint<BITS>::sqrt() const
return res;
}

// Explicit instantiations for base_uint<128>
template class base_uint<128>;

// Explicit instantiations for base_uint<256>
template base_uint<256>::base_uint(const std::string&);
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);
template base_uint<256>& base_uint<256>::operator*=(const base_uint<256>& b);
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;
template base_uint<256> base_uint<256>::sqrt() const;
template class base_uint<256>;

// This implementation directly uses shifts instead of going
// through an intermediate MPI representation.
Expand Down
54 changes: 45 additions & 9 deletions src/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class base_uint
uint32_t pn[WIDTH];
public:

template<unsigned int BITS1> friend class base_uint;

base_uint()
{
static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
Expand All @@ -42,15 +44,7 @@ class base_uint
{
static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");

for (int i = 0; i < WIDTH; i++)
pn[i] = b.pn[i];
}

base_uint& operator=(const base_uint& b)
{
for (int i = 0; i < WIDTH; i++)
pn[i] = b.pn[i];
return *this;
(*this) = b;
}

base_uint(uint64_t b)
Expand All @@ -63,6 +57,12 @@ class base_uint
pn[i] = 0;
}

template<unsigned int BITS1>
base_uint(const base_uint<BITS1>& b)
{
(*this) = b;
}

explicit base_uint(const std::string& str);

const base_uint operator~() const
Expand All @@ -84,6 +84,24 @@ class base_uint

double getdouble() const;

template<unsigned int BITS1>
base_uint& operator=(const base_uint<BITS1>& b)
{
auto width = std::min(WIDTH, base_uint<BITS1>::WIDTH);
for (int i = 0; i < width; i++)
pn[i] = b.pn[i];
for (int i = width; i < WIDTH; i++)
pn[i] = 0;
return *this;
}

base_uint& operator=(const base_uint& b)
{
for (int i = 0; i < WIDTH; i++)
pn[i] = b.pn[i];
return *this;
}

base_uint& operator=(uint64_t b)
{
pn[0] = (unsigned int)b;
Expand Down Expand Up @@ -165,7 +183,10 @@ class base_uint
return *this;
}

base_uint& operator*=(int32_t b32);
base_uint& operator*=(uint32_t b32);
base_uint& operator*=(int64_t b64);
base_uint& operator*=(uint64_t b64);
base_uint& operator*=(const base_uint& b);
base_uint& operator/=(const base_uint& b);

Expand Down Expand Up @@ -215,7 +236,10 @@ class base_uint
friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
friend inline const base_uint operator*(const base_uint& a, int32_t b) { return base_uint(a) *= b; }
friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
friend inline const base_uint operator*(const base_uint& a, int64_t b) { return base_uint(a) *= b; }
friend inline const base_uint operator*(const base_uint& a, uint64_t b) { return base_uint(a) *= b; }
friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
Expand Down Expand Up @@ -248,6 +272,18 @@ class base_uint
static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
return pn[0] | (uint64_t)pn[1] << 32;
}

template<typename Stream>
void Serialize(Stream& s) const
{
s.write((char*)pn, sizeof(pn));
}

template<typename Stream>
void Unserialize(Stream& s)
{
s.read((char*)pn, sizeof(pn));
}
};

/** 256-bit unsigned big integer. */
Expand Down
Loading

0 comments on commit 2d59e5c

Please sign in to comment.