Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
CBigNum Class Reference

Public Member Functions

 CBigNum ()
 
 CBigNum (const CBigNum &b)
 
CBigNumoperator= (const CBigNum &b)
 
 ~CBigNum ()
 
 CBigNum (signed char n)
 
 CBigNum (short n)
 
 CBigNum (int n)
 
 CBigNum (int64_t n)
 
 CBigNum (unsigned char n)
 
 CBigNum (unsigned short n)
 
 CBigNum (unsigned int n)
 
 CBigNum (uint64_t n)
 
 CBigNum (const std::vector< unsigned char > &vch)
 
void setulong (unsigned long n)
 
unsigned long getulong () const
 
unsigned int getuint () const
 
int getint () const
 
void setint64 (int64_t n)
 
void setuint64 (uint64_t n)
 
void setvch (const std::vector< unsigned char > &vch)
 
std::vector< unsigned char > getvch () const
 
CBigNumSetCompact (unsigned int nCompact)
 
unsigned int GetCompact () const
 
void SetHex (const std::string &str)
 
std::string ToString (int nBase=10) const
 
std::string GetHex () const
 
bool operator! () const
 
CBigNumoperator+= (const CBigNum &b)
 
CBigNumoperator-= (const CBigNum &b)
 
CBigNumoperator*= (const CBigNum &b)
 
CBigNumoperator/= (const CBigNum &b)
 
CBigNumoperator%= (const CBigNum &b)
 
CBigNumoperator<<= (unsigned int shift)
 
CBigNumoperator>>= (unsigned int shift)
 
CBigNumoperator++ ()
 
const CBigNum operator++ (int)
 
CBigNumoperator-- ()
 
const CBigNum operator-- (int)
 
const BIGNUMto_bignum () const
 
BIGNUMto_bignum ()
 

Detailed Description

C++ wrapper for BIGNUM (OpenSSL bignum)

Definition at line 69 of file base58.cpp.

Constructor & Destructor Documentation

◆ CBigNum() [1/11]

CBigNum::CBigNum ( )
inline

Definition at line 73 of file base58.cpp.

74 : bn(BN_new()) {}
Definition bn.h:56

◆ CBigNum() [2/11]

CBigNum::CBigNum ( const CBigNum & b)
inline

Definition at line 76 of file base58.cpp.

77 : CBigNum()
78 {
79 if (!BN_copy(bn, b.bn))
80 {
81 BN_clear_free(bn);
82 throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
83 }
84 }
CBigNum()
Definition base58.cpp:73

◆ ~CBigNum()

CBigNum::~CBigNum ( )
inline

Definition at line 93 of file base58.cpp.

94 {
95 BN_clear_free(bn);
96 }

◆ CBigNum() [3/11]

CBigNum::CBigNum ( signed char n)
inline

Definition at line 99 of file base58.cpp.

99:CBigNum() { if (n >= 0) setulong(n); else setint64(n); }
void setint64(int64_t n)
Definition base58.cpp:139
void setulong(unsigned long n)
Definition base58.cpp:114
Here is the call graph for this function:

◆ CBigNum() [4/11]

CBigNum::CBigNum ( short n)
inline

Definition at line 100 of file base58.cpp.

100:CBigNum() { if (n >= 0) setulong(n); else setint64(n); }
Here is the call graph for this function:

◆ CBigNum() [5/11]

CBigNum::CBigNum ( int n)
inline

Definition at line 101 of file base58.cpp.

101:CBigNum() { if (n >= 0) setulong(n); else setint64(n); }
Here is the call graph for this function:

◆ CBigNum() [6/11]

CBigNum::CBigNum ( int64_t n)
inline

Definition at line 102 of file base58.cpp.

102:CBigNum() { setint64(n); }
Here is the call graph for this function:

◆ CBigNum() [7/11]

CBigNum::CBigNum ( unsigned char n)
inline

Definition at line 103 of file base58.cpp.

103:CBigNum() { setulong(n); }
Here is the call graph for this function:

◆ CBigNum() [8/11]

CBigNum::CBigNum ( unsigned short n)
inline

Definition at line 104 of file base58.cpp.

104:CBigNum() { setulong(n); }
Here is the call graph for this function:

◆ CBigNum() [9/11]

CBigNum::CBigNum ( unsigned int n)
inline

Definition at line 105 of file base58.cpp.

105:CBigNum() { setulong(n); }
Here is the call graph for this function:

◆ CBigNum() [10/11]

