TrinityCore
Loading...
Searching...
No Matches
BigNumber.cpp
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
19#include "Errors.h"
20#include <openssl/bn.h>
21#include <algorithm>
22#include <memory>
23
25 : _bn(BN_new())
26{ }
27
29 : _bn(BN_dup(bn.BN()))
30{ }
31
33{
34 BN_free(_bn);
35}
36
38{
39 SetDword(uint32(abs(val)));
40 if (val < 0)
41 BN_set_negative(_bn, 1);
42}
43
45{
46 BN_set_word(_bn, val);
47}
48
50{
51 BN_set_word(_bn, (uint32)(val >> 32));
52 BN_lshift(_bn, _bn, 32);
53 BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
54}
55
56void BigNumber::SetBinary(uint8 const* bytes, int32 len, bool littleEndian)
57{
58 if (littleEndian)
59 BN_lebin2bn(bytes, len, _bn);
60 else
61 BN_bin2bn(bytes, len, _bn);
62}
63
64bool BigNumber::SetDecStr(char const* str)
65{
66 int n = BN_dec2bn(&_bn, str);
67 return n > 0;
68}
69
70bool BigNumber::SetHexStr(char const* str)
71{
72 int n = BN_hex2bn(&_bn, str);
73 return (n > 0);
74}
75
77{
78 BN_rand(_bn, numbits, 0, 1);
79}
80
82{
83 if (this == &bn)
84 return *this;
85
86 BN_copy(_bn, bn._bn);
87 return *this;
88}
89
91{
92 BN_add(_bn, _bn, bn._bn);
93 return *this;
94}
95
97{
98 BN_sub(_bn, _bn, bn._bn);
99 return *this;
100}
101
103{
104 BN_CTX *bnctx;
105
106 bnctx = BN_CTX_new();
107 BN_mul(_bn, _bn, bn._bn, bnctx);
108 BN_CTX_free(bnctx);
109
110 return *this;
111}
112
114{
115 BN_CTX *bnctx;
116
117 bnctx = BN_CTX_new();
118 BN_div(_bn, nullptr, _bn, bn._bn, bnctx);
119 BN_CTX_free(bnctx);
120
121 return *this;
122}
123
125{
126 BN_CTX *bnctx;
127
128 bnctx = BN_CTX_new();
129 BN_nnmod(_bn, _bn, bn._bn, bnctx);
130 BN_CTX_free(bnctx);
131
132 return *this;
133}
134
136{
137 BN_lshift(_bn, _bn, n);
138 return *this;
139}
140
142{
143 return BN_cmp(_bn, bn._bn);
144}
145
147{
148 BigNumber ret;
149 BN_CTX *bnctx;
150
151 bnctx = BN_CTX_new();
152 BN_exp(ret._bn, _bn, bn._bn, bnctx);
153 BN_CTX_free(bnctx);
154
155 return ret;
156}
157
158BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2) const
159{
160 BigNumber ret;
161 BN_CTX *bnctx;
162
163 bnctx = BN_CTX_new();
164 BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
165 BN_CTX_free(bnctx);
166
167 return ret;
168}
169
171{
172 return BN_num_bytes(_bn);
173}
174
176{
177 return BN_num_bits(_bn);
178}
179
181{
182 return (uint32)BN_get_word(_bn);
183}
184
186{
187 return BN_is_zero(_bn);
188}
189
191{
192 return BN_is_negative(_bn);
193}
194
195void BigNumber::GetBytes(uint8* buf, size_t bufsize, bool littleEndian) const
196{
197 int res = littleEndian ? BN_bn2lebinpad(_bn, buf, bufsize) : BN_bn2binpad(_bn, buf, bufsize);
198 ASSERT(res > 0, "Buffer of size %zu is too small to hold bignum with %d bytes.\n", bufsize, BN_num_bytes(_bn));
199}
200
201std::vector<uint8> BigNumber::ToByteVector(int32 minSize, bool littleEndian) const
202{
203 std::size_t length = std::max(GetNumBytes(), minSize);
204 std::vector<uint8> v;
205 v.resize(length);
206 GetBytes(v.data(), length, littleEndian);
207 return v;
208}
209
210std::string BigNumber::AsHexStr() const
211{
212 char* ch = BN_bn2hex(_bn);
213 std::string ret = ch;
214 OPENSSL_free(ch);
215 return ret;
216}
217
218std::string BigNumber::AsDecStr() const
219{
220 char* ch = BN_bn2dec(_bn);
221 std::string ret = ch;
222 OPENSSL_free(ch);
223 return ret;
224}
uint8_t uint8
Definition Define.h:156
int32_t int32
Definition Define.h:150
uint64_t uint64
Definition Define.h:153
uint32_t uint32
Definition Define.h:154
#define ASSERT
Definition Errors.h:80
BigNumber & operator<<=(int n)
BigNumber Exp(BigNumber const &) const
BigNumber ModExp(BigNumber const &bn1, BigNumber const &bn2) const
std::string AsDecStr() const
std::vector< uint8 > ToByteVector(int32 minSize=0, bool littleEndian=true) const
uint32 AsDword() const
BigNumber & operator/=(BigNumber const &bn)
bool SetDecStr(char const *str)
Definition BigNumber.cpp:64
void SetRand(int32 numbits)
Definition BigNumber.cpp:76
void SetDword(int32)
Definition BigNumber.cpp:37
void SetBinary(uint8 const *bytes, int32 len, bool littleEndian=true)
Definition BigNumber.cpp:56
std::string AsHexStr() const
bool IsNegative() const
int32 GetNumBits() const
int32 CompareTo(BigNumber const &bn) const
BigNumber & operator*=(BigNumber const &bn)
struct bignum_st * _bn
Definition BigNumber.h:145
BigNumber & operator%=(BigNumber const &bn)
int32 GetNumBytes() const
BigNumber & operator-=(BigNumber const &bn)
Definition BigNumber.cpp:96
void GetBytes(uint8 *buf, size_t bufsize, bool littleEndian=true) const
BigNumber & operator+=(BigNumber const &bn)
Definition BigNumber.cpp:90
bool IsZero() const
void SetQword(uint64)
Definition BigNumber.cpp:49
BigNumber & operator=(BigNumber const &bn)
Definition BigNumber.cpp:81
bool SetHexStr(char const *str)
Definition BigNumber.cpp:70