Bitcoin v0.1.3 · 2009-01-12
bignum.h
1// Copyright (c) 2009 Satoshi Nakamoto2// Distributed under the MIT/X11 software license, see the accompanying3// file license.txt or http://www.opensource.org/licenses/mit-license.php.45#include <stdexcept>6#include <vector>7#include <openssl/bn.h>8910111213class bignum_error : public std::runtime_error14{15public:16 explicit bignum_error(const std::string& str) : std::runtime_error(str) {}17};18192021class CAutoBN_CTX22{23protected:24 BN_CTX* pctx;25 BN_CTX* operator=(BN_CTX* pnew) { return pctx = pnew; }2627public:28 CAutoBN_CTX()29 {30 pctx = BN_CTX_new();31 if (pctx == NULL)32 throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");33 }3435 ~CAutoBN_CTX()36 {37 if (pctx != NULL)38 BN_CTX_free(pctx);39 }4041 operator BN_CTX*() { return pctx; }42 BN_CTX& operator*() { return *pctx; }43 BN_CTX** operator&() { return &pctx; }44 bool operator!() { return (pctx == NULL); }45};46474849class CBigNum : public BIGNUM50{51public:52 CBigNum()53 {54 BN_init(this);55 }5657 CBigNum(const CBigNum& b)58 {59 BN_init(this);60 if (!BN_copy(this, &b))61 {62 BN_clear_free(this);63 throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");64 }65 }6667 explicit CBigNum(const std::string& str)68 {69 BN_init(this);70 SetHex(str);71 }7273 CBigNum& operator=(const CBigNum& b)74 {75 if (!BN_copy(this, &b))76 throw bignum_error("CBigNum::operator= : BN_copy failed");77 return (*this);78 }7980 ~CBigNum()81 {82 BN_clear_free(this);83 }8485 CBigNum(char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }86 CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }87 CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }88 CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }89 CBigNum(int64 n) { BN_init(this); setint64(n); }90 CBigNum(unsigned char n) { BN_init(this); setulong(n); }91 CBigNum(unsigned short n) { BN_init(this); setulong(n); }92 CBigNum(unsigned int n) { BN_init(this); setulong(n); }93 CBigNum(unsigned long n) { BN_init(this); setulong(n); }94 CBigNum(uint64 n) { BN_init(this); setuint64(n); }95 explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }9697 explicit CBigNum(const std::vector<unsigned char>& vch)98 {99 BN_init(this);100 setvch(vch);101 }102103 void setulong(unsigned long n)104 {105 if (!BN_set_word(this, n))106 throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");107 }108109 unsigned long getulong() const110 {111 return BN_get_word(this);112 }113114 unsigned int getuint() const115 {116 return BN_get_word(this);117 }118119 int getint() const120 {121 unsigned long n = BN_get_word(this);122 if (!BN_is_negative(this))123 return (n > INT_MAX ? INT_MAX : n);124 else125 return (n > INT_MAX ? INT_MIN : -(int)n);126 }127128 void setint64(int64 n)129 {130 unsigned char pch[sizeof(n) + 6];131 unsigned char* p = pch + 4;132 bool fNegative = false;133 if (n < (int64)0)134 {135 n = -n;136 fNegative = true;137 }138 bool fLeadingZeroes = true;139 for (int i = 0; i < 8; i++)140 {141 unsigned char c = (n >> 56) & 0xff;142 n <<= 8;143 if (fLeadingZeroes)144 {145 if (c == 0)146 continue;147 if (c & 0x80)148 *p++ = (fNegative ? 0x80 : 0);149 else if (fNegative)150 c |= 0x80;151 fLeadingZeroes = false;152 }153 *p++ = c;154 }155 unsigned int nSize = p - (pch + 4);156 pch[0] = (nSize >> 24) & 0xff;157 pch[1] = (nSize >> 16) & 0xff;158 pch[2] = (nSize >> 8) & 0xff;159 pch[3] = (nSize) & 0xff;160 BN_mpi2bn(pch, p - pch, this);161 }162163 void setuint64(uint64 n)164 {165 unsigned char pch[sizeof(n) + 6];166 unsigned char* p = pch + 4;167 bool fLeadingZeroes = true;168 for (int i = 0; i < 8; i++)169 {170 unsigned char c = (n >> 56) & 0xff;171 n <<= 8;172 if (fLeadingZeroes)173 {174 if (c == 0)175 continue;176 if (c & 0x80)177 *p++ = 0;178 fLeadingZeroes = false;179 }180 *p++ = c;181 }182 unsigned int nSize = p - (pch + 4);183 pch[0] = (nSize >> 24) & 0xff;184 pch[1] = (nSize >> 16) & 0xff;185 pch[2] = (nSize >> 8) & 0xff;186 pch[3] = (nSize) & 0xff;187 BN_mpi2bn(pch, p - pch, this);188 }189190 void setuint256(uint256 n)191 {192 unsigned char pch[sizeof(n) + 6];193 unsigned char* p = pch + 4;194 bool fLeadingZeroes = true;195 unsigned char* pbegin = (unsigned char*)&n;196 unsigned char* psrc = pbegin + sizeof(n);197 while (psrc != pbegin)198 {199 unsigned char c = *(--psrc);200 if (fLeadingZeroes)201 {202 if (c == 0)203 continue;204 if (c & 0x80)205 *p++ = 0;206 fLeadingZeroes = false;207 }208 *p++ = c;209 }210 unsigned int nSize = p - (pch + 4);211 pch[0] = (nSize >> 24) & 0xff;212 pch[1] = (nSize >> 16) & 0xff;213 pch[2] = (nSize >> 8) & 0xff;214 pch[3] = (nSize >> 0) & 0xff;215 BN_mpi2bn(pch, p - pch, this);216 }217218 uint256 getuint256()219 {220 unsigned int nSize = BN_bn2mpi(this, NULL);221 if (nSize < 4)222 return 0;223 std::vector<unsigned char> vch(nSize);224 BN_bn2mpi(this, &vch[0]);225 if (vch.size() > 4)226 vch[4] &= 0x7f;227 uint256 n = 0;228 for (int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)229 ((unsigned char*)&n)[i] = vch[j];230 return n;231 }232233 void setvch(const std::vector<unsigned char>& vch)234 {235 std::vector<unsigned char> vch2(vch.size() + 4);236 unsigned int nSize = vch.size();237 vch2[0] = (nSize >> 24) & 0xff;238 vch2[1] = (nSize >> 16) & 0xff;239 vch2[2] = (nSize >> 8) & 0xff;240 vch2[3] = (nSize >> 0) & 0xff;241 reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);242 BN_mpi2bn(&vch2[0], vch2.size(), this);243 }244245 std::vector<unsigned char> getvch() const246 {247 unsigned int nSize = BN_bn2mpi(this, NULL);248 if (nSize < 4)249 return std::vector<unsigned char>();250 std::vector<unsigned char> vch(nSize);251 BN_bn2mpi(this, &vch[0]);252 vch.erase(vch.begin(), vch.begin() + 4);253 reverse(vch.begin(), vch.end());254 return vch;255 }256257 CBigNum& SetCompact(unsigned int nCompact)258 {259 unsigned int nSize = nCompact >> 24;260 std::vector<unsigned char> vch(4 + nSize);261 vch[3] = nSize;262 if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;263 if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;264 if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;265 BN_mpi2bn(&vch[0], vch.size(), this);266 return *this;267 }268269 unsigned int GetCompact() const270 {271 unsigned int nSize = BN_bn2mpi(this, NULL);272 std::vector<unsigned char> vch(nSize);273 nSize -= 4;274 BN_bn2mpi(this, &vch[0]);275 unsigned int nCompact = nSize << 24;276 if (nSize >= 1) nCompact |= (vch[4] << 16);277 if (nSize >= 2) nCompact |= (vch[5] << 8);278 if (nSize >= 3) nCompact |= (vch[6] << 0);279 return nCompact;280 }281282 void SetHex(const std::string& str)283 {284 // skip 0x285 const char* psz = str.c_str();286 while (isspace(*psz))287 psz++;288 bool fNegative = false;289 if (*psz == '-')290 {291 fNegative = true;292 psz++;293 }294 if (psz[0] == '0' && tolower(psz[1]) == 'x')295 psz += 2;296 while (isspace(*psz))297 psz++;298299 // hex string to bignum300 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 };301 *this = 0;302 while (isxdigit(*psz))303 {304 *this <<= 4;305 int n = phexdigit[*psz++];306 *this += n;307 }308 if (fNegative)309 *this = 0 - *this;310 }311312 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const313 {314 return ::GetSerializeSize(getvch(), nType, nVersion);315 }316317 template<typename Stream>318 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const319 {320 ::Serialize(s, getvch(), nType, nVersion);321 }322323 template<typename Stream>324 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)325 {326 vector<unsigned char> vch;327 ::Unserialize(s, vch, nType, nVersion);328 setvch(vch);329 }330331332 bool operator!() const333 {334 return BN_is_zero(this);335 }336337 CBigNum& operator+=(const CBigNum& b)338 {339 if (!BN_add(this, this, &b))340 throw bignum_error("CBigNum::operator+= : BN_add failed");341 return *this;342 }343344 CBigNum& operator-=(const CBigNum& b)345 {346 *this = *this - b;347 return *this;348 }349350 CBigNum& operator*=(const CBigNum& b)351 {352 CAutoBN_CTX pctx;353 if (!BN_mul(this, this, &b, pctx))354 throw bignum_error("CBigNum::operator*= : BN_mul failed");355 return *this;356 }357358 CBigNum& operator/=(const CBigNum& b)359 {360 *this = *this / b;361 return *this;362 }363364 CBigNum& operator%=(const CBigNum& b)365 {366 *this = *this % b;367 return *this;368 }369370 CBigNum& operator<<=(unsigned int shift)371 {372 if (!BN_lshift(this, this, shift))373 throw bignum_error("CBigNum:operator<<= : BN_lshift failed");374 return *this;375 }376377 CBigNum& operator>>=(unsigned int shift)378 {379 if (!BN_rshift(this, this, shift))380 throw bignum_error("CBigNum:operator>>= : BN_rshift failed");381 return *this;382 }383384385 CBigNum& operator++()386 {387 // prefix operator388 if (!BN_add(this, this, BN_value_one()))389 throw bignum_error("CBigNum::operator++ : BN_add failed");390 return *this;391 }392393 const CBigNum operator++(int)394 {395 // postfix operator396 const CBigNum ret = *this;397 ++(*this);398 return ret;399 }400401 CBigNum& operator--()402 {403 // prefix operator404 CBigNum r;405 if (!BN_sub(&r, this, BN_value_one()))406 throw bignum_error("CBigNum::operator-- : BN_sub failed");407 *this = r;408 return *this;409 }410411 const CBigNum operator--(int)412 {413 // postfix operator414 const CBigNum ret = *this;415 --(*this);416 return ret;417 }418419420 friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);421 friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);422 friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);423};424425426427inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)428{429 CBigNum r;430 if (!BN_add(&r, &a, &b))431 throw bignum_error("CBigNum::operator+ : BN_add failed");432 return r;433}434435inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)436{437 CBigNum r;438 if (!BN_sub(&r, &a, &b))439 throw bignum_error("CBigNum::operator- : BN_sub failed");440 return r;441}442443inline const CBigNum operator-(const CBigNum& a)444{445 CBigNum r(a);446 BN_set_negative(&r, !BN_is_negative(&r));447 return r;448}449450inline const CBigNum operator*(const CBigNum& a, const CBigNum& b)451{452 CAutoBN_CTX pctx;453 CBigNum r;454 if (!BN_mul(&r, &a, &b, pctx))455 throw bignum_error("CBigNum::operator* : BN_mul failed");456 return r;457}458459inline const CBigNum operator/(const CBigNum& a, const CBigNum& b)460{461 CAutoBN_CTX pctx;462 CBigNum r;463 if (!BN_div(&r, NULL, &a, &b, pctx))464 throw bignum_error("CBigNum::operator/ : BN_div failed");465 return r;466}467468inline const CBigNum operator%(const CBigNum& a, const CBigNum& b)469{470 CAutoBN_CTX pctx;471 CBigNum r;472 if (!BN_mod(&r, &a, &b, pctx))473 throw bignum_error("CBigNum::operator% : BN_div failed");474 return r;475}476477inline const CBigNum operator<<(const CBigNum& a, unsigned int shift)478{479 CBigNum r;480 if (!BN_lshift(&r, &a, shift))481 throw bignum_error("CBigNum:operator<< : BN_lshift failed");482 return r;483}484485inline const CBigNum operator>>(const CBigNum& a, unsigned int shift)486{487 CBigNum r;488 if (!BN_rshift(&r, &a, shift))489 throw bignum_error("CBigNum:operator>> : BN_rshift failed");490 return r;491}492493inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }494inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }495inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }496inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }497inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); }498inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); }
Click a line number to link to it; use #L10-L20 for a range. "=" next to a version means the file is identical there.
