AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
BigNumber Class Reference

#include "BigNumber.h"

Public Member Functions

 BigNumber ()
 
 BigNumber (BigNumber const &bn)
 
 BigNumber (uint32 v)
 
 BigNumber (int32 v)
 
 BigNumber (std::string const &v)
 
template<std::size_t Size>
 BigNumber (std::array< uint8, Size > const &v, bool littleEndian=true)
 
 ~BigNumber ()
 
void SetDword (int32)
 
void SetDword (uint32)
 
void SetQword (uint64)
 
void SetBinary (uint8 const *bytes, int32 len, bool littleEndian=true)
 
template<typename Container >
auto SetBinary (Container const &c, bool littleEndian=true) -> std::enable_if_t<!std::is_pointer_v< std::decay_t< Container > > >
 
bool SetHexStr (char const *str)
 
bool SetHexStr (std::string const &str)
 
void SetRand (int32 numbits)
 
BigNumberoperator= (BigNumber const &bn)
 
BigNumberoperator+= (BigNumber const &bn)
 
BigNumber operator+ (BigNumber const &bn) const
 
BigNumberoperator-= (BigNumber const &bn)
 
BigNumber operator- (BigNumber const &bn) const
 
BigNumberoperator*= (BigNumber const &bn)
 
BigNumber operator* (BigNumber const &bn) const
 
BigNumberoperator/= (BigNumber const &bn)
 
BigNumber operator/ (BigNumber const &bn) const
 
BigNumberoperator%= (BigNumber const &bn)
 
BigNumber operator% (BigNumber const &bn) const
 
BigNumberoperator<<= (int n)
 
BigNumber operator<< (int n) const
 
int CompareTo (BigNumber const &bn) const
 
bool operator<= (BigNumber const &bn) const
 
bool operator== (BigNumber const &bn) const
 
bool operator>= (BigNumber const &bn) const
 
bool operator< (BigNumber const &bn) const
 
bool operator> (BigNumber const &bn) const
 
bool IsZero () const
 
bool IsNegative () const
 
BigNumber ModExp (BigNumber const &bn1, BigNumber const &bn2) const
 
BigNumber Exp (BigNumber const &) const
 
int32 GetNumBytes () const
 
struct bignum_st * BN ()
 
struct bignum_st const * BN () const
 
uint32 AsDword () const
 
void GetBytes (uint8 *buf, std::size_t bufsize, bool littleEndian=true) const
 
std::vector< uint8ToByteVector (int32 minSize=0, bool littleEndian=true) const
 
template<std::size_t Size>
std::array< uint8, Size > ToByteArray (bool littleEndian=true) const
 
std::string AsHexStr () const
 
std::string AsDecStr () const
 

Private Attributes

struct bignum_st * _bn
 

Detailed Description

Constructor & Destructor Documentation

◆ BigNumber() [1/6]

BigNumber::BigNumber ( )
26 : _bn(BN_new())
27{ }
struct bignum_st * _bn
Definition: BigNumber.h:134

◆ BigNumber() [2/6]

BigNumber::BigNumber ( BigNumber const &  bn)
30 : _bn(BN_dup(bn.BN()))
31{ }

◆ BigNumber() [3/6]

BigNumber::BigNumber ( uint32  v)
inline
33: BigNumber() { SetDword(v); }
BigNumber()
Definition: BigNumber.cpp:25
void SetDword(int32)
Definition: BigNumber.cpp:38

◆ BigNumber() [4/6]

BigNumber::BigNumber ( int32  v)
inline
34: BigNumber() { SetDword(v); }

◆ BigNumber() [5/6]

BigNumber::BigNumber ( std::string const &  v)
inline
35: BigNumber() { SetHexStr(v); }
bool SetHexStr(char const *str)
Definition: BigNumber.cpp:65

◆ BigNumber() [6/6]

template<std::size_t Size>
BigNumber::BigNumber ( std::array< uint8, Size > const &  v,
bool  littleEndian = true 
)
inline
38: BigNumber() { SetBinary(v.data(), Size, littleEndian); }
void SetBinary(uint8 const *bytes, int32 len, bool littleEndian=true)
Definition: BigNumber.cpp:57

◆ ~BigNumber()

BigNumber::~BigNumber ( )
34{
35 BN_free(_bn);
36}

References _bn.

Member Function Documentation

◆ AsDecStr()

std::string BigNumber::AsDecStr ( ) const
209{
210 char* ch = BN_bn2dec(_bn);
211 std::string ret = ch;
212 OPENSSL_free(ch);
213 return ret;
214}

References _bn.

◆ AsDword()

uint32 BigNumber::AsDword ( ) const
171{
172 return (uint32)BN_get_word(_bn);
173}
std::uint32_t uint32
Definition: Define.h:107

References _bn.

◆ AsHexStr()

