1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_BIGNUM_H
6 #define BITCOIN_BIGNUM_H
10 #include <openssl/bn.h>
14 class bignum_error : public std::runtime_error
17 explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
26 BN_CTX* operator=(BN_CTX* pnew) { return pctx = pnew; }
33 throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");
42 operator BN_CTX*() { return pctx; }
43 BN_CTX& operator*() { return *pctx; }
44 BN_CTX** operator&() { return &pctx; }
45 bool operator!() { return (pctx == NULL); }
50 class CBigNum : public BIGNUM
58 CBigNum(const CBigNum& b)
61 if (!BN_copy(this, &b))
64 throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
68 CBigNum& operator=(const CBigNum& b)
70 if (!BN_copy(this, &b))
71 throw bignum_error("CBigNum::operator= : BN_copy failed");
80 CBigNum(char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
81 CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
82 CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
83 CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
84 CBigNum(int64 n) { BN_init(this); setint64(n); }
85 CBigNum(unsigned char n) { BN_init(this); setulong(n); }
86 CBigNum(unsigned short n) { BN_init(this); setulong(n); }
87 CBigNum(unsigned int n) { BN_init(this); setulong(n); }
88 CBigNum(unsigned long n) { BN_init(this); setulong(n); }
89 CBigNum(uint64 n) { BN_init(this); setuint64(n); }
90 explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
92 explicit CBigNum(const std::vector<unsigned char>& vch)
98 void setulong(unsigned long n)
100 if (!BN_set_word(this, n))
101 throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");
104 unsigned long getulong() const
106 return BN_get_word(this);
109 unsigned int getuint() const
111 return BN_get_word(this);
116 unsigned long n = BN_get_word(this);
117 if (!BN_is_negative(this))
118 return (n > INT_MAX ? INT_MAX : n);
120 return (n > INT_MAX ? INT_MIN : -(int)n);
123 void setint64(int64 n)
125 unsigned char pch[sizeof(n) + 6];
126 unsigned char* p = pch + 4;
127 bool fNegative = false;
133 bool fLeadingZeroes = true;
134 for (int i = 0; i < 8; i++)
136 unsigned char c = (n >> 56) & 0xff;
143 *p++ = (fNegative ? 0x80 : 0);
146 fLeadingZeroes = false;
150 unsigned int nSize = p - (pch + 4);
151 pch[0] = (nSize >> 24) & 0xff;
152 pch[1] = (nSize >> 16) & 0xff;
153 pch[2] = (nSize >> 8) & 0xff;
154 pch[3] = (nSize) & 0xff;
155 BN_mpi2bn(pch, p - pch, this);
158 void setuint64(uint64 n)
160 unsigned char pch[sizeof(n) + 6];
161 unsigned char* p = pch + 4;
162 bool fLeadingZeroes = true;
163 for (int i = 0; i < 8; i++)
165 unsigned char c = (n >> 56) & 0xff;
173 fLeadingZeroes = false;
177 unsigned int nSize = p - (pch + 4);
178 pch[0] = (nSize >> 24) & 0xff;
179 pch[1] = (nSize >> 16) & 0xff;
180 pch[2] = (nSize >> 8) & 0xff;
181 pch[3] = (nSize) & 0xff;
182 BN_mpi2bn(pch, p - pch, this);
187 unsigned int nSize = BN_bn2mpi(this, NULL);
190 std::vector<unsigned char> vch(nSize);
191 BN_bn2mpi(this, &vch[0]);
195 for (int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
196 ((unsigned char*)&n)[i] = vch[j];
200 void setuint256(uint256 n)
202 unsigned char pch[sizeof(n) + 6];
203 unsigned char* p = pch + 4;
204 bool fLeadingZeroes = true;
205 unsigned char* pbegin = (unsigned char*)&n;
206 unsigned char* psrc = pbegin + sizeof(n);
207 while (psrc != pbegin)
209 unsigned char c = *(--psrc);
216 fLeadingZeroes = false;
220 unsigned int nSize = p - (pch + 4);
221 pch[0] = (nSize >> 24) & 0xff;
222 pch[1] = (nSize >> 16) & 0xff;
223 pch[2] = (nSize >> 8) & 0xff;
224 pch[3] = (nSize >> 0) & 0xff;
225 BN_mpi2bn(pch, p - pch, this);
230 unsigned int nSize = BN_bn2mpi(this, NULL);
233 std::vector<unsigned char> vch(nSize);
234 BN_bn2mpi(this, &vch[0]);
238 for (int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
239 ((unsigned char*)&n)[i] = vch[j];
243 void setvch(const std::vector<unsigned char>& vch)
245 std::vector<unsigned char> vch2(vch.size() + 4);
246 unsigned int nSize = vch.size();
247 // BIGNUM's byte stream format expects 4 bytes of
248 // big endian size data info at the front
249 vch2[0] = (nSize >> 24) & 0xff;
250 vch2[1] = (nSize >> 16) & 0xff;
251 vch2[2] = (nSize >> 8) & 0xff;
252 vch2[3] = (nSize >> 0) & 0xff;
253 // swap data to big endian
254 reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
255 BN_mpi2bn(&vch2[0], vch2.size(), this);
258 std::vector<unsigned char> getvch() const
260 unsigned int nSize = BN_bn2mpi(this, NULL);
262 return std::vector<unsigned char>();
263 std::vector<unsigned char> vch(nSize);
264 BN_bn2mpi(this, &vch[0]);
265 vch.erase(vch.begin(), vch.begin() + 4);
266 reverse(vch.begin(), vch.end());
270 CBigNum& SetCompact(unsigned int nCompact)
272 unsigned int nSize = nCompact >> 24;
273 std::vector<unsigned char> vch(4 + nSize);
275 if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
276 if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
277 if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
278 BN_mpi2bn(&vch[0], vch.size(), this);
282 unsigned int GetCompact() const
284 unsigned int nSize = BN_bn2mpi(this, NULL);
285 std::vector<unsigned char> vch(nSize);
287 BN_bn2mpi(this, &vch[0]);
288 unsigned int nCompact = nSize << 24;
289 if (nSize >= 1) nCompact |= (vch[4] << 16);
290 if (nSize >= 2) nCompact |= (vch[5] << 8);
291 if (nSize >= 3) nCompact |= (vch[6] << 0);
295 void SetHex(const std::string& str)
298 const char* psz = str.c_str();
299 while (isspace(*psz))
301 bool fNegative = false;
307 if (psz[0] == '0' && tolower(psz[1]) == 'x')
309 while (isspace(*psz))
312 // hex string to bignum
313 static 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 };
315 while (isxdigit(*psz))
318 int n = phexdigit[*psz++];
325 std::string ToString(int nBase=10) const
328 CBigNum bnBase = nBase;
332 BN_set_negative(&bn, false);
335 if (BN_cmp(&bn, &bn0) == 0)
337 while (BN_cmp(&bn, &bn0) > 0)
339 if (!BN_div(&dv, &rem, &bn, &bnBase, pctx))
340 throw bignum_error("CBigNum::ToString() : BN_div failed");
342 unsigned int c = rem.getulong();
343 str += "0123456789abcdef"[c];
345 if (BN_is_negative(this))
347 reverse(str.begin(), str.end());
351 std::string GetHex() const
356 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const
358 return ::GetSerializeSize(getvch(), nType, nVersion);
361 template<typename Stream>
362 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
364 ::Serialize(s, getvch(), nType, nVersion);
367 template<typename Stream>
368 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
370 std::vector<unsigned char> vch;
371 ::Unserialize(s, vch, nType, nVersion);
376 bool operator!() const
378 return BN_is_zero(this);
381 CBigNum& operator+=(const CBigNum& b)
383 if (!BN_add(this, this, &b))
384 throw bignum_error("CBigNum::operator+= : BN_add failed");
388 CBigNum& operator-=(const CBigNum& b)
394 CBigNum& operator*=(const CBigNum& b)
397 if (!BN_mul(this, this, &b, pctx))
398 throw bignum_error("CBigNum::operator*= : BN_mul failed");
402 CBigNum& operator/=(const CBigNum& b)
408 CBigNum& operator%=(const CBigNum& b)
414 CBigNum& operator<<=(unsigned int shift)
416 if (!BN_lshift(this, this, shift))
417 throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
421 CBigNum& operator>>=(unsigned int shift)
423 // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
424 // if built on ubuntu 9.04 or 9.10, probably depends on version of openssl
427 if (BN_cmp(&a, this) > 0)
433 if (!BN_rshift(this, this, shift))
434 throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
439 CBigNum& operator++()
442 if (!BN_add(this, this, BN_value_one()))
443 throw bignum_error("CBigNum::operator++ : BN_add failed");
447 const CBigNum operator++(int)
450 const CBigNum ret = *this;
455 CBigNum& operator--()
459 if (!BN_sub(&r, this, BN_value_one()))
460 throw bignum_error("CBigNum::operator-- : BN_sub failed");
465 const CBigNum operator--(int)
468 const CBigNum ret = *this;
474 friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
475 friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);
476 friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);
481 inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
484 if (!BN_add(&r, &a, &b))
485 throw bignum_error("CBigNum::operator+ : BN_add failed");
489 inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
492 if (!BN_sub(&r, &a, &b))
493 throw bignum_error("CBigNum::operator- : BN_sub failed");
497 inline const CBigNum operator-(const CBigNum& a)
500 BN_set_negative(&r, !BN_is_negative(&r));
504 inline const CBigNum operator*(const CBigNum& a, const CBigNum& b)
508 if (!BN_mul(&r, &a, &b, pctx))
509 throw bignum_error("CBigNum::operator* : BN_mul failed");
513 inline const CBigNum operator/(const CBigNum& a, const CBigNum& b)
517 if (!BN_div(&r, NULL, &a, &b, pctx))
518 throw bignum_error("CBigNum::operator/ : BN_div failed");
522 inline const CBigNum operator%(const CBigNum& a, const CBigNum& b)
526 if (!BN_mod(&r, &a, &b, pctx))
527 throw bignum_error("CBigNum::operator% : BN_div failed");
531 inline const CBigNum operator<<(const CBigNum& a, unsigned int shift)
534 if (!BN_lshift(&r, &a, shift))
535 throw bignum_error("CBigNum:operator<< : BN_lshift failed");
539 inline const CBigNum operator>>(const CBigNum& a, unsigned int shift)
546 inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
547 inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
548 inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
549 inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
550 inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); }
551 inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); }