1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_TEST_BIGNUM_H
7 #define BITCOIN_TEST_BIGNUM_H
16 #include <openssl/bn.h>
18 class bignum_error : public std::runtime_error
21 explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
25 /** C++ wrapper for BIGNUM (OpenSSL bignum) */
35 CBigNum(const CBigNum& b)
38 if (!BN_copy(bn, b.bn))
41 throw bignum_error("CBigNum::CBigNum(const CBigNum&): BN_copy failed");
45 CBigNum& operator=(const CBigNum& b)
47 if (!BN_copy(bn, b.bn))
48 throw bignum_error("CBigNum::operator=: BN_copy failed");
57 CBigNum(long long n) { bn = BN_new(); setint64(n); }
59 explicit CBigNum(const std::vector<unsigned char>& vch)
67 BN_ULONG n = BN_get_word(bn);
68 if (!BN_is_negative(bn))
69 return (n > (BN_ULONG)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
71 return (n > (BN_ULONG)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
74 void setint64(int64_t sn)
76 unsigned char pch[sizeof(sn) + 6];
77 unsigned char* p = pch + 4;
83 // Since the minimum signed integer cannot be represented as positive so long as its type is signed,
84 // and it's not well-defined what happens if you make it unsigned before negating it,
85 // we instead increment the negative integer by 1, convert it, then increment the (now positive) unsigned integer by 1 to compensate
94 bool fLeadingZeroes = true;
95 for (int i = 0; i < 8; i++)
97 unsigned char c = (n >> 56) & 0xff;
104 *p++ = (fNegative ? 0x80 : 0);
107 fLeadingZeroes = false;
111 unsigned int nSize = p - (pch + 4);
112 pch[0] = (nSize >> 24) & 0xff;
113 pch[1] = (nSize >> 16) & 0xff;
114 pch[2] = (nSize >> 8) & 0xff;
115 pch[3] = (nSize) & 0xff;
116 BN_mpi2bn(pch, p - pch, bn);
119 void setvch(const std::vector<unsigned char>& vch)
121 std::vector<unsigned char> vch2(vch.size() + 4);
122 unsigned int nSize = vch.size();
123 // BIGNUM's byte stream format expects 4 bytes of
124 // big endian size data info at the front
125 vch2[0] = (nSize >> 24) & 0xff;
126 vch2[1] = (nSize >> 16) & 0xff;
127 vch2[2] = (nSize >> 8) & 0xff;
128 vch2[3] = (nSize >> 0) & 0xff;
129 // swap data to big endian
130 reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
131 BN_mpi2bn(&vch2[0], vch2.size(), bn);
134 std::vector<unsigned char> getvch() const
136 unsigned int nSize = BN_bn2mpi(bn, NULL);
138 return std::vector<unsigned char>();
139 std::vector<unsigned char> vch(nSize);
140 BN_bn2mpi(bn, &vch[0]);
141 vch.erase(vch.begin(), vch.begin() + 4);
142 reverse(vch.begin(), vch.end());
146 friend inline const CBigNum operator+(const CBigNum& a, const CBigNum& b);
147 friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
148 friend inline const CBigNum operator-(const CBigNum& a);
149 friend inline bool operator==(const CBigNum& a, const CBigNum& b);
150 friend inline bool operator!=(const CBigNum& a, const CBigNum& b);
151 friend inline bool operator<=(const CBigNum& a, const CBigNum& b);
152 friend inline bool operator>=(const CBigNum& a, const CBigNum& b);
153 friend inline bool operator<(const CBigNum& a, const CBigNum& b);
154 friend inline bool operator>(const CBigNum& a, const CBigNum& b);
159 inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
162 if (!BN_add(r.bn, a.bn, b.bn))
163 throw bignum_error("CBigNum::operator+: BN_add failed");
167 inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
170 if (!BN_sub(r.bn, a.bn, b.bn))
171 throw bignum_error("CBigNum::operator-: BN_sub failed");
175 inline const CBigNum operator-(const CBigNum& a)
178 BN_set_negative(r.bn, !BN_is_negative(r.bn));
182 inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) == 0); }
183 inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) != 0); }
184 inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) <= 0); }
185 inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) >= 0); }
186 inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) < 0); }
187 inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) > 0); }
189 #endif // BITCOIN_TEST_BIGNUM_H