|
|
@ -48,75 +48,83 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** C++ wrapper for BIGNUM (OpenSSL bignum) */ |
|
|
|
/** C++ wrapper for BIGNUM (OpenSSL bignum) */ |
|
|
|
class CBigNum : public BIGNUM |
|
|
|
class CBigNum |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BIGNUM * bn; |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BIGNUM * GetBN () { return bn; }; |
|
|
|
|
|
|
|
const BIGNUM * GetBN () const { return bn; }; |
|
|
|
|
|
|
|
|
|
|
|
CBigNum() |
|
|
|
CBigNum() |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_init(this); |
|
|
|
bn = BN_new (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CBigNum(const CBigNum& b) |
|
|
|
CBigNum(const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_init(this); |
|
|
|
bn = BN_new (); |
|
|
|
if (!BN_copy(this, &b)) |
|
|
|
if (!BN_copy(bn, b.bn)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_clear_free(this); |
|
|
|
BN_free(bn); bn = nullptr; |
|
|
|
throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); |
|
|
|
throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CBigNum& operator=(const CBigNum& b) |
|
|
|
CBigNum& operator=(const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!BN_copy(this, &b)) |
|
|
|
if (!BN_copy(bn, b.bn)) |
|
|
|
throw bignum_error("CBigNum::operator= : BN_copy failed"); |
|
|
|
throw bignum_error("CBigNum::operator= : BN_copy failed"); |
|
|
|
return (*this); |
|
|
|
return (*this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
~CBigNum() |
|
|
|
~CBigNum() |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_clear_free(this); |
|
|
|
BN_free(bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'.
|
|
|
|
//CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'.
|
|
|
|
CBigNum(signed char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(signed char n) { bn = BN_new (); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(short n) { bn = BN_new (); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(int n) { bn = BN_new (); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(long n) { bn = BN_new (); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(int64 n) { BN_init(this); setint64(n); } |
|
|
|
CBigNum(int64 n) { bn = BN_new (); setint64(n); } |
|
|
|
CBigNum(unsigned char n) { BN_init(this); setulong(n); } |
|
|
|
CBigNum(unsigned char n) { bn = BN_new (); setulong(n); } |
|
|
|
CBigNum(unsigned short n) { BN_init(this); setulong(n); } |
|
|
|
CBigNum(unsigned short n) { bn = BN_new (); setulong(n); } |
|
|
|
CBigNum(unsigned int n) { BN_init(this); setulong(n); } |
|
|
|
CBigNum(unsigned int n) { bn = BN_new (); setulong(n); } |
|
|
|
CBigNum(unsigned long n) { BN_init(this); setulong(n); } |
|
|
|
CBigNum(unsigned long n) { bn = BN_new (); setulong(n); } |
|
|
|
CBigNum(uint64 n) { BN_init(this); setuint64(n); } |
|
|
|
CBigNum(uint64 n) { bn = BN_new (); setuint64(n); } |
|
|
|
explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); } |
|
|
|
explicit CBigNum(uint256 n) { bn = BN_new (); setuint256(n); } |
|
|
|
|
|
|
|
|
|
|
|
explicit CBigNum(const std::vector<unsigned char>& vch) |
|
|
|
explicit CBigNum(const std::vector<unsigned char>& vch) |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_init(this); |
|
|
|
bn = BN_new (); |
|
|
|
setvch(vch); |
|
|
|
setvch(vch); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setulong(unsigned long n) |
|
|
|
void setulong(unsigned long n) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!BN_set_word(this, n)) |
|
|
|
if (!BN_set_word(bn, n)) |
|
|
|
throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed"); |
|
|
|
throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned long getulong() const |
|
|
|
unsigned long getulong() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
return BN_get_word(this); |
|
|
|
return BN_get_word(bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned int getuint() const |
|
|
|
unsigned int getuint() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
return BN_get_word(this); |
|
|
|
return BN_get_word(bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int getint() const |
|
|
|
int getint() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned long n = BN_get_word(this); |
|
|
|
unsigned long n = BN_get_word(bn); |
|
|
|
if (!BN_is_negative(this)) |
|
|
|
if (!BN_is_negative(bn)) |
|
|
|
return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n); |
|
|
|
return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n); |
|
|
|
else |
|
|
|
else |
|
|
|
return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n); |
|
|
|
return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n); |
|
|
@ -164,7 +172,7 @@ public: |
|
|
|
pch[1] = (nSize >> 16) & 0xff; |
|
|
|
pch[1] = (nSize >> 16) & 0xff; |
|
|
|
pch[2] = (nSize >> 8) & 0xff; |
|
|
|
pch[2] = (nSize >> 8) & 0xff; |
|
|
|
pch[3] = (nSize) & 0xff; |
|
|
|
pch[3] = (nSize) & 0xff; |
|
|
|
BN_mpi2bn(pch, p - pch, this); |
|
|
|
BN_mpi2bn(pch, p - pch, bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setuint64(uint64 n) |
|
|
|
void setuint64(uint64 n) |
|
|
@ -191,7 +199,7 @@ public: |
|
|
|
pch[1] = (nSize >> 16) & 0xff; |
|
|
|
pch[1] = (nSize >> 16) & 0xff; |
|
|
|
pch[2] = (nSize >> 8) & 0xff; |
|
|
|
pch[2] = (nSize >> 8) & 0xff; |
|
|
|
pch[3] = (nSize) & 0xff; |
|
|
|
pch[3] = (nSize) & 0xff; |
|
|
|
BN_mpi2bn(pch, p - pch, this); |
|
|
|
BN_mpi2bn(pch, p - pch, bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setuint256(uint256 n) |
|
|
|
void setuint256(uint256 n) |
|
|
@ -219,16 +227,16 @@ public: |
|
|
|
pch[1] = (nSize >> 16) & 0xff; |
|
|
|
pch[1] = (nSize >> 16) & 0xff; |
|
|
|
pch[2] = (nSize >> 8) & 0xff; |
|
|
|
pch[2] = (nSize >> 8) & 0xff; |
|
|
|
pch[3] = (nSize >> 0) & 0xff; |
|
|
|
pch[3] = (nSize >> 0) & 0xff; |
|
|
|
BN_mpi2bn(pch, p - pch, this); |
|
|
|
BN_mpi2bn(pch, p - pch, bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint256 getuint256() const |
|
|
|
uint256 getuint256() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int nSize = BN_bn2mpi(this, NULL); |
|
|
|
unsigned int nSize = BN_bn2mpi(bn, NULL); |
|
|
|
if (nSize < 4) |
|
|
|
if (nSize < 4) |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
std::vector<unsigned char> vch(nSize); |
|
|
|
std::vector<unsigned char> vch(nSize); |
|
|
|
BN_bn2mpi(this, &vch[0]); |
|
|
|
BN_bn2mpi(bn, &vch[0]); |
|
|
|
if (vch.size() > 4) |
|
|
|
if (vch.size() > 4) |
|
|
|
vch[4] &= 0x7f; |
|
|
|
vch[4] &= 0x7f; |
|
|
|
uint256 n = 0; |
|
|
|
uint256 n = 0; |
|
|
@ -249,16 +257,16 @@ public: |
|
|
|
vch2[3] = (nSize >> 0) & 0xff; |
|
|
|
vch2[3] = (nSize >> 0) & 0xff; |
|
|
|
// swap data to big endian
|
|
|
|
// swap data to big endian
|
|
|
|
reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4); |
|
|
|
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 |
|
|
|
std::vector<unsigned char> getvch() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int nSize = BN_bn2mpi(this, NULL); |
|
|
|
unsigned int nSize = BN_bn2mpi(bn, NULL); |
|
|
|
if (nSize <= 4) |
|
|
|
if (nSize <= 4) |
|
|
|
return std::vector<unsigned char>(); |
|
|
|
return std::vector<unsigned char>(); |
|
|
|
std::vector<unsigned char> vch(nSize); |
|
|
|
std::vector<unsigned char> vch(nSize); |
|
|
|
BN_bn2mpi(this, &vch[0]); |
|
|
|
BN_bn2mpi(bn, &vch[0]); |
|
|
|
vch.erase(vch.begin(), vch.begin() + 4); |
|
|
|
vch.erase(vch.begin(), vch.begin() + 4); |
|
|
|
reverse(vch.begin(), vch.end()); |
|
|
|
reverse(vch.begin(), vch.end()); |
|
|
|
return vch; |
|
|
|
return vch; |
|
|
@ -294,28 +302,28 @@ public: |
|
|
|
if (nSize <= 3) |
|
|
|
if (nSize <= 3) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nWord >>= 8*(3-nSize); |
|
|
|
nWord >>= 8*(3-nSize); |
|
|
|
BN_set_word(this, nWord); |
|
|
|
BN_set_word(bn, nWord); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_set_word(this, nWord); |
|
|
|
BN_set_word(bn, nWord); |
|
|
|
BN_lshift(this, this, 8*(nSize-3)); |
|
|
|
BN_lshift(bn, bn, 8*(nSize-3)); |
|
|
|
} |
|
|
|
} |
|
|
|
BN_set_negative(this, fNegative); |
|
|
|
BN_set_negative(bn, fNegative); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned int GetCompact() const |
|
|
|
unsigned int GetCompact() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int nSize = BN_num_bytes(this); |
|
|
|
unsigned int nSize = BN_num_bytes(bn); |
|
|
|
unsigned int nCompact = 0; |
|
|
|
unsigned int nCompact = 0; |
|
|
|
if (nSize <= 3) |
|
|
|
if (nSize <= 3) |
|
|
|
nCompact = BN_get_word(this) << 8*(3-nSize); |
|
|
|
nCompact = BN_get_word(bn) << 8*(3-nSize); |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
CBigNum bn; |
|
|
|
CBigNum bn1; |
|
|
|
BN_rshift(&bn, this, 8*(nSize-3)); |
|
|
|
BN_rshift(bn1.bn, bn, 8*(nSize-3)); |
|
|
|
nCompact = BN_get_word(&bn); |
|
|
|
nCompact = BN_get_word(bn1.bn); |
|
|
|
} |
|
|
|
} |
|
|
|
// The 0x00800000 bit denotes the sign.
|
|
|
|
// The 0x00800000 bit denotes the sign.
|
|
|
|
// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
|
|
|
|
// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
|
|
|
@ -325,7 +333,7 @@ public: |
|
|
|
nSize++; |
|
|
|
nSize++; |
|
|
|
} |
|
|
|
} |
|
|
|
nCompact |= nSize << 24; |
|
|
|
nCompact |= nSize << 24; |
|
|
|
nCompact |= (BN_is_negative(this) ? 0x00800000 : 0); |
|
|
|
nCompact |= (BN_is_negative(bn) ? 0x00800000 : 0); |
|
|
|
return nCompact; |
|
|
|
return nCompact; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -365,21 +373,21 @@ public: |
|
|
|
CBigNum bnBase = nBase; |
|
|
|
CBigNum bnBase = nBase; |
|
|
|
CBigNum bn0 = 0; |
|
|
|
CBigNum bn0 = 0; |
|
|
|
std::string str; |
|
|
|
std::string str; |
|
|
|
CBigNum bn = *this; |
|
|
|
CBigNum bn_copy = *this; |
|
|
|
BN_set_negative(&bn, false); |
|
|
|
BN_set_negative(bn_copy.bn, false); |
|
|
|
CBigNum dv; |
|
|
|
CBigNum dv; |
|
|
|
CBigNum rem; |
|
|
|
CBigNum rem; |
|
|
|
if (BN_cmp(&bn, &bn0) == 0) |
|
|
|
if (BN_cmp(bn_copy.bn, bn0.bn) == 0) |
|
|
|
return "0"; |
|
|
|
return "0"; |
|
|
|
while (BN_cmp(&bn, &bn0) > 0) |
|
|
|
while (BN_cmp(bn_copy.bn, bn0.bn) > 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) |
|
|
|
if (!BN_div(dv.bn, rem.bn, bn_copy.bn, bnBase.bn, pctx)) |
|
|
|
throw bignum_error("CBigNum::ToString() : BN_div failed"); |
|
|
|
throw bignum_error("CBigNum::ToString() : BN_div failed"); |
|
|
|
bn = dv; |
|
|
|
bn_copy = dv; |
|
|
|
unsigned int c = rem.getulong(); |
|
|
|
unsigned int c = rem.getulong(); |
|
|
|
str += "0123456789abcdef"[c]; |
|
|
|
str += "0123456789abcdef"[c]; |
|
|
|
} |
|
|
|
} |
|
|
|
if (BN_is_negative(this)) |
|
|
|
if (BN_is_negative(bn)) |
|
|
|
str += "-"; |
|
|
|
str += "-"; |
|
|
|
reverse(str.begin(), str.end()); |
|
|
|
reverse(str.begin(), str.end()); |
|
|
|
return str; |
|
|
|
return str; |
|
|
@ -412,12 +420,12 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
bool operator!() const |
|
|
|
bool operator!() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
return BN_is_zero(this); |
|
|
|
return BN_is_zero(bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CBigNum& operator+=(const CBigNum& b) |
|
|
|
CBigNum& operator+=(const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!BN_add(this, this, &b)) |
|
|
|
if (!BN_add(bn, bn, b.bn)) |
|
|
|
throw bignum_error("CBigNum::operator+= : BN_add failed"); |
|
|
|
throw bignum_error("CBigNum::operator+= : BN_add failed"); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
@ -431,7 +439,7 @@ public: |
|
|
|
CBigNum& operator*=(const CBigNum& b) |
|
|
|
CBigNum& operator*=(const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CAutoBN_CTX pctx; |
|
|
|
CAutoBN_CTX pctx; |
|
|
|
if (!BN_mul(this, this, &b, pctx)) |
|
|
|
if (!BN_mul(bn, bn, b.bn, pctx)) |
|
|
|
throw bignum_error("CBigNum::operator*= : BN_mul failed"); |
|
|
|
throw bignum_error("CBigNum::operator*= : BN_mul failed"); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
@ -450,7 +458,7 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
CBigNum& operator<<=(unsigned int shift) |
|
|
|
CBigNum& operator<<=(unsigned int shift) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!BN_lshift(this, this, shift)) |
|
|
|
if (!BN_lshift(bn, bn, shift)) |
|
|
|
throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); |
|
|
|
throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
@ -461,13 +469,13 @@ public: |
|
|
|
// if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL
|
|
|
|
// if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL
|
|
|
|
CBigNum a = 1; |
|
|
|
CBigNum a = 1; |
|
|
|
a <<= shift; |
|
|
|
a <<= shift; |
|
|
|
if (BN_cmp(&a, this) > 0) |
|
|
|
if (BN_cmp(a.bn, bn) > 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
*this = 0; |
|
|
|
*this = 0; |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!BN_rshift(this, this, shift)) |
|
|
|
if (!BN_rshift(bn, bn, shift)) |
|
|
|
throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); |
|
|
|
throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
@ -476,7 +484,7 @@ public: |
|
|
|
CBigNum& operator++() |
|
|
|
CBigNum& operator++() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// prefix operator
|
|
|
|
// prefix operator
|
|
|
|
if (!BN_add(this, this, BN_value_one())) |
|
|
|
if (!BN_add(bn, bn, BN_value_one())) |
|
|
|
throw bignum_error("CBigNum::operator++ : BN_add failed"); |
|
|
|
throw bignum_error("CBigNum::operator++ : BN_add failed"); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
@ -493,7 +501,7 @@ public: |
|
|
|
{ |
|
|
|
{ |
|
|
|
// prefix operator
|
|
|
|
// prefix operator
|
|
|
|
CBigNum r; |
|
|
|
CBigNum r; |
|
|
|
if (!BN_sub(&r, this, BN_value_one())) |
|
|
|
if (!BN_sub(r.bn, bn, BN_value_one())) |
|
|
|
throw bignum_error("CBigNum::operator-- : BN_sub failed"); |
|
|
|
throw bignum_error("CBigNum::operator-- : BN_sub failed"); |
|
|
|
*this = r; |
|
|
|
*this = r; |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
@ -507,10 +515,13 @@ public: |
|
|
|
return ret; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, const CBigNum& b); |
|
|
|
|
|
|
|
friend inline const CBigNum operator-(const CBigNum& a); |
|
|
|
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, const CBigNum& b); |
|
|
|
friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b); |
|
|
|
|
|
|
|
friend inline const CBigNum operator<<(const CBigNum& a, unsigned int shift); |
|
|
|
|
|
|
|
friend inline const CBigNum operator*(const CBigNum& a, const CBigNum& b); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -518,7 +529,7 @@ public: |
|
|
|
inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) |
|
|
|
inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CBigNum r; |
|
|
|
CBigNum r; |
|
|
|
if (!BN_add(&r, &a, &b)) |
|
|
|
if (!BN_add(r.bn, a.bn, b.bn)) |
|
|
|
throw bignum_error("CBigNum::operator+ : BN_add failed"); |
|
|
|
throw bignum_error("CBigNum::operator+ : BN_add failed"); |
|
|
|
return r; |
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
@ -526,7 +537,7 @@ inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) |
|
|
|
inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) |
|
|
|
inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CBigNum r; |
|
|
|
CBigNum r; |
|
|
|
if (!BN_sub(&r, &a, &b)) |
|
|
|
if (!BN_sub(r.bn, a.bn, b.bn)) |
|
|
|
throw bignum_error("CBigNum::operator- : BN_sub failed"); |
|
|
|
throw bignum_error("CBigNum::operator- : BN_sub failed"); |
|
|
|
return r; |
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
@ -534,7 +545,7 @@ inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) |
|
|
|
inline const CBigNum operator-(const CBigNum& a) |
|
|
|
inline const CBigNum operator-(const CBigNum& a) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CBigNum r(a); |
|
|
|
CBigNum r(a); |
|
|
|
BN_set_negative(&r, !BN_is_negative(&r)); |
|
|
|
BN_set_negative(r.bn, !BN_is_negative(r.bn)); |
|
|
|
return r; |
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -542,7 +553,7 @@ inline const CBigNum operator*(const CBigNum& a, const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CAutoBN_CTX pctx; |
|
|
|
CAutoBN_CTX pctx; |
|
|
|
CBigNum r; |
|
|
|
CBigNum r; |
|
|
|
if (!BN_mul(&r, &a, &b, pctx)) |
|
|
|
if (!BN_mul(r.bn, a.bn, b.bn, pctx)) |
|
|
|
throw bignum_error("CBigNum::operator* : BN_mul failed"); |
|
|
|
throw bignum_error("CBigNum::operator* : BN_mul failed"); |
|
|
|
return r; |
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
@ -551,7 +562,7 @@ inline const CBigNum operator/(const CBigNum& a, const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CAutoBN_CTX pctx; |
|
|
|
CAutoBN_CTX pctx; |
|
|
|
CBigNum r; |
|
|
|
CBigNum r; |
|
|
|
if (!BN_div(&r, NULL, &a, &b, pctx)) |
|
|
|
if (!BN_div(r.bn, NULL, a.bn, b.bn, pctx)) |
|
|
|
throw bignum_error("CBigNum::operator/ : BN_div failed"); |
|
|
|
throw bignum_error("CBigNum::operator/ : BN_div failed"); |
|
|
|
return r; |
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
@ -560,7 +571,7 @@ inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CAutoBN_CTX pctx; |
|
|
|
CAutoBN_CTX pctx; |
|
|
|
CBigNum r; |
|
|
|
CBigNum r; |
|
|
|
if (!BN_mod(&r, &a, &b, pctx)) |
|
|
|
if (!BN_mod(r.bn, a.bn, b.bn, pctx)) |
|
|
|
throw bignum_error("CBigNum::operator% : BN_div failed"); |
|
|
|
throw bignum_error("CBigNum::operator% : BN_div failed"); |
|
|
|
return r; |
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
@ -568,7 +579,7 @@ inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) |
|
|
|
inline const CBigNum operator<<(const CBigNum& a, unsigned int shift) |
|
|
|
inline const CBigNum operator<<(const CBigNum& a, unsigned int shift) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CBigNum r; |
|
|
|
CBigNum r; |
|
|
|
if (!BN_lshift(&r, &a, shift)) |
|
|
|
if (!BN_lshift(r.bn, a.bn, shift)) |
|
|
|
throw bignum_error("CBigNum:operator<< : BN_lshift failed"); |
|
|
|
throw bignum_error("CBigNum:operator<< : BN_lshift failed"); |
|
|
|
return r; |
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
@ -580,11 +591,11 @@ inline const CBigNum operator>>(const CBigNum& a, unsigned int shift) |
|
|
|
return r; |
|
|
|
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.GetBN (), b.GetBN ()) == 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.GetBN (), b.GetBN ()) != 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.GetBN (), b.GetBN ()) <= 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.GetBN (), b.GetBN ()) >= 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.GetBN (), b.GetBN ()) < 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.GetBN (), b.GetBN ()) > 0); } |
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|