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.
16 //////////////////////////////////////////////////////////////////////////////
23 for (int i = 0; i < WIDTH; i++)
27 uint160::uint160(const basetype& b)
29 for (int i = 0; i < WIDTH; i++)
33 uint160& uint160::operator=(const basetype& b)
35 for (int i = 0; i < WIDTH; i++)
40 uint160::uint160(uint64_t b)
43 pn[1] = (uint32_t)(b >> 32);
44 for (int i = 2; i < WIDTH; i++)
48 uint160& uint160::operator=(uint64_t b)
51 pn[1] = (uint32_t)(b >> 32);
52 for (int i = 2; i < WIDTH; i++)
57 uint160::uint160(const std::string& str)
62 uint160::uint160(const std::vector<unsigned char>& vch)
64 if (vch.size() == sizeof(pn))
65 memcpy(pn, &vch[0], sizeof(pn));
70 //////////////////////////////////////////////////////////////////////////////
77 for (int i = 0; i < WIDTH; i++)
81 uint256::uint256(const basetype& b)
83 for (int i = 0; i < WIDTH; i++)
87 uint256& uint256::operator=(const basetype& b)
89 for (int i = 0; i < WIDTH; i++)
94 uint256::uint256(uint64_t b)
97 pn[1] = (uint32_t)(b >> 32);
98 for (int i = 2; i < WIDTH; i++)
102 uint256& uint256::operator=(uint64_t b)
105 pn[1] = (uint32_t)(b >> 32);
106 for (int i = 2; i < WIDTH; i++)
111 uint256& uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow)
113 int nSize = nCompact >> 24;
114 uint32_t nWord = nCompact & 0x007fffff;
116 nWord >>= 8*(3-nSize);
120 *this <<= 8*(nSize-3);
123 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
125 *pfOverflow = nWord != 0 && ((nSize > 34) ||
126 (nWord > 0xff && nSize > 33) ||
127 (nWord > 0xffff && nSize > 32));
131 uint32_t uint256::GetCompact(bool fNegative) const
133 int nSize = (bits() + 7) / 8;
134 uint32_t nCompact = 0;
136 nCompact = Get64(0) << 8*(3-nSize);
139 uint256 bn = n >> 8*(nSize-3);
140 nCompact = bn.Get64(0);
142 // The 0x00800000 bit denotes the sign.
143 // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
144 if (nCompact & 0x00800000) {
148 assert((nCompact & ~0x007fffff) == 0);
150 nCompact |= nSize << 24;
151 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
155 uint256& uint256::operator*=(uint32_t b32)
158 for (int i = 0; i < WIDTH; i++) {
159 uint64_t n = carry + (uint64_t)b32 * pn[i];
160 pn[i] = n & 0xffffffff;
166 uint256& uint256::operator*=(const uint256& b)
170 for (int j = 0; j < WIDTH; j++) {
172 for (int i = 0; i + j < WIDTH; i++) {
173 uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
174 pn[i + j] = n & 0xffffffff;
181 uint256& uint256::operator/=(const uint256& b)
183 uint256 div = b; // make a copy, so we can shift.
184 uint256 num = *this; // make a copy, so we can subtract.
185 *this = 0; // the quotient.
186 int num_bits = num.bits();
187 int div_bits = div.bits();
189 throw uint256_error("Division by zero");
190 if (div_bits > num_bits) // the result is certainly 0.
192 int shift = num_bits - div_bits;
193 div <<= shift; // shift so that div and num align.
197 pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
199 div >>= 1; // shift back.
202 // num now contains the remainder of the division.
206 uint256::uint256(const std::string& str)
211 uint256::uint256(const std::vector<unsigned char>& vch)
213 if (vch.size() == sizeof(pn))
214 memcpy(pn, &vch[0], sizeof(pn));