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 <cstring>
22#include <algorithm>
23#include <memory>
24
26 : _bn(BN_new())
27{ }
28
30 : _bn(BN_dup(bn.BN()))
31{ }
32
34{
35 BN_free(_bn);
36}
37
39{
40 SetDword(uint32(abs(val)));
41 if (val < 0)
42 BN_set_negative(_bn, 1);
43}
44
46{
47 BN_set_word(_bn, val);
48}
49
51{
52 BN_set_word(_bn, (uint32)(val >> 32));
53 BN_lshift(_bn, _bn, 32);
54 BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
55}
56
57void BigNumber::SetBinary(uint8 const* bytes, int32 len, bool littleEndian)
58{
59 if (littleEndian)
60 BN_lebin2bn(bytes, len, _bn);
61 else
62 BN_bin2bn(bytes, len, _bn);
63}
64
65bool BigNumber::SetHexStr(char const* str)
66{
67 int n = BN_hex2bn(&_bn, str);
68 return (n > 0);
69}
70
72{
73 BN_rand(_bn, numbits, 0, 1);
74}
75
77{
78 if (this == &bn)
79 return *this;
80
81 BN_copy(_bn, bn._bn);
82 return *this;
83}
84
86{
87 BN_add(_bn, _bn, bn._bn);
88 return *this;
89}
90
92{
93 BN_sub(_bn, _bn, bn._bn);
94 return *this;
95}
96
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}
107
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}
118
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}
129
131{
132 BN_lshift(_bn, _bn, n);
133 return *this;
134}
135
137{
138 return BN_cmp(_bn, bn._bn);
139}
140
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}
152
153BigNumber 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}
164
166{
167 return BN_num_bytes(_bn);
168}
169
171{
172 return (uint32)BN_get_word(_bn);
173}
174
176{
177 return BN_is_zero(_bn);
178}
179
181{
182 return BN_is_negative(_bn);
183}
184
185void BigNumber::GetBytes(uint8* buf, size_t bufsize, bool littleEndian) const
186{
187 int res = littleEndian ? BN_bn2lebinpad(_bn, buf, bufsize) : BN_bn2binpad(_bn, buf, bufsize);
188 ASSERT(res > 0, "Buffer of size %zu is too small to hold bignum with %d bytes.\n", bufsize, BN_num_bytes(_bn));
189}
190
191std::vector<uint8> BigNumber::ToByteVector(int32 minSize, bool littleEndian) 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}
199
200std::string BigNumber::AsHexStr() const
201{
202 char* ch = BN_bn2hex(_bn);
203 std::string ret = ch;
204 OPENSSL_free(ch);
205 return ret;
206}
207
208std::string BigNumber::AsDecStr() const
209{
210 char* ch = BN_bn2dec(_bn);
211 std::string ret = ch;
212 OPENSSL_free(ch);
213 return ret;
214}
uint8_t uint8
Definition Define.h:135
int32_t int32
Definition Define.h:129
uint64_t uint64
Definition Define.h:132
uint32_t uint32
Definition Define.h:133
#define ASSERT
Definition Errors.h:68
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)
void SetRand(int32 numbits)
Definition BigNumber.cpp:71
void SetDword(int32)
Definition BigNumber.cpp:38
void SetBinary(uint8 const *bytes, int32 len, bool littleEndian=true)
Definition BigNumber.cpp:57
std::string AsHexStr() const
bool IsNegative() const
int32 CompareTo(BigNumber const &bn) const
BigNumber & operator*=(BigNumber const &bn)
Definition BigNumber.cpp:97
struct bignum_st * _bn
Definition BigNumber.h:137
BigNumber & operator%=(BigNumber const &bn)
int32 GetNumBytes() const
BigNumber & operator-=(BigNumber const &bn)
Definition BigNumber.cpp:91
void GetBytes(uint8 *buf, size_t bufsize, bool littleEndian=true) const
BigNumber & operator+=(BigNumber const &bn)
Definition BigNumber.cpp:85
bool IsZero() const
void SetQword(uint64)
Definition BigNumber.cpp:50
BigNumber & operator=(BigNumber const &bn)
Definition BigNumber.cpp:76
bool SetHexStr(char const *str)
Definition BigNumber.cpp:65