-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigint.h
1648 lines (1373 loc) · 40.8 KB
/
bigint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef BIGINT_H_INCLUDED
#define BIGINT_H_INCLUDED
#include <limits.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdio.h>
/* any unsigned integer type */
//typedef uint32_t bigint_word;
#define bigint_word uint32_t
#define BIGINT_KARATSUBA_WORD_THRESHOLD 20
#define CHARZERO_ABOVEINTZERO 48
#define BIGINT_WORD_BITS ((sizeof(bigint_word) * CHAR_BIT))
#define BIGINT_WORD_MAX ((bigint_word)-1)
#define BIGINT_HALF_WORD_MAX (BIGINT_WORD_MAX >> BIGINT_WORD_BITS / 2)
#define BIGINT_WORD_LO(a) ((a) & BIGINT_HALF_WORD_MAX)
#define BIGINT_WORD_HI(a) ((a) >> sizeof(a) * CHAR_BIT / 2)
#define BIGINT_MIN(a, b) ((a) < (b) ? (a) : (b))
#define BIGINT_MAX(a, b) ((a) > (b) ? (a) : (b))
#define BIGINT_INT_ABS(a) ((a) < 0 ? -(unsigned int)(a) : (unsigned int)(a))
#define BIGINT_LONG_ABS(a) ((a) < 0 ? -(unsigned long)(a) : (unsigned long)(a))
#define BIGINT_SWAP(type, a, b) do { type _tmp = a; a = b; b = _tmp; } while (0)
#define BIGINT_ASSERT(a, op, b) assert((a) op (b));
#define BIGINT_BITS 32
#define BIGINT_NEG_SIGN 99
#define BIGINT_REVERSE(type, data, n) do {\
int _i;\
for (_i = 0; _i < (n)/2; _i++) BIGINT_SWAP(type, data[_i], data[n - 1 - _i]);\
} while (0)
static const char Hex[]="0123456789ABCDEF";
typedef struct bigint {
bigint_word *words;
int capacity;
int size;
int neg;
} bigint;
typedef void (*bigint_rand_func)(uint8_t *dst, int n);
bigint_word bigint_word_mul_lo(bigint_word a, bigint_word b);
bigint_word bigint_word_mul_hi(bigint_word a, bigint_word b);
bigint_word bigint_word_add_get_carry(bigint_word *dst, bigint_word a, bigint_word b);
bigint_word bigint_word_sub_get_carry(bigint_word *dst, bigint_word a, bigint_word b);
bigint_word bigint_word_from_char(char c);
int bigint_word_bitlength(bigint_word a);
int bigint_word_count_trailing_zeros(bigint_word a);
bigint_word bigint_word_gcd(bigint_word a, bigint_word b);
unsigned bigint_uint_gcd(unsigned a, unsigned b);
int bigint_int_gcd(int a, int b);
bigint* bigint_init(bigint *a);
bigint* bigint_reserve(bigint *a, int capacity);
void bigint_free(bigint *a);
int bigint_cmp_abs(const bigint *a, const bigint *b);
int bigint_cmp(const bigint *a, const bigint *b);
int bigint_cmp_abs_word(const bigint *a, bigint_word b);
bigint* bigint_set_neg(bigint *dst, int neg);
bigint* bigint_negate(bigint *dst);
bigint* bigint_cpy(bigint *dst, const bigint *src);
bigint* bigint_clr_bit(bigint *dst, unsigned bit_index);
bigint* bigint_set_bit(bigint *dst, unsigned bit_index);
bigint_word bigint_get_bit(const bigint *src, unsigned bit_index);
bigint* bigint_mul(bigint *dst, const bigint *a, const bigint *b);
int bigint_count_digits(const char *src);
int bigint_digits_bound(int n_digits_src, double src_base, double dst_base);
int bigint_write_size(const bigint *a, double dst_base);
bigint* bigint_from_str_base(bigint *dst, const char *src, int src_base);
bigint* bigint_from_str(bigint *dst, const char *src);
bigint* bigint_from_int(bigint *dst, int src);
bigint* bigint_from_long(bigint *dst, long src);
bigint* bigint_from_word(bigint *dst, bigint_word a);
bigint* bigint_add_signed(bigint *dst, const bigint *a, int a_neg, const bigint *b, int b_neg);
bigint* bigint_add(bigint *dst, const bigint *a, const bigint *b);
bigint* bigint_sub(bigint *dst, const bigint *a, const bigint *b);
bigint* bigint_add_word_signed(bigint *dst, const bigint *src_a, bigint_word b, int b_neg);
bigint* bigint_add_word(bigint *dst, const bigint *src_a, bigint_word b);
bigint* bigint_sub_word(bigint *dst, const bigint *src_a, bigint_word b);
void print(bigint* num);
char *bigint_2hex(const bigint *a);
char* bigint_write_base(
char *dst,
int *n_dst,
const bigint *a,
bigint_word base,
int zero_terminate
);
/* convenience function defaults to base 10 and zero terminates */
char* bigint_write(char *dst, int n_dst, const bigint *a);
bigint* bigint_shift_left (bigint *dst, const bigint *src, unsigned shift);
bigint* bigint_shift_right(bigint *dst, const bigint *src, unsigned shift);
int bigint_bitlength(const bigint *a);
int bigint_count_trailing_zeros(const bigint *a);
bigint* bigint_div_mod(
bigint *dst_quotient,
bigint *dst_remainder,
const bigint *src_biginterator,
const bigint *src_denominator
);
bigint* bigint_div(
bigint *dst,
const bigint *numerator,
const bigint *denominator
);
bigint* bigint_mod(
bigint *dst,
const bigint *numerator,
const bigint *denominator
);
bigint* bigint_div_mod_half_word(
bigint *dst,
bigint_word *dst_remainder,
bigint_word denominator
);
bigint* bigint_gcd(bigint *dst, const bigint *src_a, const bigint *src_b);
bigint* bigint_sqrt(bigint *dst, const bigint *src);
bigint* bigint_rand_bits(bigint *dst, int n_bits, bigint_rand_func rand_func);
bigint* bigint_rand_inclusive(bigint *dst, const bigint *n, bigint_rand_func rand_func);
bigint* bigint_rand_exclusive(bigint *dst, const bigint *n, bigint_rand_func rand_func);
bigint* bigint_pow_mod(
bigint *dst,
const bigint *src_base,
const bigint *src_exponent,
const bigint *src_modulus
);
/* probability for wrong positives is approximately 1/4^n_tests */
int bigint_is_probable_prime(const bigint *n, int n_tests, bigint_rand_func rand_func);
bigint* bigint_pow_word(bigint *dst, const bigint *src, bigint_word exponent);
double bigint_double(const bigint *src);
/* low bits of a * b */
bigint_word bigint_word_mul_lo(bigint_word a, bigint_word b)
{
return a * b;
}
/* high bits of a * b */
bigint_word bigint_word_mul_hi(bigint_word a, bigint_word b)
{
bigint_word c0 = BIGINT_WORD_LO(a) * BIGINT_WORD_LO(b);
bigint_word c1 = BIGINT_WORD_LO(a) * BIGINT_WORD_HI(b);
bigint_word c2 = BIGINT_WORD_HI(a) * BIGINT_WORD_LO(b);
bigint_word c3 = BIGINT_WORD_HI(a) * BIGINT_WORD_HI(b);
bigint_word c4 = BIGINT_WORD_HI(c0) + BIGINT_WORD_LO(c1) + BIGINT_WORD_LO(c2);
return BIGINT_WORD_HI(c4) + BIGINT_WORD_HI(c1) + BIGINT_WORD_HI(c2) + c3;
}
/* dst = a + b, return carry */
bigint_word bigint_word_add_get_carry(
bigint_word *dst,
bigint_word a,
bigint_word b
)
{
a += b;
*dst = a;
return a < b;
}
/* dst = a - b, return carry */
bigint_word bigint_word_sub_get_carry(
bigint_word *dst,
bigint_word a,
bigint_word b
)
{
b = a - b;
*dst = b;
return b > a;
}
bigint_word bigint_word_from_char(char c)
{
switch (c)
{
case '-': return 99;
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
case 'g': case 'G': return 16;
case 'h': case 'H': return 17;
case 'i': case 'I': return 18;
case 'j': case 'J': return 19;
case 'k': case 'K': return 20;
case 'l': case 'L': return 21;
case 'm': case 'M': return 22;
case 'n': case 'N': return 23;
case 'o': case 'O': return 24;
case 'p': case 'P': return 25;
case 'q': case 'Q': return 26;
case 'r': case 'R': return 27;
case 's': case 'S': return 28;
case 't': case 'T': return 29;
case 'u': case 'U': return 30;
case 'v': case 'V': return 31;
case 'w': case 'W': return 32;
case 'x': case 'X': return 33;
case 'y': case 'Y': return 34;
case 'z': case 'Z': return 35;
default: return BIGINT_WORD_MAX;
}
}
int bigint_word_bitlength(bigint_word a)
{
int i;
for (i = BIGINT_WORD_BITS - 1; i >= 0; i--) if ((a >> i) & 1) return i + 1;
return 0;
}
int bigint_word_count_trailing_zeros(bigint_word a)
{
int i;
for (i = 0; i < (int)BIGINT_WORD_BITS; i++) if ((a >> i) & 1) return i;
return BIGINT_WORD_BITS;
}
bigint_word bigint_word_gcd(bigint_word a, bigint_word b)
{
while (1)
{
if (!a) return b;
b %= a;
if (!b) return a;
a %= b;
}
}
unsigned bigint_uint_gcd(unsigned a, unsigned b)
{
while (1)
{
if (!a) return b;
b %= a;
if (!b) return a;
a %= b;
}
}
int bigint_int_gcd(int a, int b)
{
return bigint_uint_gcd(BIGINT_INT_ABS(a), BIGINT_INT_ABS(b));
}
bigint* bigint_init(bigint *dst)
{
dst->words = NULL;
dst->neg = dst->size = dst->capacity = 0;
return dst;
}
bigint* bigint_reserve(bigint *dst, int capacity)
{
if (dst->capacity >= capacity) return dst;
dst->capacity = capacity;
dst->words = (bigint_word*)realloc(dst->words, capacity * sizeof(*dst->words));
/* out of memory? sorry :( */
assert(dst->words != NULL);
BIGINT_ASSERT(dst->size, <=, capacity);
return dst;
}
void bigint_free(bigint *dst)
{
free(dst->words);
bigint_init(dst);
}
int bigint_raw_cmp_abs(
const bigint_word *a, int na,
const bigint_word *b, int nb
)
{
int i;
if (na > nb) return +1;
if (na < nb) return -1;
BIGINT_ASSERT(na, ==, nb);
for (i = na - 1; i >= 0; i--)
{
if (a[i] < b[i]) return -1;
if (a[i] > b[i]) return +1;
}
return 0;
}
int bigint_cmp_abs(const bigint *a, const bigint *b)
{
return bigint_raw_cmp_abs(a->words, a->size, b->words, b->size);
}
int bigint_raw_cmp(
const bigint_word *a, int na, int a_neg,
const bigint_word *b, int nb, int b_neg
)
{
if (na == 0 && nb == 0) return 0;
if (!a_neg && !b_neg) return bigint_raw_cmp_abs(a, na, b, nb);
if ( a_neg && b_neg) return bigint_raw_cmp_abs(b, na, a, nb);
return (!a_neg && b_neg) ? +1 : -1;
}
int bigint_cmp(const bigint *a, const bigint *b)
{
return bigint_raw_cmp(a->words, a->size, a->neg, b->words, b->size, b->neg);
}
int bigint_cmp_abs_word(const bigint *a, bigint_word b)
{
if (b == 0)
{
if (a->size == 0) return 0;
return a->neg ? -1 : +1;
}
return bigint_raw_cmp_abs(a->words, a->size, &b, 1);
}
void bigint_raw_zero(bigint_word *dst, int from, int to)
{
if (from >= to) return;
memset(dst + from, 0, (to - from) * sizeof(*dst));
}
bigint* bigint_set_neg(bigint *dst, int neg)
{
dst->neg = neg;
return dst;
}
bigint* bigint_negate(bigint *dst)
{
return bigint_set_neg(dst, !dst->neg);
}
int bigint_raw_cpy(bigint_word *dst, const bigint_word *src, int n)
{
memcpy(dst, src, n * sizeof(*src));
return n;
}
bigint* bigint_cpy(bigint *dst, const bigint *src)
{
if (src == dst) return dst;
bigint_reserve(dst, src->size);
dst->size = bigint_raw_cpy(dst->words, src->words, src->size);
BIGINT_ASSERT(bigint_cmp_abs(src, dst), ==, 0);
return bigint_set_neg(dst, src->neg);
}
int bigint_raw_truncate(const bigint_word *a, int n)
{
while (n > 0 && a[n - 1] == 0) n--;
return n;
}
void bigint_raw_clr_bit(bigint_word *dst, unsigned bit_index)
{
unsigned word_index = bit_index / BIGINT_WORD_BITS;
bit_index %= BIGINT_WORD_BITS;
dst[word_index] &= BIGINT_WORD_MAX ^ (((bigint_word)1) << bit_index);
}
bigint* bigint_clr_bit(bigint *dst, unsigned bit_index)
{
bigint_raw_clr_bit(dst->words, bit_index);
dst->size = bigint_raw_truncate(dst->words, dst->size);
return dst;
}
bigint* bigint_set_bit(bigint *dst, unsigned bit_index)
{
int word_index = bit_index / BIGINT_WORD_BITS;
int n = word_index + 1;
bigint_reserve(dst, n);
bigint_raw_zero(dst->words, dst->size, n);
dst->size = BIGINT_MAX(dst->size, n);
dst->words[word_index] |= ((bigint_word)1) << bit_index % BIGINT_WORD_BITS;
return dst;
}
bigint_word bigint_get_bit(const bigint *src, unsigned bit_index)
{
int i = bit_index / BIGINT_WORD_BITS;
if (src->size <= i) return 0;
return (src->words[i] >> bit_index % BIGINT_WORD_BITS) & 1;
}
int bigint_raw_mul_word_add(
bigint_word *dst,
const bigint_word *src, int n,
bigint_word factor
)
{
int i;
bigint_word carry = 0;
for (i = 0; i < n; i++)
{
bigint_word src_word = src[i];
bigint_word dst_word = bigint_word_mul_lo(src_word, factor);
carry = bigint_word_add_get_carry(&dst_word, dst_word, carry);
carry += bigint_word_mul_hi(src_word, factor);
carry += bigint_word_add_get_carry(&dst[i], dst[i], dst_word);
}
for (; carry; i++)
{
carry = bigint_word_add_get_carry(&dst[i], dst[i], carry);
}
return bigint_raw_truncate(dst, i);
}
int bigint_raw_mul_word(
bigint_word *dst,
const bigint_word *src, int n,
bigint_word factor
)
{
int i;
bigint_word carry = 0;
for (i = 0; i < n; i++)
{
bigint_word src_word = src[i];
bigint_word dst_word = bigint_word_mul_lo(src_word, factor);
carry = bigint_word_add_get_carry(&dst_word, dst_word, carry);
carry += bigint_word_mul_hi(src_word, factor);
dst[i] = dst_word;
}
if (carry)
{
dst[i++] = carry;
}
return bigint_raw_truncate(dst, i);
}
int bigint_raw_mul_add(
bigint_word *dst,
const bigint_word *src_a, int na,
const bigint_word *src_b, int nb
)
{
int i;
if (na == 0 || nb == 0) return 0;
assert(dst != src_a);
assert(dst != src_b);
for (i = 0; i < nb; i++)
{
bigint_raw_mul_word_add(dst + i, src_a, na, src_b[i]);
}
return bigint_raw_truncate(dst, na + nb);
}
int bigint_raw_add_word(
bigint_word *dst,
const bigint_word *src, int n,
bigint_word b
)
{
int i;
bigint_word carry = b;
for (i = 0; i < n; i++)
{
carry = bigint_word_add_get_carry(&dst[i], src[i], carry);
}
for (; carry; i++)
{
carry = bigint_word_add_get_carry(&dst[i], dst[i], carry);
}
return bigint_raw_truncate(dst, i);
}
int bigint_raw_from_str_base(bigint_word *dst, const char *src, int base)
{
int n = 0;
for (; *src; src++)
{
bigint_word digit = bigint_word_from_char(*src);
if (digit == BIGINT_NEG_SIGN){
continue;
}
if (digit == BIGINT_WORD_MAX){
/* illegal character */
break;
}
n = bigint_raw_mul_word(dst, dst, n, base);
n = bigint_raw_add_word(dst, dst, n, digit);
}
return bigint_raw_truncate(dst, n);
}
int bigint_count_digits(const char *src)
{
int n = 0;
for (; *src; src++)
{
bigint_word digit = bigint_word_from_char(*src);
if (digit == BIGINT_NEG_SIGN){
continue;
}
if (digit != BIGINT_WORD_MAX)
{
n++;
}
else
{
/*found illegal character!*/
break;
}
}
return n;
}
int bigint_raw_add(
bigint_word *dst,
const bigint_word *src_a, int na,
const bigint_word *src_b, int nb
)
{
bigint_word sum, carry = 0;
int i, n = BIGINT_MIN(na, nb);
for (i = 0; i < n; i++)
{
carry = bigint_word_add_get_carry(&sum, carry, src_a[i]);
carry += bigint_word_add_get_carry(&sum, sum , src_b[i]);
dst[i] = sum;
}
for (; i < na; i++)
{
carry = bigint_word_add_get_carry(&dst[i], src_a[i], carry);
}
for (; i < nb; i++)
{
carry = bigint_word_add_get_carry(&dst[i], src_b[i], carry);
}
if (carry) dst[i++] = carry;
return bigint_raw_truncate(dst, i);
}
int bigint_raw_sub(
bigint_word *dst,
const bigint_word *src_a, int na,
const bigint_word *src_b, int nb
)
{
bigint_word dif, carry = 0;
int i;
BIGINT_ASSERT(na, >=, nb);
BIGINT_ASSERT(bigint_raw_cmp_abs(src_a, na, src_b, nb), >=, 0);
for (i = 0; i < nb; i++)
{
carry = bigint_word_sub_get_carry(&dif, src_a[i], carry);
carry += bigint_word_sub_get_carry(&dif, dif, src_b[i]);
dst[i] = dif;
}
for (; i < na; i++)
{
carry = bigint_word_sub_get_carry(&dst[i], src_a[i], carry);
}
BIGINT_ASSERT(carry, ==, 0);
return bigint_raw_truncate(dst, i);
}
int bigint_raw_mul_karatsuba(
bigint_word *dst,
const bigint_word *a, int na,
const bigint_word *b, int nb,
bigint_word *tmp
)
{
/* so many */
int n, k, m, m2;
const bigint_word *lo1, *hi1, *lo2, *hi2;
int nlo1, nhi1, nlo2, nhi2;
bigint_word *lo1hi1, *lo2hi2, *z0, *z1, *z2;
int nlo1hi1, nlo2hi2, nz0, nz1, nz2;
if (
na < BIGINT_KARATSUBA_WORD_THRESHOLD &&
nb < BIGINT_KARATSUBA_WORD_THRESHOLD
)
{
bigint_raw_zero(dst, 0, na + nb);
return bigint_raw_mul_add(dst, a, na, b, nb);
}
m = BIGINT_MAX(na, nb);
m2 = m / 2;
k = m2 + 2;
lo1 = a;
lo2 = b;
hi1 = a + m2;
hi2 = b + m2;
nlo1 = bigint_raw_truncate(lo1, BIGINT_MIN(m2, na));
nlo2 = bigint_raw_truncate(lo2, BIGINT_MIN(m2, nb));
nhi1 = bigint_raw_truncate(hi1, BIGINT_MAX(na - m2, 0));
nhi2 = bigint_raw_truncate(hi2, BIGINT_MAX(nb - m2, 0));
lo1hi1 = tmp; tmp += k;
lo2hi2 = tmp; tmp += k;
z0 = tmp; tmp += k*2;
z1 = tmp; tmp += k*2;
z2 = tmp; tmp += k*2;
nlo1hi1 = bigint_raw_add(lo1hi1, lo1, nlo1, hi1, nhi1);
nlo2hi2 = bigint_raw_add(lo2hi2, lo2, nlo2, hi2, nhi2);
nz0 = bigint_raw_mul_karatsuba(z0, lo1 , nlo1 , lo2 , nlo2 , tmp);
nz1 = bigint_raw_mul_karatsuba(z1, lo1hi1, nlo1hi1, lo2hi2, nlo2hi2, tmp);
nz2 = bigint_raw_mul_karatsuba(z2, hi1, nhi1 , hi2, nhi2, tmp);
nz1 = bigint_raw_sub(z1, z1, nz1, z0, nz0);
nz1 = bigint_raw_sub(z1, z1, nz1, z2, nz2);
n = nz0;
bigint_raw_cpy(dst, z0, n);
bigint_raw_zero(dst, n, na + nb);
n = bigint_raw_add(dst + m2*1, dst + m2*1, BIGINT_MAX(n - m2, 0), z1, nz1);
n = bigint_raw_add(dst + m2*2, dst + m2*2, BIGINT_MAX(n - m2, 0), z2, nz2);
return bigint_raw_truncate(dst, n + m2*2);
}
bigint* bigint_mul(bigint *dst, const bigint *a, const bigint *b)
{
int na = a->size;
int nb = b->size;
int n = na + nb;
bigint_word *tmp;
bigint_reserve(dst, n);
/* bigint_raw_mul_karatsuba already has this fastpath */
/* but this way we avoid allocating tmp */
if (
dst != a &&
dst != b &&
na < BIGINT_KARATSUBA_WORD_THRESHOLD &&
nb < BIGINT_KARATSUBA_WORD_THRESHOLD
)
{
bigint_raw_zero(dst->words, 0, na + nb);
dst->size = bigint_raw_mul_add(dst->words, a->words, na, b->words, nb);
}
else
{
int magical_upper_bound = BIGINT_MAX(na, nb) * 11 + 180 + n;
tmp = (bigint_word*)malloc(magical_upper_bound * sizeof(*tmp));
dst->size = bigint_raw_mul_karatsuba(tmp, a->words, na, b->words, nb, tmp + n);
bigint_raw_cpy(dst->words, tmp, dst->size);
free(tmp);
}
return bigint_set_neg(dst, a->neg ^ b->neg);
}
int bigint_digits_bound(int n_digits_src, double src_base, double dst_base)
{
/* +1 for rounding errors, just in case */
return ceil(n_digits_src * log(src_base) / log(dst_base)) + 1;
}
int bigint_write_size(const bigint *a, double dst_base)
{
double src_base = pow(2, BIGINT_WORD_BITS);
return bigint_digits_bound(a->size, src_base, dst_base)
+ sizeof('-') + sizeof('\0');
}
bigint* bigint_from_str_base(bigint *dst, const char *src, int src_base)
{
int n_digits_src, n_digits_dst;
double dst_base = pow(2.0, BIGINT_WORD_BITS);
n_digits_src = bigint_count_digits(src);
n_digits_dst = bigint_digits_bound(n_digits_src, src_base, dst_base);
bigint_reserve(dst, n_digits_dst);
dst->size = n_digits_dst;
bigint_raw_zero(dst->words, 0, n_digits_dst);
dst->size = bigint_raw_from_str_base(dst->words, src, src_base);
return bigint_set_neg(dst, *src == '-');
}
bigint* bigint_from_str(bigint *dst, const char *src)
{
return bigint_from_str_base(dst, src, 10);
}
void print(bigint* num)
{
bigint_word *numc = num->words;
int numl = num->size;
int numll = numl - 1;
char* out = (char*)malloc(numl + 1);
for(int i = 0; i < numl; i++)
{
out[i] = numc[numll - i] + CHARZERO_ABOVEINTZERO;
}
out[numl] = '\0';
puts(out);
free(out);
}
bigint* bigint_from_long(bigint *dst, long src)
{
unsigned long x = BIGINT_LONG_ABS(src);
int n = BIGINT_MAX(1, sizeof(x)/sizeof(bigint_word));
bigint_reserve(dst, n);
bigint_raw_zero(dst->words, 0, n);
memcpy(dst->words, &x, sizeof(x));
dst->size = bigint_raw_truncate(dst->words, n);
printf("asdf\n");
printf("asdf\n");
return bigint_set_neg(dst, src < 0);
}
bigint* bigint_from_int(bigint *dst, int src)
{
unsigned int x = BIGINT_INT_ABS(src);
int n = BIGINT_MAX(1, sizeof(x)/sizeof(bigint_word));
bigint_reserve(dst, n);
bigint_raw_zero(dst->words, 0, n);
memcpy(dst->words, &x, sizeof(x));
dst->size = bigint_raw_truncate(dst->words, n);
return bigint_set_neg(dst, src < 0);
}
bigint* bigint_from_word(bigint *dst, bigint_word a)
{
bigint_reserve(dst, 1);
dst->words[0] = a;
dst->size = bigint_raw_truncate(dst->words, 1);
return bigint_set_neg(dst, 0);
}
int bigint_raw_add_signed(
bigint_word *dst, int *dst_neg,
const bigint_word *a, int na, int a_neg,
const bigint_word *b, int nb, int b_neg
)
{
if (a_neg)
{
if (b_neg)
{
if (na >= nb)
{
*dst_neg = 1;
return bigint_raw_add(dst, a, na, b, nb);
}
else
{
*dst_neg = 1;
return bigint_raw_add(dst, b, nb, a, na);
}
}
else
{
if (bigint_raw_cmp_abs(a, na, b, nb) >= 0)
{
*dst_neg = 1;
return bigint_raw_sub(dst, a, na, b, nb);
}
else
{
*dst_neg = 0;
return bigint_raw_sub(dst, b, nb, a, na);
}
}
}
else
{
if (b_neg)
{
if (bigint_raw_cmp_abs(a, na, b, nb) >= 0)
{
*dst_neg = 0;
return bigint_raw_sub(dst, a, na, b, nb);
}
else
{
*dst_neg = 1;
return bigint_raw_sub(dst, b, nb, a, na);
}
}
else
{
if (na >= nb)
{
*dst_neg = 0;
return bigint_raw_add(dst, a, na, b, nb);
}
else
{
*dst_neg = 0;
return bigint_raw_add(dst, b, nb, a, na);
}
}
}
}
bigint* bigint_add_signed(
bigint *dst,
const bigint *a, int a_neg,
const bigint *b, int b_neg
)
{
int na = a->size;
int nb = b->size;
int n = BIGINT_MAX(na, nb) + 1;
bigint_reserve(dst, n);
dst->size = bigint_raw_add_signed(
dst->words, &dst->neg,
a->words, na, a_neg,
b->words, nb, b_neg
);
return dst;
}
bigint* bigint_add(bigint *dst, const bigint *a, const bigint *b)
{
return bigint_add_signed(dst, a, a->neg, b, b->neg);
}
bigint* bigint_sub(bigint *dst, const bigint *a, const bigint *b)
{
return bigint_add_signed(dst, a, a->neg, b, !b->neg);
}
bigint* bigint_add_word_signed(
bigint *dst,
const bigint *src_a,
bigint_word b, int b_neg
)
{
int na = src_a->size;
bigint_reserve(dst, na + 1);
dst->size = bigint_raw_add_signed(
dst->words, &dst->neg,
src_a->words, na, src_a->neg,
&b, 1, b_neg
);
return dst;
}
bigint* bigint_add_word(bigint *dst, const bigint *src_a, bigint_word b)
{
return bigint_add_word_signed(dst, src_a, b, 0);
}
bigint* bigint_sub_word(bigint *dst, const bigint *src_a, bigint_word b)
{
return bigint_add_word_signed(dst, src_a, b, 1);
}
int bigint_raw_shift_left(
bigint_word *dst, int n_dst,
const bigint_word *src, int n_src,
unsigned shift
)
{
int i;
int word_shift = shift / BIGINT_WORD_BITS;
int bits_shift = shift % BIGINT_WORD_BITS;
if (bits_shift)
{
bigint_word lo, hi = 0;
for (i = n_src + word_shift; i > word_shift; i--)
{
lo = src[i - word_shift - 1];
BIGINT_ASSERT(i, >=, 0);
BIGINT_ASSERT(i, <, n_dst);
dst[i] = (hi << bits_shift) | (lo >> (BIGINT_WORD_BITS - bits_shift));
hi = lo;
}
for (i = word_shift; i >= 0; i--)
{
BIGINT_ASSERT(i, >=, 0);
BIGINT_ASSERT(i, <, n_dst);
dst[i] = hi << bits_shift;
hi = 0;
}
i = n_src + word_shift + 1;
BIGINT_ASSERT(i, <=, n_dst);
return bigint_raw_truncate(dst, i);
}
else
{
/* this case is not only separate because of performance */
/* but (lo >> (BIGINT_WORD_BITS - 0)) is also undefined behaviour */
for (i = n_src + word_shift - 1; i >= word_shift; i--)
{
BIGINT_ASSERT(i, >=, 0);
BIGINT_ASSERT(i, <, n_dst);