PRCYCoin  2.0.0.7rc1
P2P Digital Currency
Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
CBigNum Class Reference

C++ wrapper for BIGNUM (OpenSSL bignum) More...

#include <bignum.h>

Public Member Functions

 CBigNum ()
 
 CBigNum (const std::string &str)
 
 CBigNum (const CBigNum &b)
 
CBigNumoperator= (const CBigNum &b)
 
 ~CBigNum ()
 
 CBigNum (signed char n)
 
 CBigNum (short n)
 
 CBigNum (int n)
 
 CBigNum (long n)
 
 CBigNum (unsigned char n)
 
 CBigNum (unsigned short n)
 
 CBigNum (unsigned int n)
 
 CBigNum (unsigned long n)
 
 CBigNum (uint256 n)
 
 CBigNum (const std::vector< unsigned char > &vch)
 
int bitSize () const
 Returns the size in bits of the underlying bignum. More...
 
void setulong (unsigned long n)
 
unsigned long getulong () const
 
unsigned int getuint () const
 
int getint () const
 
void setint64 (int64_t sn)
 
void setuint64 (uint64_t n)
 
void setuint256 (uint256 n)
 
uint256 getuint256 () const
 
void setvch (const std::vector< unsigned char > &vch)
 
std::vector< unsigned char > getvch () const
 
CBigNumSetCompact (unsigned int nCompact)
 
unsigned int GetCompact () const
 
void SetHex (const std::string &str)
 
bool SetHexBool (const std::string &str)
 
std::string ToString (int nBase=10) const
 
std::string GetHex () const
 
unsigned int GetSerializeSize (int nType=0, int nVersion=PROTOCOL_VERSION) const
 
template<typename Stream >
void Serialize (Stream &s, int nType=0, int nVersion=PROTOCOL_VERSION) const
 
template<typename Stream >
void Unserialize (Stream &s, int nType=0, int nVersion=PROTOCOL_VERSION)
 
CBigNum pow (const int e) const
 exponentiation with an int. More...
 
CBigNum pow (const CBigNum &e) const
 exponentiation this^e More...
 
CBigNum mul_mod (const CBigNum &b, const CBigNum &m) const
 modular multiplication: (this * b) mod m More...
 
CBigNum pow_mod (const CBigNum &e, const CBigNum &m) const
 modular exponentiation: this^e mod n More...
 
CBigNum inverse (const CBigNum &m) const
 Calculates the inverse of this element mod m. More...
 
CBigNum gcd (const CBigNum &b) const
 Calculates the greatest common divisor (GCD) of two numbers. More...
 
bool isPrime (const int checks=BN_prime_checks) const
 Miller-Rabin primality test on this element. More...
 
bool isOne () const
 
bool operator! () const
 
CBigNumoperator+= (const CBigNum &b)
 
CBigNumoperator-= (const CBigNum &b)
 
CBigNumoperator*= (const CBigNum &b)
 
CBigNumoperator/= (const CBigNum &b)
 
CBigNumoperator%= (const CBigNum &b)
 
CBigNumoperator<<= (unsigned int shift)
 
CBigNumoperator>>= (unsigned int shift)
 
CBigNumoperator++ ()
 
const CBigNum operator++ (int)
 
CBigNumoperator-- ()
 
const CBigNum operator-- (int)
 

Static Public Member Functions

static CBigNum randBignum (const CBigNum &range)
 Generates a cryptographically secure random number between zero and range exclusive i.e. More...
 
static CBigNum RandKBitBigum (const uint32_t k)
 Generates a cryptographically secure random k-bit number. More...
 
static CBigNum generatePrime (const unsigned int numBits, bool safe=false)
 Generates a random (safe) prime of numBits bits. More...
 

Private Attributes

BIGNUM * bn
 

Friends

const friend CBigNum operator+ (const CBigNum &a, const CBigNum &b)
 
const friend CBigNum operator- (const CBigNum &a, const CBigNum &b)
 
const friend CBigNum operator/ (const CBigNum &a, const CBigNum &b)
 
const friend CBigNum operator% (const CBigNum &a, const CBigNum &b)
 
const friend CBigNum operator* (const CBigNum &a, const CBigNum &b)
 
const friend CBigNum operator<< (const CBigNum &a, unsigned int shift)
 
const friend CBigNum operator- (const CBigNum &a)
 
bool operator== (const CBigNum &a, const CBigNum &b)
 
bool operator!= (const CBigNum &a, const CBigNum &b)
 
bool operator<= (const CBigNum &a, const CBigNum &b)
 
bool operator>= (const CBigNum &a, const CBigNum &b)
 
bool operator< (const CBigNum &a, const CBigNum &b)
 
bool operator> (const CBigNum &a, const CBigNum &b)
 

Detailed Description

C++ wrapper for BIGNUM (OpenSSL bignum)

Definition at line 54 of file bignum.h.

Constructor & Destructor Documentation

◆ CBigNum() [1/13]

CBigNum::CBigNum ( )
inline

Definition at line 58 of file bignum.h.

Here is the caller graph for this function:

◆ CBigNum() [2/13]

CBigNum::CBigNum ( const std::string &  str)
inline

Definition at line 64 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [3/13]

CBigNum::CBigNum ( const CBigNum b)
inline

Definition at line 70 of file bignum.h.

◆ ~CBigNum()

CBigNum::~CBigNum ( )
inline

Definition at line 87 of file bignum.h.

◆ CBigNum() [4/13]

CBigNum::CBigNum ( signed char  n)
inline

Definition at line 93 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [5/13]

CBigNum::CBigNum ( short  n)
inline

Definition at line 94 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [6/13]

CBigNum::CBigNum ( int  n)
inline

Definition at line 95 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [7/13]

CBigNum::CBigNum ( long  n)
inline

Definition at line 96 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [8/13]

CBigNum::CBigNum ( unsigned char  n)
inline

Definition at line 100 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [9/13]

CBigNum::CBigNum ( unsigned short  n)
inline

Definition at line 101 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [10/13]

CBigNum::CBigNum ( unsigned int  n)
inline

Definition at line 102 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [11/13]

CBigNum::CBigNum ( unsigned long  n)
inline

Definition at line 103 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [12/13]

CBigNum::CBigNum ( uint256  n)
inlineexplicit

Definition at line 105 of file bignum.h.

Here is the call graph for this function:

◆ CBigNum() [13/13]

CBigNum::CBigNum ( const std::vector< unsigned char > &  vch)
inlineexplicit

Definition at line 107 of file bignum.h.

Here is the call graph for this function:

Member Function Documentation

◆ bitSize()

int CBigNum::bitSize ( ) const
inline

Returns the size in bits of the underlying bignum.

Returns
the size

Definition at line 142 of file bignum.h.

◆ gcd()

CBigNum CBigNum::gcd ( const CBigNum b) const
inline

Calculates the greatest common divisor (GCD) of two numbers.

Parameters
mthe second element
Returns
the GCD

Definition at line 580 of file bignum.h.

◆ generatePrime()

static CBigNum CBigNum::generatePrime ( const unsigned int  numBits,
bool  safe = false 
)
inlinestatic

Generates a random (safe) prime of numBits bits.

Parameters
numBitsthe number of bits
safetrue for a safe prime
Returns
the prime

Definition at line 568 of file bignum.h.

◆ GetCompact()

unsigned int CBigNum::GetCompact ( ) const
inline

Definition at line 354 of file bignum.h.

◆ GetHex()

std::string CBigNum::GetHex ( ) const
inline

Definition at line 467 of file bignum.h.

Here is the call graph for this function:

◆ getint()

int CBigNum::getint ( ) const
inline

Definition at line 162 of file bignum.h.

◆ GetSerializeSize()

unsigned int CBigNum::GetSerializeSize ( int  nType = 0,
int  nVersion = PROTOCOL_VERSION 
) const
inline

Definition at line 472 of file bignum.h.

◆ getuint()

unsigned int CBigNum::getuint ( ) const
inline

Definition at line 157 of file bignum.h.

◆ getuint256()

uint256 CBigNum::getuint256 ( ) const
inline

Definition at line 271 of file bignum.h.

◆ getulong()

unsigned long CBigNum::getulong ( ) const
inline

Definition at line 152 of file bignum.h.

Here is the caller graph for this function:

◆ getvch()

std::vector<unsigned char> CBigNum::getvch ( ) const
inline

Definition at line 301 of file bignum.h.

◆ inverse()

CBigNum CBigNum::inverse ( const CBigNum m) const
inline

Calculates the inverse of this element mod m.

i.e. i such this*i = 1 mod m

Parameters
mthe modu
Returns
the inverse

Definition at line 554 of file bignum.h.

Here is the caller graph for this function:

◆ isOne()

bool CBigNum::isOne ( ) const
inline

Definition at line 603 of file bignum.h.

◆ isPrime()

bool CBigNum::isPrime ( const int  checks = BN_prime_checks) const
inline

Miller-Rabin primality test on this element.

Parameters
checksoptional, the number of Miller-Rabin tests to run default causes error rate of 2^-80.
Returns
true if prime

Definition at line 594 of file bignum.h.

◆ mul_mod()

CBigNum CBigNum::mul_mod ( const CBigNum b,
const CBigNum m 
) const
inline

modular multiplication: (this * b) mod m

Parameters
boperand
mmodulus

Definition at line 518 of file bignum.h.

◆ operator!()

bool CBigNum::operator! ( ) const
inline

Definition at line 609 of file bignum.h.

◆ operator%=()

CBigNum& CBigNum::operator%= ( const CBigNum b)
inline

Definition at line 644 of file bignum.h.

◆ operator*=()

CBigNum& CBigNum::operator*= ( const CBigNum b)
inline

Definition at line 628 of file bignum.h.

◆ operator++() [1/2]

CBigNum& CBigNum::operator++ ( )
inline

Definition at line 677 of file bignum.h.

◆ operator++() [2/2]

const CBigNum CBigNum::operator++ ( int  )
inline

Definition at line 685 of file bignum.h.

◆ operator+=()

CBigNum& CBigNum::operator+= ( const CBigNum b)
inline

Definition at line 614 of file bignum.h.

◆ operator--() [1/2]

CBigNum& CBigNum::operator-- ( )
inline

Definition at line 693 of file bignum.h.

◆ operator--() [2/2]

const CBigNum CBigNum::operator-- ( int  )
inline

Definition at line 703 of file bignum.h.

◆ operator-=()

CBigNum& CBigNum::operator-= ( const CBigNum b)
inline

Definition at line 621 of file bignum.h.

◆ operator/=()

CBigNum& CBigNum::operator/= ( const CBigNum b)
inline

Definition at line 636 of file bignum.h.

◆ operator<<=()

CBigNum& CBigNum::operator<<= ( unsigned int  shift)
inline

Definition at line 652 of file bignum.h.

◆ operator=()

CBigNum& CBigNum::operator= ( const CBigNum b)
inline

Definition at line 80 of file bignum.h.

◆ operator>>=()

CBigNum& CBigNum::operator>>= ( unsigned int  shift)
inline

Definition at line 659 of file bignum.h.

◆ pow() [1/2]

CBigNum CBigNum::pow ( const CBigNum e) const
inline

exponentiation this^e

Parameters
ethe exponent
Returns

Definition at line 505 of file bignum.h.

◆ pow() [2/2]

CBigNum CBigNum::pow ( const int  e) const
inline

exponentiation with an int.

this^e

Parameters
ethe exponent as an int
Returns

Definition at line 496 of file bignum.h.

Here is the call graph for this function:

◆ pow_mod()

