Commit | Line | Data |
---|---|---|
8bd66202 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
db0e8ccd | 2 | // Copyright (c) 2009-2013 The Bitcoin developers |
8bd66202 | 3 | // Distributed under the MIT/X11 software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
51ed9ec9 | 5 | |
223b6f1b WL |
6 | #ifndef BITCOIN_BIGNUM_H |
7 | #define BITCOIN_BIGNUM_H | |
8bd66202 | 8 | |
51ed9ec9 BD |
9 | #include "serialize.h" |
10 | #include "uint256.h" | |
11 | #include "version.h" | |
12 | ||
8bd66202 | 13 | #include <stdexcept> |
51ed9ec9 | 14 | #include <stdint.h> |
8bd66202 | 15 | #include <vector> |
8bd66202 | 16 | |
51ed9ec9 | 17 | #include <openssl/bn.h> |
8bd66202 | 18 | |
6b8de05d | 19 | /** Errors thrown by the bignum class */ |
8bd66202 GA |
20 | class bignum_error : public std::runtime_error |
21 | { | |
22 | public: | |
23 | explicit bignum_error(const std::string& str) : std::runtime_error(str) {} | |
24 | }; | |
25 | ||
26 | ||
6b8de05d | 27 | /** RAII encapsulated BN_CTX (OpenSSL bignum context) */ |
8bd66202 GA |
28 | class CAutoBN_CTX |
29 | { | |
30 | protected: | |
31 | BN_CTX* pctx; | |
32 | BN_CTX* operator=(BN_CTX* pnew) { return pctx = pnew; } | |
33 | ||
34 | public: | |
35 | CAutoBN_CTX() | |
36 | { | |
37 | pctx = BN_CTX_new(); | |
38 | if (pctx == NULL) | |
39 | throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL"); | |
40 | } | |
41 | ||
42 | ~CAutoBN_CTX() | |
43 | { | |
44 | if (pctx != NULL) | |
45 | BN_CTX_free(pctx); | |
46 | } | |
47 | ||
48 | operator BN_CTX*() { return pctx; } | |
49 | BN_CTX& operator*() { return *pctx; } | |
50 | BN_CTX** operator&() { return &pctx; } | |
51 | bool operator!() { return (pctx == NULL); } | |
52 | }; | |
53 | ||
54 | ||
7e05b972 | 55 | /** C++ wrapper for BIGNUM (OpenSSL bignum) */ |
8bd66202 GA |
56 | class CBigNum : public BIGNUM |
57 | { | |
58 | public: | |
59 | CBigNum() | |
60 | { | |
61 | BN_init(this); | |
62 | } | |
63 | ||
64 | CBigNum(const CBigNum& b) | |
65 | { | |
66 | BN_init(this); | |
67 | if (!BN_copy(this, &b)) | |
68 | { | |
69 | BN_clear_free(this); | |
70 | throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); | |
71 | } | |
72 | } | |
73 | ||
74 | CBigNum& operator=(const CBigNum& b) | |
75 | { | |
76 | if (!BN_copy(this, &b)) | |
77 | throw bignum_error("CBigNum::operator= : BN_copy failed"); | |
78 | return (*this); | |
79 | } | |
80 | ||
81 | ~CBigNum() | |
82 | { | |
83 | BN_clear_free(this); | |
84 | } | |
85 | ||
8c8e8c2e | 86 | //CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'. |
51ed9ec9 BD |
87 | CBigNum(signed char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } |
88 | CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } | |
89 | CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } | |
90 | CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } | |
91 | CBigNum(long long n) { BN_init(this); setint64(n); } | |
92 | CBigNum(unsigned char n) { BN_init(this); setulong(n); } | |
93 | CBigNum(unsigned short n) { BN_init(this); setulong(n); } | |
94 | CBigNum(unsigned int n) { BN_init(this); setulong(n); } | |
95 | CBigNum(unsigned long n) { BN_init(this); setulong(n); } | |
96 | CBigNum(unsigned long long n) { BN_init(this); setuint64(n); } | |
97 | explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); } | |
8bd66202 GA |
98 | |
99 | explicit CBigNum(const std::vector<unsigned char>& vch) | |
100 | { | |
101 | BN_init(this); | |
102 | setvch(vch); | |
103 | } | |
104 | ||
105 | void setulong(unsigned long n) | |
106 | { | |
107 | if (!BN_set_word(this, n)) | |
108 | throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed"); | |
109 | } | |
110 | ||
111 | unsigned long getulong() const | |
112 | { | |
113 | return BN_get_word(this); | |
114 | } | |
115 | ||
116 | unsigned int getuint() const | |
117 | { | |
118 | return BN_get_word(this); | |
119 | } | |
120 | ||
121 | int getint() const | |
122 | { | |
123 | unsigned long n = BN_get_word(this); | |
124 | if (!BN_is_negative(this)) | |
1d8c7a95 | 125 | return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n); |
8bd66202 | 126 | else |
1d8c7a95 | 127 | return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n); |
8bd66202 GA |
128 | } |
129 | ||
51ed9ec9 | 130 | void setint64(int64_t sn) |
8bd66202 | 131 | { |
fe78c9ae | 132 | unsigned char pch[sizeof(sn) + 6]; |
8bd66202 | 133 | unsigned char* p = pch + 4; |
fe78c9ae | 134 | bool fNegative; |
51ed9ec9 | 135 | uint64_t n; |
fe78c9ae | 136 | |
51ed9ec9 | 137 | if (sn < (int64_t)0) |
8bd66202 | 138 | { |
f0bf5fb2 | 139 | // Since the minimum signed integer cannot be represented as positive so long as its type is signed, |
140 | // and it's not well-defined what happens if you make it unsigned before negating it, | |
141 | // we instead increment the negative integer by 1, convert it, then increment the (now positive) unsigned integer by 1 to compensate | |
0f5a2a82 LD |
142 | n = -(sn + 1); |
143 | ++n; | |
8bd66202 | 144 | fNegative = true; |
fe78c9ae RC |
145 | } else { |
146 | n = sn; | |
147 | fNegative = false; | |
8bd66202 | 148 | } |
fe78c9ae | 149 | |
8bd66202 GA |
150 | bool fLeadingZeroes = true; |
151 | for (int i = 0; i < 8; i++) | |
152 | { | |
153 | unsigned char c = (n >> 56) & 0xff; | |
154 | n <<= 8; | |
155 | if (fLeadingZeroes) | |
156 | { | |
157 | if (c == 0) | |
158 | continue; | |
159 | if (c & 0x80) | |
160 | *p++ = (fNegative ? 0x80 : 0); | |
161 | else if (fNegative) | |
162 | c |= 0x80; | |
163 | fLeadingZeroes = false; | |
164 | } | |
165 | *p++ = c; | |
166 | } | |
167 | unsigned int nSize = p - (pch + 4); | |
168 | pch[0] = (nSize >> 24) & 0xff; | |
169 | pch[1] = (nSize >> 16) & 0xff; | |
170 | pch[2] = (nSize >> 8) & 0xff; | |
171 | pch[3] = (nSize) & 0xff; | |
172 | BN_mpi2bn(pch, p - pch, this); | |
173 | } | |
174 | ||
51ed9ec9 | 175 | void setuint64(uint64_t n) |
8bd66202 GA |
176 | { |
177 | unsigned char pch[sizeof(n) + 6]; | |
178 | unsigned char* p = pch + 4; | |
179 | bool fLeadingZeroes = true; | |
180 | for (int i = 0; i < 8; i++) | |
181 | { | |
182 | unsigned char c = (n >> 56) & 0xff; | |
183 | n <<= 8; | |
184 | if (fLeadingZeroes) | |
185 | { | |
186 | if (c == 0) | |
187 | continue; | |
188 | if (c & 0x80) | |
189 | *p++ = 0; | |
190 | fLeadingZeroes = false; | |
191 | } | |
192 | *p++ = c; | |
193 | } | |
194 | unsigned int nSize = p - (pch + 4); | |
195 | pch[0] = (nSize >> 24) & 0xff; | |
196 | pch[1] = (nSize >> 16) & 0xff; | |
197 | pch[2] = (nSize >> 8) & 0xff; | |
198 | pch[3] = (nSize) & 0xff; | |
199 | BN_mpi2bn(pch, p - pch, this); | |
200 | } | |
201 | ||
202 | void setuint256(uint256 n) | |
203 | { | |
204 | unsigned char pch[sizeof(n) + 6]; | |
205 | unsigned char* p = pch + 4; | |
206 | bool fLeadingZeroes = true; | |
207 | unsigned char* pbegin = (unsigned char*)&n; | |
208 | unsigned char* psrc = pbegin + sizeof(n); | |
209 | while (psrc != pbegin) | |
210 | { | |
211 | unsigned char c = *(--psrc); | |
212 | if (fLeadingZeroes) | |
213 | { | |
214 | if (c == 0) | |
215 | continue; | |
216 | if (c & 0x80) | |
217 | *p++ = 0; | |
218 | fLeadingZeroes = false; | |
219 | } | |
220 | *p++ = c; | |
221 | } | |
222 | unsigned int nSize = p - (pch + 4); | |
223 | pch[0] = (nSize >> 24) & 0xff; | |
224 | pch[1] = (nSize >> 16) & 0xff; | |
225 | pch[2] = (nSize >> 8) & 0xff; | |
226 | pch[3] = (nSize >> 0) & 0xff; | |
227 | BN_mpi2bn(pch, p - pch, this); | |
228 | } | |
229 | ||
1657c4bc | 230 | uint256 getuint256() const |
8bd66202 GA |
231 | { |
232 | unsigned int nSize = BN_bn2mpi(this, NULL); | |
233 | if (nSize < 4) | |
234 | return 0; | |
235 | std::vector<unsigned char> vch(nSize); | |
236 | BN_bn2mpi(this, &vch[0]); | |
237 | if (vch.size() > 4) | |
238 | vch[4] &= 0x7f; | |
239 | uint256 n = 0; | |
faf705a4 | 240 | for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) |
8bd66202 GA |
241 | ((unsigned char*)&n)[i] = vch[j]; |
242 | return n; | |
243 | } | |
244 | ||
245 | void setvch(const std::vector<unsigned char>& vch) | |
246 | { | |
247 | std::vector<unsigned char> vch2(vch.size() + 4); | |
248 | unsigned int nSize = vch.size(); | |
a9d3af88 DH |
249 | // BIGNUM's byte stream format expects 4 bytes of |
250 | // big endian size data info at the front | |
8bd66202 GA |
251 | vch2[0] = (nSize >> 24) & 0xff; |
252 | vch2[1] = (nSize >> 16) & 0xff; | |
253 | vch2[2] = (nSize >> 8) & 0xff; | |
254 | vch2[3] = (nSize >> 0) & 0xff; | |
a9d3af88 | 255 | // swap data to big endian |
8bd66202 GA |
256 | reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4); |
257 | BN_mpi2bn(&vch2[0], vch2.size(), this); | |
258 | } | |
259 | ||
260 | std::vector<unsigned char> getvch() const | |
261 | { | |
262 | unsigned int nSize = BN_bn2mpi(this, NULL); | |
a06113b0 | 263 | if (nSize <= 4) |
8bd66202 GA |
264 | return std::vector<unsigned char>(); |
265 | std::vector<unsigned char> vch(nSize); | |
266 | BN_bn2mpi(this, &vch[0]); | |
267 | vch.erase(vch.begin(), vch.begin() + 4); | |
268 | reverse(vch.begin(), vch.end()); | |
269 | return vch; | |
270 | } | |
271 | ||
8bd66202 GA |
272 | void SetHex(const std::string& str) |
273 | { | |
274 | // skip 0x | |
275 | const char* psz = str.c_str(); | |
276 | while (isspace(*psz)) | |
277 | psz++; | |
278 | bool fNegative = false; | |
279 | if (*psz == '-') | |
280 | { | |
281 | fNegative = true; | |
282 | psz++; | |
283 | } | |
284 | if (psz[0] == '0' && tolower(psz[1]) == 'x') | |
285 | psz += 2; | |
286 | while (isspace(*psz)) | |
287 | psz++; | |
288 | ||
289 | // hex string to bignum | |
8bd66202 | 290 | *this = 0; |
f171ec0c OL |
291 | int n; |
292 | while ((n = HexDigit(*psz)) != -1) | |
8bd66202 GA |
293 | { |
294 | *this <<= 4; | |
8bd66202 | 295 | *this += n; |
f171ec0c | 296 | ++psz; |
8bd66202 GA |
297 | } |
298 | if (fNegative) | |
299 | *this = 0 - *this; | |
300 | } | |
301 | ||
302 | std::string ToString(int nBase=10) const | |
303 | { | |
304 | CAutoBN_CTX pctx; | |
305 | CBigNum bnBase = nBase; | |
306 | CBigNum bn0 = 0; | |
223b6f1b | 307 | std::string str; |
8bd66202 GA |
308 | CBigNum bn = *this; |
309 | BN_set_negative(&bn, false); | |
310 | CBigNum dv; | |
311 | CBigNum rem; | |
312 | if (BN_cmp(&bn, &bn0) == 0) | |
313 | return "0"; | |
314 | while (BN_cmp(&bn, &bn0) > 0) | |
315 | { | |
316 | if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) | |
317 | throw bignum_error("CBigNum::ToString() : BN_div failed"); | |
318 | bn = dv; | |
319 | unsigned int c = rem.getulong(); | |
320 | str += "0123456789abcdef"[c]; | |
321 | } | |
322 | if (BN_is_negative(this)) | |
323 | str += "-"; | |
324 | reverse(str.begin(), str.end()); | |
325 | return str; | |
326 | } | |
327 | ||
328 | std::string GetHex() const | |
329 | { | |
330 | return ToString(16); | |
331 | } | |
332 | ||
f8ded588 | 333 | unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const |
8bd66202 GA |
334 | { |
335 | return ::GetSerializeSize(getvch(), nType, nVersion); | |
336 | } | |
337 | ||
338 | template<typename Stream> | |
f8ded588 | 339 | void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const |
8bd66202 GA |
340 | { |
341 | ::Serialize(s, getvch(), nType, nVersion); | |
342 | } | |
343 | ||
344 | template<typename Stream> | |
f8ded588 | 345 | void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) |
8bd66202 | 346 | { |
223b6f1b | 347 | std::vector<unsigned char> vch; |
8bd66202 GA |
348 | ::Unserialize(s, vch, nType, nVersion); |
349 | setvch(vch); | |
350 | } | |
351 | ||
352 | ||
353 | bool operator!() const | |
354 | { | |
355 | return BN_is_zero(this); | |
356 | } | |
357 | ||
358 | CBigNum& operator+=(const CBigNum& b) | |
359 | { | |
360 | if (!BN_add(this, this, &b)) | |
361 | throw bignum_error("CBigNum::operator+= : BN_add failed"); | |
362 | return *this; | |
363 | } | |
364 | ||
365 | CBigNum& operator-=(const CBigNum& b) | |
366 | { | |
367 | *this = *this - b; | |
368 | return *this; | |
369 | } | |
370 | ||
371 | CBigNum& operator*=(const CBigNum& b) | |
372 | { | |
373 | CAutoBN_CTX pctx; | |
374 | if (!BN_mul(this, this, &b, pctx)) | |
375 | throw bignum_error("CBigNum::operator*= : BN_mul failed"); | |
376 | return *this; | |
377 | } | |
378 | ||
379 | CBigNum& operator/=(const CBigNum& b) | |
380 | { | |
381 | *this = *this / b; | |
382 | return *this; | |
383 | } | |
384 | ||
385 | CBigNum& operator%=(const CBigNum& b) | |
386 | { | |
387 | *this = *this % b; | |
388 | return *this; | |
389 | } | |
390 | ||
391 | CBigNum& operator<<=(unsigned int shift) | |
392 | { | |
393 | if (!BN_lshift(this, this, shift)) | |
394 | throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); | |
395 | return *this; | |
396 | } | |
397 | ||
398 | CBigNum& operator>>=(unsigned int shift) | |
399 | { | |
73aa2626 | 400 | // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number |
a7f82808 | 401 | // if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL |
73aa2626 SN |
402 | CBigNum a = 1; |
403 | a <<= shift; | |
404 | if (BN_cmp(&a, this) > 0) | |
405 | { | |
406 | *this = 0; | |
407 | return *this; | |
408 | } | |
409 | ||
8bd66202 GA |
410 | if (!BN_rshift(this, this, shift)) |
411 | throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); | |
412 | return *this; | |
413 | } | |
414 | ||
415 | ||
416 | CBigNum& operator++() | |
417 | { | |
418 | // prefix operator | |
419 | if (!BN_add(this, this, BN_value_one())) | |
420 | throw bignum_error("CBigNum::operator++ : BN_add failed"); | |
421 | return *this; | |
422 | } | |
423 | ||
424 | const CBigNum operator++(int) | |
425 | { | |
426 | // postfix operator | |
427 | const CBigNum ret = *this; | |
428 | ++(*this); | |
429 | return ret; | |
430 | } | |
431 | ||
432 | CBigNum& operator--() | |
433 | { | |
434 | // prefix operator | |
435 | CBigNum r; | |
436 | if (!BN_sub(&r, this, BN_value_one())) | |
437 | throw bignum_error("CBigNum::operator-- : BN_sub failed"); | |
438 | *this = r; | |
439 | return *this; | |
440 | } | |
441 | ||
442 | const CBigNum operator--(int) | |
443 | { | |
444 | // postfix operator | |
445 | const CBigNum ret = *this; | |
446 | --(*this); | |
447 | return ret; | |
448 | } | |
449 | ||
450 | ||
451 | friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b); | |
452 | friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b); | |
453 | friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b); | |
454 | }; | |
455 | ||
456 | ||
457 | ||
458 | inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) | |
459 | { | |
460 | CBigNum r; | |
461 | if (!BN_add(&r, &a, &b)) | |
462 | throw bignum_error("CBigNum::operator+ : BN_add failed"); | |
463 | return r; | |
464 | } | |
465 | ||
466 | inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) | |
467 | { | |
468 | CBigNum r; | |
469 | if (!BN_sub(&r, &a, &b)) | |
470 | throw bignum_error("CBigNum::operator- : BN_sub failed"); | |
471 | return r; | |
472 | } | |
473 | ||
474 | inline const CBigNum operator-(const CBigNum& a) | |
475 | { | |
476 | CBigNum r(a); | |
477 | BN_set_negative(&r, !BN_is_negative(&r)); | |
478 | return r; | |
479 | } | |
480 | ||
481 | inline const CBigNum operator*(const CBigNum& a, const CBigNum& b) | |
482 | { | |
483 | CAutoBN_CTX pctx; | |
484 | CBigNum r; | |
485 | if (!BN_mul(&r, &a, &b, pctx)) | |
486 | throw bignum_error("CBigNum::operator* : BN_mul failed"); | |
487 | return r; | |
488 | } | |
489 | ||
490 | inline const CBigNum operator/(const CBigNum& a, const CBigNum& b) | |
491 | { | |
492 | CAutoBN_CTX pctx; | |
493 | CBigNum r; | |
494 | if (!BN_div(&r, NULL, &a, &b, pctx)) | |
495 | throw bignum_error("CBigNum::operator/ : BN_div failed"); | |
496 | return r; | |
497 | } | |
498 | ||
499 | inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) | |
500 | { | |
501 | CAutoBN_CTX pctx; | |
502 | CBigNum r; | |
503 | if (!BN_mod(&r, &a, &b, pctx)) | |
504 | throw bignum_error("CBigNum::operator% : BN_div failed"); | |
505 | return r; | |
506 | } | |
507 | ||
508 | inline const CBigNum operator<<(const CBigNum& a, unsigned int shift) | |
509 | { | |
510 | CBigNum r; | |
511 | if (!BN_lshift(&r, &a, shift)) | |
512 | throw bignum_error("CBigNum:operator<< : BN_lshift failed"); | |
513 | return r; | |
514 | } | |
515 | ||
516 | inline const CBigNum operator>>(const CBigNum& a, unsigned int shift) | |
517 | { | |
73aa2626 SN |
518 | CBigNum r = a; |
519 | r >>= shift; | |
8bd66202 GA |
520 | return r; |
521 | } | |
522 | ||
523 | inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); } | |
524 | inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); } | |
525 | inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); } | |
526 | inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); } | |
527 | inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); } | |
528 | inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); } | |
223b6f1b WL |
529 | |
530 | #endif |