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