1 // Copyright (c) 2009-2010 Satoshi Nakamoto
\r
2 // Distributed under the MIT/X11 software license, see the accompanying
\r
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
\r
7 #include <openssl/bn.h>
\r
13 class bignum_error : public std::runtime_error
\r
16 explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
\r
25 BN_CTX* operator=(BN_CTX* pnew) { return pctx = pnew; }
\r
30 pctx = BN_CTX_new();
\r
32 throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");
\r
41 operator BN_CTX*() { return pctx; }
\r
42 BN_CTX& operator*() { return *pctx; }
\r
43 BN_CTX** operator&() { return &pctx; }
\r
44 bool operator!() { return (pctx == NULL); }
\r
49 class CBigNum : public BIGNUM
\r
57 CBigNum(const CBigNum& b)
\r
60 if (!BN_copy(this, &b))
\r
62 BN_clear_free(this);
\r
63 throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
\r
67 CBigNum& operator=(const CBigNum& b)
\r
69 if (!BN_copy(this, &b))
\r
70 throw bignum_error("CBigNum::operator= : BN_copy failed");
\r
76 BN_clear_free(this);
\r
79 CBigNum(char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
\r
80 CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
\r
81 CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
\r
82 CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
\r
83 CBigNum(int64 n) { BN_init(this); setint64(n); }
\r
84 CBigNum(unsigned char n) { BN_init(this); setulong(n); }
\r
85 CBigNum(unsigned short n) { BN_init(this); setulong(n); }
\r
86 CBigNum(unsigned int n) { BN_init(this); setulong(n); }
\r
87 CBigNum(unsigned long n) { BN_init(this); setulong(n); }
\r
88 CBigNum(uint64 n) { BN_init(this); setuint64(n); }
\r
89 explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
\r
91 explicit CBigNum(const std::vector<unsigned char>& vch)
\r
97 void setulong(unsigned long n)
\r
99 if (!BN_set_word(this, n))
\r
100 throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");
\r
103 unsigned long getulong() const
\r
105 return BN_get_word(this);
\r
108 unsigned int getuint() const
\r
110 return BN_get_word(this);
\r
115 unsigned long n = BN_get_word(this);
\r
116 if (!BN_is_negative(this))
\r
117 return (n > INT_MAX ? INT_MAX : n);
\r
119 return (n > INT_MAX ? INT_MIN : -(int)n);
\r
122 void setint64(int64 n)
\r
124 unsigned char pch[sizeof(n) + 6];
\r
125 unsigned char* p = pch + 4;
\r
126 bool fNegative = false;
\r
132 bool fLeadingZeroes = true;
\r
133 for (int i = 0; i < 8; i++)
\r
135 unsigned char c = (n >> 56) & 0xff;
\r
137 if (fLeadingZeroes)
\r
142 *p++ = (fNegative ? 0x80 : 0);
\r
143 else if (fNegative)
\r
145 fLeadingZeroes = false;
\r
149 unsigned int nSize = p - (pch + 4);
\r
150 pch[0] = (nSize >> 24) & 0xff;
\r
151 pch[1] = (nSize >> 16) & 0xff;
\r
152 pch[2] = (nSize >> 8) & 0xff;
\r
153 pch[3] = (nSize) & 0xff;
\r
154 BN_mpi2bn(pch, p - pch, this);
\r
157 void setuint64(uint64 n)
\r
159 unsigned char pch[sizeof(n) + 6];
\r
160 unsigned char* p = pch + 4;
\r
161 bool fLeadingZeroes = true;
\r
162 for (int i = 0; i < 8; i++)
\r
164 unsigned char c = (n >> 56) & 0xff;
\r
166 if (fLeadingZeroes)
\r
172 fLeadingZeroes = false;
\r
176 unsigned int nSize = p - (pch + 4);
\r
177 pch[0] = (nSize >> 24) & 0xff;
\r
178 pch[1] = (nSize >> 16) & 0xff;
\r
179 pch[2] = (nSize >> 8) & 0xff;
\r
180 pch[3] = (nSize) & 0xff;
\r
181 BN_mpi2bn(pch, p - pch, this);
\r
184 void setuint256(uint256 n)
\r
186 unsigned char pch[sizeof(n) + 6];
\r
187 unsigned char* p = pch + 4;
\r
188 bool fLeadingZeroes = true;
\r
189 unsigned char* pbegin = (unsigned char*)&n;
\r
190 unsigned char* psrc = pbegin + sizeof(n);
\r
191 while (psrc != pbegin)
\r
193 unsigned char c = *(--psrc);
\r
194 if (fLeadingZeroes)
\r
200 fLeadingZeroes = false;
\r
204 unsigned int nSize = p - (pch + 4);
\r
205 pch[0] = (nSize >> 24) & 0xff;
\r
206 pch[1] = (nSize >> 16) & 0xff;
\r
207 pch[2] = (nSize >> 8) & 0xff;
\r
208 pch[3] = (nSize >> 0) & 0xff;
\r
209 BN_mpi2bn(pch, p - pch, this);
\r
212 uint256 getuint256()
\r
214 unsigned int nSize = BN_bn2mpi(this, NULL);
\r
217 std::vector<unsigned char> vch(nSize);
\r
218 BN_bn2mpi(this, &vch[0]);
\r
219 if (vch.size() > 4)
\r
222 for (int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
\r
223 ((unsigned char*)&n)[i] = vch[j];
\r
227 void setvch(const std::vector<unsigned char>& vch)
\r
229 std::vector<unsigned char> vch2(vch.size() + 4);
\r
230 unsigned int nSize = vch.size();
\r
231 vch2[0] = (nSize >> 24) & 0xff;
\r
232 vch2[1] = (nSize >> 16) & 0xff;
\r
233 vch2[2] = (nSize >> 8) & 0xff;
\r
234 vch2[3] = (nSize >> 0) & 0xff;
\r
235 reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
\r
236 BN_mpi2bn(&vch2[0], vch2.size(), this);
\r
239 std::vector<unsigned char> getvch() const
\r
241 unsigned int nSize = BN_bn2mpi(this, NULL);
\r
243 return std::vector<unsigned char>();
\r
244 std::vector<unsigned char> vch(nSize);
\r
245 BN_bn2mpi(this, &vch[0]);
\r
246 vch.erase(vch.begin(), vch.begin() + 4);
\r
247 reverse(vch.begin(), vch.end());
\r
251 CBigNum& SetCompact(unsigned int nCompact)
\r
253 unsigned int nSize = nCompact >> 24;
\r
254 std::vector<unsigned char> vch(4 + nSize);
\r
256 if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
\r
257 if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
\r
258 if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
\r
259 BN_mpi2bn(&vch[0], vch.size(), this);
\r
263 unsigned int GetCompact() const
\r
265 unsigned int nSize = BN_bn2mpi(this, NULL);
\r
266 std::vector<unsigned char> vch(nSize);
\r
268 BN_bn2mpi(this, &vch[0]);
\r
269 unsigned int nCompact = nSize << 24;
\r
270 if (nSize >= 1) nCompact |= (vch[4] << 16);
\r
271 if (nSize >= 2) nCompact |= (vch[5] << 8);
\r
272 if (nSize >= 3) nCompact |= (vch[6] << 0);
\r
276 void SetHex(const std::string& str)
\r
279 const char* psz = str.c_str();
\r
280 while (isspace(*psz))
\r
282 bool fNegative = false;
\r
288 if (psz[0] == '0' && tolower(psz[1]) == 'x')
\r
290 while (isspace(*psz))
\r
293 // hex string to bignum
\r
294 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 };
\r
296 while (isxdigit(*psz))
\r
299 int n = phexdigit[*psz++];
\r
306 std::string ToString(int nBase=10) const
\r
309 CBigNum bnBase = nBase;
\r
312 CBigNum bn = *this;
\r
313 BN_set_negative(&bn, false);
\r
316 if (BN_cmp(&bn, &bn0) == 0)
\r
318 while (BN_cmp(&bn, &bn0) > 0)
\r
320 if (!BN_div(&dv, &rem, &bn, &bnBase, pctx))
\r
321 throw bignum_error("CBigNum::ToString() : BN_div failed");
\r
323 unsigned int c = rem.getulong();
\r
324 str += "0123456789abcdef"[c];
\r
326 if (BN_is_negative(this))
\r
328 reverse(str.begin(), str.end());
\r
332 std::string GetHex() const
\r
334 return ToString(16);
\r
337 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const
\r
339 return ::GetSerializeSize(getvch(), nType, nVersion);
\r
342 template<typename Stream>
\r
343 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
\r
345 ::Serialize(s, getvch(), nType, nVersion);
\r
348 template<typename Stream>
\r
349 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
\r
351 vector<unsigned char> vch;
\r
352 ::Unserialize(s, vch, nType, nVersion);
\r
357 bool operator!() const
\r
359 return BN_is_zero(this);
\r
362 CBigNum& operator+=(const CBigNum& b)
\r
364 if (!BN_add(this, this, &b))
\r
365 throw bignum_error("CBigNum::operator+= : BN_add failed");
\r
369 CBigNum& operator-=(const CBigNum& b)
\r
375 CBigNum& operator*=(const CBigNum& b)
\r
378 if (!BN_mul(this, this, &b, pctx))
\r
379 throw bignum_error("CBigNum::operator*= : BN_mul failed");
\r
383 CBigNum& operator/=(const CBigNum& b)
\r
389 CBigNum& operator%=(const CBigNum& b)
\r
395 CBigNum& operator<<=(unsigned int shift)
\r
397 if (!BN_lshift(this, this, shift))
\r
398 throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
\r
402 CBigNum& operator>>=(unsigned int shift)
\r
404 // Note: BN_rshift segfaults on 64-bit ubuntu 9.10 if 2^shift is greater than the number
\r
405 if (!BN_rshift(this, this, shift))
\r
406 throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
\r
411 CBigNum& operator++()
\r
414 if (!BN_add(this, this, BN_value_one()))
\r
415 throw bignum_error("CBigNum::operator++ : BN_add failed");
\r
419 const CBigNum operator++(int)
\r
421 // postfix operator
\r
422 const CBigNum ret = *this;
\r
427 CBigNum& operator--()
\r
431 if (!BN_sub(&r, this, BN_value_one()))
\r
432 throw bignum_error("CBigNum::operator-- : BN_sub failed");
\r
437 const CBigNum operator--(int)
\r
439 // postfix operator
\r
440 const CBigNum ret = *this;
\r
446 friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
\r
447 friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);
\r
448 friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);
\r
453 inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
\r
456 if (!BN_add(&r, &a, &b))
\r
457 throw bignum_error("CBigNum::operator+ : BN_add failed");
\r
461 inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
\r
464 if (!BN_sub(&r, &a, &b))
\r
465 throw bignum_error("CBigNum::operator- : BN_sub failed");
\r
469 inline const CBigNum operator-(const CBigNum& a)
\r
472 BN_set_negative(&r, !BN_is_negative(&r));
\r
476 inline const CBigNum operator*(const CBigNum& a, const CBigNum& b)
\r
480 if (!BN_mul(&r, &a, &b, pctx))
\r
481 throw bignum_error("CBigNum::operator* : BN_mul failed");
\r
485 inline const CBigNum operator/(const CBigNum& a, const CBigNum& b)
\r
489 if (!BN_div(&r, NULL, &a, &b, pctx))
\r
490 throw bignum_error("CBigNum::operator/ : BN_div failed");
\r
494 inline const CBigNum operator%(const CBigNum& a, const CBigNum& b)
\r
498 if (!BN_mod(&r, &a, &b, pctx))
\r
499 throw bignum_error("CBigNum::operator% : BN_div failed");
\r
503 inline const CBigNum operator<<(const CBigNum& a, unsigned int shift)
\r
506 if (!BN_lshift(&r, &a, shift))
\r
507 throw bignum_error("CBigNum:operator<< : BN_lshift failed");
\r
511 inline const CBigNum operator>>(const CBigNum& a, unsigned int shift)
\r
514 // Note: BN_rshift segfaults on 64-bit ubuntu 9.10 if 2^shift is greater than the number
\r
515 if (!BN_rshift(&r, &a, shift))
\r
516 throw bignum_error("CBigNum:operator>> : BN_rshift failed");
\r
520 inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
\r
521 inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
\r
522 inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
\r
523 inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
\r
524 inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); }
\r
525 inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); }
\r