It's a c++: use string.clear()
[novacoin.git] / src / bignum.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_BIGNUM_H
6 #define BITCOIN_BIGNUM_H
7
8 #include <stdexcept>
9 #include <vector>
10 #include <openssl/bn.h>
11 #include "util.h"
12
13 /** Errors thrown by the bignum class */
14 class bignum_error : public std::runtime_error
15 {
16 public:
17     explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
18 };
19
20
21 /** RAII encapsulated BN_CTX (OpenSSL bignum context) */
22 class CAutoBN_CTX
23 {
24 protected:
25     BN_CTX* pctx;
26     BN_CTX* operator=(BN_CTX* pnew) { return pctx = pnew; }
27
28 public:
29     CAutoBN_CTX()
30     {
31         pctx = BN_CTX_new();
32         if (pctx == NULL)
33             throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");
34     }
35
36     ~CAutoBN_CTX()
37     {
38         if (pctx != NULL)
39             BN_CTX_free(pctx);
40     }
41
42     operator BN_CTX*() { return pctx; }
43     BN_CTX& operator*() { return *pctx; }
44     BN_CTX** operator&() { return &pctx; }
45     bool operator!() { return (pctx == NULL); }
46 };
47
48
49 /** C++ wrapper for BIGNUM (OpenSSL bignum) */
50 class CBigNum : public BIGNUM
51 {
52 public:
53     CBigNum()
54     {
55         BN_init(this);
56     }
57
58     CBigNum(const CBigNum& b)
59     {
60         BN_init(this);
61         if (!BN_copy(this, &b))
62         {
63             BN_clear_free(this);
64             throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
65         }
66     }
67
68     CBigNum& operator=(const CBigNum& b)
69     {
70         if (!BN_copy(this, &b))
71             throw bignum_error("CBigNum::operator= : BN_copy failed");
72         return (*this);
73     }
74
75     ~CBigNum()
76     {
77         BN_clear_free(this);
78     }
79
80     CBigNum(int8_t  n)  { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); }
81     CBigNum(int16_t n)  { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); }
82     CBigNum(int32_t n)  { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); }
83     CBigNum(int64_t n)  { BN_init(this); if (n >= 0) setuint64(n); else setint64(n); }
84
85     CBigNum(uint8_t  n) { BN_init(this); setuint32(n); }
86     CBigNum(uint16_t n) { BN_init(this); setuint32(n); }
87     CBigNum(uint32_t n) { BN_init(this); setuint32(n); }
88     CBigNum(uint64_t n) { BN_init(this); setuint64(n); }
89
90     explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
91     explicit CBigNum(const std::vector<uint8_t>& vch)
92     {
93         BN_init(this);
94         setvch(vch);
95     }
96
97     /** Generates a cryptographically secure random number between zero and range exclusive
98     * i.e. 0 < returned number < range
99     * @param range The upper bound on the number.
100     * @return
101     */
102     static CBigNum  randBignum(const CBigNum& range) {
103         CBigNum ret;
104         if(!BN_rand_range(&ret, &range)){
105             throw bignum_error("CBigNum:rand element : BN_rand_range failed");
106         }
107         return ret;
108     }
109
110     /** Generates a cryptographically secure random k-bit number
111     * @param k The bit length of the number.
112     * @return
113     */
114     static CBigNum RandKBitBigum(const uint32_t k){
115         CBigNum ret;
116         if(!BN_rand(&ret, k, -1, 0)){
117             throw bignum_error("CBigNum:rand element : BN_rand failed");
118         }
119         return ret;
120     }
121
122     /**Returns the size in bits of the underlying bignum.
123      *
124      * @return the size
125      */
126     int bitSize() const{
127         return  BN_num_bits(this);
128     }
129
130
131     void setuint32(uint32_t n)
132     {
133         if (!BN_set_word(this, n))
134             throw bignum_error("CBigNum conversion from uint32_t : BN_set_word failed");
135     }
136
137     uint32_t getuint32() const
138     {
139         return BN_get_word(this);
140     }
141
142     int32_t getint32() const
143     {
144         uint64_t n = BN_get_word(this);
145         if (!BN_is_negative(this))
146             return (n > (uint64_t)std::numeric_limits<int32_t>::max() ? std::numeric_limits<int32_t>::max() : (int32_t)n);
147         else
148             return (n > (uint64_t)std::numeric_limits<int32_t>::max() ? std::numeric_limits<int32_t>::min() : -(int32_t)n);
149     }
150
151     void setint64(int64_t sn)
152     {
153         uint8_t pch[sizeof(sn) + 6];
154         uint8_t* p = pch + 4;
155         bool fNegative;
156         uint64_t n;
157
158         if (sn < (int64_t)0)
159         {
160             // 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
161             n = -(sn + 1);
162             ++n;
163             fNegative = true;
164         } else {
165             n = sn;
166             fNegative = false;
167         }
168
169         bool fLeadingZeroes = true;
170         for (int i = 0; i < 8; i++)
171         {
172             uint8_t c = (n >> 56) & 0xff;
173             n <<= 8;
174             if (fLeadingZeroes)
175             {
176                 if (c == 0)
177                     continue;
178                 if (c & 0x80)
179                     *p++ = (fNegative ? 0x80 : 0);
180                 else if (fNegative)
181                     c |= 0x80;
182                 fLeadingZeroes = false;
183             }
184             *p++ = c;
185         }
186         uint32_t nSize = (uint32_t) (p - (pch + 4));
187         pch[0] = (nSize >> 24) & 0xff;
188         pch[1] = (nSize >> 16) & 0xff;
189         pch[2] = (nSize >> 8) & 0xff;
190         pch[3] = (nSize) & 0xff;
191         BN_mpi2bn(pch, (int)(p - pch), this);
192     }
193
194     uint64_t getuint64()
195     {
196         size_t nSize = BN_bn2mpi(this, NULL);
197         if (nSize < 4)
198             return 0;
199         std::vector<uint8_t> vch(nSize);
200         BN_bn2mpi(this, &vch[0]);
201         if (vch.size() > 4)
202             vch[4] &= 0x7f;
203         uint64_t n = 0;
204         for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
205             ((uint8_t*)&n)[i] = vch[j];
206         return n;
207     }
208
209     //supress msvc C4127: conditional expression is constant
210     inline bool check(bool value) {return value;}
211
212     void setuint64(uint64_t n)
213     {
214         // Use BN_set_word if word size is sufficient for uint64_t
215         if (check(sizeof(n) <= sizeof(BN_ULONG)))
216         {
217             if (!BN_set_word(this, (BN_ULONG)n))
218                 throw bignum_error("CBigNum conversion from uint64_t : BN_set_word failed");
219             return;
220         }
221
222         uint8_t pch[sizeof(n) + 6];
223         uint8_t* p = pch + 4;
224         bool fLeadingZeroes = true;
225         for (int i = 0; i < 8; i++)
226         {
227             uint8_t c = (n >> 56) & 0xff;
228             n <<= 8;
229             if (fLeadingZeroes)
230             {
231                 if (c == 0)
232                     continue;
233                 if (c & 0x80)
234                     *p++ = 0;
235                 fLeadingZeroes = false;
236             }
237             *p++ = c;
238         }
239         uint32_t nSize = (uint32_t) (p - (pch + 4));
240         pch[0] = (nSize >> 24) & 0xff;
241         pch[1] = (nSize >> 16) & 0xff;
242         pch[2] = (nSize >> 8) & 0xff;
243         pch[3] = (nSize) & 0xff;
244         BN_mpi2bn(pch, (int)(p - pch), this);
245     }
246
247     void setuint160(uint160 n)
248     {
249         uint8_t pch[sizeof(n) + 6];
250         uint8_t* p = pch + 4;
251         bool fLeadingZeroes = true;
252         uint8_t* pbegin = (uint8_t*)&n;
253         uint8_t* psrc = pbegin + sizeof(n);
254         while (psrc != pbegin)
255         {
256             uint8_t c = *(--psrc);
257             if (fLeadingZeroes)
258             {
259                 if (c == 0)
260                     continue;
261                 if (c & 0x80)
262                     *p++ = 0;
263                 fLeadingZeroes = false;
264             }
265             *p++ = c;
266         }
267         uint32_t nSize = (uint32_t) (p - (pch + 4));
268         pch[0] = (nSize >> 24) & 0xff;
269         pch[1] = (nSize >> 16) & 0xff;
270         pch[2] = (nSize >> 8) & 0xff;
271         pch[3] = (nSize >> 0) & 0xff;
272         BN_mpi2bn(pch, (int) (p - pch), this);
273     }
274
275     uint160 getuint160() const
276     {
277         unsigned int nSize = BN_bn2mpi(this, NULL);
278         if (nSize < 4)
279             return 0;
280         std::vector<uint8_t> vch(nSize);
281         BN_bn2mpi(this, &vch[0]);
282         if (vch.size() > 4)
283             vch[4] &= 0x7f;
284         uint160 n = 0;
285         for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
286             ((uint8_t*)&n)[i] = vch[j];
287         return n;
288     }
289
290     void setuint256(uint256 n)
291     {
292         uint8_t pch[sizeof(n) + 6];
293         uint8_t* p = pch + 4;
294         bool fLeadingZeroes = true;
295         uint8_t* pbegin = (uint8_t*)&n;
296         uint8_t* psrc = pbegin + sizeof(n);
297         while (psrc != pbegin)
298         {
299             uint8_t c = *(--psrc);
300             if (fLeadingZeroes)
301             {
302                 if (c == 0)
303                     continue;
304                 if (c & 0x80)
305                     *p++ = 0;
306                 fLeadingZeroes = false;
307             }
308             *p++ = c;
309         }
310         uint32_t nSize = (uint32_t) (p - (pch + 4));
311         pch[0] = (nSize >> 24) & 0xff;
312         pch[1] = (nSize >> 16) & 0xff;
313         pch[2] = (nSize >> 8) & 0xff;
314         pch[3] = (nSize >> 0) & 0xff;
315         BN_mpi2bn(pch, (int) (p - pch), this);
316     }
317
318     uint256 getuint256() const
319     {
320         unsigned int nSize = BN_bn2mpi(this, NULL);
321         if (nSize < 4)
322             return 0;
323         std::vector<uint8_t> vch(nSize);
324         BN_bn2mpi(this, &vch[0]);
325         if (vch.size() > 4)
326             vch[4] &= 0x7f;
327         uint256 n = 0;
328         for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
329             ((uint8_t*)&n)[i] = vch[j];
330         return n;
331     }
332
333     void setBytes(const std::vector<uint8_t>& vchBytes)
334     {
335         BN_bin2bn(&vchBytes[0], (int) vchBytes.size(), this);
336     }
337
338     std::vector<uint8_t> getBytes() const
339     {
340         int nBytes = BN_num_bytes(this);
341
342         std::vector<uint8_t> vchBytes(nBytes);
343
344         int n = BN_bn2bin(this, &vchBytes[0]);
345         if (n != nBytes) {
346             throw bignum_error("CBigNum::getBytes : BN_bn2bin failed");
347         }
348
349         return vchBytes;
350     }
351
352     void setvch(const std::vector<uint8_t>& vch)
353     {
354         std::vector<uint8_t> vch2(vch.size() + 4);
355         uint32_t nSize = (uint32_t) vch.size();
356         // BIGNUM's byte stream format expects 4 bytes of
357         // big endian size data info at the front
358         vch2[0] = (nSize >> 24) & 0xff;
359         vch2[1] = (nSize >> 16) & 0xff;
360         vch2[2] = (nSize >> 8) & 0xff;
361         vch2[3] = (nSize >> 0) & 0xff;
362         // swap data to big endian
363         reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
364         BN_mpi2bn(&vch2[0], (int) vch2.size(), this);
365     }
366
367     std::vector<uint8_t> getvch() const
368     {
369         unsigned int nSize = BN_bn2mpi(this, NULL);
370         if (nSize <= 4)
371             return std::vector<uint8_t>();
372         std::vector<uint8_t> vch(nSize);
373         BN_bn2mpi(this, &vch[0]);
374         vch.erase(vch.begin(), vch.begin() + 4);
375         reverse(vch.begin(), vch.end());
376         return vch;
377     }
378
379     CBigNum& SetCompact(uint32_t nCompact)
380     {
381         uint32_t nSize = nCompact >> 24;
382         std::vector<uint8_t> vch(4 + nSize);
383         vch[3] = nSize;
384         if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
385         if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
386         if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
387         BN_mpi2bn(&vch[0], (int) vch.size(), this);
388         return *this;
389     }
390
391     uint32_t GetCompact() const
392     {
393         uint32_t nSize = BN_bn2mpi(this, NULL);
394         std::vector<uint8_t> vch(nSize);
395         nSize -= 4;
396         BN_bn2mpi(this, &vch[0]);
397         uint32_t nCompact = nSize << 24;
398         if (nSize >= 1) nCompact |= (vch[4] << 16);
399         if (nSize >= 2) nCompact |= (vch[5] << 8);
400         if (nSize >= 3) nCompact |= (vch[6] << 0);
401         return nCompact;
402     }
403
404     void SetHex(const std::string& str)
405     {
406         // skip 0x
407         const char* psz = str.c_str();
408         while (isspace(*psz))
409             psz++;
410         bool fNegative = false;
411         if (*psz == '-')
412         {
413             fNegative = true;
414             psz++;
415         }
416         if (psz[0] == '0' && tolower(psz[1]) == 'x')
417             psz += 2;
418         while (isspace(*psz))
419             psz++;
420
421         // hex string to bignum
422         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 };
423         *this = 0;
424         while (isxdigit(*psz))
425         {
426             *this <<= 4;
427             int n = phexdigit[(uint8_t)*psz++];
428             *this += n;
429         }
430         if (fNegative)
431             *this = 0 - *this;
432     }
433
434     std::string ToString(int nBase=10) const
435     {
436         CAutoBN_CTX pctx;
437         CBigNum bnBase = nBase;
438         CBigNum bn0 = 0;
439         std::string str;
440         CBigNum bn = *this;
441         BN_set_negative(&bn, false);
442         CBigNum dv;
443         CBigNum rem;
444         if (BN_cmp(&bn, &bn0) == 0)
445             return "0";
446         while (BN_cmp(&bn, &bn0) > 0)
447         {
448             if (!BN_div(&dv, &rem, &bn, &bnBase, pctx))
449                 throw bignum_error("CBigNum::ToString() : BN_div failed");
450             bn = dv;
451             uint32_t c = rem.getuint32();
452             str += "0123456789abcdef"[c];
453         }
454         if (BN_is_negative(this))
455             str += "-";
456         reverse(str.begin(), str.end());
457         return str;
458     }
459
460     std::string GetHex() const
461     {
462         return ToString(16);
463     }
464
465     unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
466     {
467         return ::GetSerializeSize(getvch(), nType, nVersion);
468     }
469
470     template<typename Stream>
471     void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const
472     {
473         ::Serialize(s, getvch(), nType, nVersion);
474     }
475
476     template<typename Stream>
477     void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
478     {
479         std::vector<uint8_t> vch;
480         ::Unserialize(s, vch, nType, nVersion);
481         setvch(vch);
482     }
483
484     /**
485     * exponentiation with an int. this^e
486     * @param e the exponent as an int
487     * @return
488     */
489     CBigNum pow(const int e) const {
490         return this->pow(CBigNum(e));
491     }
492
493     /**
494      * exponentiation this^e
495      * @param e the exponent
496      * @return
497      */
498     CBigNum pow(const CBigNum& e) const {
499         CAutoBN_CTX pctx;
500         CBigNum ret;
501         if (!BN_exp(&ret, this, &e, pctx))
502             throw bignum_error("CBigNum::pow : BN_exp failed");
503         return ret;
504     }
505
506     /**
507      * modular multiplication: (this * b) mod m
508      * @param b operand
509      * @param m modulus
510      */
511     CBigNum mul_mod(const CBigNum& b, const CBigNum& m) const {
512         CAutoBN_CTX pctx;
513         CBigNum ret;
514         if (!BN_mod_mul(&ret, this, &b, &m, pctx))
515             throw bignum_error("CBigNum::mul_mod : BN_mod_mul failed");
516
517         return ret;
518     }
519
520     /**
521      * modular exponentiation: this^e mod n
522      * @param e exponent
523      * @param m modulus
524      */
525     CBigNum pow_mod(const CBigNum& e, const CBigNum& m) const {
526         CAutoBN_CTX pctx;
527         CBigNum ret;
528         if( e < 0){
529             // g^-x = (g^-1)^x
530             CBigNum inv = this->inverse(m);
531             CBigNum posE = e * -1;
532             if (!BN_mod_exp(&ret, &inv, &posE, &m, pctx))
533                 throw bignum_error("CBigNum::pow_mod: BN_mod_exp failed on negative exponent");
534         }else
535             if (!BN_mod_exp(&ret, this, &e, &m, pctx))
536                 throw bignum_error("CBigNum::pow_mod : BN_mod_exp failed");
537
538         return ret;
539     }
540
541     /**
542     * Calculates the inverse of this element mod m.
543     * i.e. i such this*i = 1 mod m
544     * @param m the modu
545     * @return the inverse
546     */
547     CBigNum inverse(const CBigNum& m) const {
548         CAutoBN_CTX pctx;
549         CBigNum ret;
550         if (!BN_mod_inverse(&ret, this, &m, pctx))
551             throw bignum_error("CBigNum::inverse*= :BN_mod_inverse");
552         return ret;
553     }
554
555     /**
556      * Generates a random (safe) prime of numBits bits
557      * @param numBits the number of bits
558      * @param safe true for a safe prime
559      * @return the prime
560      */
561     static CBigNum generatePrime(const unsigned int numBits, bool safe = false) {
562         CBigNum ret;
563         if(!BN_generate_prime_ex(&ret, numBits, (safe == true), NULL, NULL, NULL))
564             throw bignum_error("CBigNum::generatePrime*= :BN_generate_prime_ex");
565         return ret;
566     }
567
568     /**
569      * Calculates the greatest common divisor (GCD) of two numbers.
570      * @param m the second element
571      * @return the GCD
572      */
573     CBigNum gcd( const CBigNum& b) const{
574         CAutoBN_CTX pctx;
575         CBigNum ret;
576         if (!BN_gcd(&ret, this, &b, pctx))
577             throw bignum_error("CBigNum::gcd*= :BN_gcd");
578         return ret;
579     }
580
581     /**
582     * Miller-Rabin primality test on this element
583     * @param checks: optional, the number of Miller-Rabin tests to run
584     * default causes error rate of 2^-80.
585     * @return true if prime
586     */
587     bool isPrime(const int checks=BN_prime_checks) const {
588         CAutoBN_CTX pctx;
589         int ret = BN_is_prime(this, checks, NULL, pctx, NULL);
590         if(ret < 0){
591             throw bignum_error("CBigNum::isPrime :BN_is_prime");
592         }
593         return ret != 0;
594     }
595
596     bool isOne() const {
597         return BN_is_one(this);
598     }
599
600
601     bool operator!() const
602     {
603         return BN_is_zero(this);
604     }
605
606     CBigNum& operator+=(const CBigNum& b)
607     {
608         if (!BN_add(this, this, &b))
609             throw bignum_error("CBigNum::operator+= : BN_add failed");
610         return *this;
611     }
612
613     CBigNum& operator-=(const CBigNum& b)
614     {
615         *this = *this - b;
616         return *this;
617     }
618
619     CBigNum& operator*=(const CBigNum& b)
620     {
621         CAutoBN_CTX pctx;
622         if (!BN_mul(this, this, &b, pctx))
623             throw bignum_error("CBigNum::operator*= : BN_mul failed");
624         return *this;
625     }
626
627     CBigNum& operator/=(const CBigNum& b)
628     {
629         *this = *this / b;
630         return *this;
631     }
632
633     CBigNum& operator%=(const CBigNum& b)
634     {
635         *this = *this % b;
636         return *this;
637     }
638
639     CBigNum& operator<<=(unsigned int shift)
640     {
641         if (!BN_lshift(this, this, shift))
642             throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
643         return *this;
644     }
645
646     CBigNum& operator>>=(unsigned int shift)
647     {
648         // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
649         //   if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL
650         CBigNum a = 1;
651         a <<= shift;
652         if (BN_cmp(&a, this) > 0)
653         {
654             *this = 0;
655             return *this;
656         }
657
658         if (!BN_rshift(this, this, shift))
659             throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
660         return *this;
661     }
662
663
664     CBigNum& operator++()
665     {
666         // prefix operator
667         if (!BN_add(this, this, BN_value_one()))
668             throw bignum_error("CBigNum::operator++ : BN_add failed");
669         return *this;
670     }
671
672     const CBigNum operator++(int)
673     {
674         // postfix operator
675         const CBigNum ret = *this;
676         ++(*this);
677         return ret;
678     }
679
680     CBigNum& operator--()
681     {
682         // prefix operator
683         CBigNum r;
684         if (!BN_sub(&r, this, BN_value_one()))
685             throw bignum_error("CBigNum::operator-- : BN_sub failed");
686         *this = r;
687         return *this;
688     }
689
690     const CBigNum operator--(int)
691     {
692         // postfix operator
693         const CBigNum ret = *this;
694         --(*this);
695         return ret;
696     }
697
698
699     friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
700     friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);
701     friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);
702     friend inline const CBigNum operator*(const CBigNum& a, const CBigNum& b);
703     friend inline bool operator<(const CBigNum& a, const CBigNum& b);
704 };
705
706
707
708 inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
709 {
710     CBigNum r;
711     if (!BN_add(&r, &a, &b))
712         throw bignum_error("CBigNum::operator+ : BN_add failed");
713     return r;
714 }
715
716 inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
717 {
718     CBigNum r;
719     if (!BN_sub(&r, &a, &b))
720         throw bignum_error("CBigNum::operator- : BN_sub failed");
721     return r;
722 }
723
724 inline const CBigNum operator-(const CBigNum& a)
725 {
726     CBigNum r(a);
727     BN_set_negative(&r, !BN_is_negative(&r));
728     return r;
729 }
730
731 inline const CBigNum operator*(const CBigNum& a, const CBigNum& b)
732 {
733     CAutoBN_CTX pctx;
734     CBigNum r;
735     if (!BN_mul(&r, &a, &b, pctx))
736         throw bignum_error("CBigNum::operator* : BN_mul failed");
737     return r;
738 }
739
740 inline const CBigNum operator/(const CBigNum& a, const CBigNum& b)
741 {
742     CAutoBN_CTX pctx;
743     CBigNum r;
744     if (!BN_div(&r, NULL, &a, &b, pctx))
745         throw bignum_error("CBigNum::operator/ : BN_div failed");
746     return r;
747 }
748
749 inline const CBigNum operator%(const CBigNum& a, const CBigNum& b)
750 {
751     CAutoBN_CTX pctx;
752     CBigNum r;
753     if (!BN_nnmod(&r, &a, &b, pctx))
754         throw bignum_error("CBigNum::operator% : BN_div failed");
755     return r;
756 }
757
758 inline const CBigNum operator<<(const CBigNum& a, unsigned int shift)
759 {
760     CBigNum r;
761     if (!BN_lshift(&r, &a, shift))
762         throw bignum_error("CBigNum:operator<< : BN_lshift failed");
763     return r;
764 }
765
766 inline const CBigNum operator>>(const CBigNum& a, unsigned int shift)
767 {
768     CBigNum r = a;
769     r >>= shift;
770     return r;
771 }
772
773 inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
774 inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
775 inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
776 inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
777 inline bool operator<(const CBigNum& a, const CBigNum& b)  { return (BN_cmp(&a, &b) < 0); }
778 inline bool operator>(const CBigNum& a, const CBigNum& b)  { return (BN_cmp(&a, &b) > 0); }
779
780 inline std::ostream& operator<<(std::ostream &strm, const CBigNum &b) { return strm << b.ToString(10); }
781
782 typedef  CBigNum Bignum;
783
784 #endif