|
|
@ -48,75 +48,76 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** C++ wrapper for BIGNUM (OpenSSL bignum) */ |
|
|
|
/** C++ wrapper for BIGNUM (OpenSSL bignum) */ |
|
|
|
class CBigNum : public BIGNUM |
|
|
|
class CBigNum |
|
|
|
{ |
|
|
|
{ |
|
|
|
public: |
|
|
|
public: |
|
|
|
|
|
|
|
BIGNUM *bn; |
|
|
|
CBigNum() |
|
|
|
CBigNum() |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_init(this); |
|
|
|
this->bn = BN_new(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CBigNum(const CBigNum& b) |
|
|
|
CBigNum(const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_init(this); |
|
|
|
this->bn = BN_new(); |
|
|
|
if (!BN_copy(this, &b)) |
|
|
|
if (!BN_copy(this->bn, b.bn)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_clear_free(this); |
|
|
|
BN_clear_free(this->bn); |
|
|
|
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(this->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_clear_free(this->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) { this->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) { this->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) { this->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) { this->bn = BN_new(); if (n >= 0) setulong(n); else setint64(n); } |
|
|
|
CBigNum(int64 n) { BN_init(this); setint64(n); } |
|
|
|
CBigNum(int64 n) { this->bn = BN_new(); setint64(n); } |
|
|
|
CBigNum(unsigned char n) { BN_init(this); setulong(n); } |
|
|
|
CBigNum(unsigned char n) { this->bn = BN_new(); setulong(n); } |
|
|
|
CBigNum(unsigned short n) { BN_init(this); setulong(n); } |
|
|
|
CBigNum(unsigned short n) { this->bn = BN_new(); setulong(n); } |
|
|
|
CBigNum(unsigned int n) { BN_init(this); setulong(n); } |
|
|
|
CBigNum(unsigned int n) { this->bn = BN_new(); setulong(n); } |
|
|
|
CBigNum(unsigned long n) { BN_init(this); setulong(n); } |
|
|
|
CBigNum(unsigned long n) { this->bn = BN_new(); setulong(n); } |
|
|
|
CBigNum(uint64 n) { BN_init(this); setuint64(n); } |
|
|
|
CBigNum(uint64 n) { this->bn = BN_new(); setuint64(n); } |
|
|
|
explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); } |
|
|
|
explicit CBigNum(uint256 n) { this->bn = BN_new(); setuint256(n); } |
|
|
|
|
|
|
|
|
|
|
|
explicit CBigNum(const std::vector<unsigned char>& vch) |
|
|
|
explicit CBigNum(const std::vector<unsigned char>& vch) |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_init(this); |
|
|
|
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(this->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(this->bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned int getuint() const |
|
|
|
unsigned int getuint() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
return BN_get_word(this); |
|
|
|
return BN_get_word(this->bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int getint() const |
|
|
|
int getint() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned long n = BN_get_word(this); |
|
|
|
unsigned long n = BN_get_word(this->bn); |
|
|
|
if (!BN_is_negative(this)) |
|
|
|
if (!BN_is_negative(this->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 +165,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, this->bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setuint64(uint64 n) |
|
|
|
void setuint64(uint64 n) |
|
|
@ -191,7 +192,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, this->bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setuint256(uint256 n) |
|
|
|
void setuint256(uint256 n) |
|
|
@ -219,16 +220,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, this->bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint256 getuint256() const |
|
|
|
uint256 getuint256() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int nSize = BN_bn2mpi(this, NULL); |
|
|
|
unsigned int nSize = BN_bn2mpi(this->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(this->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 +250,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(), this->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(this->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(this->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 +295,28 @@ public: |
|
|
|
if (nSize <= 3) |
|
|
|
if (nSize <= 3) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nWord >>= 8*(3-nSize); |
|
|
|
nWord >>= 8*(3-nSize); |
|
|
|
BN_set_word(this, nWord); |
|
|
|
BN_set_word(this->bn, nWord); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
BN_set_word(this, nWord); |
|
|
|
BN_set_word(this->bn, nWord); |
|
|
|
BN_lshift(this, this, 8*(nSize-3)); |
|
|
|
BN_lshift(this->bn, this->bn, 8*(nSize-3)); |
|
|
|
} |
|
|
|
} |
|
|
|
BN_set_negative(this, fNegative); |
|
|
|
BN_set_negative(this->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(this->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(this->bn) << 8*(3-nSize); |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
CBigNum bn; |
|
|
|
CBigNum bn; |
|
|
|
BN_rshift(&bn, this, 8*(nSize-3)); |
|
|
|
BN_rshift(bn.bn, this->bn, 8*(nSize-3)); |
|
|
|
nCompact = BN_get_word(&bn); |
|
|
|
nCompact = BN_get_word(bn.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 +326,7 @@ public: |
|
|
|
nSize++; |
|
|
|
nSize++; |
|
|
|
} |
|
|
|
} |
|
|
|
nCompact |= nSize << 24; |
|
|
|
nCompact |= nSize << 24; |
|
|
|
nCompact |= (BN_is_negative(this) ? 0x00800000 : 0); |
|
|
|
nCompact |= (BN_is_negative(this->bn) ? 0x00800000 : 0); |
|
|
|
return nCompact; |
|
|
|
return nCompact; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -366,20 +367,20 @@ public: |
|
|
|
CBigNum bn0 = 0; |
|
|
|
CBigNum bn0 = 0; |
|
|
|
std::string str; |
|
|
|
std::string str; |
|
|
|
CBigNum bn = *this; |
|
|
|
CBigNum bn = *this; |
|
|
|
BN_set_negative(&bn, false); |
|
|
|
BN_set_negative(bn.bn, false); |
|
|
|
CBigNum dv; |
|
|
|
CBigNum dv; |
|
|
|
CBigNum rem; |
|
|
|
CBigNum rem; |
|
|
|
if (BN_cmp(&bn, &bn0) == 0) |
|
|
|
if (BN_cmp(bn.bn, bn0.bn) == 0) |
|
|
|
return "0"; |
|
|
|
return "0"; |
|
|
|
while (BN_cmp(&bn, &bn0) > 0) |
|
|
|
while (BN_cmp(bn.bn, bn0.bn) > 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) |
|
|
|
if (!BN_div(dv.bn, rem.bn, bn.bn, bnBase.bn, pctx)) |
|
|
|
throw bignum_error("CBigNum::ToString() : BN_div failed"); |
|
|
|
throw bignum_error("CBigNum::ToString() : BN_div failed"); |
|
|
|
bn = dv; |
|
|
|
bn = 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(this->bn)) |
|
|
|
str += "-"; |
|
|
|
str += "-"; |
|
|
|
reverse(str.begin(), str.end()); |
|
|
|
reverse(str.begin(), str.end()); |
|
|
|
return str; |
|
|
|
return str; |
|
|
@ -412,12 +413,12 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
bool operator!() const |
|
|
|
bool operator!() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
return BN_is_zero(this); |
|
|
|
return BN_is_zero(this->bn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CBigNum& operator+=(const CBigNum& b) |
|
|
|
CBigNum& operator+=(const CBigNum& b) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!BN_add(this, this, &b)) |
|
|
|
if (!BN_add(this->bn, this->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 +432,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(this->bn, this->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 +451,7 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
CBigNum& operator<<=(unsigned int shift) |
|
|
|
CBigNum& operator<<=(unsigned int shift) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!BN_lshift(this, this, shift)) |
|
|
|
if (!BN_lshift(this->bn, this->bn, shift)) |
|
|
|
throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); |
|
|
|
throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
@ -461,13 +462,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, this->bn) > 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
*this = 0; |
|
|
|
*this = 0; |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!BN_rshift(this, this, shift)) |
|
|
|
if (!BN_rshift(this->bn, this->bn, shift)) |
|
|
|
throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); |
|
|
|
throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); |
|
|
|
return *this; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
@ -476,7 +477,7 @@ public: |
|
|
|
CBigNum& operator++() |
|
|
|
CBigNum& operator++() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// prefix operator
|
|
|
|
// prefix operator
|
|
|
|
if (!BN_add(this, this, BN_value_one())) |
|
|
|
if (!BN_add(this->bn, this->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 +494,7 @@ public: |
|
|
|
{ |
|
|
|
{ |
|
|
|
// prefix operator
|
|
|
|
// prefix operator
|
|
|
|
CBigNum r; |
|
|
|
CBigNum r; |
|
|
|
if (!BN_sub(&r, this, BN_value_one())) |
|
|
|
if (!BN_sub(r.bn, this->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; |
|
|
@ -518,7 +519,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 +527,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 +535,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 +543,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 +552,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 +561,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 +569,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 +581,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.bn, b.bn) == 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, &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, &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, &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, &b) > 0); } |
|
|
|
inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) > 0); } |
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|