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.
9 #include <openssl/rand.h>
17 //////////////////////////////////////////////////////////////////////////////
24 for (int i = 0; i < WIDTH; i++)
28 uint160::uint160(const basetype& b)
30 for (int i = 0; i < WIDTH; i++)
34 uint160& uint160::operator=(const basetype& b)
36 for (int i = 0; i < WIDTH; i++)
41 uint160::uint160(uint64_t b)
44 pn[1] = (uint32_t)(b >> 32);
45 for (int i = 2; i < WIDTH; i++)
49 uint160& uint160::operator=(uint64_t b)
52 pn[1] = (uint32_t)(b >> 32);
53 for (int i = 2; i < WIDTH; i++)
58 uint160::uint160(const std::string& str)
63 uint160::uint160(const std::vector<unsigned char>& vch)
65 if (vch.size() == sizeof(pn))
66 memcpy(pn, &vch[0], sizeof(pn));
71 //////////////////////////////////////////////////////////////////////////////
78 for (int i = 0; i < WIDTH; i++)
82 uint256::uint256(const basetype& b)
84 for (int i = 0; i < WIDTH; i++)
88 uint256& uint256::operator=(const basetype& b)
90 for (int i = 0; i < WIDTH; i++)
95 uint256::uint256(uint64_t b)
98 pn[1] = (uint32_t)(b >> 32);
99 for (int i = 2; i < WIDTH; i++)
103 uint256& uint256::operator=(uint64_t b)
106 pn[1] = (uint32_t)(b >> 32);
107 for (int i = 2; i < WIDTH; i++)
112 uint256& uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow)
114 int nSize = nCompact >> 24;
115 uint32_t nWord = nCompact & 0x007fffff;
117 nWord >>= 8*(3-nSize);
121 *this <<= 8*(nSize-3);
124 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
126 *pfOverflow = nWord != 0 && ((nSize > 34) ||
127 (nWord > 0xff && nSize > 33) ||
128 (nWord > 0xffff && nSize > 32));
132 uint32_t uint256::GetCompact(bool fNegative) const
134 int nSize = (bits() + 7) / 8;
135 uint32_t nCompact = 0;
137 nCompact = Get64(0) << 8*(3-nSize);
140 uint256 bn = n >> 8*(nSize-3);
141 nCompact = bn.Get64(0);
143 // The 0x00800000 bit denotes the sign.
144 // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
145 if (nCompact & 0x00800000) {
149 assert((nCompact & ~0x007fffff) == 0);
151 nCompact |= nSize << 24;
152 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
156 uint256& uint256::operator*=(uint32_t b32)
159 for (int i = 0; i < WIDTH; i++) {
160 uint64_t n = carry + (uint64_t)b32 * pn[i];
161 pn[i] = n & 0xffffffff;
167 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;
182 uint256& uint256::operator/=(const uint256& b)
184 uint256 div = b; // make a copy, so we can shift.
185 uint256 num = *this; // make a copy, so we can subtract.
186 *this = 0; // the quotient.
187 int num_bits = num.bits();
188 int div_bits = div.bits();
190 throw uint256_error("Division by zero");
191 if (div_bits > num_bits) // the result is certainly 0.
193 int shift = num_bits - div_bits;
194 div <<= shift; // shift so that div and num align.
198 pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
200 div >>= 1; // shift back.
203 // num now contains the remainder of the division.
207 uint256::uint256(const std::string& str)
212 uint256::uint256(const std::vector<unsigned char>& vch)
214 if (vch.size() == sizeof(pn))
215 memcpy(pn, &vch[0], sizeof(pn));
220 uint256 GetRandHash()
223 RAND_bytes(hash.begin(), hash.size());