1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
12 //////////////////////////////////////////////////////////////////////////////
19 for (int i = 0; i < WIDTH; i++)
23 uint160::uint160(const basetype& b)
25 for (int i = 0; i < WIDTH; i++)
29 uint160& uint160::operator=(const basetype& b)
31 for (int i = 0; i < WIDTH; i++)
36 uint160::uint160(uint64_t b)
39 pn[1] = (uint32_t)(b >> 32);
40 for (int i = 2; i < WIDTH; i++)
44 uint160& uint160::operator=(uint64_t b)
47 pn[1] = (uint32_t)(b >> 32);
48 for (int i = 2; i < WIDTH; i++)
53 uint160::uint160(const std::string& str)
58 uint160::uint160(const std::vector<unsigned char>& vch)
60 if (vch.size() == sizeof(pn))
61 memcpy(pn, &vch[0], sizeof(pn));
66 //////////////////////////////////////////////////////////////////////////////
73 for (int i = 0; i < WIDTH; i++)
77 uint256::uint256(const basetype& b)
79 for (int i = 0; i < WIDTH; i++)
83 uint256& uint256::operator=(const basetype& b)
85 for (int i = 0; i < WIDTH; i++)
90 uint256::uint256(uint64_t b)
93 pn[1] = (uint32_t)(b >> 32);
94 for (int i = 2; i < WIDTH; i++)
98 uint256& uint256::operator=(uint64_t b)
101 pn[1] = (uint32_t)(b >> 32);
102 for (int i = 2; i < WIDTH; i++)
107 uint256& uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow)
109 int nSize = nCompact >> 24;
110 uint32_t nWord = nCompact & 0x007fffff;
112 nWord >>= 8*(3-nSize);
116 *this <<= 8*(nSize-3);
119 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
121 *pfOverflow = nWord != 0 && ((nSize > 34) ||
122 (nWord > 0xff && nSize > 33) ||
123 (nWord > 0xffff && nSize > 32));
127 uint32_t uint256::GetCompact(bool fNegative) const
129 int nSize = (bits() + 7) / 8;
130 uint32_t nCompact = 0;
132 nCompact = Get64(0) << 8*(3-nSize);
135 uint256 bn = n >> 8*(nSize-3);
136 nCompact = bn.Get64(0);
138 // The 0x00800000 bit denotes the sign.
139 // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
140 if (nCompact & 0x00800000) {
144 assert((nCompact & ~0x007fffff) == 0);
146 nCompact |= nSize << 24;
147 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
151 uint256& uint256::operator*=(uint32_t b32)
154 for (int i = 0; i < WIDTH; i++) {
155 uint64_t n = carry + (uint64_t)b32 * pn[i];
156 pn[i] = n & 0xffffffff;
162 uint256& uint256::operator*=(const uint256& b)
165 for (int j = 0; j < WIDTH; j++) {
167 for (int i = 0; i + j < WIDTH; i++) {
168 uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
169 pn[i + j] = n & 0xffffffff;
177 uint256& uint256::operator/=(const uint256& b)
179 uint256 div = b; // make a copy, so we can shift.
180 uint256 num = *this; // make a copy, so we can subtract.
181 *this = 0; // the quotient.
182 int num_bits = num.bits();
183 int div_bits = div.bits();
185 throw uint256_error("Division by zero");
186 if (div_bits > num_bits) // the result is certainly 0.
188 int shift = num_bits - div_bits;
189 div <<= shift; // shift so that div and num align.
193 pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
195 div >>= 1; // shift back.
198 // num now contains the remainder of the division.
202 uint256::uint256(const std::string& str)
207 uint256::uint256(const std::vector<unsigned char>& vch)
209 if (vch.size() == sizeof(pn))
210 memcpy(pn, &vch[0], sizeof(pn));