std::string BigNumber::AsHexStr ( ) const
201{
202 char* ch = BN_bn2hex(_bn);
203 std::string ret = ch;
204 OPENSSL_free(ch);
205 return ret;
206}

References _bn.

◆ BN() [1/2]

struct bignum_st * BigNumber::BN ( )
inline
114{ return _bn; }

◆ BN() [2/2]

struct bignum_st const * BigNumber::BN ( ) const
inline
115{ return _bn; }

◆ CompareTo()

int BigNumber::CompareTo ( BigNumber const &  bn) const
137{
138 return BN_cmp(_bn, bn._bn);
139}

References _bn.

◆ Exp()

BigNumber BigNumber::Exp ( BigNumber const &  bn) const
142{
143 BigNumber ret;
144 BN_CTX *bnctx;
145
146 bnctx = BN_CTX_new();
147 BN_exp(ret._bn, _bn, bn._bn, bnctx);
148 BN_CTX_free(bnctx);
149
150 return ret;
151}
Definition: BigNumber.h:29

References _bn.

◆ GetBytes()

void BigNumber::GetBytes ( uint8 buf,
std::size_t  bufsize,
bool  littleEndian = true 
) const
186{
187 int res = littleEndian ? BN_bn2lebinpad(_bn, buf, bufsize) : BN_bn2binpad(_bn, buf, bufsize);
188 ASSERT(res > 0, "Buffer of size {} is too small to hold bignum with {} bytes.\n", bufsize, BN_num_bytes(_bn));
189}
#define ASSERT
Definition: Errors.h:68

References _bn, and ASSERT.

Referenced by ToByteVector().

◆ GetNumBytes()

int32 BigNumber::GetNumBytes ( ) const
166{
167 return BN_num_bytes(_bn);
168}

References _bn.

Referenced by GetCheckPacketSize(), and ToByteVector().

◆ IsNegative()

bool BigNumber::IsNegative ( ) const
181{
182 return BN_is_negative(_bn);
183}

References _bn.

◆ IsZero()

bool BigNumber::IsZero ( ) const
176{
177 return BN_is_zero(_bn);
178}

References _bn.

◆ ModExp()

BigNumber BigNumber::ModExp ( BigNumber const &  bn1,
BigNumber const &  bn2 
) const
154{
155 BigNumber ret;
156 BN_CTX *bnctx;
157
158 bnctx = BN_CTX_new();
159 BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
160 BN_CTX_free(bnctx);
161
162 return ret;
163}

References _bn.

Referenced by Acore::Crypto::SRP6::_B(), Acore::Crypto::SRP6::CalculateVerifier(), and Acore::Crypto::SRP6::VerifyChallengeResponse().

◆ operator%()

BigNumber BigNumber::operator% ( BigNumber const &  bn) const
inline
87 {
88 BigNumber t(*this);
89 return t %= bn;
90 }

◆ operator%=()

BigNumber & BigNumber::operator%= ( BigNumber const &  bn)
120{
121 BN_CTX *bnctx;
122
123 bnctx = BN_CTX_new();
124 BN_mod(_bn, _bn, bn._bn, bnctx);
125 BN_CTX_free(bnctx);
126
127 return *this;
128}

References _bn.

◆ operator*()

BigNumber BigNumber::operator* ( BigNumber const &  bn) const
inline
73 {
74 BigNumber t(*this);
75 return t *= bn;
76 }

◆ operator*=()

BigNumber & BigNumber::operator*= ( BigNumber const &  bn)
98{
99 BN_CTX *bnctx;
100
101 bnctx = BN_CTX_new();
102 BN_mul(_bn, _bn, bn._bn, bnctx);
103 BN_CTX_free(bnctx);
104
105 return *this;
106}

References _bn.

◆ operator+()

BigNumber BigNumber::operator+ ( BigNumber const &  bn) const
inline
59 {
60 BigNumber t(*this);
61 return t += bn;
62 }

◆ operator+=()

BigNumber & BigNumber::operator+= ( BigNumber const &  bn)
86{
87 BN_add(_bn, _bn, bn._bn);
88 return *this;
89}

References _bn.

◆ operator-()

BigNumber BigNumber::operator- ( BigNumber const &  bn) const
inline
66 {
67 BigNumber t(*this);
68 return t -= bn;
69 }

◆ operator-=()

BigNumber & BigNumber::operator-= ( BigNumber const &  bn)
92{
93 BN_sub(_bn, _bn, bn._bn);
94 return *this;
95}

References _bn.

◆ operator/()

BigNumber BigNumber::operator/ ( BigNumber const &  bn) const
inline
80 {
81 BigNumber t(*this);
82 return t /= bn;
83 }

◆ operator/=()

