Use fixed data types for some basic structures
[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<unsigned char>& 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() : 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         unsigned char pch[sizeof(sn) + 6];
154         unsigned char* 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             unsigned char 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 = 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, p - pch, this);
192     }
193
194     uint64_t getuint64()
195     {
196         unsigned int nSize = BN_bn2mpi(this, NULL);
197         if (nSize < 4)
198             return 0;
199         std::vector<unsigned char> vch(nSize);
200         BN_bn2mpi(this, &vch[0]);
201         if (vch.size() > 4)
202             vch[4] &= 0x7f;
203         uint64_t n = 0;
204         for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
205             ((unsigned char*)&n)[i] = vch[j];
206         return n;
207     }
208
209     void setuint64(uint64_t n)
210     {
211         unsigned char pch[sizeof(n) + 6];
212         unsigned char* p = pch + 4;
213         bool fLeadingZeroes = true;
214         for (int i = 0; i < 8; i++)
215         {
216             unsigned char c = (n >> 56) & 0xff;
217             n <<= 8;
218             if (fLeadingZeroes)
219             {
220                 if (c == 0)
221                     continue;
222                 if (c & 0x80)
223                     *p++ = 0;
224                 fLeadingZeroes = false;
225             }
226             *p++ = c;
227         }
228         uint32_t nSize = p - (pch + 4);
229         pch[0] = (nSize >> 24) & 0xff;
230         pch[1] = (nSize >> 16) & 0xff;
231         pch[2] = (nSize >> 8) & 0xff;
232         pch[3] = (nSize) & 0xff;
233         BN_mpi2bn(pch, p - pch, this);
234     }
235
236     void setuint160(uint160 n)
237     {
238         unsigned char pch[sizeof(n) + 6];
239         unsigned char* p = pch + 4;
240         bool fLeadingZeroes = true;
241         unsigned char* pbegin = (unsigned char*)&n;
242         unsigned char* psrc = pbegin + sizeof(n);
243         while (psrc != pbegin)
244         {
245             unsigned char c = *(--psrc);
246             if (fLeadingZeroes)
247             {
248                 if (c == 0)
249                     continue;
250                 if (c & 0x80)
251                     *p++ = 0;
252                 fLeadingZeroes = false;
253             }
254             *p++ = c;
255         }
256         uint32_t nSize = p - (pch + 4);
257         pch[0] = (nSize >> 24) & 0xff;
258         pch[1] = (nSize >> 16) & 0xff;
259         pch[2] = (nSize >> 8) & 0xff;
260         pch[3] = (nSize >> 0) & 0xff;
261         BN_mpi2bn(pch, p - pch, this);
262     }
263
264     uint160 getuint160() const
265     {
266         unsigned int nSize = BN_bn2mpi(this, NULL);
267         if (nSize < 4)
268             return 0;
269         std::vector<unsigned char> vch(nSize);
270         BN_bn2mpi(this, &vch[0]);
271         if (vch.size() > 4)
272             vch[4] &= 0x7f;
273         uint160 n = 0;
274         for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
275             ((unsigned char*)&n)[i] = vch[j];
276         return n;
277     }
278
279     void setuint256(uint256 n)
280     {
281         unsigned char pch[sizeof(n) + 6];
282         unsigned char* p = pch + 4;
283         bool fLeadingZeroes = true;
284         unsigned char* pbegin = (unsigned char*)&n;
285         unsigned char* psrc = pbegin + sizeof(n);
286         while (psrc != pbegin)
287         {
288             unsigned char c = *(--psrc);
289             if (fLeadingZeroes)
290             {
291                 if (c == 0)
292                     continue;
293                 if (c & 0x80)
294                     *p++ = 0;
295                 fLeadingZeroes = false;
296             }
297             *p++ = c;
298         }
299         uint32_t nSize = p - (pch + 4);
300         pch[0] = (nSize >> 24) & 0xff;
301         pch[1] = (nSize >> 16) & 0xff;
302         pch[2] = (nSize >> 8) & 0xff;
303         pch[3] = (nSize >> 0) & 0xff;
304         BN_mpi2bn(pch, p - pch, this);
305     }
306
307     uint256 getuint256() const
308     {
309         unsigned int nSize = BN_bn2mpi(this, NULL);
310         if (nSize < 4)
311             return 0;
312         std::vector<unsigned char> vch(nSize);
313         BN_bn2mpi(this, &vch[0]);
314         if (vch.size() > 4)
315             vch[4] &= 0x7f;
316         uint256 n = 0;
317         for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
318             ((unsigned char*)&n)[i] = vch[j];
319         return n;
320     }
321
322     void setBytes(const std::vector<unsigned char>& vchBytes)
323     {
324         BN_bin2bn(&vchBytes[0], vchBytes.size(), this);
325     }
326
327     std::vector<unsigned char> getBytes() const
328     {
329         int nBytes = BN_num_bytes(this);
330
331         std::vector<unsigned char> vchBytes(nBytes);
332
333         int n = BN_bn2bin(this, &vchBytes[0]);
334         if (n != nBytes) {
335             throw bignum_error("CBigNum::getBytes : BN_bn2bin failed");
336         }
337
338         return vchBytes;
339     }
340
341     void setvch(const std::vector<unsigned char>& vch)
342     {
343         std::vector<unsigned char> vch2(vch.size() + 4);
344         uint32_t nSize = vch.size();
345         // BIGNUM's byte stream format expects 4 bytes of
346         // big endian size data info at the front
347         vch2[0] = (nSize >> 24) & 0xff;
348         vch2[1] = (nSize >> 16) & 0xff;
349         vch2[2] = (nSize >> 8) & 0xff;
350         vch2[3] = (nSize >> 0) & 0xff;
351         // swap data to big endian
352         reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
353         BN_mpi2bn(&vch2[0], vch2.size(), this);
354     }
355
356     std::vector<unsigned char> getvch() const
357     {
358         unsigned int nSize = BN_bn2mpi(this, NULL);
359         if (nSize <= 4)
360             return std::vector<unsigned char>();
361         std::vector<unsigned char> vch(nSize);
362         BN_bn2mpi(this, &vch[0]);
363         vch.erase(vch.begin(), vch.begin() + 4);
364         reverse(vch.begin(), vch.end());
365         return vch;
366     }
367
368     CBigNum& SetCompact(uint32_t nCompact)
369     {
370         uint32_t nSize = nCompact >> 24;
371         std::vector<unsigned char> vch(4 + nSize);
372         vch[3] = nSize;
373         if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
374         if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
375         if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
376         BN_mpi2bn(&vch[0], vch.size(), this);
377         return *this;
378     }
379
380     uint32_t GetCompact() const
381     {
382         uint32_t nSize = BN_bn2mpi(this, NULL);
383         std::vector<unsigned char> vch(nSize);
384         nSize -= 4;
385         BN_bn2mpi(this, &vch[0]);
386         uint32_t nCompact = nSize << 24;
387         if (nSize >= 1) nCompact |= (vch[4] << 16);
388         if (nSize >= 2) nCompact |= (vch[5] << 8);
389         if (nSize >= 3) nCompact |= (vch[6] << 0);
390         return nCompact;
391     }
392
393     void SetHex(const std::string& str)
394     {
395         // skip 0x
396         const char* psz = str.c_str();
397         while (isspace(*psz))
398             psz++;
399         bool fNegative = false;
400         if (*psz == '-')
401         {
402             fNegative = true;
403             psz++;
404         }
405         if (psz[0] == '0' && tolower(psz[1]) == 'x')
406             psz += 2;
407         while (isspace(*psz))
408             psz++;
409
410         // hex string to bignum
411         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 };
412         *this = 0;
413         while (isxdigit(*psz))
414         {
415             *this <<= 4;
416             int n = phexdigit[(unsigned char)*psz++];
417             *this += n;
418         }
419         if (fNegative)
420             *this = 0 - *this;
421     }
422
423     std::string ToString(int nBase=10) const
424     {
425         CAutoBN_CTX pctx;
426         CBigNum bnBase = nBase;
427         CBigNum bn0 = 0;
428         std::string str;
429         CBigNum bn = *this;
430         BN_set_negative(&bn, false);
431         CBigNum dv;
432         CBigNum rem;
433         if (BN_cmp(&bn, &bn0) == 0)
434             return "0";
435         while (BN_cmp(&bn, &bn0) > 0)
436         {
437             if (!BN_div(&dv, &rem, &bn, &bnBase, pctx))
438                 throw bignum_error("CBigNum::ToString() : BN_div failed");
439             bn = dv;
440             unsigned int c = rem.getuint32();
441             str += "0123456789abcdef"[c];
442         }
443         if (BN_is_negative(this))
444             str += "-";
445         reverse(str.begin(), str.end());
446         return str;
447     }
448
449     std::string GetHex() const
450     {
451         return ToString(16);
452     }
453
454     unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
455     {
456         return ::GetSerializeSize(getvch(), nType, nVersion);
457     }
458
459     template<typename Stream>
460     void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const
461     {
462         ::Serialize(s, getvch(), nType, nVersion);
463     }
464
465     template<typename Stream>
466     void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
467     {
468         std::vector<unsigned char> vch;
469         ::Unserialize(s, vch, nType, nVersion);
470         setvch(vch);
471     }
472
473     /**
474     * exponentiation with an int. this^e
475     * @param e the exponent as an int
476     * @return
477     */
478     CBigNum pow(const int e) const {
479         return this->pow(CBigNum(e));
480     }
481
482     /**
483      * exponentiation this^e
484      * @param e the exponent
485      * @return
486      */
487     CBigNum pow(const CBigNum& e) const {
488         CAutoBN_CTX pctx;
489         CBigNum ret;
490         if (!BN_exp(&ret, this, &e, pctx))
491             throw bignum_error("CBigNum::pow : BN_exp failed");
492         return ret;
493     }
494
495     /**
496      * modular multiplication: (this * b) mod m
497      * @param b operand
498      * @param m modulus
499      */
500     CBigNum mul_mod(const CBigNum& b, const CBigNum& m) const {
501         CAutoBN_CTX pctx;
502         CBigNum ret;
503         if (!BN_mod_mul(&ret, this, &b, &m, pctx))
504             throw bignum_error("CBigNum::mul_mod : BN_mod_mul failed");
505         
506         return ret;
507     }
508
509     /**
510      * modular exponentiation: this^e mod n
511      * @param e exponent
512      * @param m modulus
513      */
514     CBigNum pow_mod(const CBigNum& e, const CBigNum& m) const {
515         CAutoBN_CTX pctx;
516         CBigNum ret;
517         if( e < 0){
518             // g^-x = (g^-1)^x
519             CBigNum inv = this->inverse(m);
520             CBigNum posE = e * -1;
521             if (!BN_mod_exp(&ret, &inv, &posE, &m, pctx))
522                 throw bignum_error("CBigNum::pow_mod: BN_mod_exp failed on negative exponent");
523         }else
524             if (!BN_mod_exp(&ret, this, &e, &m, pctx))
525                 throw bignum_error("CBigNum::pow_mod : BN_mod_exp failed");
526
527         return ret;
528     }
529
530     /**
531     * Calculates the inverse of this element mod m.
532     * i.e. i such this*i = 1 mod m
533     * @param m the modu
534     * @return the inverse
535     */
536     CBigNum inverse(const CBigNum& m) const {
537         CAutoBN_CTX pctx;
538         CBigNum ret;
539         if (!BN_mod_inverse(&ret, this, &m, pctx))
540             throw bignum_error("CBigNum::inverse*= :BN_mod_inverse");
541         return ret;
542     }
543
544     /**
545      * Generates a random (safe) prime of numBits bits
546      * @param numBits the number of bits
547      * @param safe true for a safe prime
548      * @return the prime
549      */
550     static CBigNum generatePrime(const unsigned int numBits, bool safe = false) {
551         CBigNum ret;
552         if(!BN_generate_prime_ex(&ret, numBits, (safe == true), NULL, NULL, NULL))
553             throw bignum_error("CBigNum::generatePrime*= :BN_generate_prime_ex");
554         return ret;
555     }
556
557     /**
558      * Calculates the greatest common divisor (GCD) of two numbers.
559      * @param m the second element
560      * @return the GCD
561      */
562     CBigNum gcd( const CBigNum& b) const{
563         CAutoBN_CTX pctx;
564         CBigNum ret;
565         if (!BN_gcd(&ret, this, &b, pctx))
566             throw bignum_error("CBigNum::gcd*= :BN_gcd");
567         return ret;
568     }
569
570     /**
571     * Miller-Rabin primality test on this element
572     * @param checks: optional, the number of Miller-Rabin tests to run
573     * default causes error rate of 2^-80.
574     * @return true if prime
575     */
576     bool isPrime(const int checks=BN_prime_checks) const {
577         CAutoBN_CTX pctx;
578         int ret = BN_is_prime(this, checks, NULL, pctx, NULL);
579         if(ret < 0){
580             throw bignum_error("CBigNum::isPrime :BN_is_prime");
581         }
582         return ret;
583     }
584
585     bool isOne() const {
586         return BN_is_one(this);
587     }
588
589
590     bool operator!() const
591     {
592         return BN_is_zero(this);
593     }
594
595     CBigNum& operator+=(const CBigNum& b)
596     {
597         if (!BN_add(this, this, &b))
598             throw bignum_error("CBigNum::operator+= : BN_add failed");
599         return *this;
600     }
601
602     CBigNum& operator-=(const CBigNum& b)
603     {
604         *this = *this - b;
605         return *this;
606     }
607
608     CBigNum& operator*=(const CBigNum& b)
609     {
610         CAutoBN_CTX pctx;
611         if (!BN_mul(this, this, &b, pctx))
612             throw bignum_error("CBigNum::operator*= : BN_mul failed");
613         return *this;
614     }
615
616     CBigNum& operator/=(const CBigNum& b)
617     {
618         *this = *this / b;
619         return *this;
620     }
621
622     CBigNum& operator%=(const CBigNum& b)
623     {
624         *this = *this % b;
625         return *this;
626     }
627
628     CBigNum& operator<<=(unsigned int shift)
629     {
630         if (!BN_lshift(this, this, shift))
631             throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
632         return *this;
633     }
634
635     CBigNum& operator>>=(unsigned int shift)
636     {
637         // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
638         //   if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL
639         CBigNum a = 1;
640         a <<= shift;
641         if (BN_cmp(&a, this) > 0)
642         {
643             *this = 0;
644             return *this;
645         }
646
647         if (!BN_rshift(this, this, shift))
648             throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
649         return *this;
650     }
651
652
653     CBigNum& operator++()
654     {
655         // prefix operator
656         if (!BN_add(this, this, BN_value_one()))
657             throw bignum_error("CBigNum::operator++ : BN_add failed");
658         return *this;
659     }
660
661     const CBigNum operator++(int)
662     {
663         // postfix operator
664         const CBigNum ret = *this;
665         ++(*this);
666         return ret;
667     }
668
669     CBigNum& operator--()
670     {
671         // prefix operator
672         CBigNum r;
673         if (!BN_sub(&r, this, BN_value_one()))
674             throw bignum_error("CBigNum::operator-- : BN_sub failed");
675         *this = r;
676         return *this;
677     }
678
679     const CBigNum operator--(int)
680     {
681         // postfix operator
682         const CBigNum ret = *this;
683         --(*this);
684         return ret;
685     }
686
687
688     friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
689     friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);
690     friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);
691     friend inline const CBigNum operator*(const CBigNum& a, const CBigNum& b);
692     friend inline bool operator<(const CBigNum& a, const CBigNum& b);
693 };
694
695
696
697 inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
698 {
699     CBigNum r;
700     if (!BN_add(&r, &a, &b))
701         throw bignum_error("CBigNum::operator+ : BN_add failed");
702     return r;
703 }
704
705 inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
706 {
707     CBigNum r;
708     if (!BN_sub(&r, &a, &b))
709         throw bignum_error("CBigNum::operator- : BN_sub failed");
710     return r;
711 }
712
713 inline const CBigNum operator-(const CBigNum& a)
714 {
715     CBigNum r(a);
716     BN_set_negative(&r, !BN_is_negative(&r));
717     return r;
718 }
719
720 inline const CBigNum operator*(const CBigNum& a, const CBigNum& b)
721 {
722     CAutoBN_CTX pctx;
723     CBigNum r;
724     if (!BN_mul(&r, &a, &b, pctx))
725         throw bignum_error("CBigNum::operator* : BN_mul failed");
726     return r;
727 }
728
729 inline const CBigNum operator/(const CBigNum& a, const CBigNum& b)
730 {
731     CAutoBN_CTX pctx;
732     CBigNum r;
733     if (!BN_div(&r, NULL, &a, &b, pctx))
734         throw bignum_error("CBigNum::operator/ : BN_div failed");
735     return r;
736 }
737
738 inline const CBigNum operator%(const CBigNum& a, const CBigNum& b)
739 {
740     CAutoBN_CTX pctx;
741     CBigNum r;
742     if (!BN_nnmod(&r, &a, &b, pctx))
743         throw bignum_error("CBigNum::operator% : BN_div failed");
744     return r;
745 }
746
747 inline const CBigNum operator<<(const CBigNum& a, unsigned int shift)
748 {
749     CBigNum r;
750     if (!BN_lshift(&r, &a, shift))
751         throw bignum_error("CBigNum:operator<< : BN_lshift failed");
752     return r;
753 }
754
755 inline const CBigNum operator>>(const CBigNum& a, unsigned int shift)
756 {
757     CBigNum r = a;
758     r >>= shift;
759     return r;
760 }
761
762 inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
763 inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
764 inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
765 inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
766 inline bool operator<(const CBigNum& a, const CBigNum& b)  { return (BN_cmp(&a, &b) < 0); }
767 inline bool operator>(const CBigNum& a, const CBigNum& b)  { return (BN_cmp(&a, &b) > 0); }
768
769 inline std::ostream& operator<<(std::ostream &strm, const CBigNum &b) { return strm << b.ToString(10); }
770
771 typedef  CBigNum Bignum;
772
773 #endif