Remove boost namespace from init.cpp
[novacoin.git] / src / uint256.cpp
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.
5
6 #include <limits>
7 #include <stdio.h>
8 #include <string.h>
9 #include <cassert>
10 #include <stdexcept>
11
12 #include <openssl/rand.h>
13
14 #include "uint256.h"
15
16 using namespace std;
17
18 //////////////////////////////////////////////////////////////////////////////
19 //
20 // uint160
21 //
22
23 uint160::uint160()
24 {
25     for (int i = 0; i < WIDTH; i++)
26         pn[i] = 0;
27 }
28
29 uint160::uint160(const basetype& b)
30 {
31     for (int i = 0; i < WIDTH; i++)
32         pn[i] = b.pn[i];
33 }
34
35 uint160& uint160::operator=(const basetype& b)
36 {
37     for (int i = 0; i < WIDTH; i++)
38         pn[i] = b.pn[i];
39     return *this;
40 }
41
42 uint160::uint160(uint64_t b)
43 {
44     pn[0] = (uint32_t)b;
45     pn[1] = (uint32_t)(b >> 32);
46     for (int i = 2; i < WIDTH; i++)
47         pn[i] = 0;
48 }
49
50 uint160& uint160::operator=(uint64_t b)
51 {
52     pn[0] = (uint32_t)b;
53     pn[1] = (uint32_t)(b >> 32);
54     for (int i = 2; i < WIDTH; i++)
55         pn[i] = 0;
56     return *this;
57 }
58
59 uint160::uint160(const std::string& str)
60 {
61     SetHex(str);
62 }
63
64 uint160::uint160(const std::vector<unsigned char>& vch)
65 {
66     if (vch.size() == sizeof(pn))
67         memcpy(pn, &vch[0], sizeof(pn));
68     else
69         *this = 0;
70 }
71
72 //////////////////////////////////////////////////////////////////////////////
73 //
74 // uint256
75 //
76
77 uint256::uint256()
78 {
79     for (int i = 0; i < WIDTH; i++)
80         pn[i] = 0;
81 }
82
83 uint256::uint256(const basetype& b)
84 {
85     for (int i = 0; i < WIDTH; i++)
86         pn[i] = b.pn[i];
87 }
88
89 uint256& uint256::operator=(const basetype& b)
90 {
91     for (int i = 0; i < WIDTH; i++)
92         pn[i] = b.pn[i];
93     return *this;
94 }
95
96 uint256::uint256(uint64_t b)
97 {
98     pn[0] = (uint32_t)b;
99     pn[1] = (uint32_t)(b >> 32);
100     for (int i = 2; i < WIDTH; i++)
101         pn[i] = 0;
102 }
103
104 uint256& uint256::operator=(uint64_t b)
105 {
106     pn[0] = (uint32_t)b;
107     pn[1] = (uint32_t)(b >> 32);
108     for (int i = 2; i < WIDTH; i++)
109         pn[i] = 0;
110     return *this;
111 }
112
113 uint256& uint256::SetCompact(uint32_t nCompact, bool *pfNegative, bool *pfOverflow)
114 {
115     int nSize = nCompact >> 24;
116     uint32_t nWord = nCompact & 0x007fffff;
117     if (nSize <= 3) {
118         nWord >>= 8*(3-nSize);
119         *this = nWord;
120     } else {
121         *this = nWord;
122         *this <<= 8*(nSize-3);
123     }
124     if (pfNegative)
125         *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
126     if (pfOverflow)
127         *pfOverflow = nWord != 0 && ((nSize > 34) ||
128                                  (nWord > 0xff && nSize > 33) ||
129                                  (nWord > 0xffff && nSize > 32));
130     return *this;
131 }
132
133 uint32_t uint256::GetCompact(bool fNegative) const
134 {
135     int nSize = (bits() + 7) / 8;
136     uint32_t nCompact = 0;
137     if (nSize <= 3) {
138         nCompact = Get64(0) << 8*(3-nSize);
139     } else {
140         uint256 n = *this;
141         uint256 bn = n >> 8*(nSize-3);
142         nCompact = bn.Get64(0);
143     }
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) {
147         nCompact >>= 8;
148         nSize++;
149     }
150     assert((nCompact & ~0x007fffff) == 0);
151     assert(nSize < 256);
152     nCompact |= nSize << 24;
153     nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
154     return nCompact;
155 }
156
157 uint256& uint256::operator*=(uint32_t b32)
158 {
159     uint64_t carry = 0;
160     for (int i = 0; i < WIDTH; i++) {
161         uint64_t n = carry + (uint64_t)b32 * pn[i];
162         pn[i] = n & 0xffffffff;
163         carry = n >> 32;
164     }
165     return *this;
166 }
167
168 uint256& uint256::operator*=(const uint256& b)
169 {
170     uint256 a = *this;
171     *this = 0;
172     for (int j = 0; j < WIDTH; j++) {
173         uint64_t carry = 0;
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;
177             carry = n >> 32;
178         }
179     }
180     return *this;
181 }
182
183 uint256& uint256::operator/=(const uint256& b)
184 {
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();
190     if (div_bits == 0)
191         throw uint256_error("Division by zero");
192     if (div_bits > num_bits) // the result is certainly 0.
193         return *this;
194     int shift = num_bits - div_bits;
195     div <<= shift; // shift so that div and num align.
196     while (shift >= 0) {
197         if (num >= div) {
198             num -= div;
199             pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
200         }
201         div >>= 1; // shift back.
202         shift--;
203     }
204     // num now contains the remainder of the division.
205     return *this;
206 }
207
208 uint256::uint256(const std::string& str)
209 {
210     SetHex(str);
211 }
212
213 uint256::uint256(const std::vector<unsigned char>& vch)
214 {
215     if (vch.size() == sizeof(pn))
216         memcpy(pn, &vch[0], sizeof(pn));
217     else
218         *this = 0;
219 }
220
221 uint256 GetRandHash()
222 {
223     uint256 hash;
224     RAND_bytes(hash.begin(), hash.size());
225     return hash;
226 }