TrinityCore
Loading...
Searching...
No Matches
BigNumber.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef _AUTH_BIGNUMBER_H
19#define _AUTH_BIGNUMBER_H
20
21#include "Define.h"
22#include <array>
23#include <memory>
24#include <string>
25#include <vector>
26
27struct bignum_st;
28
30{
31 public:
32 BigNumber();
33 BigNumber(BigNumber const& bn);
34 BigNumber(uint32 v) : BigNumber() { SetDword(v); }
35 BigNumber(int32 v) : BigNumber() { SetDword(v); }
36 BigNumber(std::string const& v) : BigNumber() { SetHexStr(v); }
37 template <size_t Size>
38 BigNumber(std::array<uint8, Size> const& v, bool littleEndian = true) : BigNumber() { SetBinary(v.data(), Size, littleEndian); }
39
40 ~BigNumber();
41
42 void SetDword(int32);
43 void SetDword(uint32);
44 void SetQword(uint64);
45 void SetBinary(uint8 const* bytes, int32 len, bool littleEndian = true);
46 template <typename Container>
47 auto SetBinary(Container const& c, bool littleEndian = true) -> std::enable_if_t<!std::is_pointer_v<std::decay_t<Container>>> { SetBinary(std::data(c), std::size(c), littleEndian); }
48 bool SetHexStr(char const* str);
49 bool SetHexStr(std::string const& str) { return SetHexStr(str.c_str()); }
50
51 void SetRand(int32 numbits);
52
53 BigNumber& operator=(BigNumber const& bn);
54
55 BigNumber& operator+=(BigNumber const& bn);
56 BigNumber operator+(BigNumber const& bn) const
57 {
58 BigNumber t(*this);
59 return t += bn;
60 }
61
62 BigNumber& operator-=(BigNumber const& bn);
63 BigNumber operator-(BigNumber const& bn) const
64 {
65 BigNumber t(*this);
66 return t -= bn;
67 }
68
69 BigNumber& operator*=(BigNumber const& bn);
70 BigNumber operator*(BigNumber const& bn) const
71 {
72 BigNumber t(*this);
73 return t *= bn;
74 }
75
76 BigNumber& operator/=(BigNumber const& bn);
77 BigNumber operator/(BigNumber const& bn) const
78 {
79 BigNumber t(*this);
80 return t /= bn;
81 }
82
83 BigNumber& operator%=(BigNumber const& bn);
84 BigNumber operator%(BigNumber const& bn) const
85 {
86 BigNumber t(*this);
87 return t %= bn;
88 }
89
90 BigNumber& operator<<=(int n);
91 BigNumber operator<<(int n) const
92 {
93 BigNumber t(*this);
94 return t <<= n;
95 }
96
97 int32 CompareTo(BigNumber const& bn) const;
98 bool operator==(BigNumber const& bn) const { return (CompareTo(bn) == 0); }
99 std::strong_ordering operator<=>(BigNumber const& other) const
100 {
101 int32 cmp = CompareTo(other);
102 if (cmp < 0)
103 return std::strong_ordering::less;
104 if (cmp > 0)
105 return std::strong_ordering::greater;
106 return std::strong_ordering::equal;
107 }
108
109 bool IsZero() const;
110 bool IsNegative() const;
111
112 BigNumber ModExp(BigNumber const& bn1, BigNumber const& bn2) const;
113 BigNumber Exp(BigNumber const&) const;
114
115 int32 GetNumBytes() const;
116
117 struct bignum_st* BN() { return _bn; }
118 struct bignum_st const* BN() const { return _bn; }
119
120 uint32 AsDword() const;
121
122 void GetBytes(uint8* buf, size_t bufsize, bool littleEndian = true) const;
123 std::vector<uint8> ToByteVector(int32 minSize = 0, bool littleEndian = true) const;
124
125 template <std::size_t Size>
126 std::array<uint8, Size> ToByteArray(bool littleEndian = true) const
127 {
128 std::array<uint8, Size> buf;
129 GetBytes(buf.data(), Size, littleEndian);
130 return buf;
131 }
132
133 std::string AsHexStr() const;
134 std::string AsDecStr() const;
135
136 private:
137 struct bignum_st* _bn;
138
139};
140#endif
uint8_t uint8
Definition Define.h:135
#define TC_COMMON_API
Definition Define.h:96
int32_t int32
Definition Define.h:129
uint64_t uint64
Definition Define.h:132
uint32_t uint32
Definition Define.h:133
struct bignum_st * BN()
Definition BigNumber.h:117
BigNumber(std::array< uint8, Size > const &v, bool littleEndian=true)
Definition BigNumber.h:38
std::array< uint8, Size > ToByteArray(bool littleEndian=true) const
Definition BigNumber.h:126
BigNumber(uint32 v)
Definition BigNumber.h:34
BigNumber(int32 v)
Definition BigNumber.h:35
BigNumber operator*(BigNumber const &bn) const
Definition BigNumber.h:70
BigNumber operator-(BigNumber const &bn) const
Definition BigNumber.h:63
struct bignum_st const * BN() const
Definition BigNumber.h:118
BigNumber operator%(BigNumber const &bn) const
Definition BigNumber.h:84
BigNumber operator+(BigNumber const &bn) const
Definition BigNumber.h:56
auto SetBinary(Container const &c, bool littleEndian=true) -> std::enable_if_t<!std::is_pointer_v< std::decay_t< Container > > >
Definition BigNumber.h:47
struct bignum_st * _bn
Definition BigNumber.h:137
bool SetHexStr(std::string const &str)
Definition BigNumber.h:49
BigNumber(std::string const &v)
Definition BigNumber.h:36
BigNumber operator/(BigNumber const &bn) const
Definition BigNumber.h:77
std::strong_ordering operator<=>(BigNumber const &other) const
Definition BigNumber.h:99
BigNumber operator<<(int n) const
Definition BigNumber.h:91
bool operator==(BigNumber const &bn) const
Definition BigNumber.h:98