-
+ 50F0E628773C067FFD2E796319FED525C520B5F97640AAFD2EE87B4834A018ED48772E8E347E088A4566D6E2CAC9D836E2ED4A607457589635B6EB46E928D055
bitcoin/src/bignum.h
(0 . 0)(1 . 538)
380 // Copyright (c) 2009-2010 Satoshi Nakamoto
381 // Copyright (c) 2011 The Bitcoin developers
382 // Distributed under the MIT/X11 software license, see the accompanying
383 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
384 #ifndef BITCOIN_BIGNUM_H
385 #define BITCOIN_BIGNUM_H
386
387 #include <stdexcept>
388 #include <vector>
389 #include <openssl/bn.h>
390
391 #include "util.h"
392
393 class bignum_error : public std::runtime_error
394 {
395 public:
396 explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
397 };
398
399
400
401 class CAutoBN_CTX
402 {
403 protected:
404 BN_CTX* pctx;
405 BN_CTX* operator=(BN_CTX* pnew) { return pctx = pnew; }
406
407 public:
408 CAutoBN_CTX()
409 {
410 pctx = BN_CTX_new();
411 if (pctx == NULL)
412 throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");
413 }
414
415 ~CAutoBN_CTX()
416 {
417 if (pctx != NULL)
418 BN_CTX_free(pctx);
419 }
420
421 operator BN_CTX*() { return pctx; }
422 BN_CTX& operator*() { return *pctx; }
423 BN_CTX** operator&() { return &pctx; }
424 bool operator!() { return (pctx == NULL); }
425 };
426
427
428
429 class CBigNum : public BIGNUM
430 {
431 public:
432 CBigNum()
433 {
434 BN_init(this);
435 }
436
437 CBigNum(const CBigNum& b)
438 {
439 BN_init(this);
440 if (!BN_copy(this, &b))
441 {
442 BN_clear_free(this);
443 throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
444 }
445 }
446
447 CBigNum& operator=(const CBigNum& b)
448 {
449 if (!BN_copy(this, &b))
450 throw bignum_error("CBigNum::operator= : BN_copy failed");
451 return (*this);
452 }
453
454 ~CBigNum()
455 {
456 BN_clear_free(this);
457 }
458
459 CBigNum(char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
460 CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
461 CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
462 CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
463 CBigNum(int64 n) { BN_init(this); setint64(n); }
464 CBigNum(unsigned char n) { BN_init(this); setulong(n); }
465 CBigNum(unsigned short n) { BN_init(this); setulong(n); }
466 CBigNum(unsigned int n) { BN_init(this); setulong(n); }
467 CBigNum(unsigned long n) { BN_init(this); setulong(n); }
468 CBigNum(uint64 n) { BN_init(this); setuint64(n); }
469 explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
470
471 explicit CBigNum(const std::vector<unsigned char>& vch)
472 {
473 BN_init(this);
474 setvch(vch);
475 }
476
477 void setulong(unsigned long n)
478 {
479 if (!BN_set_word(this, n))
480 throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");
481 }
482
483 unsigned long getulong() const
484 {
485 return BN_get_word(this);
486 }
487
488 unsigned int getuint() const
489 {
490 return BN_get_word(this);
491 }
492
493 int getint() const
494 {
495 unsigned long n = BN_get_word(this);
496 if (!BN_is_negative(this))
497 return (n > INT_MAX ? INT_MAX : n);
498 else
499 return (n > INT_MAX ? INT_MIN : -(int)n);
500 }
501
502 void setint64(int64 n)
503 {
504 unsigned char pch[sizeof(n) + 6];
505 unsigned char* p = pch + 4;
506 bool fNegative = false;
507 if (n < (int64)0)
508 {
509 n = -n;
510 fNegative = true;
511 }
512 bool fLeadingZeroes = true;
513 for (int i = 0; i < 8; i++)
514 {
515 unsigned char c = (n >> 56) & 0xff;
516 n <<= 8;
517 if (fLeadingZeroes)
518 {
519 if (c == 0)
520 continue;
521 if (c & 0x80)
522 *p++ = (fNegative ? 0x80 : 0);
523 else if (fNegative)
524 c |= 0x80;
525 fLeadingZeroes = false;
526 }
527 *p++ = c;
528 }
529 unsigned int nSize = p - (pch + 4);
530 pch[0] = (nSize >> 24) & 0xff;
531 pch[1] = (nSize >> 16) & 0xff;
532 pch[2] = (nSize >> 8) & 0xff;
533 pch[3] = (nSize) & 0xff;
534 BN_mpi2bn(pch, p - pch, this);
535 }
536
537 void setuint64(uint64 n)
538 {
539 unsigned char pch[sizeof(n) + 6];
540 unsigned char* p = pch + 4;
541 bool fLeadingZeroes = true;
542 for (int i = 0; i < 8; i++)
543 {
544 unsigned char c = (n >> 56) & 0xff;
545 n <<= 8;
546 if (fLeadingZeroes)
547 {
548 if (c == 0)
549 continue;
550 if (c & 0x80)
551 *p++ = 0;
552 fLeadingZeroes = false;
553 }
554 *p++ = c;
555 }
556 unsigned int nSize = p - (pch + 4);
557 pch[0] = (nSize >> 24) & 0xff;
558 pch[1] = (nSize >> 16) & 0xff;
559 pch[2] = (nSize >> 8) & 0xff;
560 pch[3] = (nSize) & 0xff;
561 BN_mpi2bn(pch, p - pch, this);
562 }
563
564 void setuint256(uint256 n)
565 {
566 unsigned char pch[sizeof(n) + 6];
567 unsigned char* p = pch + 4;
568 bool fLeadingZeroes = true;
569 unsigned char* pbegin = (unsigned char*)&n;
570 unsigned char* psrc = pbegin + sizeof(n);
571 while (psrc != pbegin)
572 {
573 unsigned char c = *(--psrc);
574 if (fLeadingZeroes)
575 {
576 if (c == 0)
577 continue;
578 if (c & 0x80)
579 *p++ = 0;
580 fLeadingZeroes = false;
581 }
582 *p++ = c;
583 }
584 unsigned int nSize = p - (pch + 4);
585 pch[0] = (nSize >> 24) & 0xff;
586 pch[1] = (nSize >> 16) & 0xff;
587 pch[2] = (nSize >> 8) & 0xff;
588 pch[3] = (nSize >> 0) & 0xff;
589 BN_mpi2bn(pch, p - pch, this);
590 }
591
592 uint256 getuint256()
593 {
594 unsigned int nSize = BN_bn2mpi(this, NULL);
595 if (nSize < 4)
596 return 0;
597 std::vector<unsigned char> vch(nSize);
598 BN_bn2mpi(this, &vch[0]);
599 if (vch.size() > 4)
600 vch[4] &= 0x7f;
601 uint256 n = 0;
602 for (int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
603 ((unsigned char*)&n)[i] = vch[j];
604 return n;
605 }
606
607 void setvch(const std::vector<unsigned char>& vch)
608 {
609 std::vector<unsigned char> vch2(vch.size() + 4);
610 unsigned int nSize = vch.size();
611 // BIGNUM's byte stream format expects 4 bytes of
612 // big endian size data info at the front
613 vch2[0] = (nSize >> 24) & 0xff;
614 vch2[1] = (nSize >> 16) & 0xff;
615 vch2[2] = (nSize >> 8) & 0xff;
616 vch2[3] = (nSize >> 0) & 0xff;
617 // swap data to big endian
618 reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
619 BN_mpi2bn(&vch2[0], vch2.size(), this);
620 }
621
622 std::vector<unsigned char> getvch() const
623 {
624 unsigned int nSize = BN_bn2mpi(this, NULL);
625 if (nSize < 4)
626 return std::vector<unsigned char>();
627 std::vector<unsigned char> vch(nSize);
628 BN_bn2mpi(this, &vch[0]);
629 vch.erase(vch.begin(), vch.begin() + 4);
630 reverse(vch.begin(), vch.end());
631 return vch;
632 }
633
634 CBigNum& SetCompact(unsigned int nCompact)
635 {
636 unsigned int nSize = nCompact >> 24;
637 std::vector<unsigned char> vch(4 + nSize);
638 vch[3] = nSize;
639 if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
640 if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
641 if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
642 BN_mpi2bn(&vch[0], vch.size(), this);
643 return *this;
644 }
645
646 unsigned int GetCompact() const
647 {
648 unsigned int nSize = BN_bn2mpi(this, NULL);
649 std::vector<unsigned char> vch(nSize);
650 nSize -= 4;
651 BN_bn2mpi(this, &vch[0]);
652 unsigned int nCompact = nSize << 24;
653 if (nSize >= 1) nCompact |= (vch[4] << 16);
654 if (nSize >= 2) nCompact |= (vch[5] << 8);
655 if (nSize >= 3) nCompact |= (vch[6] << 0);
656 return nCompact;
657 }
658
659 void SetHex(const std::string& str)
660 {
661 // skip 0x
662 const char* psz = str.c_str();
663 while (isspace(*psz))
664 psz++;
665 bool fNegative = false;
666 if (*psz == '-')
667 {
668 fNegative = true;
669 psz++;
670 }
671 if (psz[0] == '0' && tolower(psz[1]) == 'x')
672 psz += 2;
673 while (isspace(*psz))
674 psz++;
675
676 // hex string to bignum
677 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 };
678 *this = 0;
679 while (isxdigit(*psz))
680 {
681 *this <<= 4;
682 int n = phexdigit[*psz++];
683 *this += n;
684 }
685 if (fNegative)
686 *this = 0 - *this;
687 }
688
689 std::string ToString(int nBase=10) const
690 {
691 CAutoBN_CTX pctx;
692 CBigNum bnBase = nBase;
693 CBigNum bn0 = 0;
694 std::string str;
695 CBigNum bn = *this;
696 BN_set_negative(&bn, false);
697 CBigNum dv;
698 CBigNum rem;
699 if (BN_cmp(&bn, &bn0) == 0)
700 return "0";
701 while (BN_cmp(&bn, &bn0) > 0)
702 {
703 if (!BN_div(&dv, &rem, &bn, &bnBase, pctx))
704 throw bignum_error("CBigNum::ToString() : BN_div failed");
705 bn = dv;
706 unsigned int c = rem.getulong();
707 str += "0123456789abcdef"[c];
708 }
709 if (BN_is_negative(this))
710 str += "-";
711 reverse(str.begin(), str.end());
712 return str;
713 }
714
715 std::string GetHex() const
716 {
717 return ToString(16);
718 }
719
720 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const
721 {
722 return ::GetSerializeSize(getvch(), nType, nVersion);
723 }
724
725 template<typename Stream>
726 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
727 {
728 ::Serialize(s, getvch(), nType, nVersion);
729 }
730
731 template<typename Stream>
732 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
733 {
734 std::vector<unsigned char> vch;
735 ::Unserialize(s, vch, nType, nVersion);
736 setvch(vch);
737 }
738
739
740 bool operator!() const
741 {
742 return BN_is_zero(this);
743 }
744
745 CBigNum& operator+=(const CBigNum& b)
746 {
747 if (!BN_add(this, this, &b))
748 throw bignum_error("CBigNum::operator+= : BN_add failed");
749 return *this;
750 }
751
752 CBigNum& operator-=(const CBigNum& b)
753 {
754 *this = *this - b;
755 return *this;
756 }
757
758 CBigNum& operator*=(const CBigNum& b)
759 {
760 CAutoBN_CTX pctx;
761 if (!BN_mul(this, this, &b, pctx))
762 throw bignum_error("CBigNum::operator*= : BN_mul failed");
763 return *this;
764 }
765
766 CBigNum& operator/=(const CBigNum& b)
767 {
768 *this = *this / b;
769 return *this;
770 }
771
772 CBigNum& operator%=(const CBigNum& b)
773 {
774 *this = *this % b;
775 return *this;
776 }
777
778 CBigNum& operator<<=(unsigned int shift)
779 {
780 if (!BN_lshift(this, this, shift))
781 throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
782 return *this;
783 }
784
785 CBigNum& operator>>=(unsigned int shift)
786 {
787 // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
788 // if built on ubuntu 9.04 or 9.10, probably depends on version of openssl
789 CBigNum a = 1;
790 a <<= shift;
791 if (BN_cmp(&a, this) > 0)
792 {
793 *this = 0;
794 return *this;
795 }
796
797 if (!BN_rshift(this, this, shift))
798 throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
799 return *this;
800 }
801
802
803 CBigNum& operator++()
804 {
805 // prefix operator
806 if (!BN_add(this, this, BN_value_one()))
807 throw bignum_error("CBigNum::operator++ : BN_add failed");
808 return *this;
809 }
810
811 const CBigNum operator++(int)
812 {
813 // postfix operator
814 const CBigNum ret = *this;
815 ++(*this);
816 return ret;
817 }
818
819 CBigNum& operator--()
820 {
821 // prefix operator
822 CBigNum r;
823 if (!BN_sub(&r, this, BN_value_one()))
824 throw bignum_error("CBigNum::operator-- : BN_sub failed");
825 *this = r;
826 return *this;
827 }
828
829 const CBigNum operator--(int)
830 {
831 // postfix operator
832 const CBigNum ret = *this;
833 --(*this);
834 return ret;
835 }
836
837
838 friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
839 friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);
840 friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);
841 };
842
843
844
845 inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
846 {
847 CBigNum r;
848 if (!BN_add(&r, &a, &b))
849 throw bignum_error("CBigNum::operator+ : BN_add failed");
850 return r;
851 }
852
853 inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
854 {
855 CBigNum r;
856 if (!BN_sub(&r, &a, &b))
857 throw bignum_error("CBigNum::operator- : BN_sub failed");
858 return r;
859 }
860
861 inline const CBigNum operator-(const CBigNum& a)
862 {
863 CBigNum r(a);
864 BN_set_negative(&r, !BN_is_negative(&r));
865 return r;
866 }
867
868 inline const CBigNum operator*(const CBigNum& a, const CBigNum& b)
869 {
870 CAutoBN_CTX pctx;
871 CBigNum r;
872 if (!BN_mul(&r, &a, &b, pctx))
873 throw bignum_error("CBigNum::operator* : BN_mul failed");
874 return r;
875 }
876
877 inline const CBigNum operator/(const CBigNum& a, const CBigNum& b)
878 {
879 CAutoBN_CTX pctx;
880 CBigNum r;
881 if (!BN_div(&r, NULL, &a, &b, pctx))
882 throw bignum_error("CBigNum::operator/ : BN_div failed");
883 return r;
884 }
885
886 inline const CBigNum operator%(const CBigNum& a, const CBigNum& b)
887 {
888 CAutoBN_CTX pctx;
889 CBigNum r;
890 if (!BN_mod(&r, &a, &b, pctx))
891 throw bignum_error("CBigNum::operator% : BN_div failed");
892 return r;
893 }
894
895 inline const CBigNum operator<<(const CBigNum& a, unsigned int shift)
896 {
897 CBigNum r;
898 if (!BN_lshift(&r, &a, shift))
899 throw bignum_error("CBigNum:operator<< : BN_lshift failed");
900 return r;
901 }
902
903 inline const CBigNum operator>>(const CBigNum& a, unsigned int shift)
904 {
905 CBigNum r = a;
906 r >>= shift;
907 return r;
908 }
909
910 inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
911 inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
912 inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
913 inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
914 inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); }
915 inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); }
916
917 #endif