4 * Created on: 09.04.2014
9 #include <bigint2_io.h>
20 #define MAX(a,b) ((a) > (b) ? (a) : (b))
21 #define MIN(a,b) ((a) < (b) ? (a) : (b))
23 void *(*int_realloc)(void *ptr, size_t size);
24 void (*int_free)(void *ptr);
27 * \brief used to check if bigint is large enough
28 * Checks if the memory of a is large enough to hold min_size
29 * words, and if this is not the case tries to increase the allocated memory
30 * amount (conserving the memory content).
31 * If the allocated memory insufficient and the buffer could not be enlarged
32 * E_OOM is returned, otherwise 0 is returned.
35 int check_size(bigint_t *a, bigint_length_t min_size) {
37 if (a->allocated_W >= min_size) {
40 tmp = int_realloc(a->wordv, min_size * sizeof(bigint_word_t));
45 a->allocated_W = min_size;
49 int bigint_copy(bigint_t *dest, const bigint_t *a) {
54 r = check_size(dest, a->length_W);
59 dest->length_W = a->length_W;
60 memcpy(dest->wordv, a->wordv, a->length_W * sizeof(bigint_word_t));
65 int bigint_free(bigint_t *a) {
69 memset(a, 0, sizeof(bigint_t));
74 * \brief dest = |a| + |b|
75 * Adds the bigints a and b and stores the result in dest.
78 int bigint_add_u(bigint_t *dest, const bigint_t *a, const bigint_t *b) {
79 bigint_word_t t, c1, c2, c = 0;
83 if (!dest || !a || !b) {
88 if ((r = check_size(dest, a->length_W))) {
92 j = MIN(a->length_W, b->length_W);
93 for (i = 0; i < j; ++i) {
94 t = a->wordv[i] + b->wordv[i];
96 dest->wordv[i] = t + c;
97 c2 = dest->wordv[i] < t;
100 for (; i < a->length_W; ++i) {
102 dest->wordv[i] = t + c;
103 c = dest->wordv[i] < t;
110 * \brief dest = |a| + |b|
111 * Adds the bigints a and b and stores the result in dest.
114 int bigint_add_auto_u(bigint_t *dest, const bigint_t *a, const bigint_t *b) {
115 bigint_word_t t, c1, c2, c = 0;
116 bigint_length_t i, j;
119 if (!dest || !a || !b) {
123 if (a->length_W < b->length_W) {
130 if ((r = check_size(dest, a->length_W + 1))) {
134 j = MIN(a->length_W, b->length_W);
135 for (i = 0; i < j; ++i) {
136 t = a->wordv[i] + b->wordv[i];
137 c1 = t < a->wordv[i];
138 dest->wordv[i] = t + c;
139 c2 = dest->wordv[i] < t;
142 for (; i < a->length_W; ++i) {
144 dest->wordv[i] = t + c;
145 c = dest->wordv[i] < t;
148 dest->wordv[i++] = c;
158 int bigint_invert(bigint_t *dest, const bigint_t *a) {
162 if ((r = check_size(dest, a->length_W))) {
168 dest->wordv[i] = ~a->wordv[i];
170 dest->length_W = a->length_W;
175 * \brief dest = a + 1
177 int bigint_add_one(bigint_t *dest, const bigint_t *a) {
179 .wordv = (bigint_word_t*)"\x01",
184 return bigint_add_u(dest, a, &one);
190 int bigint_negate(bigint_t *dest, const bigint_t *a) {
193 if ((r = check_size(dest, a->length_W))) {
197 r |= bigint_invert(dest, a);
198 r |= bigint_add_one(dest, dest);
203 * \brief dest = |a| - |b|
204 * Subtracts b from a and stores the result in dest.
207 int bigint_sub_u(bigint_t *dest, const bigint_t *a, const bigint_t *b) {
208 bigint_length_t i, j;
209 bigint_word_t t, c1, c2, c = 0;
212 if (!dest || !a || !b) {
217 if ((r = check_size(dest, MAX(a->length_W, b->length_W)))) {
222 j = MIN(a->length_W, b->length_W);
223 for (i = 0; i < j; ++i) {
224 t = a->wordv[i] - b->wordv[i];
225 c1 = t > a->wordv[i];
226 dest->wordv[i] = t - c;
227 c2 = dest->wordv[i] > t;
230 for (; i < a->length_W; ++i) {
232 dest->wordv[i] = t - c;
233 c = dest->wordv[i] > t;
235 dest->length_W = a->length_W;
242 int bigint_shiftleft_words(bigint_t *dest, const bigint_t *a, bigint_length_t s) {
249 bigint_copy(dest, a);
253 if ((r = check_size(dest, a->length_W + s))) {
256 memmove(&dest->wordv[s], &a->wordv[0], a->length_W * sizeof(bigint_word_t));
257 memset(&dest->wordv[0], 0, s * sizeof(bigint_word_t));
258 dest->length_W = a->length_W + s;
266 int bigint_shiftleft_1bit(bigint_t *a) {
276 for(i = 0; i < a->length_W; ++i) {
278 c2 = t >> (BIGINT_WORD_SIZE - 1);
292 int bigint_shiftleft_small(bigint_t *dest, const bigint_t *a, uint_fast8_t s) {
293 bigint_wordplus_t t = 0;
294 bigint_length_t i, l;
300 l = bigint_get_first_set_bit(a) + BIGINT_WORD_SIZE;
303 if ((r = check_size(dest, (l + s ) / BIGINT_WORD_SIZE))) {
308 dest->length_W = (l + s ) / BIGINT_WORD_SIZE;
309 l /= BIGINT_WORD_SIZE;
310 for(i = 0; i < l; ++i) {
311 t |= (bigint_wordplus_t)a->wordv[i] << s;
313 t >>= BIGINT_WORD_SIZE;
322 * \brief dest = a << s
324 int bigint_shiftleft(bigint_t *dest, const bigint_t *a, bigint_length_t s) {
326 if((r = bigint_shiftleft_small(dest, a, s % BIGINT_WORD_SIZE))) {
329 if ((r = bigint_shiftleft_words(dest, dest, s / BIGINT_WORD_SIZE))) {
338 int bigint_shiftright_1bit(bigint_t *a) {
348 for(i = a->length_W; i != 0; --i) {
352 t |= c1 << (BIGINT_WORD_SIZE - 1);
361 * \brief dest = a ** 2
363 int bigint_square(bigint_t *dest, const bigint_t *a) {
364 bigint_word_t c1, c2;
365 bigint_wordplus_t uv;
367 bigint_length_t i, j;
373 if ((r = check_size(dest, a->length_W * 2))) {
378 bigint_t t = {NULL, 0, 0, 0};
380 r = bigint_square(dest, &t);
384 memset(dest->wordv, 0, 2 * a->length_W * sizeof(bigint_word_t));
385 for(i = 0; i < a->length_W; ++i) {
386 uv = (bigint_wordplus_t)dest->wordv[2 * i]
387 + (bigint_wordplus_t)a->wordv[i]
388 * (bigint_wordplus_t)a->wordv[i];
389 dest->wordv[2 * i] = uv;
390 c1 = uv >> BIGINT_WORD_SIZE;
392 for (j = i + 1; j < a->length_W; ++j) {
393 uv = (bigint_wordplus_t)a->wordv[i]
394 * (bigint_wordplus_t)a->wordv[j];
395 q = uv >> (2 * BIGINT_WORD_SIZE - 1);
397 uv += dest->wordv[i + j];
398 q += (uv < dest->wordv[i + j]); /* this might be dangerous! XXX */
400 q += (uv < c1); /* this might be dangerous! XXX */
401 dest->wordv[i + j] = uv;
402 c1 = c2 + (uv >> BIGINT_WORD_SIZE);
403 c2 = q + (c1 < c2); /* this might be dangerous! XXX */
405 dest->wordv[i + a->length_W] += c1;
406 dest->wordv[i + a->length_W + 1] = c2 + (dest->wordv[i + a->length_W] < c1); /* this might be dangerous! XXX */
408 dest->length_W = a->length_W * 2;
414 * \brief dest = |a * b|
415 * unsigned multiply a bigint (a) by a word (b)
417 int bigint_mul_word(bigint_t *dest, const bigint_t *a, const bigint_word_t b) {
418 bigint_wordplus_t c1 = 0;
422 if (!dest || !a || !b) {
425 if ((r = check_size(dest, a->length_W + 1))) {
429 for(i = 0; i < a->length_W; ++i) {
430 c1 += (bigint_wordplus_t)b * (bigint_wordplus_t)a->wordv[i];
432 c1 >>= BIGINT_WORD_SIZE;
434 dest->wordv[i++] = c1;
436 BIGINT_SET_POS(dest);
441 * \brief dest = a * b
443 int bigint_mul_schoolbook(bigint_t *dest, const bigint_t *a, const bigint_t *b) {
446 bigint_length_t i, j;
449 if (!dest || !a || !b) {
452 if ((r = check_size(dest, a->length_W + b->length_W))) {
457 bigint_t t = {NULL, 0, 0, 0};
459 r = bigint_mul_schoolbook(dest, &t, b);
464 bigint_t t = {NULL, 0, 0, 0};
466 r = bigint_mul_schoolbook(dest, a, &t);
470 memset(dest->wordv, 0, (a->length_W + b->length_W) * sizeof(bigint_word_t));
471 for(i = 0; i < b->length_W; ++i) {
473 for(j = 0; j < a->length_W; ++j) {
474 v = (bigint_wordplus_t)a->wordv[j]
475 * (bigint_wordplus_t)b->wordv[i];
476 v += (bigint_wordplus_t)dest->wordv[i + j];
477 v += (bigint_wordplus_t)c;
478 dest->wordv[i + j] = v;
479 c = v >> BIGINT_WORD_SIZE;
481 dest->wordv[i + j] = c;
484 dest->length_W = a->length_W + b->length_W;
485 dest->info &= ~BIGINT_SIGN_MASK;
486 dest->info |= (a->info ^ b->info) & BIGINT_SIGN_MASK;
493 int8_t bigint_cmp_u(const bigint_t *a, const bigint_t *b) {
496 if (a->length_W == 0 && b->length_W == 0) {
499 if (b->length_W == 0) {
502 if (a->length_W == 0) {
505 if (a->length_W < b->length_W) {
512 for (i = a->length_W - 1; i > b->length_W - 1; --i) {
520 if (a->wordv[i] != b->wordv[i]) {
521 return a->wordv[i] > b->wordv[i] ? r : -r;
527 int8_t bigint_cmp_s(const bigint_t *a, const bigint_t *b) {
528 if ( bigint_get_first_set_bit(a) == (bigint_length_t)-1 &&
529 bigint_get_first_set_bit(b) == (bigint_length_t)-1 ) {
532 if ((a->info & BIGINT_SIGN_MASK) ^ (b->info & BIGINT_SIGN_MASK)) {
533 return (a->info & BIGINT_SIGN_MASK) ? -1 : 1;
536 r = bigint_cmp_u(a, b);
537 return (a->info & BIGINT_SIGN_MASK) ? -r : r;
546 int8_t bigint_cmp_scale(const bigint_t *a, const bigint_t *b, bigint_length_t s) {
549 return bigint_cmp_u(a, b);
551 if (a->length_W == 0 && b->length_W == 0) {
554 if (b->length_W == 0) {
557 if (a->length_W == 0) {
560 if (s > a->length_W) {
563 for (i = a->length_W - 1; i > b->length_W + s - 1; --i) {
568 for (; i > s - 1; --i) {
569 if (a->wordv[i] != b->wordv[i + s]){
570 return a->wordv[i] > b->wordv[i + s] ? 1 : -1;
585 * \brief dest = |a| - |b| * B ** s
586 * Subtracts b from a and stores the result in dest.
589 int bigint_sub_scale(bigint_t *dest, const bigint_t *a, const bigint_t *b, bigint_length_t s) {
590 bigint_length_t i, j;
591 bigint_word_t t, c1, c2, c = 0;
594 if (!dest || !a || !b) {
599 if ((r = check_size(dest, MAX(a->length_W, b->length_W)))) {
604 j = MIN(a->length_W, b->length_W);
605 for (i = 0; i < j; ++i) {
606 t = a->wordv[i + s] - b->wordv[i];
607 c1 = t > a->wordv[i + s];
608 dest->wordv[i + s] = t - c;
609 c2 = dest->wordv[i + s] > t;
612 for (; i < a->length_W; ++i) {
614 dest->wordv[i + s] = t - c;
615 c = dest->wordv[i + s] > t;
617 dest->length_W = a->length_W;
624 int bigint_adjust_length(bigint_t *a) {
630 while (a->length_W) {
631 if (a->wordv[a->length_W - 1]) {
639 bigint_length_t bigint_get_first_set_bit(const bigint_t *a) {
643 return (bigint_length_t)-1;
645 if (a->length_W == 0) {
646 return (bigint_length_t)-1;
649 while (r && a->wordv[r] == 0) {
654 return (bigint_length_t)-1;
656 r *= BIGINT_WORD_SIZE;
669 int bigint_divide(bigint_t *q, bigint_t *r, const bigint_t *a, const bigint_t *b) {
670 bigint_t a_ = {NULL, 0, 0, 0}, x_ = {NULL, 0, 0, 0};
671 bigint_length_t i, la, lb;
674 if (!a || !b || (!q && !r)) {
678 la = bigint_get_first_set_bit(a);
679 lb = bigint_get_first_set_bit(b);
680 if (lb == (bigint_length_t)-1) {
683 if (la == (bigint_length_t)-1) {
703 if ((ret = check_size(q, (i + BIGINT_WORD_SIZE) / BIGINT_WORD_SIZE))) {
706 q->length_W = (i + BIGINT_WORD_SIZE) / BIGINT_WORD_SIZE;
707 memset(q->wordv, 0, q->allocated_W * sizeof(bigint_word_t));
710 if ((ret = check_size(q, (lb + BIGINT_WORD_SIZE - 1) / BIGINT_WORD_SIZE))) {
714 if ((ret = bigint_copy(&a_, a))) {
717 if ((ret = bigint_shiftleft(&x_, b, i))) {
722 printf("DBG: x' = ");
723 bigint_print_hex(&x_);
726 printf("; la = %d; lb = %d; i = %d\n", la, lb, i);
729 if (bigint_cmp_u(&a_, &x_) >= 0) {
730 bigint_sub_u(&a_, &a_, &x_);
732 q->wordv[i / BIGINT_WORD_SIZE] |= 1 << (i % BIGINT_WORD_SIZE);
735 bigint_shiftright_1bit(&x_);
748 int bigint_sub_s(bigint_t *dest, const bigint_t *a, const bigint_t *b) {
751 if (a->length_W == 0) {
752 bigint_copy(dest, b);
753 dest->info ^= BIGINT_SIGN_MASK;
756 if ((a->info ^ b->info) & BIGINT_SIGN_MASK) {
757 /* different signs */
758 if ((r = bigint_add_auto_u(dest, a, b))) {
761 dest->info = a->info;
763 s = bigint_cmp_u(a, b);
765 if (( r = bigint_sub_u(dest, a, b))) {
768 dest->info = a->info;
770 if (( r = bigint_sub_u(dest, b, a))) {
773 dest->info = (~a->info) & BIGINT_SIGN_MASK;
782 int bigint_add_s(bigint_t *dest, const bigint_t *a, const bigint_t *b) {
785 if ((a->info ^ b->info) & BIGINT_SIGN_MASK) {
786 /* different signs */
787 s = bigint_cmp_u(a, b);
789 if (( r = bigint_sub_u(dest, a, b))) {
792 dest->info = a->info;
794 if (( r = bigint_sub_u(dest, b, a))) {
797 dest->info = (~a->info) & BIGINT_SIGN_MASK;
800 if ((r = bigint_add_auto_u(dest, a, b))) {
803 dest->info = a->info;
808 int8_t bigint_is_even(const bigint_t *a) {
812 if (a->length_W == 0 || (a->wordv[0] & 1) == 0) {
818 int8_t bigint_is_odd(const bigint_t *a) {
822 if (a->length_W > 0 && (a->wordv[0] & 1) == 1) {
830 * (a, b) -> (x, y, v)
834 int bigint_gcdext(bigint_t *gcd, bigint_t *x, bigint_t *y, const bigint_t *a, const bigint_t *b) {
835 bigint_length_t g = 0;
836 bigint_t u = {NULL, 0, 0, 0}, v = {NULL, 0, 0, 0};
837 bigint_t x_ = {NULL, 0, 0, 0}, y_ = {NULL, 0, 0, 0};
838 bigint_t A = {NULL, 0, 0, 0}, B = {NULL, 0, 0, 0};
839 bigint_t C = {NULL, 0, 0, 0}, D = {NULL, 0, 0, 0};
841 if ((r = bigint_copy(&x_, a))) {
844 if ((r = bigint_copy(&y_, b))) {
848 bigint_adjust_length(&x_);
849 bigint_adjust_length(&y_);
850 if (x_.length_W == 0 || y_.length_W == 0) {
855 while (((x_.wordv[0] | y_.wordv[0]) & 1) == 0) {
857 bigint_shiftright_1bit(&x_);
858 bigint_shiftright_1bit(&y_);
860 if ((r = check_size(&A, 1 + (bigint_get_first_set_bit(&y_) + BIGINT_WORD_SIZE) / BIGINT_WORD_SIZE))) {
865 if ((r = check_size(&C, 1 + (bigint_get_first_set_bit(&y_) + BIGINT_WORD_SIZE) / BIGINT_WORD_SIZE))) {
871 if ((r = check_size(&B, 1 + (bigint_get_first_set_bit(&x_) + BIGINT_WORD_SIZE) / BIGINT_WORD_SIZE))) {
878 if ((r = check_size(&D, 1 + (bigint_get_first_set_bit(&x_) + BIGINT_WORD_SIZE) / BIGINT_WORD_SIZE))) {
886 if ((r = bigint_copy(&u, &x_))) {
895 if ((r = bigint_copy(&v, &y_))) {
905 A.wordv[0] = D.wordv[0] = 1;
906 A.length_W = D.length_W = 1;
907 C.length_W = B.length_W = 0;
910 while ((u.wordv[0] & 1) == 0) {
911 bigint_shiftright_1bit(&u);
912 if (bigint_is_odd(&A) || bigint_is_odd(&B)) {
913 bigint_add_s(&A, &A, &y_);
914 bigint_sub_s(&B, &B, &x_);
916 bigint_shiftright_1bit(&A);
917 bigint_shiftright_1bit(&B);
919 while ((v.wordv[0] & 1) == 0) {
920 bigint_shiftright_1bit(&v);
921 if (bigint_is_odd(&C) || bigint_is_odd(&D)) {
922 bigint_add_s(&C, &C, &y_);
923 bigint_sub_s(&D, &D, &x_);
925 bigint_shiftright_1bit(&C);
926 bigint_shiftright_1bit(&D);
928 if (bigint_cmp_s(&u, &v) >= 0) {
929 bigint_sub_s(&u, &u, &v);
930 bigint_sub_s(&A, &A, &C);
931 bigint_sub_s(&B, &B, &D);
933 bigint_sub_s(&v, &v, &u);
934 bigint_sub_s(&C, &C, &A);
935 bigint_sub_s(&D, &D, &B);
937 bigint_adjust_length(&u);
938 } while (u.length_W != 0);
940 bigint_shiftleft(gcd, &v, g);