1+ /*
2+ * Copyright (C) 2005-2012 MaNGOS <http://getmangos.com/>
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; either version 2 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program; if not, write to the Free Software
16+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+ */
18+
19+ #include " BigNumber.h"
20+ #include < openssl/bn.h>
21+ #include < algorithm>
22+
23+ BigNumber::BigNumber ()
24+ {
25+ _bn = BN_new ();
26+ }
27+
28+ BigNumber::BigNumber (BigNumber const & bn)
29+ {
30+ _bn = BN_dup (bn._bn );
31+ }
32+
33+ BigNumber::BigNumber (uint32 val)
34+ {
35+ _bn = BN_new ();
36+ BN_set_word (_bn, val);
37+ }
38+
39+ BigNumber::~BigNumber ()
40+ {
41+ BN_free (_bn);
42+ }
43+
44+ void BigNumber::SetDword (uint32 val)
45+ {
46+ BN_set_word (_bn, val);
47+ }
48+
49+ void BigNumber::SetQword (uint64 val)
50+ {
51+ BN_add_word (_bn, (uint32)(val >> 32 ));
52+ BN_lshift (_bn, _bn, 32 );
53+ BN_add_word (_bn, (uint32)(val & 0xFFFFFFFF ));
54+ }
55+
56+ void BigNumber::SetBinary (uint8 const * bytes, int len)
57+ {
58+ uint8 t[1000 ];
59+ for (int i = 0 ; i < len; i++)
60+ t[i] = bytes[len - 1 - i];
61+ BN_bin2bn (t, len, _bn);
62+ }
63+
64+ void BigNumber::SetHexStr (char const * str)
65+ {
66+ BN_hex2bn (&_bn, str);
67+ }
68+
69+ void BigNumber::SetRand (int numbits)
70+ {
71+ BN_rand (_bn, numbits, 0 , 1 );
72+ }
73+
74+ BigNumber BigNumber::operator =(BigNumber const & bn)
75+ {
76+ BN_copy (_bn, bn._bn );
77+ return *this ;
78+ }
79+
80+ BigNumber BigNumber::operator +=(BigNumber const & bn)
81+ {
82+ BN_add (_bn, _bn, bn._bn );
83+ return *this ;
84+ }
85+
86+ BigNumber BigNumber::operator -=(BigNumber const & bn)
87+ {
88+ BN_sub (_bn, _bn, bn._bn );
89+ return *this ;
90+ }
91+
92+ BigNumber BigNumber::operator *=(BigNumber const & bn)
93+ {
94+ BN_CTX *bnctx;
95+
96+ bnctx = BN_CTX_new ();
97+ BN_mul (_bn, _bn, bn._bn , bnctx);
98+ BN_CTX_free (bnctx);
99+
100+ return *this ;
101+ }
102+
103+ BigNumber BigNumber::operator /=(BigNumber const & bn)
104+ {
105+ BN_CTX *bnctx;
106+
107+ bnctx = BN_CTX_new ();
108+ BN_div (_bn, nullptr , _bn, bn._bn , bnctx);
109+ BN_CTX_free (bnctx);
110+
111+ return *this ;
112+ }
113+
114+ BigNumber BigNumber::operator %=(BigNumber const & bn)
115+ {
116+ BN_CTX *bnctx;
117+
118+ bnctx = BN_CTX_new ();
119+ BN_mod (_bn, _bn, bn._bn , bnctx);
120+ BN_CTX_free (bnctx);
121+
122+ return *this ;
123+ }
124+
125+ BigNumber BigNumber::Exp (BigNumber const & bn)
126+ {
127+ BigNumber ret;
128+ BN_CTX *bnctx;
129+
130+ bnctx = BN_CTX_new ();
131+ BN_exp (ret._bn , _bn, bn._bn , bnctx);
132+ BN_CTX_free (bnctx);
133+
134+ return ret;
135+ }
136+
137+ BigNumber BigNumber::ModExp (BigNumber const & bn1, BigNumber const & bn2)
138+ {
139+ BigNumber ret;
140+ BN_CTX *bnctx;
141+
142+ bnctx = BN_CTX_new ();
143+ BN_mod_exp (ret._bn , _bn, bn1._bn , bn2._bn , bnctx);
144+ BN_CTX_free (bnctx);
145+
146+ return ret;
147+ }
148+
149+ int BigNumber::GetNumBytes (void )
150+ {
151+ return BN_num_bytes (_bn);
152+ }
153+
154+ uint32 BigNumber::AsDword ()
155+ {
156+ return (uint32)BN_get_word (_bn);
157+ }
158+
159+ bool BigNumber::isZero () const
160+ {
161+ return BN_is_zero (_bn)!=0 ;
162+ }
163+
164+ std::vector<uint8> BigNumber::AsByteArray (int minSize, bool reverse)
165+ {
166+ int length = (minSize >= GetNumBytes ()) ? minSize : GetNumBytes ();
167+
168+ std::vector<uint8> byteArray (length);
169+
170+ // If we need more bytes than length of BigNumber set the rest to 0
171+ if (length > GetNumBytes ())
172+ memset ((void *)byteArray.data (), 0 , length);
173+
174+ // Padding should add leading zeroes, not trailing
175+ int paddingOffset = length - GetNumBytes ();
176+
177+ BN_bn2bin (_bn, (unsigned char *)byteArray.data () + paddingOffset);
178+
179+ if (reverse)
180+ std::reverse (byteArray.begin (), byteArray.end ());
181+
182+
183+ return byteArray;
184+ }
185+
186+ char const * BigNumber::AsHexStr ()
187+ {
188+ return BN_bn2hex (_bn);
189+ }
190+
191+ char const * BigNumber::AsDecStr ()
192+ {
193+ return BN_bn2dec (_bn);
194+ }
0 commit comments