CBigNum::CBigNum ( uint64_t n)
inline

Definition at line 106 of file base58.cpp.

106:CBigNum() { setuint64(n); }
void setuint64(uint64_t n)
Definition base58.cpp:174
Here is the call graph for this function:

◆ CBigNum() [11/11]

CBigNum::CBigNum ( const std::vector< unsigned char > & vch)
inlineexplicit

Definition at line 108 of file base58.cpp.

109 : CBigNum()
110 {
111 setvch(vch);
112 }
void setvch(const std::vector< unsigned char > &vch)
Definition base58.cpp:202
Here is the call graph for this function:

Member Function Documentation

◆ GetCompact()

unsigned int CBigNum::GetCompact ( ) const
inline

Definition at line 241 of file base58.cpp.

242 {
243 unsigned int nSize = BN_bn2mpi(bn, NULL);
244 std::vector<unsigned char> vch(nSize);
245 nSize -= 4;
246 BN_bn2mpi(bn, &vch[0]);
247 unsigned int nCompact = nSize << 24;
248 if (nSize >= 1) nCompact |= (vch[4] << 16);
249 if (nSize >= 2) nCompact |= (vch[5] << 8);
250 if (nSize >= 3) nCompact |= (vch[6] << 0);
251 return nCompact;
252 }

◆ GetHex()

std::string CBigNum::GetHex ( ) const
inline

Definition at line 310 of file base58.cpp.

311 {
312 return ToString(16);
313 }
std::string ToString(int nBase=10) const
Definition base58.cpp:284
Here is the call graph for this function:

◆ getint()

int CBigNum::getint ( ) const
inline

Definition at line 130 of file base58.cpp.

131 {
132 unsigned long n = BN_get_word(bn);
133 if (!BN_is_negative(bn))
134 return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
135 else
136 return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
137 }

◆ getuint()

unsigned int CBigNum::getuint ( ) const
inline

Definition at line 125 of file base58.cpp.

126 {
127 return BN_get_word(bn);
128 }

◆ getulong()

unsigned long CBigNum::getulong ( ) const
inline

Definition at line 120 of file base58.cpp.

121 {
122 return BN_get_word(bn);
123 }
Here is the caller graph for this function:

◆ getvch()

std::vector< unsigned char > CBigNum::getvch ( ) const
inline

Definition at line 217 of file base58.cpp.

218 {
219 unsigned int nSize = BN_bn2mpi(bn, NULL);
220 if (nSize <= 4)
221 return std::vector<unsigned char>();
222 std::vector<unsigned char> vch(nSize);
223 BN_bn2mpi(bn, &vch[0]);
224 vch.erase(vch.begin(), vch.begin() + 4);
225 reverse(vch.begin(), vch.end());
226 return vch;
227 }

◆ operator!()

bool CBigNum::operator! ( ) const
inline

Definition at line 317 of file base58.cpp.

318 {
319 return BN_is_zero(bn);
320 }

◆ operator%=()

CBigNum & CBigNum::operator%= ( const CBigNum & b)
inline

Definition at line 352 of file base58.cpp.

353 {
354 CAutoBN_CTX pctx;
355 if (!BN_div(NULL, bn, bn, b.bn, pctx))
356 throw bignum_error("CBigNum::operator%= : BN_div failed");
357 return *this;
358 }

◆ operator*=()

CBigNum & CBigNum::operator*= ( const CBigNum & b)
inline

Definition at line 336 of file base58.cpp.

337 {
338 CAutoBN_CTX pctx;
339 if (!BN_mul(bn, bn, b.bn, pctx))
340 throw bignum_error("CBigNum::operator*= : BN_mul failed");
341 return *this;
342 }

◆ operator++() [1/2]

CBigNum & CBigNum::operator++ ( )
inline

Definition at line 385 of file base58.cpp.

386 {
387 // prefix operator
388 if (!BN_add(bn, bn, BN_value_one()))
389 throw bignum_error("CBigNum::operator++ : BN_add failed");
390 return *this;
391 }

◆ operator++() [2/2]

const CBigNum CBigNum::operator++ ( int )
inline

Definition at line 393 of file base58.cpp.

394 {
395 // postfix operator
396 const CBigNum ret = *this;
397 ++(*this);
398 return ret;
399 }
CK_RV ret

◆ operator+=()

CBigNum & CBigNum::operator+= ( const CBigNum & b)
inline

Definition at line 322 of file base58.cpp.

