bignum.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_BIGNUM_H
  3. #define ECOIN_BIGNUM_H
  4. #include <stdexcept>
  5. #include <vector>
  6. #include <openssl/bn.h>
  7. #include "util.h" // for uint64
  8. /** Errors thrown by the bignum class */
  9. class bignum_error : public std::runtime_error
  10. {
  11. public:
  12. explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
  13. };
  14. /** RAII encapsulated BN_CTX (OpenSSL bignum context) */
  15. class CAutoBN_CTX
  16. {
  17. protected:
  18. BN_CTX* pctx;
  19. BN_CTX* operator=(BN_CTX* pnew) { return pctx = pnew; }
  20. public:
  21. CAutoBN_CTX()
  22. {
  23. pctx = BN_CTX_new();
  24. if (pctx == NULL)
  25. throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");
  26. }
  27. ~CAutoBN_CTX()
  28. {
  29. if (pctx != NULL)
  30. BN_CTX_free(pctx);
  31. }
  32. operator BN_CTX*() { return pctx; }
  33. BN_CTX& operator*() { return *pctx; }
  34. BN_CTX** operator&() { return &pctx; }
  35. bool operator!() { return (pctx == NULL); }
  36. };
  37. /** C++ wrapper for BIGNUM (OpenSSL bignum) */
  38. class CBigNum : public BIGNUM
  39. {
  40. public:
  41. CBigNum()
  42. {
  43. BN_init(this);
  44. }
  45. CBigNum(const CBigNum& b)
  46. {
  47. BN_init(this);
  48. if (!BN_copy(this, &b))
  49. {
  50. BN_clear_free(this);
  51. throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
  52. }
  53. }
  54. CBigNum& operator=(const CBigNum& b)
  55. {
  56. if (!BN_copy(this, &b))
  57. throw bignum_error("CBigNum::operator= : BN_copy failed");
  58. return (*this);
  59. }
  60. ~CBigNum()
  61. {
  62. BN_clear_free(this);
  63. }
  64. //CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'.
  65. CBigNum(signed char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
  66. CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
  67. CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
  68. CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
  69. CBigNum(int64 n) { BN_init(this); setint64(n); }
  70. CBigNum(unsigned char n) { BN_init(this); setulong(n); }
  71. CBigNum(unsigned short n) { BN_init(this); setulong(n); }
  72. CBigNum(unsigned int n) { BN_init(this); setulong(n); }
  73. CBigNum(unsigned long n) { BN_init(this); setulong(n); }
  74. CBigNum(uint64 n) { BN_init(this); setuint64(n); }
  75. explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
  76. explicit CBigNum(const std::vector<unsigned char>& vch)
  77. {
  78. BN_init(this);
  79. setvch(vch);
  80. }
  81. /** Generates a cryptographically secure random number between zero and range exclusive
  82. * i.e. 0 < returned number < range
  83. * @param range The upper bound on the number.
  84. * @return
  85. */
  86. static CBigNum randBignum(const CBigNum& range) {
  87. CBigNum ret;
  88. if(!BN_rand_range(&ret, &range)){
  89. throw bignum_error("CBigNum:rand element : BN_rand_range failed");
  90. }
  91. return ret;
  92. }
  93. /** Generates a cryptographically secure random k-bit number
  94. * @param k The bit length of the number.
  95. * @return
  96. */
  97. static CBigNum RandKBitBigum(const uint32_t k){
  98. CBigNum ret;
  99. if(!BN_rand(&ret, k, -1, 0)){
  100. throw bignum_error("CBigNum:rand element : BN_rand failed");
  101. }
  102. return ret;
  103. }
  104. /**Returns the size in bits of the underlying bignum.
  105. *
  106. * @return the size
  107. */
  108. int bitSize() const{
  109. return BN_num_bits(this);
  110. }
  111. void setulong(unsigned long n)
  112. {
  113. if (!BN_set_word(this, n))
  114. throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");
  115. }
  116. unsigned long getulong() const
  117. {
  118. return BN_get_word(this);
  119. }
  120. unsigned int getuint() const
  121. {
  122. return BN_get_word(this);
  123. }
  124. int getint() const
  125. {
  126. unsigned long n = BN_get_word(this);
  127. if (!BN_is_negative(this))
  128. return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
  129. else
  130. return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
  131. }
  132. void setint64(int64 sn)
  133. {
  134. unsigned char pch[sizeof(sn) + 6];
  135. unsigned char* p = pch + 4;
  136. bool fNegative;
  137. uint64 n;
  138. if (sn < (int64)0)
  139. {
  140. // Since the minimum signed integer cannot be represented as positive so long as its type is signed, and it's not well-defined what happens if you make it unsigned before negating it, we instead increment the negative integer by 1, convert it, then increment the (now positive) unsigned integer by 1 to compensate
  141. n = -(sn + 1);
  142. ++n;
  143. fNegative = true;
  144. } else {
  145. n = sn;
  146. fNegative = false;
  147. }
  148. bool fLeadingZeroes = true;
  149. for (int i = 0; i < 8; i++)
  150. {
  151. unsigned char c = (n >> 56) & 0xff;
  152. n <<= 8;
  153. if (fLeadingZeroes)
  154. {
  155. if (c == 0)
  156. continue;
  157. if (c & 0x80)
  158. *p++ = (fNegative ? 0x80 : 0);
  159. else if (fNegative)
  160. c |= 0x80;
  161. fLeadingZeroes = false;
  162. }
  163. *p++ = c;
  164. }
  165. unsigned int nSize = p - (pch + 4);
  166. pch[0] = (nSize >> 24) & 0xff;
  167. pch[1] = (nSize >> 16) & 0xff;
  168. pch[2] = (nSize >> 8) & 0xff;
  169. pch[3] = (nSize) & 0xff;
  170. BN_mpi2bn(pch, p - pch, this);
  171. }
  172. uint64 getuint64()
  173. {
  174. unsigned int nSize = BN_bn2mpi(this, NULL);
  175. if (nSize < 4)
  176. return 0;
  177. std::vector<unsigned char> vch(nSize);
  178. BN_bn2mpi(this, &vch[0]);
  179. if (vch.size() > 4)
  180. vch[4] &= 0x7f;
  181. uint64 n = 0;
  182. for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
  183. ((unsigned char*)&n)[i] = vch[j];
  184. return n;
  185. }
  186. void setuint64(uint64 n)
  187. {
  188. unsigned char pch[sizeof(n) + 6];
  189. unsigned char* p = pch + 4;
  190. bool fLeadingZeroes = true;
  191. for (int i = 0; i < 8; i++)
  192. {
  193. unsigned char c = (n >> 56) & 0xff;
  194. n <<= 8;
  195. if (fLeadingZeroes)
  196. {
  197. if (c == 0)
  198. continue;
  199. if (c & 0x80)
  200. *p++ = 0;
  201. fLeadingZeroes = false;
  202. }
  203. *p++ = c;
  204. }
  205. unsigned int nSize = p - (pch + 4);
  206. pch[0] = (nSize >> 24) & 0xff;
  207. pch[1] = (nSize >> 16) & 0xff;
  208. pch[2] = (nSize >> 8) & 0xff;
  209. pch[3] = (nSize) & 0xff;
  210. BN_mpi2bn(pch, p - pch, this);
  211. }
  212. void setuint256(uint256 n)
  213. {
  214. unsigned char pch[sizeof(n) + 6];
  215. unsigned char* p = pch + 4;
  216. bool fLeadingZeroes = true;
  217. unsigned char* pbegin = (unsigned char*)&n;
  218. unsigned char* psrc = pbegin + sizeof(n);
  219. while (psrc != pbegin)
  220. {
  221. unsigned char c = *(--psrc);
  222. if (fLeadingZeroes)
  223. {
  224. if (c == 0)
  225. continue;
  226. if (c & 0x80)
  227. *p++ = 0;
  228. fLeadingZeroes = false;
  229. }
  230. *p++ = c;
  231. }
  232. unsigned int nSize = p - (pch + 4);
  233. pch[0] = (nSize >> 24) & 0xff;
  234. pch[1] = (nSize >> 16) & 0xff;
  235. pch[2] = (nSize >> 8) & 0xff;
  236. pch[3] = (nSize >> 0) & 0xff;
  237. BN_mpi2bn(pch, p - pch, this);
  238. }
  239. uint256 getuint256() const
  240. {
  241. unsigned int nSize = BN_bn2mpi(this, NULL);
  242. if (nSize < 4)
  243. return 0;
  244. std::vector<unsigned char> vch(nSize);
  245. BN_bn2mpi(this, &vch[0]);
  246. if (vch.size() > 4)
  247. vch[4] &= 0x7f;
  248. uint256 n = 0;
  249. for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
  250. ((unsigned char*)&n)[i] = vch[j];
  251. return n;
  252. }
  253. void setvch(const std::vector<unsigned char>& vch)
  254. {
  255. std::vector<unsigned char> vch2(vch.size() + 4);
  256. unsigned int nSize = vch.size();
  257. // BIGNUM's byte stream format expects 4 bytes of
  258. // big endian size data info at the front
  259. vch2[0] = (nSize >> 24) & 0xff;
  260. vch2[1] = (nSize >> 16) & 0xff;
  261. vch2[2] = (nSize >> 8) & 0xff;
  262. vch2[3] = (nSize >> 0) & 0xff;
  263. // swap data to big endian
  264. reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
  265. BN_mpi2bn(&vch2[0], vch2.size(), this);
  266. }
  267. std::vector<unsigned char> getvch() const
  268. {
  269. unsigned int nSize = BN_bn2mpi(this, NULL);
  270. if (nSize <= 4)
  271. return std::vector<unsigned char>();
  272. std::vector<unsigned char> vch(nSize);
  273. BN_bn2mpi(this, &vch[0]);
  274. vch.erase(vch.begin(), vch.begin() + 4);
  275. reverse(vch.begin(), vch.end());
  276. return vch;
  277. }
  278. CBigNum& SetCompact(unsigned int nCompact)
  279. {
  280. unsigned int nSize = nCompact >> 24;
  281. std::vector<unsigned char> vch(4 + nSize);
  282. vch[3] = nSize;
  283. if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
  284. if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
  285. if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
  286. BN_mpi2bn(&vch[0], vch.size(), this);
  287. return *this;
  288. }
  289. unsigned int GetCompact() const
  290. {
  291. unsigned int nSize = BN_bn2mpi(this, NULL);
  292. std::vector<unsigned char> vch(nSize);
  293. nSize -= 4;
  294. BN_bn2mpi(this, &vch[0]);
  295. unsigned int nCompact = nSize << 24;
  296. if (nSize >= 1) nCompact |= (vch[4] << 16);
  297. if (nSize >= 2) nCompact |= (vch[5] << 8);
  298. if (nSize >= 3) nCompact |= (vch[6] << 0);
  299. return nCompact;
  300. }
  301. void SetHex(const std::string& str)
  302. {
  303. // skip 0x
  304. const char* psz = str.c_str();
  305. while (isspace(*psz))
  306. psz++;
  307. bool fNegative = false;
  308. if (*psz == '-')
  309. {
  310. fNegative = true;
  311. psz++;
  312. }
  313. if (psz[0] == '0' && tolower(psz[1]) == 'x')
  314. psz += 2;
  315. while (isspace(*psz))
  316. psz++;
  317. // hex string to bignum
  318. static const 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 };
  319. *this = 0;
  320. while (isxdigit(*psz))
  321. {
  322. *this <<= 4;
  323. int n = phexdigit[(unsigned char)*psz++];
  324. *this += n;
  325. }
  326. if (fNegative)
  327. *this = 0 - *this;
  328. }
  329. std::string ToString(int nBase=10) const
  330. {
  331. CAutoBN_CTX pctx;
  332. CBigNum bnBase = nBase;
  333. CBigNum bn0 = 0;
  334. std::string str;
  335. CBigNum bn = *this;
  336. BN_set_negative(&bn, false);
  337. CBigNum dv;
  338. CBigNum rem;
  339. if (BN_cmp(&bn, &bn0) == 0)
  340. return "0";
  341. while (BN_cmp(&bn, &bn0) > 0)
  342. {
  343. if (!BN_div(&dv, &rem, &bn, &bnBase, pctx))
  344. throw bignum_error("CBigNum::ToString() : BN_div failed");
  345. bn = dv;
  346. unsigned int c = rem.getulong();
  347. str += "0123456789abcdef"[c];
  348. }
  349. if (BN_is_negative(this))
  350. str += "-";
  351. reverse(str.begin(), str.end());
  352. return str;
  353. }
  354. std::string GetHex() const
  355. {
  356. return ToString(16);
  357. }
  358. unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
  359. {
  360. return ::GetSerializeSize(getvch(), nType, nVersion);
  361. }
  362. template<typename Stream>
  363. void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const
  364. {
  365. ::Serialize(s, getvch(), nType, nVersion);
  366. }
  367. template<typename Stream>
  368. void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
  369. {
  370. std::vector<unsigned char> vch;
  371. ::Unserialize(s, vch, nType, nVersion);
  372. setvch(vch);
  373. }
  374. /**
  375. * exponentiation with an int. this^e
  376. * @param e the exponent as an int
  377. * @return
  378. */
  379. CBigNum pow(const int e) const {
  380. return this->pow(CBigNum(e));
  381. }
  382. /**
  383. * exponentiation this^e
  384. * @param e the exponent
  385. * @return
  386. */
  387. CBigNum pow(const CBigNum& e) const {
  388. CAutoBN_CTX pctx;
  389. CBigNum ret;
  390. if (!BN_exp(&ret, this, &e, pctx))
  391. throw bignum_error("CBigNum::pow : BN_exp failed");
  392. return ret;
  393. }
  394. /**
  395. * modular multiplication: (this * b) mod m
  396. * @param b operand
  397. * @param m modulus
  398. */
  399. CBigNum mul_mod(const CBigNum& b, const CBigNum& m) const {
  400. CAutoBN_CTX pctx;
  401. CBigNum ret;
  402. if (!BN_mod_mul(&ret, this, &b, &m, pctx))
  403. throw bignum_error("CBigNum::mul_mod : BN_mod_mul failed");
  404. return ret;
  405. }
  406. /**
  407. * modular exponentiation: this^e mod n
  408. * @param e exponent
  409. * @param m modulus
  410. */
  411. CBigNum pow_mod(const CBigNum& e, const CBigNum& m) const {
  412. CAutoBN_CTX pctx;
  413. CBigNum ret;
  414. if( e < 0){
  415. // g^-x = (g^-1)^x
  416. CBigNum inv = this->inverse(m);
  417. CBigNum posE = e * -1;
  418. if (!BN_mod_exp(&ret, &inv, &posE, &m, pctx))
  419. throw bignum_error("CBigNum::pow_mod: BN_mod_exp failed on negative exponent");
  420. }else
  421. if (!BN_mod_exp(&ret, this, &e, &m, pctx))
  422. throw bignum_error("CBigNum::pow_mod : BN_mod_exp failed");
  423. return ret;
  424. }
  425. /**
  426. * Calculates the inverse of this element mod m.
  427. * i.e. i such this*i = 1 mod m
  428. * @param m the modu
  429. * @return the inverse
  430. */
  431. CBigNum inverse(const CBigNum& m) const {
  432. CAutoBN_CTX pctx;
  433. CBigNum ret;
  434. if (!BN_mod_inverse(&ret, this, &m, pctx))
  435. throw bignum_error("CBigNum::inverse*= :BN_mod_inverse");
  436. return ret;
  437. }
  438. /**
  439. * Generates a random (safe) prime of numBits bits
  440. * @param numBits the number of bits
  441. * @param safe true for a safe prime
  442. * @return the prime
  443. */
  444. static CBigNum generatePrime(const unsigned int numBits, bool safe = false) {
  445. CBigNum ret;
  446. if(!BN_generate_prime_ex(&ret, numBits, (safe == true), NULL, NULL, NULL))
  447. throw bignum_error("CBigNum::generatePrime*= :BN_generate_prime_ex");
  448. return ret;
  449. }
  450. /**
  451. * Calculates the greatest common divisor (GCD) of two numbers.
  452. * @param m the second element
  453. * @return the GCD
  454. */
  455. CBigNum gcd( const CBigNum& b) const{
  456. CAutoBN_CTX pctx;
  457. CBigNum ret;
  458. if (!BN_gcd(&ret, this, &b, pctx))
  459. throw bignum_error("CBigNum::gcd*= :BN_gcd");
  460. return ret;
  461. }
  462. /**
  463. * Miller-Rabin primality test on this element
  464. * @param checks: optional, the number of Miller-Rabin tests to run
  465. * default causes error rate of 2^-80.
  466. * @return true if prime
  467. */
  468. bool isPrime(const int checks=BN_prime_checks) const {
  469. CAutoBN_CTX pctx;
  470. int ret = BN_is_prime(this, checks, NULL, pctx, NULL);
  471. if(ret < 0){
  472. throw bignum_error("CBigNum::isPrime :BN_is_prime");
  473. }
  474. return ret;
  475. }
  476. bool isOne() const {
  477. return BN_is_one(this);
  478. }
  479. bool operator!() const
  480. {
  481. return BN_is_zero(this);
  482. }
  483. CBigNum& operator+=(const CBigNum& b)
  484. {
  485. if (!BN_add(this, this, &b))
  486. throw bignum_error("CBigNum::operator+= : BN_add failed");
  487. return *this;
  488. }
  489. CBigNum& operator-=(const CBigNum& b)
  490. {
  491. *this = *this - b;
  492. return *this;
  493. }
  494. CBigNum& operator*=(const CBigNum& b)
  495. {
  496. CAutoBN_CTX pctx;
  497. if (!BN_mul(this, this, &b, pctx))
  498. throw bignum_error("CBigNum::operator*= : BN_mul failed");
  499. return *this;
  500. }
  501. CBigNum& operator/=(const CBigNum& b)
  502. {
  503. *this = *this / b;
  504. return *this;
  505. }
  506. CBigNum& operator%=(const CBigNum& b)
  507. {
  508. *this = *this % b;
  509. return *this;
  510. }
  511. CBigNum& operator<<=(unsigned int shift)
  512. {
  513. if (!BN_lshift(this, this, shift))
  514. throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
  515. return *this;
  516. }
  517. CBigNum& operator>>=(unsigned int shift)
  518. {
  519. // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
  520. // if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL
  521. CBigNum a = 1;
  522. a <<= shift;
  523. if (BN_cmp(&a, this) > 0)
  524. {
  525. *this = 0;
  526. return *this;
  527. }
  528. if (!BN_rshift(this, this, shift))
  529. throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
  530. return *this;
  531. }
  532. CBigNum& operator++()
  533. {
  534. // prefix operator
  535. if (!BN_add(this, this, BN_value_one()))
  536. throw bignum_error("CBigNum::operator++ : BN_add failed");
  537. return *this;
  538. }
  539. const CBigNum operator++(int)
  540. {
  541. // postfix operator
  542. const CBigNum ret = *this;
  543. ++(*this);
  544. return ret;
  545. }
  546. CBigNum& operator--()
  547. {
  548. // prefix operator
  549. CBigNum r;
  550. if (!BN_sub(&r, this, BN_value_one()))
  551. throw bignum_error("CBigNum::operator-- : BN_sub failed");
  552. *this = r;
  553. return *this;
  554. }
  555. const CBigNum operator--(int)
  556. {
  557. // postfix operator
  558. const CBigNum ret = *this;
  559. --(*this);
  560. return ret;
  561. }
  562. friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
  563. friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);
  564. friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);
  565. friend inline const CBigNum operator*(const CBigNum& a, const CBigNum& b);
  566. friend inline bool operator<(const CBigNum& a, const CBigNum& b);
  567. };
  568. inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
  569. {
  570. CBigNum r;
  571. if (!BN_add(&r, &a, &b))
  572. throw bignum_error("CBigNum::operator+ : BN_add failed");
  573. return r;
  574. }
  575. inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
  576. {
  577. CBigNum r;
  578. if (!BN_sub(&r, &a, &b))
  579. throw bignum_error("CBigNum::operator- : BN_sub failed");
  580. return r;
  581. }
  582. inline const CBigNum operator-(const CBigNum& a)
  583. {
  584. CBigNum r(a);
  585. BN_set_negative(&r, !BN_is_negative(&r));
  586. return r;
  587. }
  588. inline const CBigNum operator*(const CBigNum& a, const CBigNum& b)
  589. {
  590. CAutoBN_CTX pctx;
  591. CBigNum r;
  592. if (!BN_mul(&r, &a, &b, pctx))
  593. throw bignum_error("CBigNum::operator* : BN_mul failed");
  594. return r;
  595. }
  596. inline const CBigNum operator/(const CBigNum& a, const CBigNum& b)
  597. {
  598. CAutoBN_CTX pctx;
  599. CBigNum r;
  600. if (!BN_div(&r, NULL, &a, &b, pctx))
  601. throw bignum_error("CBigNum::operator/ : BN_div failed");
  602. return r;
  603. }
  604. inline const CBigNum operator%(const CBigNum& a, const CBigNum& b)
  605. {
  606. CAutoBN_CTX pctx;
  607. CBigNum r;
  608. if (!BN_nnmod(&r, &a, &b, pctx))
  609. throw bignum_error("CBigNum::operator% : BN_div failed");
  610. return r;
  611. }
  612. inline const CBigNum operator<<(const CBigNum& a, unsigned int shift)
  613. {
  614. CBigNum r;
  615. if (!BN_lshift(&r, &a, shift))
  616. throw bignum_error("CBigNum:operator<< : BN_lshift failed");
  617. return r;
  618. }
  619. inline const CBigNum operator>>(const CBigNum& a, unsigned int shift)
  620. {
  621. CBigNum r = a;
  622. r >>= shift;
  623. return r;
  624. }
  625. inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
  626. inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
  627. inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
  628. inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
  629. inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); }
  630. inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); }
  631. inline std::ostream& operator<<(std::ostream &strm, const CBigNum &b) { return strm << b.ToString(10); }
  632. typedef CBigNum Bignum;
  633. #endif