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.
14 //////////////////////////////////////////////////////////////////////////////
21 for (int i = 0; i < WIDTH; i++)
25 uint160::uint160(const basetype& b)
27 for (int i = 0; i < WIDTH; i++)
31 uint160& uint160::operator=(const basetype& b)
33 for (int i = 0; i < WIDTH; i++)
38 uint160::uint160(uint64_t b)
41 pn[1] = (uint32_t)(b >> 32);
42 for (int i = 2; i < WIDTH; i++)
46 uint160& uint160::operator=(uint64_t b)
49 pn[1] = (uint32_t)(b >> 32);
50 for (int i = 2; i < WIDTH; i++)
55 uint160::uint160(const std::string& str)
60 uint160::uint160(const std::vector<unsigned char>& vch)
62 if (vch.size() == sizeof(pn))
63 memcpy(pn, &vch[0], sizeof(pn));
68 //////////////////////////////////////////////////////////////////////////////
75 for (int i = 0; i < WIDTH; i++)
79 uint256::uint256(const basetype& b)
81 for (int i = 0; i < WIDTH; i++)
85 uint256& uint256::operator=(const basetype& b)
87 for (int i = 0; i < WIDTH; i++)
92 uint256::uint256(uint64_t b)
95 pn[1] = (uint32_t)(b >> 32);
96 for (int i = 2; i < WIDTH; i++)
100 uint256& uint256::operator=(uint64_t b)
103 pn[1] = (uint32_t)(b >> 32);
104 for (int i = 2; i < WIDTH; i++)
109 uint256& uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow)
111 int nSize = nCompact >> 24;
112 uint32_t nWord = nCompact & 0x007fffff;
114 nWord >>= 8*(3-nSize);
118 *this <<= 8*(nSize-3);
121 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
123 *pfOverflow = nWord != 0 && ((nSize > 34) ||
124 (nWord > 0xff && nSize > 33) ||
125 (nWord > 0xffff && nSize > 32));
129 uint32_t uint256::GetCompact(bool fNegative) const
131 int nSize = (bits() + 7) / 8;
132 uint32_t nCompact = 0;
134 nCompact = Get64(0) << 8*(3-nSize);
137 uint256 bn = n >> 8*(nSize-3);
138 nCompact = bn.Get64(0);
140 // The 0x00800000 bit denotes the sign.
141 // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
142 if (nCompact & 0x00800000) {
146 assert((nCompact & ~0x007fffff) == 0);
148 nCompact |= nSize << 24;
149 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
153 uint256& uint256::operator*=(uint32_t b32)
156 for (int i = 0; i < WIDTH; i++) {
157 uint64_t n = carry + (uint64_t)b32 * pn[i];
158 pn[i] = n & 0xffffffff;
164 uint256& uint256::operator*=(const uint256& b)
167 for (int j = 0; j < WIDTH; j++) {
169 for (int i = 0; i + j < WIDTH; i++) {
170 uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
171 pn[i + j] = n & 0xffffffff;
179 uint256& uint256::operator/=(const uint256& b)
181 uint256 div = b; // make a copy, so we can shift.
182 uint256 num = *this; // make a copy, so we can subtract.
183 *this = 0; // the quotient.
184 int num_bits = num.bits();
185 int div_bits = div.bits();
187 throw uint256_error("Division by zero");
188 if (div_bits > num_bits) // the result is certainly 0.
190 int shift = num_bits - div_bits;
191 div <<= shift; // shift so that div and num align.
195 pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
197 div >>= 1; // shift back.
200 // num now contains the remainder of the division.
204 uint256::uint256(const std::string& str)
209 uint256::uint256(const std::vector<unsigned char>& vch)
211 if (vch.size() == sizeof(pn))
212 memcpy(pn, &vch[0], sizeof(pn));