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 #include <openssl/rand.h>
18 //////////////////////////////////////////////////////////////////////////////
25 for (int i = 0; i < WIDTH; i++)
29 uint160::uint160(const basetype& b)
31 for (int i = 0; i < WIDTH; i++)
35 uint160& uint160::operator=(const basetype& b)
37 for (int i = 0; i < WIDTH; i++)
42 uint160::uint160(uint64_t b)
45 pn[1] = (uint32_t)(b >> 32);
46 for (int i = 2; i < WIDTH; i++)
50 uint160& uint160::operator=(uint64_t b)
53 pn[1] = (uint32_t)(b >> 32);
54 for (int i = 2; i < WIDTH; i++)
59 uint160::uint160(const std::string& str)
64 uint160::uint160(const std::vector<unsigned char>& vch)
66 if (vch.size() == sizeof(pn))
67 memcpy(pn, &vch[0], sizeof(pn));
72 //////////////////////////////////////////////////////////////////////////////
79 for (int i = 0; i < WIDTH; i++)
83 uint256::uint256(const basetype& b)
85 for (int i = 0; i < WIDTH; i++)
89 uint256& uint256::operator=(const basetype& b)
91 for (int i = 0; i < WIDTH; i++)
96 uint256::uint256(uint64_t b)
99 pn[1] = (uint32_t)(b >> 32);
100 for (int i = 2; i < WIDTH; i++)
104 uint256& uint256::operator=(uint64_t b)
107 pn[1] = (uint32_t)(b >> 32);
108 for (int i = 2; i < WIDTH; i++)
113 uint256& uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow)
115 int nSize = nCompact >> 24;
116 uint32_t nWord = nCompact & 0x007fffff;
118 nWord >>= 8*(3-nSize);
122 *this <<= 8*(nSize-3);
125 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
127 *pfOverflow = nWord != 0 && ((nSize > 34) ||
128 (nWord > 0xff && nSize > 33) ||
129 (nWord > 0xffff && nSize > 32));
133 uint32_t uint256::GetCompact(bool fNegative) const
135 int nSize = (bits() + 7) / 8;
136 uint32_t nCompact = 0;
138 nCompact = Get64(0) << 8*(3-nSize);
141 uint256 bn = n >> 8*(nSize-3);
142 nCompact = bn.Get64(0);
144 // The 0x00800000 bit denotes the sign.
145 // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
146 if (nCompact & 0x00800000) {
150 assert((nCompact & ~0x007fffff) == 0);
152 nCompact |= nSize << 24;
153 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
157 uint256& uint256::operator*=(uint32_t b32)
160 for (int i = 0; i < WIDTH; i++) {
161 uint64_t n = carry + (uint64_t)b32 * pn[i];
162 pn[i] = n & 0xffffffff;
168 uint256& uint256::operator*=(const uint256& b)
172 for (int j = 0; j < WIDTH; j++) {
174 for (int i = 0; i + j < WIDTH; i++) {
175 uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
176 pn[i + j] = n & 0xffffffff;
183 uint256& uint256::operator/=(const uint256& b)
185 uint256 div = b; // make a copy, so we can shift.
186 uint256 num = *this; // make a copy, so we can subtract.
187 *this = 0; // the quotient.
188 int num_bits = num.bits();
189 int div_bits = div.bits();
191 throw uint256_error("Division by zero");
192 if (div_bits > num_bits) // the result is certainly 0.
194 int shift = num_bits - div_bits;
195 div <<= shift; // shift so that div and num align.
199 pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
201 div >>= 1; // shift back.
204 // num now contains the remainder of the division.
208 uint256::uint256(const std::string& str)
213 uint256::uint256(const std::vector<unsigned char>& vch)
215 if (vch.size() == sizeof(pn))
216 memcpy(pn, &vch[0], sizeof(pn));
221 uint256 GetRandHash()
224 RAND_bytes(hash.begin(), hash.size());