Debian Codegraph @

src/common/bigint.cpp

raw symbols SCIP
1 /*
2 * Class for simple, arbitrary size unsigned integer math.
3 * Note that some method implementations might lack features that weren't required so far!
4 *
5 * Copyright (C) 2008 - Olaf Freyer
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 */
17
18 #include "common/setup_before.h"
19 #include "bigint.h"
20
21 #include <algorithm>
22 #include <cassert>
23 #include <cstdint>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <cstring>
27 #include <iostream>
28 #include <stdexcept>
29 #include <sstream>
30
31 #include "common/xalloc.h"
32
33 #include "common/setup_after.h"
34
35 namespace pvpgn
36 {
37
38 static const char small_digits[] = "0123456789abcdef";
39
40 BigInt::BigInt()
41 {
42 segment_count = 1;
43 segment = (bigint_base*)xmalloc(segment_count * sizeof(bigint_base));
44 segment[0] = 0;
45 }
46
47 BigInt::BigInt(std::uint8_t input)
48 {
49 segment_count = 1;
50 segment = (bigint_base*)xmalloc(segment_count * sizeof(bigint_base));
51 segment[0] = input;
52 }
53
54 BigInt::BigInt(std::uint16_t input)
55 {
56 segment_count = 1;
57 segment = (bigint_base*)xmalloc(segment_count * sizeof(bigint_base));
58 segment[0] = input;
59 }
60
61 BigInt::BigInt(std::uint32_t input)
62 {
63 #ifndef HAVE_UINT64_T
64 int i;
65 #endif
66 segment_count = sizeof(std::uint32_t) / sizeof(bigint_base);
67 segment = (bigint_base*)xmalloc(segment_count * sizeof(bigint_base));
68 #ifdef HAVE_UINT64_T
69 segment[0] = input;
70 #else
71 for (i = 0; i < segment_count; i++){
72 segment[i] = input & bigint_base_mask;
73 input >>= bigint_base_bitcount;
74 }
75 #endif
76 }
77
78 BigInt::BigInt(std::uint64_t input)
79 {
80 int i;
81 segment_count = sizeof(std::uint64_t) / sizeof(bigint_base);
82 segment = (bigint_base*)xmalloc(segment_count * sizeof(bigint_base));
83 for (i = 0; i < segment_count; i++){
84 segment[i] = input & bigint_base_mask;
85 input >>= bigint_base_bitcount;
86 }
87 }
88
89 BigInt::BigInt(const BigInt& input)
90 :segment_count(input.segment_count)
91 {
92 segment = (bigint_base*)xmalloc(segment_count * sizeof(bigint_base));
93 std::memcpy(segment, input.segment, segment_count * sizeof(bigint_base));
94 }
95
96 BigInt&
97 BigInt::operator=(const BigInt& input)
98 {
99 if (&input != this) {
100 segment_count = input.segment_count;
101 segment = (bigint_base*)xrealloc(segment, segment_count * sizeof(bigint_base));
102 std::memcpy(segment, input.segment, segment_count * sizeof(bigint_base));
103 }
104 return *this;
105 }
106
107 BigInt::BigInt(unsigned char const * input, int input_size, int blockSize, bool bigEndian)
108 {
109 int i, j;
110 unsigned char *in;
111 unsigned char *inPointer;
112
113 if (bigEndian){
114 in = (unsigned char*)input;
115 inPointer = (unsigned char*)in;
116 }
117 else {
118 in = (unsigned char*)xmalloc(input_size);
119 inPointer = (unsigned char*)in + input_size - 1;
120 if (blockSize == 1)
121 std::memcpy(in, input, input_size);
122 else {
123 assert(blockSize % 2 == 0);
124 for (i = 0; i < input_size; i += blockSize)
125 {
126 for (j = 0; j < blockSize / 2; j++)
127 {
128 in[i + j] = input[i + blockSize - (j + 1)];
129 in[i + blockSize - (j + 1)] = input[i + j];
130 }
131 }
132 }
133 }
134
135 segment_count = input_size / sizeof(bigint_base);
136 if (input_size % sizeof(bigint_base))
137 segment_count++;
138 segment = (bigint_base*)xmalloc(segment_count * sizeof(bigint_base));
139 std::memset(segment, 0, segment_count * sizeof(bigint_base));
140
141
142 for (i = input_size - 1; i >= 0; i--)
143 {
144 j = i / sizeof(bigint_base);
145 segment[j] <<= 8;
146 if (bigEndian)
147 segment[j] += *(inPointer++);
148 else
149 segment[j] += *(inPointer--);
150 }
151
152 if (!bigEndian)
153 xfree(in);
154 }
155
156 BigInt::~BigInt() throw()
157 {
158 if (segment)
159 xfree(segment);
160 }
161
162 bool
163 BigInt::operator== (const BigInt& right) const
164 {
165 int i;
166 bool result;
167
168 result = (segment_count == right.segment_count);
169 for (i = 0; (i < segment_count) && result; i++)
170 result = result && (segment[i] == right.segment[i]);
171
172 return result;
173 }
174
175 bool
176 BigInt::operator< (const BigInt& right) const
177 {
178 if (segment_count < right.segment_count) {
179 return true;
180 }
181 else if (segment_count > right.segment_count){
182 return false;
183 }
184 else {
185 int i;
186 for (i = segment_count - 1; i >= 0; i--) {
187 if (segment[i]<right.segment[i])
188 return true;
189 else if (segment[i]>right.segment[i])
190 return false;
191 }
192 }
193
194 return false;
195 }
196 bool
197 BigInt::operator> (const BigInt& right) const
198 {
199 if (segment_count > right.segment_count) {
200 return true;
201 }
202 else if (segment_count < right.segment_count){
203 return false;
204 }
205 else {
206 int i;
207 for (i = segment_count - 1; i >= 0; i--) {
208 if (segment[i]>right.segment[i])
209 return true;
210 else if (segment[i] < right.segment[i])
211 return false;
212 }
213 }
214
215 return false;
216 }
217
218 BigInt
219 BigInt::operator+ (const BigInt& right) const
220 {
221 int i, max_segment_count;
222 bigint_extended sum;
223 bigint_extended lhs, rhs;
224 bigint_base carry = 0;
225 BigInt result;
226
227 max_segment_count = std::max(segment_count, right.segment_count);
228 result.segment_count = max_segment_count + 1;
229 result.segment = (bigint_base*)xrealloc(result.segment, result.segment_count * sizeof(bigint_base));
230
231 for (i = 0; i < max_segment_count; i++)
232 {
233 lhs = (i < segment_count) ? segment[i] : 0;
234 rhs = (i<right.segment_count) ? right.segment[i] : 0;
235 sum = lhs + rhs + carry;
236 result.segment[i] = sum & bigint_base_mask;
237 carry = sum >> bigint_base_bitcount;
238 }
239
240 result.segment[i] = carry;
241
242 for (max_segment_count = result.segment_count - 1; max_segment_count>0; max_segment_count--) {
243 if (result.segment[max_segment_count] != 0)
244 break;
245 }
246
247 if (result.segment_count != max_segment_count + 1)
248 {
249 result.segment_count = max_segment_count + 1;
250 result.segment = (bigint_base*)xrealloc(result.segment, result.segment_count * sizeof(bigint_base));
251 }
252
253 return result;
254 }
255
256 BigInt
257 BigInt::operator- (const BigInt& right) const
258 {
259 int i, max_segment_count;
260 bigint_base diff;
261 bigint_base lhs, rhs;
262 bigint_base carry = 0;
263 BigInt result;
264
265 // Currently we only implement unsigned math
266 // so we can either throw exception or simply return 0.
267 if (!(this->operator>(right)))
268 {
269 return result;
270 }
271
272 result.segment_count = segment_count;
273 result.segment = (bigint_base*)xrealloc(result.segment, result.segment_count * sizeof(bigint_base));
274 for (i = 0; i < result.segment_count; i++)
275 {
276 lhs = (i < segment_count) ? segment[i] : 0;
277 rhs = (i < right.segment_count) ? right.segment[i] : 0;
278 rhs += carry;
279 if (lhs < rhs)
280 {
281 diff = (bigint_extended_carry + lhs) - rhs;
282 carry = 1;
283 }
284 else
285 {
286 diff = (lhs)-rhs;
287 carry = 0;
288 }
289 result.segment[i] = diff;
290 }
291
292 for (max_segment_count = result.segment_count - 1; max_segment_count > 0; max_segment_count--) {
293 if (result.segment[max_segment_count] != 0)
294 break;
295 }
296
297 if (result.segment_count != max_segment_count + 1)
298 {
299 result.segment_count = max_segment_count + 1;
300 result.segment = (bigint_base*)xrealloc(result.segment, result.segment_count * sizeof(bigint_base));
301 }
302
303 return result;
304 }
305
306 BigInt
307 BigInt::operator* (const BigInt& right) const
308 {
309 int i, j, index, max_segment_count;
310 bigint_extended prod, sum;
311 bigint_extended lhs, rhs;
312 bigint_base carry = 0;
313 BigInt result;
314
315 if ((segment_count == 1 && segment[0] == 0) || (right.segment_count == 1 && right.segment[0] == 0))
316 return result;
317
318 result.segment_count = segment_count + right.segment_count;
319 result.segment = (bigint_base*)xrealloc(result.segment, result.segment_count * sizeof(bigint_base));
320 std::memset(result.segment, 0, result.segment_count * sizeof(bigint_base));
321 for (i = 0; i < segment_count; i++)
322 {
323 for (j = 0; j < right.segment_count; j++)
324 {
325 lhs = (i < segment_count) ? segment[i] : 0;
326 rhs = (j < right.segment_count) ? right.segment[j] : 0;
327 prod = lhs * rhs;
328 index = i + j;
329 sum = result.segment[index] + prod + carry;
330 result.segment[index] = sum & bigint_base_mask;
331 carry = sum >> bigint_base_bitcount;
332 }
333
334 if (carry) {
335 index = i + j;
336 sum = result.segment[index] + carry;
337 result.segment[index] = sum & bigint_base_mask;
338 carry = sum >> bigint_base_bitcount;
339 }
340 }
341
342 result.segment[i + j - 1] += carry;
343
344 for (max_segment_count = result.segment_count - 1; max_segment_count > 0; max_segment_count--) {
345 if (result.segment[max_segment_count] != 0)
346 break;
347 }
348
349 if (result.segment_count != max_segment_count + 1)
350 {
351 result.segment_count = max_segment_count + 1;
352 result.segment = (bigint_base*)xrealloc(result.segment, result.segment_count * sizeof(bigint_base));
353 }
354
355 return result;
356 }
357
358 BigInt
359 BigInt::operator/ (const BigInt& right) const
360 {
361 int i, j, max_segment_count;
362 BigInt quotient;
363 BigInt remainder;
364 BigInt m;
365 BigInt q;
366 BigInt p;
367 bigint_extended n, d, qest;
368
369
370 if (this->operator<(right)){
371 return quotient;
372 }
373
374 quotient.segment_count = (segment_count - right.segment_count) + 1;
375 quotient.segment = (bigint_base*)xrealloc(quotient.segment, quotient.segment_count*sizeof(bigint_base));
376 std::memset(quotient.segment, 0, quotient.segment_count * sizeof(bigint_base));
377
378 remainder.segment_count = right.segment_count + 1;
379 remainder.segment = (bigint_base*)xrealloc(remainder.segment, remainder.segment_count*sizeof(bigint_base));
380 std::memset(remainder.segment, 0, remainder.segment_count * sizeof(bigint_base));
381
382 for (j = 0; j < right.segment_count; j++){
383 remainder.segment[j] = segment[(segment_count - right.segment_count) + j];
384 }
385
386 n = 0;
387 for (i = segment_count; i >= right.segment_count; i--){
388 //now do some "educated guessing"
389 //qest=n/q
390 //calculate qest*right
391 // adjust by +/-1 until in target range
392 n = remainder.segment[right.segment_count];
393 n <<= bigint_base_bitcount;
394 n += remainder.segment[right.segment_count - 1];
395 d = right.segment[right.segment_count - 1];
396
397 qest = n / d;
398 p = BigInt(qest) * right;
399 while (1) {
400 if (p == remainder) {
401 break;
402 }
403 else if (p < remainder) {
404 if ((remainder - p) > right) {
405 qest++;
406 p = p + right;
407 }
408 else
409 {
410 break;
411 }
412 }
413 else {
414 qest--;
415 p = p - right;
416 }
417 };
418
419 quotient.segment[i - right.segment_count] = qest & bigint_base_mask;
420 remainder = remainder - p;
421
422 if (i > right.segment_count) {
423 remainder = remainder << sizeof(bigint_base);
424 remainder.segment[0] = segment[i - (right.segment_count + 1)];
425 }
426 }
427
428 for (max_segment_count = quotient.segment_count - 1; max_segment_count > 0; max_segment_count--) {
429 if (quotient.segment[max_segment_count] != 0)
430 break;
431 }
432
433 if (quotient.segment_count != max_segment_count + 1)
434 {
435 quotient.segment_count = max_segment_count + 1;
436 quotient.segment = (bigint_base*)xrealloc(quotient.segment, quotient.segment_count * sizeof(bigint_base));
437 }
438
439 return quotient;
440 }
441
442 BigInt
443 BigInt::operator% (const BigInt& right) const
444 {
445 int i, j;
446 BigInt remainder;
447 BigInt m;
448 BigInt q;
449 BigInt p;
450 bigint_extended n, d, qest;
451
452
453 if (this->operator<(right)){
454 return *this;
455 }
456
457 remainder.segment_count = right.segment_count + 1;
458 remainder.segment = (bigint_base*)xrealloc(remainder.segment, remainder.segment_count*sizeof(bigint_base));
459 std::memset(remainder.segment, 0, remainder.segment_count * sizeof(bigint_base));
460
461 for (j = 0; j < right.segment_count; j++){
462 remainder.segment[j] = segment[(segment_count - right.segment_count) + j];
463 }
464
465 n = 0;
466 for (i = segment_count; i >= right.segment_count; i--){
467 //now do some "educated guessing"
468 //qest=n/q
469 //calculate qest*right
470 // adjust by +/-1 until in target range
471 n = remainder.segment[right.segment_count];
472 n <<= bigint_base_bitcount;
473 n += remainder.segment[right.segment_count - 1];
474 d = right.segment[right.segment_count - 1];
475
476 qest = n / d;
477 p = BigInt(qest) * right;
478 while (1) {
479 if (p == remainder) {
480 break;
481 }
482 else if (p < remainder) {
483 if ((remainder - p) > right) {
484 qest++;
485 p = p + right;
486 }
487 else
488 {
489 break;
490 }
491 }
492 else {
493 qest--;
494 p = p - right;
495 }
496 };
497
498
499 remainder = remainder - p;
500
501 if (i > right.segment_count) {
502 remainder = remainder << sizeof(bigint_base);
503 remainder.segment[0] = segment[i - (right.segment_count + 1)];
504 }
505 }
506
507 return remainder;
508 }
509
510 BigInt
511 BigInt::operator<< (int bytesToShift) const {
512 int i;
513 BigInt result;
514 assert(bytesToShift >= 0);
515 //currently we only implement (and need) segmentwise shifting
516 assert(bytesToShift%sizeof(bigint_base) == 0);
517
518 if (bytesToShift == 0)
519 return result;
520
521 int segmentsToShift = bytesToShift / sizeof(bigint_base);
522 result.segment_count = segment_count + segmentsToShift;
523 result.segment = (bigint_base*)xrealloc(result.segment, result.segment_count * sizeof(bigint_base));
524
525 for (i = segment_count - 1; i >= 0; i--) {
526 result.segment[i + segmentsToShift] = segment[i];
527 }
528
529 for (i = 0; i < segmentsToShift; i++) {
530 result.segment[i] = 0;
531 }
532
533 return result;
534 }
535
536 BigInt
537 BigInt::random(int size)
538 {
539 BigInt result;
540 int i, j;
541 unsigned int r;
542
543 assert(size > 0);
544 assert(size%sizeof(bigint_base) == 0);
545
546 result.segment_count = size / sizeof(bigint_base);
547 result.segment = (bigint_base*)xrealloc(result.segment, result.segment_count * sizeof(bigint_base));
548
549 for (i = 0; i < result.segment_count; i++){
550 result.segment[i] = 0;
551 for (j = 0; j < sizeof(bigint_base); j += sizeof(unsigned int)){
552 r = rand();
553 result.segment[i] <<= (sizeof(unsigned int));
554 result.segment[i] = result.segment[i] + r;
555 }
556 }
557
558 return result;
559 }
560
561 BigInt
562 BigInt::powm(const BigInt& exp, const BigInt& mod) const
563 {
564 if (exp.segment_count == 1)
565 {
566 if (exp.segment[0] == 0x02)
567 {
568 return (*this * *this) % mod;
569 }
570 else if (exp.segment[0] == 0x01)
571 {
572 return *this;
573 }
574 else if (exp.segment[0] == 0x00)
575 {
576 return BigInt((std::uint8_t)0x01);
577 }
578 }
579
580 //trying a divide&conquer approach
581 if (exp.segment[0] % 2 == 0)
582 {
583 // exp is even
584 BigInt half = exp / (BigInt((std::uint8_t)0x02));
585 BigInt halfpow = this->powm(half, mod);
586 return (halfpow * halfpow) % mod;
587 }
588 else
589 {
590 // exp is odd
591 BigInt half = exp / (BigInt((std::uint8_t)0x02));
592 BigInt halfpow = this->powm(half, mod);
593 return (((halfpow * halfpow) % mod) * *this) % mod;
594 }
595 }
596
597 unsigned char*
598 BigInt::getData(int byteCount, int blockSize, bool bigEndian) const
599 {
600 unsigned char* result;
601
602 result = (unsigned char*)xmalloc(byteCount);
603 getData(result, byteCount, blockSize, bigEndian);
604
605 return result;
606 }
607
608 void
609 BigInt::getData(unsigned char* out, int byteCount, int blockSize, bool bigEndian) const
610 {
611 int i, j;
612 unsigned char* pos;
613
614 std::memset(out, 0, byteCount);
615 pos = out + (byteCount - 1);
616
617 for (i = 0; i < segment_count; i++)
618 {
619 bigint_base data;
620 data = segment[segment_count - (i + 1)];
621 for (j = 0; j < sizeof(bigint_base); j++)
622 {
623 if (pos < out)
624 break;
625 *pos = data & 0xff;
626
627 pos--;
628 data >>= 8;
629 }
630 }
631
632 if (!bigEndian && blockSize>1) {
633 unsigned char val;
634 assert(blockSize % 2 == 0);
635 for (i = 0; i < byteCount; i += blockSize)
636 {
637 for (j = 0; j < blockSize / 2; j++)
638 {
639 val = out[i + j];
640 out[i + j] = out[i + blockSize - (j + 1)];
641 out[i + blockSize - (j + 1)] = val;
642 }
643 }
644 }
645 }
646
647 std::string
648 BigInt::toHexString() const
649 {
650 int i;
651 char data;
652 std::ostringstream result;
653 bigint_base* src;
654
655 // handle most significant segment
656 src = &segment[segment_count - 1];
657 if (segment_count == 1 && *src == 0)
658 {
659 result << "00";
660 }
661 else
662 {
663 int sum = 0;
664 for (i = sizeof(bigint_base)-1; i >= 0; i--)
665 {
666 data = ((*src) & (0xff << (i * 8))) >> (i * 8);
667 sum += data;
668 if (sum)
669 {
670 result << small_digits[(data & 0xf0) >> 4];
671 result << small_digits[(data & 0x0f)];
672 }
673 }
674
675 // handle rest
676 for (src = &segment[segment_count - 2]; src >= segment; src--)
677 {
678 for (i = sizeof(bigint_base)-1; i >= 0; i--)
679 {
680 data = ((*src) & (0xff << (i * 8))) >> (i * 8);
681 result << small_digits[(data & 0xf0) >> 4];
682 result << small_digits[(data & 0x0f)];
683 }
684 }
685 }
686
687 return result.str();
688
689 }
690
691 }