4 CAutoBN_CTX::CAutoBN_CTX() {
7 throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");
10 CAutoBN_CTX::~CAutoBN_CTX() {
19 CBigNum::CBigNum(const CBigNum &b) {
20 BIGNUM *dup = BN_dup(b.bn);
22 throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_dup failed");
27 CBigNum &CBigNum::operator=(const CBigNum &b) {
28 BIGNUM *dup = BN_dup(b.bn);
30 throw bignum_error("CBigNum::operator= : BN_dup failed");
36 CBigNum::CBigNum(const BIGNUM *bnp) {
37 BIGNUM *dup = BN_dup(bnp);
39 throw bignum_error("CBigNum::CBigNum(const BIGNUM*) : BN_dup failed");
48 void CBigNum::setuint32(uint32_t n) {
49 if (!BN_set_word(bn, n))
50 throw bignum_error("CBigNum conversion from uint32_t : BN_set_word failed");
53 uint32_t CBigNum::getuint32() const {
54 return BN_get_word(bn);
57 int32_t CBigNum::getint32() const {
58 uint64_t n = BN_get_word(bn);
59 if (!BN_is_negative(bn))
60 return (n > (uint64_t)std::numeric_limits<int32_t>::max() ? std::numeric_limits<int32_t>::max() : (int32_t)n);
62 return (n > (uint64_t)std::numeric_limits<int32_t>::max() ? std::numeric_limits<int32_t>::min() : -(int32_t)n);
65 void CBigNum::setint64(int64_t sn) {
66 uint8_t pch[sizeof(sn) + 6];
73 // 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
82 bool fLeadingZeroes = true;
83 for (int i = 0; i < 8; i++)
85 uint8_t c = (n >> 56) & 0xff;
92 *p++ = (fNegative ? 0x80 : 0);
95 fLeadingZeroes = false;
99 uint32_t nSize = (uint32_t) (p - (pch + 4));
100 pch[0] = (nSize >> 24) & 0xff;
101 pch[1] = (nSize >> 16) & 0xff;
102 pch[2] = (nSize >> 8) & 0xff;
103 pch[3] = (nSize) & 0xff;
104 BN_mpi2bn(pch, (int)(p - pch), bn);
107 uint64_t CBigNum::getuint64() {
108 size_t nSize = BN_bn2mpi(bn, nullptr);
111 std::vector<uint8_t> vch(nSize);
112 BN_bn2mpi(bn, &vch[0]);
116 for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
117 ((uint8_t*)&n)[i] = vch[j];
121 void CBigNum::setuint64(uint64_t n) {
122 // Use BN_set_word if word size is sufficient for uint64_t
123 if (check(sizeof(n) <= sizeof(BN_ULONG)))
125 if (!BN_set_word(bn, (BN_ULONG)n))
126 throw bignum_error("CBigNum conversion from uint64_t : BN_set_word failed");
130 uint8_t pch[sizeof(n) + 6];
131 uint8_t* p = pch + 4;
132 bool fLeadingZeroes = true;
133 for (int i = 0; i < 8; i++)
135 uint8_t c = (n >> 56) & 0xff;
143 fLeadingZeroes = false;
147 uint32_t nSize = (uint32_t) (p - (pch + 4));
148 pch[0] = (nSize >> 24) & 0xff;
149 pch[1] = (nSize >> 16) & 0xff;
150 pch[2] = (nSize >> 8) & 0xff;
151 pch[3] = (nSize) & 0xff;
152 BN_mpi2bn(pch, (int)(p - pch), bn);
155 void CBigNum::setuint160(uint160 n) {
156 uint8_t pch[sizeof(n) + 6];
157 uint8_t* p = pch + 4;
158 bool fLeadingZeroes = true;
159 uint8_t* pbegin = (uint8_t*)&n;
160 uint8_t* psrc = pbegin + sizeof(n);
161 while (psrc != pbegin)
163 uint8_t c = *(--psrc);
170 fLeadingZeroes = false;
174 uint32_t nSize = (uint32_t) (p - (pch + 4));
175 pch[0] = (nSize >> 24) & 0xff;
176 pch[1] = (nSize >> 16) & 0xff;
177 pch[2] = (nSize >> 8) & 0xff;
178 pch[3] = (nSize >> 0) & 0xff;
179 BN_mpi2bn(pch, (int) (p - pch), bn);
182 uint160 CBigNum::getuint160() const {
183 unsigned int nSize = BN_bn2mpi(bn, nullptr);
186 std::vector<uint8_t> vch(nSize);
187 BN_bn2mpi(bn, &vch[0]);
191 for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
192 ((uint8_t*)&n)[i] = vch[j];
196 void CBigNum::setuint256(uint256 n) {
197 uint8_t pch[sizeof(n) + 6];
198 uint8_t* p = pch + 4;
199 bool fLeadingZeroes = true;
200 uint8_t* pbegin = (uint8_t*)&n;
201 uint8_t* psrc = pbegin + sizeof(n);
202 while (psrc != pbegin)
204 uint8_t c = *(--psrc);
211 fLeadingZeroes = false;
215 uint32_t nSize = (uint32_t) (p - (pch + 4));
216 pch[0] = (nSize >> 24) & 0xff;
217 pch[1] = (nSize >> 16) & 0xff;
218 pch[2] = (nSize >> 8) & 0xff;
219 pch[3] = (nSize >> 0) & 0xff;
220 BN_mpi2bn(pch, (int) (p - pch), bn);
223 uint256 CBigNum::getuint256() const {
224 unsigned int nSize = BN_bn2mpi(bn, nullptr);
227 std::vector<uint8_t> vch(nSize);
228 BN_bn2mpi(bn, &vch[0]);
232 for (size_t i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
233 ((uint8_t*)&n)[i] = vch[j];
237 void CBigNum::setBytes(const std::vector<uint8_t> &vchBytes) {
238 BN_bin2bn(&vchBytes[0], (int) vchBytes.size(), bn);
241 std::vector<uint8_t> CBigNum::getBytes() const {
242 int nBytes = BN_num_bytes(bn);
244 std::vector<uint8_t> vchBytes(nBytes);
246 int n = BN_bn2bin(bn, &vchBytes[0]);
248 throw bignum_error("CBigNum::getBytes : BN_bn2bin failed");
254 void CBigNum::setvch(const std::vector<uint8_t> &vch) {
255 std::vector<uint8_t> vch2(vch.size() + 4);
256 uint32_t nSize = (uint32_t) vch.size();
257 // BIGNUM's byte stream format expects 4 bytes of
258 // big endian size data info at the front
259 vch2[0] = (nSize >> 24) & 0xff;
260 vch2[1] = (nSize >> 16) & 0xff;
261 vch2[2] = (nSize >> 8) & 0xff;
262 vch2[3] = (nSize >> 0) & 0xff;
263 // swap data to big endian
264 std::reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
265 BN_mpi2bn(&vch2[0], (int) vch2.size(), bn);
268 std::vector<uint8_t> CBigNum::getvch() const {
269 unsigned int nSize = BN_bn2mpi(bn, nullptr);
272 std::vector<uint8_t> vch(nSize);
273 BN_bn2mpi(bn, &vch[0]);
274 vch.erase(vch.begin(), vch.begin() + 4);
275 std::reverse(vch.begin(), vch.end());
279 CBigNum &CBigNum::SetCompact(uint32_t nCompact) {
280 uint32_t nSize = nCompact >> 24;
281 std::vector<uint8_t> vch(4 + nSize);
283 if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
284 if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
285 if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
286 BN_mpi2bn(&vch[0], (int) vch.size(), bn);
290 uint32_t CBigNum::GetCompact() const {
291 uint32_t nSize = BN_bn2mpi(bn, nullptr);
292 std::vector<uint8_t> vch(nSize);
294 BN_bn2mpi(bn, &vch[0]);
295 uint32_t nCompact = nSize << 24;
296 if (nSize >= 1) nCompact |= (vch[4] << 16);
297 if (nSize >= 2) nCompact |= (vch[5] << 8);
298 if (nSize >= 3) nCompact |= (vch[6] << 0);
302 void CBigNum::SetHex(const std::string &str) {
304 const char* psz = str.c_str();
305 while (isspace(*psz))
307 bool fNegative = false;
313 if (psz[0] == '0' && tolower(psz[1]) == 'x')
315 while (isspace(*psz))
318 // hex string to bignum
319 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 };
321 while (isxdigit(*psz))
324 int n = phexdigit[(uint8_t)*psz++];
331 std::string CBigNum::ToString(int nBase) const {
333 CBigNum bnBase = nBase;
337 BN_set_negative(bn.bn, false);
340 if (BN_cmp(bn.bn, bn0.bn) == 0)
342 while (BN_cmp(bn.bn, bn0.bn) > 0)
344 if (!BN_div(dv.bn, rem.bn, bn.bn, bnBase.bn, pctx))
345 throw bignum_error("CBigNum::ToString() : BN_div failed");
347 uint32_t c = rem.getuint32();
348 str += "0123456789abcdef"[c];
350 if (BN_is_negative(bn.bn))
352 std::reverse(str.begin(), str.end());
356 bool CBigNum::operator!() const {
357 return BN_is_zero(bn);
360 CBigNum &CBigNum::operator+=(const CBigNum &b) {
361 if (!BN_add(bn, bn, b.bn))
362 throw bignum_error("CBigNum::operator+= : BN_add failed");
366 CBigNum &CBigNum::operator-=(const CBigNum &b) {
371 CBigNum &CBigNum::operator*=(const CBigNum &b) {
373 if (!BN_mul(bn, bn, b.bn, pctx))
374 throw bignum_error("CBigNum::operator*= : BN_mul failed");
378 CBigNum &CBigNum::operator/=(const CBigNum &b) {
383 CBigNum &CBigNum::operator%=(const CBigNum &b) {
388 CBigNum &CBigNum::operator<<=(unsigned int shift) {
389 if (!BN_lshift(bn, bn, shift))
390 throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
394 CBigNum &CBigNum::operator>>=(unsigned int shift) {
395 // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
396 // if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL
399 if (BN_cmp(a.bn, bn) > 0)
405 if (!BN_rshift(bn, bn, shift))
406 throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
410 CBigNum &CBigNum::operator++() {
412 if (!BN_add(bn, bn, BN_value_one()))
413 throw bignum_error("CBigNum::operator++ : BN_add failed");
417 CBigNum &CBigNum::operator--() {
420 if (!BN_sub(r.bn, bn, BN_value_one()))
421 throw bignum_error("CBigNum::operator-- : BN_sub failed");
426 const CBigNum CBigNum::operator--(int) {
428 const CBigNum ret = *this;
433 const CBigNum CBigNum::operator++(int) {
435 const CBigNum ret = *this;