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