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
17 #include <openssl/bn.h>
19 class bignum_error : public std::runtime_error
22 explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
26 /** C++ wrapper for BIGNUM (OpenSSL bignum) */
37 CBigNum(const CBigNum& b)
41 if (!BN_copy(bn, b.bn))
44 throw bignum_error("CBigNum::CBigNum(const CBigNum&): BN_copy failed");
48 CBigNum& operator=(const CBigNum& b)
50 if (!BN_copy(bn, b.bn))
51 throw bignum_error("CBigNum::operator=: BN_copy failed");
60 CBigNum(long long n) { bn = BN_new(); assert(bn); setint64(n); }
62 explicit CBigNum(const std::vector<unsigned char>& vch)
71 BN_ULONG n = BN_get_word(bn);
72 if (!BN_is_negative(bn))
73 return (n > (BN_ULONG)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
75 return (n > (BN_ULONG)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
78 void setint64(int64_t sn)
80 unsigned char pch[sizeof(sn) + 6];
81 unsigned char* p = pch + 4;
87 // Since the minimum signed integer cannot be represented as positive so long as its type is signed,
88 // and it's not well-defined what happens if you make it unsigned before negating it,
89 // we instead increment the negative integer by 1, convert it, then increment the (now positive) unsigned integer by 1 to compensate
98 bool fLeadingZeroes = true;
99 for (int i = 0; i < 8; i++)
101 unsigned char c = (n >> 56) & 0xff;
108 *p++ = (fNegative ? 0x80 : 0);
111 fLeadingZeroes = false;
115 unsigned int nSize = p - (pch + 4);
116 pch[0] = (nSize >> 24) & 0xff;
117 pch[1] = (nSize >> 16) & 0xff;
118 pch[2] = (nSize >> 8) & 0xff;
119 pch[3] = (nSize) & 0xff;
120 BN_mpi2bn(pch, p - pch, bn);
123 void setvch(const std::vector<unsigned char>& vch)
125 std::vector<unsigned char> vch2(vch.size() + 4);
126 unsigned int nSize = vch.size();
127 // BIGNUM's byte stream format expects 4 bytes of
128 // big endian size data info at the front
129 vch2[0] = (nSize >> 24) & 0xff;
130 vch2[1] = (nSize >> 16) & 0xff;
131 vch2[2] = (nSize >> 8) & 0xff;
132 vch2[3] = (nSize >> 0) & 0xff;
133 // swap data to big endian
134 reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
135 BN_mpi2bn(&vch2[0], vch2.size(), bn);
138 std::vector<unsigned char> getvch() const
140 unsigned int nSize = BN_bn2mpi(bn, NULL);
142 return std::vector<unsigned char>();
143 std::vector<unsigned char> vch(nSize);
144 BN_bn2mpi(bn, &vch[0]);
145 vch.erase(vch.begin(), vch.begin() + 4);
146 reverse(vch.begin(), vch.end());
150 friend inline const CBigNum operator+(const CBigNum& a, const CBigNum& b);
151 friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
152 friend inline const CBigNum operator-(const CBigNum& a);
153 friend inline bool operator==(const CBigNum& a, const CBigNum& b);
154 friend inline bool operator!=(const CBigNum& a, const CBigNum& b);
155 friend inline bool operator<=(const CBigNum& a, const CBigNum& b);
156 friend inline bool operator>=(const CBigNum& a, const CBigNum& b);
157 friend inline bool operator<(const CBigNum& a, const CBigNum& b);
158 friend inline bool operator>(const CBigNum& a, const CBigNum& b);
163 inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
166 if (!BN_add(r.bn, a.bn, b.bn))
167 throw bignum_error("CBigNum::operator+: BN_add failed");
171 inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
174 if (!BN_sub(r.bn, a.bn, b.bn))
175 throw bignum_error("CBigNum::operator-: BN_sub failed");
179 inline const CBigNum operator-(const CBigNum& a)
182 BN_set_negative(r.bn, !BN_is_negative(r.bn));
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); }
188 inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) <= 0); }
189 inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) >= 0); }
190 inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) < 0); }
191 inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) > 0); }
193 #endif // BITCOIN_TEST_BIGNUM_H