323 {
324 if (!BN_add(bn, bn, b.bn))
325 throw bignum_error("CBigNum::operator+= : BN_add failed");
326 return *this;
327 }

◆ operator--() [1/2]

CBigNum & CBigNum::operator-- ( )
inline

Definition at line 401 of file base58.cpp.

402 {
403 // prefix operator
404 CBigNum r;
405 if (!BN_sub(r.bn, bn, BN_value_one()))
406 throw bignum_error("CBigNum::operator-- : BN_sub failed");
407 *this = r;
408 return *this;
409 }
const mie::Vuint & r
Definition bn.cpp:28

◆ operator--() [2/2]

const CBigNum CBigNum::operator-- ( int )
inline

Definition at line 411 of file base58.cpp.

412 {
413 // postfix operator
414 const CBigNum ret = *this;
415 --(*this);
416 return ret;
417 }

◆ operator-=()

CBigNum & CBigNum::operator-= ( const CBigNum & b)
inline

Definition at line 329 of file base58.cpp.

330 {
331 if (!BN_sub(bn, bn, b.bn))
332 throw bignum_error("CBigNum::operator-= : BN_sub failed");
333 return *this;
334 }

◆ operator/=()

CBigNum & CBigNum::operator/= ( const CBigNum & b)
inline

Definition at line 344 of file base58.cpp.

345 {
346 CAutoBN_CTX pctx;
347 if (!BN_div(bn, NULL, bn, b.bn, pctx))
348 throw bignum_error("CBigNum::operator/= : BN_div failed");
349 return *this;
350 }

◆ operator<<=()

CBigNum & CBigNum::operator<<= ( unsigned int shift)
inline

Definition at line 360 of file base58.cpp.

361 {
362 if (!BN_lshift(bn, bn, shift))
363 throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
364 return *this;
365 }

◆ operator=()

CBigNum & CBigNum::operator= ( const CBigNum & b)
inline

Definition at line 86 of file base58.cpp.

87 {
88 if (!BN_copy(bn, b.bn))
89 throw bignum_error("CBigNum::operator= : BN_copy failed");
90 return (*this);
91 }

◆ operator>>=()

CBigNum & CBigNum::operator>>= ( unsigned int shift)
inline

Definition at line 367 of file base58.cpp.

368 {
369 // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
370 // if built on ubuntu 9.04 or 9.10, probably depends on version of openssl
371 CBigNum a = 1;
372 a <<= shift;
373 if (BN_cmp(a.bn, bn) > 0)
374 {
375 *this = 0;
376 return *this;
377 }
378
379 if (!BN_rshift(bn, bn, shift))
380 throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
381 return *this;
382 }
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181

◆ SetCompact()

CBigNum & CBigNum::SetCompact ( unsigned int nCompact)
inline

Definition at line 229 of file base58.cpp.

230 {
231 unsigned int nSize = nCompact >> 24;
232 std::vector<unsigned char> vch(4 + nSize);
233 vch[3] = nSize;
234 if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
235 if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
236 if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
237 BN_mpi2bn(&vch[0], vch.size(), bn);
238 return *this;
239 }

◆ SetHex()

void CBigNum::SetHex ( const std::string & str)
inline

Definition at line 254 of file base58.cpp.

255 {
256 // skip 0x
257 const char* psz = str.c_str();
258 while (isspace(*psz))
259 psz++;
260 bool fNegative = false;
261 if (*psz == '-')
262 {
263 fNegative = true;
264 psz++;
265 }
266 if (psz[0] == '0' && tolower(psz[1]) == 'x')
267 psz += 2;
268 while (isspace(*psz))
269 psz++;
270
271 // hex string to bignum
272 static signed char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
273 *this = 0;
274 while (isxdigit(*psz))
275 {
276 *this <<= 4;
277 int n = phexdigit[(unsigned char)*psz++];
278 *this += n;
279 }
280 if (fNegative)
281 BN_set_negative(bn, 1);
282 }
return str
Definition CLI11.hpp:1359

◆ setint64()

void CBigNum::setint64 ( int64_t n)
inline

Definition at line 139 of file base58.cpp.

