TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BigNumber Class Reference

#include <BigNumber.h>

Public Member Functions

 BigNumber ()
 
 BigNumber (BigNumber const &bn)
 
 BigNumber (uint32)
 
 ~BigNumber ()
 
void SetDword (uint32)
 
void SetQword (uint64)
 
void SetBinary (uint8 const *bytes, int32 len)
 
void SetHexStr (char const *str)
 
void SetRand (int32 numbits)
 
BigNumberoperator= (BigNumber const &bn)
 
BigNumber operator+= (BigNumber const &bn)
 
BigNumber operator+ (BigNumber const &bn)
 
BigNumber operator-= (BigNumber const &bn)
 
BigNumber operator- (BigNumber const &bn)
 
BigNumber operator*= (BigNumber const &bn)
 
BigNumber operator* (BigNumber const &bn)
 
BigNumber operator/= (BigNumber const &bn)
 
BigNumber operator/ (BigNumber const &bn)
 
BigNumber operator%= (BigNumber const &bn)
 
BigNumber operator% (BigNumber const &bn)
 
bool IsZero () const
 
bool IsNegative () const
 
BigNumber ModExp (BigNumber const &bn1, BigNumber const &bn2)
 
BigNumber Exp (BigNumber const &)
 
int32 GetNumBytes (void)
 
struct bignum_st * BN ()
 
uint32 AsDword ()
 
std::unique_ptr< uint8[]> AsByteArray (int32 minSize=0, bool littleEndian=true)
 
std::string AsHexStr () const
 
std::string AsDecStr () const
 

Private Attributes

struct bignum_st * _bn
 

Constructor & Destructor Documentation

BigNumber::BigNumber ( )
27  : _bn(BN_new())
28 { }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber::BigNumber ( BigNumber const bn)
31  : _bn(BN_dup(bn._bn))
32 { }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber::BigNumber ( uint32  val)
35  : _bn(BN_new())
36 {
37  BN_set_word(_bn, val);
38 }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber::~BigNumber ( )
41 {
42  BN_free(_bn);
43 }
struct bignum_st * _bn
Definition: BigNumber.h:98

Member Function Documentation

std::unique_ptr< uint8[]> BigNumber::AsByteArray ( int32  minSize = 0,
bool  littleEndian = true 
)
178 {
179  int numBytes = GetNumBytes();
180  int length = (minSize >= numBytes) ? minSize : numBytes;
181 
182  uint8* array = new uint8[length];
183 
184  // If we need more bytes than length of BigNumber set the rest to 0
185  if (length > numBytes)
186  memset((void*)array, 0, length);
187 
188  BN_bn2bin(_bn, (unsigned char *)array);
189 
190  // openssl's BN stores data internally in big endian format, reverse if little endian desired
191  if (littleEndian)
192  std::reverse(array, array + numBytes);
193 
194  std::unique_ptr<uint8[]> ret(array);
195  return ret;
196 }
int32 GetNumBytes(void)
Definition: BigNumber.cpp:157
float length(float v)
Definition: vectorMath.h:208
struct bignum_st * _bn
Definition: BigNumber.h:98
uint8_t uint8
Definition: Define.h:152

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string BigNumber::AsDecStr ( ) const
207 {
208  char* ch = BN_bn2dec(_bn);
209  std::string ret = ch;
210  OPENSSL_free(ch);
211  return ret;
212 }
struct bignum_st * _bn
Definition: BigNumber.h:98
uint32 BigNumber::AsDword ( )
163 {
164  return (uint32)BN_get_word(_bn);
165 }
uint32_t uint32
Definition: Define.h:150
struct bignum_st * _bn
Definition: BigNumber.h:98
std::string BigNumber::AsHexStr ( ) const
199 {
200  char* ch = BN_bn2hex(_bn);
201  std::string ret = ch;
202  OPENSSL_free(ch);
203  return ret;
204 }
struct bignum_st * _bn
Definition: BigNumber.h:98

+ Here is the caller graph for this function:

struct bignum_st* BigNumber::BN ( )
inline
88 { return _bn; }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber BigNumber::Exp ( BigNumber const bn)
134 {
135  BigNumber ret;
136  BN_CTX *bnctx;
137 
138  bnctx = BN_CTX_new();
139  BN_exp(ret._bn, _bn, bn._bn, bnctx);
140  BN_CTX_free(bnctx);
141 
142  return ret;
143 }
Definition: BigNumber.h:28
struct bignum_st * _bn
Definition: BigNumber.h:98
int32 BigNumber::GetNumBytes ( void  )
158 {
159  return BN_num_bytes(_bn);
160 }
struct bignum_st * _bn
Definition: BigNumber.h:98

+ Here is the caller graph for this function:

bool BigNumber::IsNegative ( ) const
173 {
174  return BN_is_negative(_bn);
175 }
struct bignum_st * _bn
Definition: BigNumber.h:98

+ Here is the caller graph for this function:

bool BigNumber::IsZero ( ) const
168 {
169  return BN_is_zero(_bn);
170 }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber BigNumber::ModExp ( BigNumber const bn1,
BigNumber const bn2 
)
146 {
147  BigNumber ret;
148  BN_CTX *bnctx;
149 
150  bnctx = BN_CTX_new();
151  BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
152  BN_CTX_free(bnctx);
153 
154  return ret;
155 }
Definition: BigNumber.h:28
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber BigNumber::operator% ( BigNumber const bn)
inline
75  {
76  BigNumber t(*this);
77  return t %= bn;
78  }
Definition: BigNumber.h:28
BigNumber BigNumber::operator%= ( BigNumber const bn)
123 {
124  BN_CTX *bnctx;
125 
126  bnctx = BN_CTX_new();
127  BN_mod(_bn, _bn, bn._bn, bnctx);
128  BN_CTX_free(bnctx);
129 
130  return *this;
131 }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber BigNumber::operator* ( BigNumber const bn)
inline
61  {
62  BigNumber t(*this);
63  return t *= bn;
64  }
Definition: BigNumber.h:28
BigNumber BigNumber::operator*= ( BigNumber const bn)
101 {
102  BN_CTX *bnctx;
103 
104  bnctx = BN_CTX_new();
105  BN_mul(_bn, _bn, bn._bn, bnctx);
106  BN_CTX_free(bnctx);
107 
108  return *this;
109 }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber BigNumber::operator+ ( BigNumber const bn)
inline
47  {
48  BigNumber t(*this);
49  return t += bn;
50  }
Definition: BigNumber.h:28
BigNumber BigNumber::operator+= ( BigNumber const bn)
89 {
90  BN_add(_bn, _bn, bn._bn);
91  return *this;
92 }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber BigNumber::operator- ( BigNumber const bn)
inline
54  {
55  BigNumber t(*this);
56  return t -= bn;
57  }
Definition: BigNumber.h:28
BigNumber BigNumber::operator-= ( BigNumber const bn)
95 {
96  BN_sub(_bn, _bn, bn._bn);
97  return *this;
98 }
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber BigNumber::operator/ ( BigNumber const bn)
inline
68  {
69  BigNumber t(*this);
70  return t /= bn;
71  }
Definition: BigNumber.h:28
BigNumber BigNumber::operator/= ( BigNumber const bn)
112 {
113  BN_CTX *bnctx;
114 
115  bnctx = BN_CTX_new();
116  BN_div(_bn, NULL, _bn, bn._bn, bnctx);
117  BN_CTX_free(bnctx);
118 
119  return *this;
120 }
arena_t NULL
Definition: jemalloc_internal.h:624
struct bignum_st * _bn
Definition: BigNumber.h:98
BigNumber & BigNumber::operator= ( BigNumber const bn)
80 {
81  if (this == &bn)
82  return *this;
83 
84  BN_copy(_bn, bn._bn);
85  return *this;
86 }
struct bignum_st * _bn
Definition: BigNumber.h:98
void BigNumber::SetBinary ( uint8 const bytes,
int32  len 
)
58 {
59  uint8* array = new uint8[len];
60 
61  for (int i = 0; i < len; i++)
62  array[i] = bytes[len - 1 - i];
63 
64  BN_bin2bn(array, len, _bn);
65 
66  delete[] array;
67 }
struct bignum_st * _bn
Definition: BigNumber.h:98
uint8_t uint8
Definition: Define.h:152

+ Here is the caller graph for this function:

void BigNumber::SetDword ( uint32  val)
46 {
47  BN_set_word(_bn, val);
48 }
struct bignum_st * _bn
Definition: BigNumber.h:98
void BigNumber::SetHexStr ( char const str)
70 {
71  BN_hex2bn(&_bn, str);
72 }
struct bignum_st * _bn
Definition: BigNumber.h:98

+ Here is the caller graph for this function:

void BigNumber::SetQword ( uint64  val)
51 {
52  BN_set_word(_bn, (uint32)(val >> 32));
53  BN_lshift(_bn, _bn, 32);
54  BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
55 }
uint32_t uint32
Definition: Define.h:150
struct bignum_st * _bn
Definition: BigNumber.h:98
void BigNumber::SetRand ( int32  numbits)
75 {
76  BN_rand(_bn, numbits, 0, 1);
77 }
struct bignum_st * _bn
Definition: BigNumber.h:98

+ Here is the caller graph for this function:

Member Data Documentation

struct bignum_st* BigNumber::_bn
private

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