BigNumber & BigNumber::operator/= ( BigNumber const &  bn)
109{
110 BN_CTX *bnctx;
111
112 bnctx = BN_CTX_new();
113 BN_div(_bn, nullptr, _bn, bn._bn, bnctx);
114 BN_CTX_free(bnctx);
115
116 return *this;
117}

References _bn.

◆ operator<()

bool BigNumber::operator< ( BigNumber const &  bn) const
inline
103{ return (CompareTo(bn) < 0); }
int CompareTo(BigNumber const &bn) const
Definition: BigNumber.cpp:136

◆ operator<<()

BigNumber BigNumber::operator<< ( int  n) const
inline
94 {
95 BigNumber t(*this);
96 return t <<= n;
97 }

◆ operator<<=()

BigNumber & BigNumber::operator<<= ( int  n)
131{
132 BN_lshift(_bn, _bn, n);
133 return *this;
134}

References _bn.

◆ operator<=()

bool BigNumber::operator<= ( BigNumber const &  bn) const
inline
100{ return (CompareTo(bn) <= 0); }

◆ operator=()

BigNumber & BigNumber::operator= ( BigNumber const &  bn)
77{
78 if (this == &bn)
79 return *this;
80
81 BN_copy(_bn, bn._bn);
82 return *this;
83}

References _bn.

◆ operator==()

bool BigNumber::operator== ( BigNumber const &  bn) const
inline
101{ return (CompareTo(bn) == 0); }

◆ operator>()

bool BigNumber::operator> ( BigNumber const &  bn) const
inline
104{ return (CompareTo(bn) > 0); }

◆ operator>=()

bool BigNumber::operator>= ( BigNumber const &  bn) const
inline
102{ return (CompareTo(bn) >= 0); }

◆ SetBinary() [1/2]

template<typename Container >
auto BigNumber::SetBinary ( Container const &  c,
bool  littleEndian = true 
) -> std::enable_if_t<!std::is_pointer_v<std::decay_t<Container>>>
inline
48{ SetBinary(std::data(c), std::size(c), littleEndian); }

References SetBinary().

Referenced by SetBinary().

◆ SetBinary() [2/2]

void BigNumber::SetBinary ( uint8 const *  bytes,
int32  len,
bool  littleEndian = true 
)
58{
59 if (littleEndian)
60 BN_lebin2bn(bytes, len, _bn);
61 else
62 BN_bin2bn(bytes, len, _bn);
63}

References _bn.

◆ SetDword() [1/2]

void BigNumber::SetDword ( int32  val)
39{
40 SetDword(uint32(std::abs(val)));
41 if (val < 0)
42 BN_set_negative(_bn, 1);
43}

References _bn, and SetDword().

Referenced by SetDword().

◆ SetDword() [2/2]

void BigNumber::SetDword ( uint32  val)
46{
47 BN_set_word(_bn, val);
48}

References _bn.

◆ SetHexStr() [1/2]

bool BigNumber::SetHexStr ( char const *  str)
66{
67 int n = BN_hex2bn(&_bn, str);
68 return (n > 0);
69}

References _bn.

Referenced by GetHexFromConfig(), and WardenCheckMgr::LoadWardenChecks().

◆ SetHexStr() [2/2]

bool BigNumber::SetHexStr ( std::string const &  str)
inline
51{ return SetHexStr(str.c_str()); }

References SetHexStr().

Referenced by SetHexStr().

◆ SetQword()

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}

References _bn.

◆ SetRand()

void BigNumber::SetRand ( int32  numbits)
72{
73 BN_rand(_bn, numbits, 0, 1);
74}

References _bn.

Referenced by SecretMgr::AttemptTransition(), and main().

◆ ToByteArray()

template<std::size_t Size>
std::array< uint8, Size > BigNumber::ToByteArray ( bool  littleEndian = true) const
inline
124 {
125 std::array<uint8, Size> buf;
126 GetBytes(buf.data(), Size, littleEndian);
127 return buf;
128 }
void GetBytes(uint8 *buf, std::size_t bufsize, bool littleEndian=true) const
Definition: BigNumber.cpp:185

Referenced by Acore::Crypto::AEDecrypt(), Acore::Crypto::AEEncryptWithRandomIV(), Acore::Crypto::SRP6::CalculateVerifier(), and WardenWin::HandleData().

◆ ToByteVector()

std::vector< uint8 > BigNumber::ToByteVector ( int32  minSize = 0,
bool  littleEndian = true 
) const
192{
193 std::size_t length = std::max(GetNumBytes(), minSize);
194 std::vector<uint8> v;
195 v.resize(length);
196 GetBytes(v.data(), length, littleEndian);
197 return v;
198}
int32 GetNumBytes() const
Definition: BigNumber.cpp:165

References GetBytes(), and GetNumBytes().

Referenced by WardenWin::HandleData(), Acore::Crypto::Argon2::Hash(), and WardenWin::RequestChecks().

Member Data Documentation

◆ _bn