140 {
141 unsigned char pch[sizeof(n) + 6];
142 unsigned char* p = pch + 4;
143 bool fNegative = false;
144 if (n < (int64_t)0)
145 {
146 n = -n;
147 fNegative = true;
148 }
149 bool fLeadingZeroes = true;
150 for (int i = 0; i < 8; i++)
151 {
152 unsigned char c = (n >> 56) & 0xff;
153 n <<= 8;
154 if (fLeadingZeroes)
155 {
156 if (c == 0)
157 continue;
158 if (c & 0x80)
159 *p++ = (fNegative ? 0x80 : 0);
160 else if (fNegative)
161 c |= 0x80;
162 fLeadingZeroes = false;
163 }
164 *p++ = c;
165 }
166 unsigned int nSize = p - (pch + 4);
167 pch[0] = (nSize >> 24) & 0xff;
168 pch[1] = (nSize >> 16) & 0xff;
169 pch[2] = (nSize >> 8) & 0xff;
170 pch[3] = (nSize) & 0xff;
171 BN_mpi2bn(pch, p - pch, bn);
172 }
const mie::Vuint & p
Definition bn.cpp:27
signed __int64 int64_t
Definition stdint.h:135
Here is the caller graph for this function:

◆ setuint64()

void CBigNum::setuint64 ( uint64_t n)
inline

Definition at line 174 of file base58.cpp.

175 {
176 unsigned char pch[sizeof(n) + 6];
177 unsigned char* p = pch + 4;
178 bool fLeadingZeroes = true;
179 for (int i = 0; i < 8; i++)
180 {
181 unsigned char c = (n >> 56) & 0xff;
182 n <<= 8;
183 if (fLeadingZeroes)
184 {
185 if (c == 0)
186 continue;
187 if (c & 0x80)
188 *p++ = 0;
189 fLeadingZeroes = false;
190 }
191 *p++ = c;
192 }
193 unsigned int nSize = p - (pch + 4);
194 pch[0] = (nSize >> 24) & 0xff;
195 pch[1] = (nSize >> 16) & 0xff;
196 pch[2] = (nSize >> 8) & 0xff;
197 pch[3] = (nSize) & 0xff;
198 BN_mpi2bn(pch, p - pch, bn);
199 }
Here is the caller graph for this function:

◆ setulong()

void CBigNum::setulong ( unsigned long n)
inline

Definition at line 114 of file base58.cpp.

115 {
116 if (!BN_set_word(bn, n))
117 throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");
118 }
Here is the caller graph for this function:

◆ setvch()

void CBigNum::setvch ( const std::vector< unsigned char > & vch)
inline

Definition at line 202 of file base58.cpp.

203 {
204 std::vector<unsigned char> vch2(vch.size() + 4);
205 unsigned int nSize = vch.size();
206 // BIGNUM's byte stream format expects 4 bytes of
207 // big endian size data info at the front
208 vch2[0] = (nSize >> 24) & 0xff;
209 vch2[1] = (nSize >> 16) & 0xff;
210 vch2[2] = (nSize >> 8) & 0xff;
211 vch2[3] = (nSize >> 0) & 0xff;
212 // swap data to big endian
213 reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
214 BN_mpi2bn(&vch2[0], vch2.size(), bn);
215 }
Here is the caller graph for this function:

◆ to_bignum() [1/2]

BIGNUM * CBigNum::to_bignum ( )
inline

Definition at line 422 of file base58.cpp.

422 {
423 return bn;
424 }

◆ to_bignum() [2/2]

const BIGNUM * CBigNum::to_bignum ( ) const
inline

Definition at line 419 of file base58.cpp.

419 {
420 return bn;
421 }
Here is the caller graph for this function:

◆ ToString()

std::string CBigNum::ToString ( int nBase = 10) const
inline

Definition at line 284 of file base58.cpp.

285 {
286 CAutoBN_CTX pctx;
287 CBigNum bnBase = nBase;
288 CBigNum bn0 = 0;
289 std::string str;
290 CBigNum bn = *this;
291 BN_set_negative(bn.bn, false);
292 CBigNum dv;
293 CBigNum rem;
294 if (BN_cmp(bn.bn, bn0.bn) == 0)
295 return "0";
296 while (BN_cmp(bn.bn, bn0.bn) > 0)
297 {
298 if (!BN_div(dv.bn, rem.bn, bn.bn, bnBase.bn, pctx))
299 throw bignum_error("CBigNum::ToString() : BN_div failed");
300 bn = dv;
301 unsigned int c = rem.getulong();
302 str += "0123456789abcdef"[c];
303 }
304 if (BN_is_negative(this->bn))
305 str += "-";
306 reverse(str.begin(), str.end());
307 return str;
308 }
unsigned long getulong() const
Definition base58.cpp:120
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: