From eb2ce22eff2e64ebd30a80d0edc043d27695075a Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 8 Jun 2012 20:45:20 -0700 Subject: [PATCH] Our bit order change to uint256 broke the logic used to convert CBigNum's to and from uint256's. This code is really awful though. We probably should just re-implement what we need of it. (Which is very little.) It looks like the author just didn't know about BN_bin2bn and BIN_bn2bin. --- src/bignum.h | 44 ++++++++------------------------------------ 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/src/bignum.h b/src/bignum.h index 01e614a79c..e11de65026 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -186,47 +186,19 @@ public: BN_mpi2bn(pch, p - pch, this); } - void setuint256(uint256 n) + void setuint256(const uint256& n) { - unsigned char pch[sizeof(n) + 6]; - unsigned char* p = pch + 4; - bool fLeadingZeroes = true; - unsigned char* pbegin = (unsigned char*)&n; - unsigned char* psrc = pbegin + sizeof(n); - while (psrc != pbegin) - { - unsigned char c = *(--psrc); - if (fLeadingZeroes) - { - if (c == 0) - continue; - if (c & 0x80) - *p++ = 0; - fLeadingZeroes = false; - } - *p++ = c; - } - unsigned int nSize = p - (pch + 4); - pch[0] = (nSize >> 24) & 0xff; - pch[1] = (nSize >> 16) & 0xff; - pch[2] = (nSize >> 8) & 0xff; - pch[3] = (nSize >> 0) & 0xff; - BN_mpi2bn(pch, p - pch, this); + BN_bin2bn(n.begin(), n.size(), NULL); } uint256 getuint256() { - unsigned int nSize = BN_bn2mpi(this, NULL); - if (nSize < 4) - return 0; - std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); - if (vch.size() > 4) - vch[4] &= 0x7f; - uint256 n = 0; - for (int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) - ((unsigned char*)&n)[i] = vch[j]; - return n; + uint256 ret; + int size = BN_num_bytes(this); + if (size > ret.size()) + return ret; + BN_bn2bin(this, ret.begin() + (ret.size() - BN_num_bytes(this))); + return ret; } void setvch(const std::vector& vch)