CBigNum CBigNum::pow_mod ( const CBigNum e,
const CBigNum m 
) const
inline

modular exponentiation: this^e mod n

Parameters
eexponent
mmodulus

Definition at line 532 of file bignum.h.

Here is the call graph for this function:

◆ randBignum()

static CBigNum CBigNum::randBignum ( const CBigNum range)
inlinestatic

Generates a cryptographically secure random number between zero and range exclusive i.e.

0 < returned number < range

Parameters
rangeThe upper bound on the number.
Returns

Definition at line 118 of file bignum.h.

◆ RandKBitBigum()

static CBigNum CBigNum::RandKBitBigum ( const uint32_t  k)
inlinestatic

Generates a cryptographically secure random k-bit number.

Parameters
kThe bit length of the number.
Returns

Definition at line 130 of file bignum.h.

◆ Serialize()

template<typename Stream >
void CBigNum::Serialize ( Stream &  s,
int  nType = 0,
int  nVersion = PROTOCOL_VERSION 
) const
inline

Definition at line 478 of file bignum.h.

◆ SetCompact()

CBigNum& CBigNum::SetCompact ( unsigned int  nCompact)
inline

Definition at line 335 of file bignum.h.

◆ SetHex()

void CBigNum::SetHex ( const std::string &  str)
inline

Definition at line 378 of file bignum.h.

◆ SetHexBool()

bool CBigNum::SetHexBool ( const std::string &  str)
inline

Definition at line 408 of file bignum.h.

Here is the caller graph for this function:

◆ setint64()

void CBigNum::setint64 ( int64_t  sn)
inline

Definition at line 171 of file bignum.h.

Here is the caller graph for this function:

◆ setuint256()

void CBigNum::setuint256 ( uint256  n)
inline

Definition at line 243 of file bignum.h.

Here is the caller graph for this function:

◆ setuint64()

void CBigNum::setuint64 ( uint64_t  n)
inline

Definition at line 216 of file bignum.h.

◆ setulong()

void CBigNum::setulong ( unsigned long  n)
inline

Definition at line 146 of file bignum.h.

Here is the caller graph for this function:

◆ setvch()

void CBigNum::setvch ( const std::vector< unsigned char > &  vch)
inline

Definition at line 286 of file bignum.h.

Here is the caller graph for this function:

◆ ToString()

std::string CBigNum::ToString ( int  nBase = 10) const
inline

Definition at line 441 of file bignum.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Unserialize()

template<typename Stream >
void CBigNum::Unserialize ( Stream &  s,
int  nType = 0,
int  nVersion = PROTOCOL_VERSION 
)
inline

Definition at line 484 of file bignum.h.

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 794 of file bignum.h.

◆ operator%

const friend CBigNum operator% ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 769 of file bignum.h.

◆ operator*

const friend CBigNum operator* ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 751 of file bignum.h.

◆ operator+

const friend CBigNum operator+ ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 728 of file bignum.h.

◆ operator- [1/2]

const friend CBigNum operator- ( const CBigNum a)
friend

Definition at line 744 of file bignum.h.

◆ operator- [2/2]

const friend CBigNum operator- ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 736 of file bignum.h.

◆ operator/

const friend CBigNum operator/ ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 760 of file bignum.h.

◆ operator<

bool operator< ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 797 of file bignum.h.

◆ operator<<

const friend CBigNum operator<< ( const CBigNum a,
unsigned int  shift 
)
friend

Definition at line 778 of file bignum.h.

◆ operator<=

bool operator<= ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 795 of file bignum.h.

◆ operator==

bool operator== ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 793 of file bignum.h.

◆ operator>

bool operator> ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 798 of file bignum.h.

◆ operator>=

bool operator>= ( const CBigNum a,
const CBigNum b 
)
friend

Definition at line 796 of file bignum.h.

Member Data Documentation

◆ bn

BIGNUM* CBigNum::bn
private

Definition at line 56 of file bignum.h.


The documentation for this class was generated from the following file: