]>
Commit | Line | Data |
---|---|---|
8bd66202 GA |
1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | // Distributed under the MIT/X11 software license, see the accompanying | |
3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. | |
4 | ||
5 | #include <limits.h> | |
6 | #include <string> | |
7 | #if defined(_MSC_VER) || defined(__BORLANDC__) | |
8 | typedef __int64 int64; | |
9 | typedef unsigned __int64 uint64; | |
10 | #else | |
11 | typedef long long int64; | |
12 | typedef unsigned long long uint64; | |
13 | #endif | |
14 | #if defined(_MSC_VER) && _MSC_VER < 1300 | |
15 | #define for if (false) ; else for | |
16 | #endif | |
17 | ||
18 | ||
19 | inline int Testuint256AdHoc(vector<string> vArg); | |
20 | ||
21 | ||
22 | ||
23 | // We have to keep a separate base class without constructors | |
24 | // so the compiler will let us use it in a union | |
25 | template<unsigned int BITS> | |
26 | class base_uint | |
27 | { | |
28 | protected: | |
29 | enum { WIDTH=BITS/32 }; | |
30 | unsigned int pn[WIDTH]; | |
31 | public: | |
32 | ||
33 | bool operator!() const | |
34 | { | |
35 | for (int i = 0; i < WIDTH; i++) | |
36 | if (pn[i] != 0) | |
37 | return false; | |
38 | return true; | |
39 | } | |
40 | ||
41 | const base_uint operator~() const | |
42 | { | |
43 | base_uint ret; | |
44 | for (int i = 0; i < WIDTH; i++) | |
45 | ret.pn[i] = ~pn[i]; | |
46 | return ret; | |
47 | } | |
48 | ||
49 | const base_uint operator-() const | |
50 | { | |
51 | base_uint ret; | |
52 | for (int i = 0; i < WIDTH; i++) | |
53 | ret.pn[i] = ~pn[i]; | |
54 | ret++; | |
55 | return ret; | |
56 | } | |
57 | ||
58 | ||
59 | base_uint& operator=(uint64 b) | |
60 | { | |
61 | pn[0] = (unsigned int)b; | |
62 | pn[1] = (unsigned int)(b >> 32); | |
63 | for (int i = 2; i < WIDTH; i++) | |
64 | pn[i] = 0; | |
65 | return *this; | |
66 | } | |
67 | ||
68 | base_uint& operator^=(const base_uint& b) | |
69 | { | |
70 | for (int i = 0; i < WIDTH; i++) | |
71 | pn[i] ^= b.pn[i]; | |
72 | return *this; | |
73 | } | |
74 | ||
75 | base_uint& operator&=(const base_uint& b) | |
76 | { | |
77 | for (int i = 0; i < WIDTH; i++) | |
78 | pn[i] &= b.pn[i]; | |
79 | return *this; | |
80 | } | |
81 | ||
82 | base_uint& operator|=(const base_uint& b) | |
83 | { | |
84 | for (int i = 0; i < WIDTH; i++) | |
85 | pn[i] |= b.pn[i]; | |
86 | return *this; | |
87 | } | |
88 | ||
89 | base_uint& operator^=(uint64 b) | |
90 | { | |
91 | pn[0] ^= (unsigned int)b; | |
92 | pn[1] ^= (unsigned int)(b >> 32); | |
93 | return *this; | |
94 | } | |
95 | ||
96 | base_uint& operator&=(uint64 b) | |
97 | { | |
98 | pn[0] &= (unsigned int)b; | |
99 | pn[1] &= (unsigned int)(b >> 32); | |
100 | return *this; | |
101 | } | |
102 | ||
103 | base_uint& operator|=(uint64 b) | |
104 | { | |
105 | pn[0] |= (unsigned int)b; | |
106 | pn[1] |= (unsigned int)(b >> 32); | |
107 | return *this; | |
108 | } | |
109 | ||
110 | base_uint& operator<<=(unsigned int shift) | |
111 | { | |
112 | base_uint a(*this); | |
113 | for (int i = 0; i < WIDTH; i++) | |
114 | pn[i] = 0; | |
115 | int k = shift / 32; | |
116 | shift = shift % 32; | |
117 | for (int i = 0; i < WIDTH; i++) | |
118 | { | |
119 | if (i+k+1 < WIDTH && shift != 0) | |
120 | pn[i+k+1] |= (a.pn[i] >> (32-shift)); | |
121 | if (i+k < WIDTH) | |
122 | pn[i+k] |= (a.pn[i] << shift); | |
123 | } | |
124 | return *this; | |
125 | } | |
126 | ||
127 | base_uint& operator>>=(unsigned int shift) | |
128 | { | |
129 | base_uint a(*this); | |
130 | for (int i = 0; i < WIDTH; i++) | |
131 | pn[i] = 0; | |
132 | int k = shift / 32; | |
133 | shift = shift % 32; | |
134 | for (int i = 0; i < WIDTH; i++) | |
135 | { | |
136 | if (i-k-1 >= 0 && shift != 0) | |
137 | pn[i-k-1] |= (a.pn[i] << (32-shift)); | |
138 | if (i-k >= 0) | |
139 | pn[i-k] |= (a.pn[i] >> shift); | |
140 | } | |
141 | return *this; | |
142 | } | |
143 | ||
144 | base_uint& operator+=(const base_uint& b) | |
145 | { | |
146 | uint64 carry = 0; | |
147 | for (int i = 0; i < WIDTH; i++) | |
148 | { | |
149 | uint64 n = carry + pn[i] + b.pn[i]; | |
150 | pn[i] = n & 0xffffffff; | |
151 | carry = n >> 32; | |
152 | } | |
153 | return *this; | |
154 | } | |
155 | ||
156 | base_uint& operator-=(const base_uint& b) | |
157 | { | |
158 | *this += -b; | |
159 | return *this; | |
160 | } | |
161 | ||
162 | base_uint& operator+=(uint64 b64) | |
163 | { | |
164 | base_uint b; | |
165 | b = b64; | |
166 | *this += b; | |
167 | return *this; | |
168 | } | |
169 | ||
170 | base_uint& operator-=(uint64 b64) | |
171 | { | |
172 | base_uint b; | |
173 | b = b64; | |
174 | *this += -b; | |
175 | return *this; | |
176 | } | |
177 | ||
178 | ||
179 | base_uint& operator++() | |
180 | { | |
181 | // prefix operator | |
182 | int i = 0; | |
183 | while (++pn[i] == 0 && i < WIDTH-1) | |
184 | i++; | |
185 | return *this; | |
186 | } | |
187 | ||
188 | const base_uint operator++(int) | |
189 | { | |
190 | // postfix operator | |
191 | const base_uint ret = *this; | |
192 | ++(*this); | |
193 | return ret; | |
194 | } | |
195 | ||
196 | base_uint& operator--() | |
197 | { | |
198 | // prefix operator | |
199 | int i = 0; | |
200 | while (--pn[i] == -1 && i < WIDTH-1) | |
201 | i++; | |
202 | return *this; | |
203 | } | |
204 | ||
205 | const base_uint operator--(int) | |
206 | { | |
207 | // postfix operator | |
208 | const base_uint ret = *this; | |
209 | --(*this); | |
210 | return ret; | |
211 | } | |
212 | ||
213 | ||
214 | friend inline bool operator<(const base_uint& a, const base_uint& b) | |
215 | { | |
216 | for (int i = base_uint::WIDTH-1; i >= 0; i--) | |
217 | { | |
218 | if (a.pn[i] < b.pn[i]) | |
219 | return true; | |
220 | else if (a.pn[i] > b.pn[i]) | |
221 | return false; | |
222 | } | |
223 | return false; | |
224 | } | |
225 | ||
226 | friend inline bool operator<=(const base_uint& a, const base_uint& b) | |
227 | { | |
228 | for (int i = base_uint::WIDTH-1; i >= 0; i--) | |
229 | { | |
230 | if (a.pn[i] < b.pn[i]) | |
231 | return true; | |
232 | else if (a.pn[i] > b.pn[i]) | |
233 | return false; | |
234 | } | |
235 | return true; | |
236 | } | |
237 | ||
238 | friend inline bool operator>(const base_uint& a, const base_uint& b) | |
239 | { | |
240 | for (int i = base_uint::WIDTH-1; i >= 0; i--) | |
241 | { | |
242 | if (a.pn[i] > b.pn[i]) | |
243 | return true; | |
244 | else if (a.pn[i] < b.pn[i]) | |
245 | return false; | |
246 | } | |
247 | return false; | |
248 | } | |
249 | ||
250 | friend inline bool operator>=(const base_uint& a, const base_uint& b) | |
251 | { | |
252 | for (int i = base_uint::WIDTH-1; i >= 0; i--) | |
253 | { | |
254 | if (a.pn[i] > b.pn[i]) | |
255 | return true; | |
256 | else if (a.pn[i] < b.pn[i]) | |
257 | return false; | |
258 | } | |
259 | return true; | |
260 | } | |
261 | ||
262 | friend inline bool operator==(const base_uint& a, const base_uint& b) | |
263 | { | |
264 | for (int i = 0; i < base_uint::WIDTH; i++) | |
265 | if (a.pn[i] != b.pn[i]) | |
266 | return false; | |
267 | return true; | |
268 | } | |
269 | ||
270 | friend inline bool operator==(const base_uint& a, uint64 b) | |
271 | { | |
272 | if (a.pn[0] != (unsigned int)b) | |
273 | return false; | |
274 | if (a.pn[1] != (unsigned int)(b >> 32)) | |
275 | return false; | |
276 | for (int i = 2; i < base_uint::WIDTH; i++) | |
277 | if (a.pn[i] != 0) | |
278 | return false; | |
279 | return true; | |
280 | } | |
281 | ||
282 | friend inline bool operator!=(const base_uint& a, const base_uint& b) | |
283 | { | |
284 | return (!(a == b)); | |
285 | } | |
286 | ||
287 | friend inline bool operator!=(const base_uint& a, uint64 b) | |
288 | { | |
289 | return (!(a == b)); | |
290 | } | |
291 | ||
292 | ||
293 | ||
294 | std::string GetHex() const | |
295 | { | |
296 | char psz[sizeof(pn)*2 + 1]; | |
297 | for (int i = 0; i < sizeof(pn); i++) | |
298 | sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]); | |
299 | return string(psz, psz + sizeof(pn)*2); | |
300 | } | |
301 | ||
302 | void SetHex(const char* psz) | |
303 | { | |
304 | for (int i = 0; i < WIDTH; i++) | |
305 | pn[i] = 0; | |
306 | ||
307 | // skip leading spaces | |
308 | while (isspace(*psz)) | |
309 | psz++; | |
310 | ||
311 | // skip 0x | |
312 | if (psz[0] == '0' && tolower(psz[1]) == 'x') | |
313 | psz += 2; | |
314 | ||
315 | // hex string to uint | |
316 | static char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 }; | |
317 | const char* pbegin = psz; | |
318 | while (phexdigit[*psz] || *psz == '0') | |
319 | psz++; | |
320 | psz--; | |
321 | unsigned char* p1 = (unsigned char*)pn; | |
322 | unsigned char* pend = p1 + WIDTH * 4; | |
323 | while (psz >= pbegin && p1 < pend) | |
324 | { | |
325 | *p1 = phexdigit[(unsigned char)*psz--]; | |
326 | if (psz >= pbegin) | |
327 | { | |
328 | *p1 |= (phexdigit[(unsigned char)*psz--] << 4); | |
329 | p1++; | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
334 | void SetHex(const std::string& str) | |
335 | { | |
336 | SetHex(str.c_str()); | |
337 | } | |
338 | ||
339 | std::string ToString() const | |
340 | { | |
341 | return (GetHex()); | |
342 | } | |
343 | ||
344 | unsigned char* begin() | |
345 | { | |
346 | return (unsigned char*)&pn[0]; | |
347 | } | |
348 | ||
349 | unsigned char* end() | |
350 | { | |
351 | return (unsigned char*)&pn[WIDTH]; | |
352 | } | |
353 | ||
354 | unsigned int size() | |
355 | { | |
356 | return sizeof(pn); | |
357 | } | |
358 | ||
359 | ||
360 | unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const | |
361 | { | |
362 | return sizeof(pn); | |
363 | } | |
364 | ||
365 | template<typename Stream> | |
366 | void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const | |
367 | { | |
368 | s.write((char*)pn, sizeof(pn)); | |
369 | } | |
370 | ||
371 | template<typename Stream> | |
372 | void Unserialize(Stream& s, int nType=0, int nVersion=VERSION) | |
373 | { | |
374 | s.read((char*)pn, sizeof(pn)); | |
375 | } | |
376 | ||
377 | ||
378 | friend class uint160; | |
379 | friend class uint256; | |
380 | friend inline int Testuint256AdHoc(vector<string> vArg); | |
381 | }; | |
382 | ||
383 | typedef base_uint<160> base_uint160; | |
384 | typedef base_uint<256> base_uint256; | |
385 | ||
386 | ||
387 | ||
388 | // | |
389 | // uint160 and uint256 could be implemented as templates, but to keep | |
390 | // compile errors and debugging cleaner, they're copy and pasted. | |
391 | // | |
392 | ||
393 | ||
394 | ||
395 | ////////////////////////////////////////////////////////////////////////////// | |
396 | // | |
397 | // uint160 | |
398 | // | |
399 | ||
400 | class uint160 : public base_uint160 | |
401 | { | |
402 | public: | |
403 | typedef base_uint160 basetype; | |
404 | ||
405 | uint160() | |
406 | { | |
407 | for (int i = 0; i < WIDTH; i++) | |
408 | pn[i] = 0; | |
409 | } | |
410 | ||
411 | uint160(const basetype& b) | |
412 | { | |
413 | for (int i = 0; i < WIDTH; i++) | |
414 | pn[i] = b.pn[i]; | |
415 | } | |
416 | ||
417 | uint160& operator=(const basetype& b) | |
418 | { | |
419 | for (int i = 0; i < WIDTH; i++) | |
420 | pn[i] = b.pn[i]; | |
421 | return *this; | |
422 | } | |
423 | ||
424 | uint160(uint64 b) | |
425 | { | |
426 | pn[0] = (unsigned int)b; | |
427 | pn[1] = (unsigned int)(b >> 32); | |
428 | for (int i = 2; i < WIDTH; i++) | |
429 | pn[i] = 0; | |
430 | } | |
431 | ||
432 | uint160& operator=(uint64 b) | |
433 | { | |
434 | pn[0] = (unsigned int)b; | |
435 | pn[1] = (unsigned int)(b >> 32); | |
436 | for (int i = 2; i < WIDTH; i++) | |
437 | pn[i] = 0; | |
438 | return *this; | |
439 | } | |
440 | ||
441 | explicit uint160(const std::string& str) | |
442 | { | |
443 | SetHex(str); | |
444 | } | |
445 | ||
446 | explicit uint160(const std::vector<unsigned char>& vch) | |
447 | { | |
448 | if (vch.size() == sizeof(pn)) | |
449 | memcpy(pn, &vch[0], sizeof(pn)); | |
450 | else | |
451 | *this = 0; | |
452 | } | |
453 | }; | |
454 | ||
455 | inline bool operator==(const uint160& a, uint64 b) { return (base_uint160)a == b; } | |
456 | inline bool operator!=(const uint160& a, uint64 b) { return (base_uint160)a != b; } | |
457 | inline const uint160 operator<<(const base_uint160& a, unsigned int shift) { return uint160(a) <<= shift; } | |
458 | inline const uint160 operator>>(const base_uint160& a, unsigned int shift) { return uint160(a) >>= shift; } | |
459 | inline const uint160 operator<<(const uint160& a, unsigned int shift) { return uint160(a) <<= shift; } | |
460 | inline const uint160 operator>>(const uint160& a, unsigned int shift) { return uint160(a) >>= shift; } | |
461 | ||
462 | inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; } | |
463 | inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; } | |
464 | inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; } | |
465 | inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; } | |
466 | inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; } | |
467 | ||
468 | inline bool operator<(const base_uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; } | |
469 | inline bool operator<=(const base_uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; } | |
470 | inline bool operator>(const base_uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; } | |
471 | inline bool operator>=(const base_uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; } | |
472 | inline bool operator==(const base_uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; } | |
473 | inline bool operator!=(const base_uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; } | |
474 | inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; } | |
475 | inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; } | |
476 | inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; } | |
477 | inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; } | |
478 | inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; } | |
479 | ||
480 | inline bool operator<(const uint160& a, const base_uint160& b) { return (base_uint160)a < (base_uint160)b; } | |
481 | inline bool operator<=(const uint160& a, const base_uint160& b) { return (base_uint160)a <= (base_uint160)b; } | |
482 | inline bool operator>(const uint160& a, const base_uint160& b) { return (base_uint160)a > (base_uint160)b; } | |
483 | inline bool operator>=(const uint160& a, const base_uint160& b) { return (base_uint160)a >= (base_uint160)b; } | |
484 | inline bool operator==(const uint160& a, const base_uint160& b) { return (base_uint160)a == (base_uint160)b; } | |
485 | inline bool operator!=(const uint160& a, const base_uint160& b) { return (base_uint160)a != (base_uint160)b; } | |
486 | inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^ (base_uint160)b; } | |
487 | inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a & (base_uint160)b; } | |
488 | inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a | (base_uint160)b; } | |
489 | inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a + (base_uint160)b; } | |
490 | inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a - (base_uint160)b; } | |
491 | ||
492 | inline bool operator<(const uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; } | |
493 | inline bool operator<=(const uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; } | |
494 | inline bool operator>(const uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; } | |
495 | inline bool operator>=(const uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; } | |
496 | inline bool operator==(const uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; } | |
497 | inline bool operator!=(const uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; } | |
498 | inline const uint160 operator^(const uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; } | |
499 | inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; } | |
500 | inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; } | |
501 | inline const uint160 operator+(const uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; } | |
502 | inline const uint160 operator-(const uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; } | |
503 | ||
504 | ||
505 | ||
506 | ||
507 | ||
508 | ||
509 | ////////////////////////////////////////////////////////////////////////////// | |
510 | // | |
511 | // uint256 | |
512 | // | |
513 | ||
514 | class uint256 : public base_uint256 | |
515 | { | |
516 | public: | |
517 | typedef base_uint256 basetype; | |
518 | ||
519 | uint256() | |
520 | { | |
521 | for (int i = 0; i < WIDTH; i++) | |
522 | pn[i] = 0; | |
523 | } | |
524 | ||
525 | uint256(const basetype& b) | |
526 | { | |
527 | for (int i = 0; i < WIDTH; i++) | |
528 | pn[i] = b.pn[i]; | |
529 | } | |
530 | ||
531 | uint256& operator=(const basetype& b) | |
532 | { | |
533 | for (int i = 0; i < WIDTH; i++) | |
534 | pn[i] = b.pn[i]; | |
535 | return *this; | |
536 | } | |
537 | ||
538 | uint256(uint64 b) | |
539 | { | |
540 | pn[0] = (unsigned int)b; | |
541 | pn[1] = (unsigned int)(b >> 32); | |
542 | for (int i = 2; i < WIDTH; i++) | |
543 | pn[i] = 0; | |
544 | } | |
545 | ||
546 | uint256& operator=(uint64 b) | |
547 | { | |
548 | pn[0] = (unsigned int)b; | |
549 | pn[1] = (unsigned int)(b >> 32); | |
550 | for (int i = 2; i < WIDTH; i++) | |
551 | pn[i] = 0; | |
552 | return *this; | |
553 | } | |
554 | ||
555 | explicit uint256(const std::string& str) | |
556 | { | |
557 | SetHex(str); | |
558 | } | |
559 | ||
560 | explicit uint256(const std::vector<unsigned char>& vch) | |
561 | { | |
562 | if (vch.size() == sizeof(pn)) | |
563 | memcpy(pn, &vch[0], sizeof(pn)); | |
564 | else | |
565 | *this = 0; | |
566 | } | |
567 | }; | |
568 | ||
569 | inline bool operator==(const uint256& a, uint64 b) { return (base_uint256)a == b; } | |
570 | inline bool operator!=(const uint256& a, uint64 b) { return (base_uint256)a != b; } | |
571 | inline const uint256 operator<<(const base_uint256& a, unsigned int shift) { return uint256(a) <<= shift; } | |
572 | inline const uint256 operator>>(const base_uint256& a, unsigned int shift) { return uint256(a) >>= shift; } | |
573 | inline const uint256 operator<<(const uint256& a, unsigned int shift) { return uint256(a) <<= shift; } | |
574 | inline const uint256 operator>>(const uint256& a, unsigned int shift) { return uint256(a) >>= shift; } | |
575 | ||
576 | inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; } | |
577 | inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; } | |
578 | inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; } | |
579 | inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; } | |
580 | inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; } | |
581 | ||
582 | inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; } | |
583 | inline bool operator<=(const base_uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; } | |
584 | inline bool operator>(const base_uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; } | |
585 | inline bool operator>=(const base_uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; } | |
586 | inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; } | |
587 | inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; } | |
588 | inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; } | |
589 | inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; } | |
590 | inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; } | |
591 | inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; } | |
592 | inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; } | |
593 | ||
594 | inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; } | |
595 | inline bool operator<=(const uint256& a, const base_uint256& b) { return (base_uint256)a <= (base_uint256)b; } | |
596 | inline bool operator>(const uint256& a, const base_uint256& b) { return (base_uint256)a > (base_uint256)b; } | |
597 | inline bool operator>=(const uint256& a, const base_uint256& b) { return (base_uint256)a >= (base_uint256)b; } | |
598 | inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; } | |
599 | inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; } | |
600 | inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; } | |
601 | inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a & (base_uint256)b; } | |
602 | inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; } | |
603 | inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a + (base_uint256)b; } | |
604 | inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a - (base_uint256)b; } | |
605 | ||
606 | inline bool operator<(const uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; } | |
607 | inline bool operator<=(const uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; } | |
608 | inline bool operator>(const uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; } | |
609 | inline bool operator>=(const uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; } | |
610 | inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; } | |
611 | inline bool operator!=(const uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; } | |
612 | inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; } | |
613 | inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; } | |
614 | inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; } | |
615 | inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; } | |
616 | inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; } | |
617 | ||
618 | ||
619 | ||
620 | ||
621 | ||
622 | ||
623 | ||
624 | ||
625 | ||
626 | ||
627 | ||
628 | ||
629 | inline int Testuint256AdHoc(vector<string> vArg) | |
630 | { | |
631 | uint256 g(0); | |
632 | ||
633 | ||
634 | printf("%s\n", g.ToString().c_str()); | |
635 | g--; printf("g--\n"); | |
636 | printf("%s\n", g.ToString().c_str()); | |
637 | g--; printf("g--\n"); | |
638 | printf("%s\n", g.ToString().c_str()); | |
639 | g++; printf("g++\n"); | |
640 | printf("%s\n", g.ToString().c_str()); | |
641 | g++; printf("g++\n"); | |
642 | printf("%s\n", g.ToString().c_str()); | |
643 | g++; printf("g++\n"); | |
644 | printf("%s\n", g.ToString().c_str()); | |
645 | g++; printf("g++\n"); | |
646 | printf("%s\n", g.ToString().c_str()); | |
647 | ||
648 | ||
649 | ||
650 | uint256 a(7); | |
651 | printf("a=7\n"); | |
652 | printf("%s\n", a.ToString().c_str()); | |
653 | ||
654 | uint256 b; | |
655 | printf("b undefined\n"); | |
656 | printf("%s\n", b.ToString().c_str()); | |
657 | int c = 3; | |
658 | ||
659 | a = c; | |
660 | a.pn[3] = 15; | |
661 | printf("%s\n", a.ToString().c_str()); | |
662 | uint256 k(c); | |
663 | ||
664 | a = 5; | |
665 | a.pn[3] = 15; | |
666 | printf("%s\n", a.ToString().c_str()); | |
667 | b = 1; | |
668 | b <<= 52; | |
669 | ||
670 | a |= b; | |
671 | ||
672 | a ^= 0x500; | |
673 | ||
674 | printf("a %s\n", a.ToString().c_str()); | |
675 | ||
676 | a = a | b | (uint256)0x1000; | |
677 | ||
678 | ||
679 | printf("a %s\n", a.ToString().c_str()); | |
680 | printf("b %s\n", b.ToString().c_str()); | |
681 | ||
682 | a = 0xfffffffe; | |
683 | a.pn[4] = 9; | |
684 | ||
685 | printf("%s\n", a.ToString().c_str()); | |
686 | a++; | |
687 | printf("%s\n", a.ToString().c_str()); | |
688 | a++; | |
689 | printf("%s\n", a.ToString().c_str()); | |
690 | a++; | |
691 | printf("%s\n", a.ToString().c_str()); | |
692 | a++; | |
693 | printf("%s\n", a.ToString().c_str()); | |
694 | ||
695 | a--; | |
696 | printf("%s\n", a.ToString().c_str()); | |
697 | a--; | |
698 | printf("%s\n", a.ToString().c_str()); | |
699 | a--; | |
700 | printf("%s\n", a.ToString().c_str()); | |
701 | uint256 d = a--; | |
702 | printf("%s\n", d.ToString().c_str()); | |
703 | printf("%s\n", a.ToString().c_str()); | |
704 | a--; | |
705 | printf("%s\n", a.ToString().c_str()); | |
706 | a--; | |
707 | printf("%s\n", a.ToString().c_str()); | |
708 | ||
709 | d = a; | |
710 | ||
711 | printf("%s\n", d.ToString().c_str()); | |
712 | for (int i = uint256::WIDTH-1; i >= 0; i--) printf("%08x", d.pn[i]); printf("\n"); | |
713 | ||
714 | uint256 neg = d; | |
715 | neg = ~neg; | |
716 | printf("%s\n", neg.ToString().c_str()); | |
717 | ||
718 | ||
719 | uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111"); | |
720 | printf("\n"); | |
721 | printf("%s\n", e.ToString().c_str()); | |
722 | ||
723 | ||
724 | printf("\n"); | |
725 | uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111"); | |
726 | uint256 x2; | |
727 | printf("%s\n", x1.ToString().c_str()); | |
728 | for (int i = 0; i < 270; i += 4) | |
729 | { | |
730 | x2 = x1 << i; | |
731 | printf("%s\n", x2.ToString().c_str()); | |
732 | } | |
733 | ||
734 | printf("\n"); | |
735 | printf("%s\n", x1.ToString().c_str()); | |
736 | for (int i = 0; i < 270; i += 4) | |
737 | { | |
738 | x2 = x1; | |
739 | x2 >>= i; | |
740 | printf("%s\n", x2.ToString().c_str()); | |
741 | } | |
742 | ||
743 | ||
744 | for (int i = 0; i < 100; i++) | |
745 | { | |
746 | uint256 k = (~uint256(0) >> i); | |
747 | printf("%s\n", k.ToString().c_str()); | |
748 | } | |
749 | ||
750 | for (int i = 0; i < 100; i++) | |
751 | { | |
752 | uint256 k = (~uint256(0) << i); | |
753 | printf("%s\n", k.ToString().c_str()); | |
754 | } | |
755 | ||
756 | return (0); | |
757 | } |