// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2013 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+// file COPYING or https://www.opensource.org/licenses/mit-license.php .
#ifndef BITCOIN_TEST_BIGNUM_H
#define BITCOIN_TEST_BIGNUM_H
#include <algorithm>
+#include <cassert>
#include <limits>
#include <stdexcept>
#include <stdint.h>
/** C++ wrapper for BIGNUM (OpenSSL bignum) */
-class CBigNum : public BIGNUM
+class CBigNum
{
+ BIGNUM* bn;
public:
CBigNum()
{
- BN_init(this);
+ bn = BN_new();
+ assert(bn);
}
CBigNum(const CBigNum& b)
{
- BN_init(this);
- if (!BN_copy(this, &b))
+ bn = BN_new();
+ assert(bn);
+ if (!BN_copy(bn, b.bn))
{
- BN_clear_free(this);
- throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
+ BN_clear_free(bn);
+ throw bignum_error("CBigNum::CBigNum(const CBigNum&): BN_copy failed");
}
}
CBigNum& operator=(const CBigNum& b)
{
- if (!BN_copy(this, &b))
- throw bignum_error("CBigNum::operator= : BN_copy failed");
+ if (!BN_copy(bn, b.bn))
+ throw bignum_error("CBigNum::operator=: BN_copy failed");
return (*this);
}
~CBigNum()
{
- BN_clear_free(this);
+ BN_clear_free(bn);
}
- CBigNum(long long n) { BN_init(this); setint64(n); }
+ CBigNum(long long n) { bn = BN_new(); assert(bn); setint64(n); }
explicit CBigNum(const std::vector<unsigned char>& vch)
{
- BN_init(this);
+ bn = BN_new();
+ assert(bn);
setvch(vch);
}
int getint() const
{
- BN_ULONG n = BN_get_word(this);
- if (!BN_is_negative(this))
+ BN_ULONG n = BN_get_word(bn);
+ if (!BN_is_negative(bn))
return (n > (BN_ULONG)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
else
return (n > (BN_ULONG)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
pch[1] = (nSize >> 16) & 0xff;
pch[2] = (nSize >> 8) & 0xff;
pch[3] = (nSize) & 0xff;
- BN_mpi2bn(pch, p - pch, this);
+ BN_mpi2bn(pch, p - pch, bn);
}
void setvch(const std::vector<unsigned char>& vch)
vch2[3] = (nSize >> 0) & 0xff;
// swap data to big endian
reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
- BN_mpi2bn(&vch2[0], vch2.size(), this);
+ BN_mpi2bn(&vch2[0], vch2.size(), bn);
}
std::vector<unsigned char> getvch() const
{
- unsigned int nSize = BN_bn2mpi(this, NULL);
+ unsigned int nSize = BN_bn2mpi(bn, NULL);
if (nSize <= 4)
return std::vector<unsigned char>();
std::vector<unsigned char> vch(nSize);
- BN_bn2mpi(this, &vch[0]);
+ BN_bn2mpi(bn, &vch[0]);
vch.erase(vch.begin(), vch.begin() + 4);
reverse(vch.begin(), vch.end());
return vch;
}
+ friend inline const CBigNum operator+(const CBigNum& a, const CBigNum& b);
friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
+ friend inline const CBigNum operator-(const CBigNum& a);
+ friend inline bool operator==(const CBigNum& a, const CBigNum& b);
+ friend inline bool operator!=(const CBigNum& a, const CBigNum& b);
+ friend inline bool operator<=(const CBigNum& a, const CBigNum& b);
+ friend inline bool operator>=(const CBigNum& a, const CBigNum& b);
+ friend inline bool operator<(const CBigNum& a, const CBigNum& b);
+ friend inline bool operator>(const CBigNum& a, const CBigNum& b);
};
inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
{
CBigNum r;
- if (!BN_add(&r, &a, &b))
- throw bignum_error("CBigNum::operator+ : BN_add failed");
+ if (!BN_add(r.bn, a.bn, b.bn))
+ throw bignum_error("CBigNum::operator+: BN_add failed");
return r;
}
inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
{
CBigNum r;
- if (!BN_sub(&r, &a, &b))
- throw bignum_error("CBigNum::operator- : BN_sub failed");
+ if (!BN_sub(r.bn, a.bn, b.bn))
+ throw bignum_error("CBigNum::operator-: BN_sub failed");
return r;
}
inline const CBigNum operator-(const CBigNum& a)
{
CBigNum r(a);
- BN_set_negative(&r, !BN_is_negative(&r));
+ BN_set_negative(r.bn, !BN_is_negative(r.bn));
return r;
}
-inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
-inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
-inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
-inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
-inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); }
-inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); }
+inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) == 0); }
+inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) != 0); }
+inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) <= 0); }
+inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) >= 0); }
+inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) < 0); }
+inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) > 0); }
#endif // BITCOIN_TEST_BIGNUM_H