3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * \license GPLv3 or later
30 #define STRING(x) STRING2(x)
31 #define STR_LINE STRING(__LINE__)
40 #include "bigint_io.h"
45 #define MAX(a,b) (((a)>(b))?(a):(b))
49 #define MIN(a,b) (((a)<(b))?(a):(b))
52 #define SET_FBS(a, v) do{(a)->info &=~BIGINT_FBS_MASK; (a)->info |= (v);}while(0)
53 #define GET_FBS(a) ((a)->info&BIGINT_FBS_MASK)
54 #define SET_NEG(a) (a)->info |= BIGINT_NEG_MASK
55 #define SET_POS(a) (a)->info &= ~BIGINT_NEG_MASK
56 #define XCHG(a,b) do{(a)^=(b); (b)^=(a); (a)^=(b);}while(0)
57 #define XCHG_PTR(a,b) do{ a = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b))); \
58 b = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b))); \
59 a = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b)));}while(0)
61 #define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
63 /******************************************************************************/
64 void bigint_adjust(bigint_t* a){
65 while(a->length_W!=0 && a->wordv[a->length_W-1]==0){
73 uint8_t i = BIGINT_WORD_SIZE-1;
74 t = a->wordv[a->length_W-1];
75 while((t&(1L<<(BIGINT_WORD_SIZE-1)))==0 && i){
82 /******************************************************************************/
84 uint16_t bigint_length_b(const bigint_t* a){
85 if(!a->length_W || a->length_W==0){
88 return (a->length_W-1) * BIGINT_WORD_SIZE + GET_FBS(a);
91 /******************************************************************************/
93 uint16_t bigint_length_B(const bigint_t* a){
94 return a->length_W * sizeof(bigint_word_t);
97 /******************************************************************************/
99 uint32_t bigint_get_first_set_bit(const bigint_t* a){
101 return (uint32_t)(-1);
103 return (a->length_W-1)*sizeof(bigint_word_t)*8+GET_FBS(a);
107 /******************************************************************************/
109 uint32_t bigint_get_last_set_bit(const bigint_t* a){
114 return (uint32_t)(-1);
116 while(a->wordv[r]==0 && r<a->length_W){
119 if(a->wordv[r] == 0){
120 return (uint32_t)(-1);
122 while((x&a->wordv[r])==0){
126 return r*BIGINT_WORD_SIZE+b;
129 /******************************************************************************/
131 void bigint_copy(bigint_t* dest, const bigint_t* src){
132 if(dest->wordv != src->wordv){
133 memcpy(dest->wordv, src->wordv, src->length_W * sizeof(bigint_word_t));
135 dest->length_W = src->length_W;
136 dest->info = src->info;
139 /******************************************************************************/
141 /* this should be implemented in assembly */
142 void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
144 bigint_wordplus_t t = 0LL;
145 if(a->length_W < b->length_W){
148 for(i = 0; i < b->length_W; ++i){
149 // t = (bigint_wordplus_t)(a->wordv[i]) + (bigint_wordplus_t)(b->wordv[i]) + t;
152 dest->wordv[i] = (bigint_word_t)t;
153 t >>= BIGINT_WORD_SIZE;
155 for(; i<a->length_W; ++i){
157 dest->wordv[i] = (bigint_word_t)t;
158 t >>= BIGINT_WORD_SIZE;
161 dest->wordv[i++] = (bigint_word_t)t;
167 /******************************************************************************/
169 /* this should be implemented in assembly */
170 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
171 if(a->length_W == 0){
175 bigint_add_u(dest, dest, a);
179 #if BIGINT_WORD_SIZE == 8
180 memset(dest->wordv + dest->length_W, 0, MAX(dest->length_W, a->length_W + scale) - dest->length_W);
181 x.wordv = dest->wordv + scale;
182 x.length_W = dest->length_W - scale;
183 if((int16_t)x.length_W < 0){
189 bigint_add_u(&x, &x, a);
190 dest->length_W = x.length_W + scale;
195 uint16_t word_shift = scale / sizeof(bigint_word_t), byte_shift = scale % sizeof(bigint_word_t);
196 bigint_word_t bv[a->length_W + 1];
198 bv[0] = bv[a->length_W] = 0;
199 memcpy((uint8_t*)bv + byte_shift, a->wordv, a->length_W * sizeof(bigint_word_t));
200 s.length_W = a->length_W + 1;
202 memset(dest->wordv + dest->length_W, 0, (MAX(dest->length_W, s.length_W + word_shift) - dest->length_W) * sizeof(bigint_word_t));
203 x.wordv = dest->wordv + word_shift;
204 x.length_W = dest->length_W - word_shift;
205 if((int16_t)x.length_W < 0){
211 bigint_add_u(&x, &x, &s);
212 dest->length_W = x.length_W + word_shift;
218 /******************************************************************************/
220 /* this should be implemented in assembly */
221 void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
224 bigint_wordplus_signed_t t=0LL;
226 if(b->length_W == 0){
227 bigint_copy(dest, a);
231 if(a->length_W == 0){
232 bigint_copy(dest, b);
236 r = bigint_cmp_u(a,b);
238 bigint_set_zero(dest);
242 bigint_sub_u(dest, b, a);
246 for(i = 0; i < a->length_W; ++i){
252 dest->wordv[i]=(bigint_word_t)t;
264 /******************************************************************************/
266 int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
267 if(a->length_W > b->length_W){
270 if(a->length_W < b->length_W){
279 if(a->wordv[i] != b->wordv[i]){
280 if(a->wordv[i] > b->wordv[i]){
290 /******************************************************************************/
292 void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
295 s |= GET_SIGN(b)?1:0;
297 case 0: /* both positive */
298 bigint_add_u(dest, a,b);
301 case 1: /* a positive, b negative */
302 bigint_sub_u(dest, a, b);
304 case 2: /* a negative, b positive */
305 bigint_sub_u(dest, b, a);
307 case 3: /* both negative */
308 bigint_add_u(dest, a, b);
311 default: /* how can this happen?*/
316 /******************************************************************************/
318 void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
321 s |= GET_SIGN(b)?1:0;
323 case 0: /* both positive */
324 bigint_sub_u(dest, a,b);
326 case 1: /* a positive, b negative */
327 bigint_add_u(dest, a, b);
330 case 2: /* a negative, b positive */
331 bigint_add_u(dest, a, b);
334 case 3: /* both negative */
335 bigint_sub_u(dest, b, a);
337 default: /* how can this happen?*/
343 /******************************************************************************/
345 int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
347 if(a->length_W==0 && b->length_W==0){
351 s |= GET_SIGN(b)?1:0;
353 case 0: /* both positive */
354 return bigint_cmp_u(a, b);
356 case 1: /* a positive, b negative */
359 case 2: /* a negative, b positive */
362 case 3: /* both negative */
363 return bigint_cmp_u(b, a);
365 default: /* how can this happen?*/
368 return 0; /* just to satisfy the compiler */
371 /******************************************************************************/
373 void bigint_shiftleft(bigint_t* a, uint16_t shift){
374 uint16_t byteshift, words_to_shift;
378 bigint_wordplus_t t = 0;
382 byteshift = shift / 8;
383 bitshift = shift & 7;
386 memmove(((uint8_t*)a->wordv) + byteshift, a->wordv, a->length_W * sizeof(bigint_word_t));
387 memset(a->wordv, 0, byteshift);
390 a->length_W += (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
394 p = a->wordv + byteshift / sizeof(bigint_word_t);
395 words_to_shift = a->length_W + (byteshift % sizeof(bigint_word_t)?1:0);
396 for(i=0; i < words_to_shift; ++i){
397 t |= ((bigint_wordplus_t)p[i]) << bitshift;
398 p[i] = (bigint_word_t)t;
399 t >>= BIGINT_WORD_SIZE;
402 p[i] = (bigint_word_t)t;
405 a->length_W += (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
409 /******************************************************************************/
411 void bigint_shiftright(bigint_t* a, uint16_t shift){
415 bigint_wordplus_t t = 0;
416 byteshift = shift / 8;
417 bitshift = shift & 7;
419 if(byteshift >= a->length_W * sizeof(bigint_word_t)){ /* we would shift out more than we have */
423 if(byteshift == a->length_W * sizeof(bigint_word_t) - 1 && bitshift > GET_FBS(a)){
429 memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_W * sizeof(bigint_word_t) - byteshift);
432 byteshift /= sizeof(bigint_word_t); /* byteshift is now wordshift */
433 a->length_W -= byteshift;
434 if(bitshift != 0 && a->length_W){
435 /* shift to the right */
438 t |= ((bigint_wordplus_t)(a->wordv[i])) << (BIGINT_WORD_SIZE - bitshift);
439 a->wordv[i] = (bigint_word_t)(t >> BIGINT_WORD_SIZE);
440 t <<= BIGINT_WORD_SIZE;
446 /******************************************************************************/
448 void bigint_xor(bigint_t* dest, const bigint_t* a){
450 for(i=0; i<a->length_W; ++i){
451 dest->wordv[i] ^= a->wordv[i];
456 /******************************************************************************/
458 void bigint_set_zero(bigint_t* a){
462 /******************************************************************************/
464 /* using the Karatsuba-Algorithm */
465 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
466 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
467 if(a->length_W == 0 || b->length_W == 0){
468 bigint_set_zero(dest);
471 if(dest == a || dest == b){
473 bigint_word_t d_b[a->length_W + b->length_W];
475 bigint_mul_u(&d, a, b);
476 bigint_copy(dest, &d);
479 if(a->length_W == 1 || b->length_W == 1){
480 if(a->length_W != 1){
483 bigint_wordplus_t t = 0;
485 bigint_word_t x = a->wordv[0];
486 for(i=0; i < b->length_W; ++i){
487 t += ((bigint_wordplus_t)b->wordv[i]) * ((bigint_wordplus_t)x);
488 dest->wordv[i] = (bigint_word_t)t;
489 t >>= BIGINT_WORD_SIZE;
493 dest->wordv[i] = (bigint_word_t)t;
500 if(a->length_W * sizeof(bigint_word_t) <= 4 && b->length_W * sizeof(bigint_word_t) <= 4){
503 memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
504 memcpy(&q, b->wordv, b->length_W*sizeof(bigint_word_t));
505 r = (uint64_t)p * (uint64_t)q;
506 memcpy(dest->wordv, &r, (dest->length_W = a->length_W + b->length_W)*sizeof(bigint_word_t));
510 /* split a in xh & xl; split b in yh & yl */
511 const uint16_t n = (MAX(a->length_W, b->length_W)+1)/2;
512 bigint_t xl, xh, yl, yh;
516 bigint_set_zero(&xh);
517 xl.length_W = a->length_W;
523 xh.wordv = &(a->wordv[n]);
524 xh.length_W = a->length_W-n;
528 bigint_set_zero(&yh);
529 yl.length_W = b->length_W;
535 yh.wordv = &(b->wordv[n]);
536 yh.length_W = b->length_W-n;
539 /* now we have split up a and b */
540 /* remember we want to do:
541 * x*y = (xh * b**n + xl) * (yh * b**n + yl)
542 * = (xh * yh) * b**2n + xh * b**n * yl + yh * b**n * xl + xl * yl
543 * = (xh * yh) * b**2n + (xh * yl + yh * xl) * b**n + xl *yl
544 * // xh * yl + yh * xl = (xh + yh) * (xl + yl) - xh * yh - xl * yl
545 * x*y = (xh * yh) * b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + xl*yl
546 * 5 9 2 4 3 7 5 6 1 8 1
548 bigint_word_t tmp_b[2 * n + 2], m_b[2 * (n + 1)];
549 bigint_t tmp, tmp2, m;
551 tmp2.wordv = &(tmp_b[n + 1]);
554 bigint_mul_u(dest, &xl, &yl); /* 1: dest <= xl*yl */
555 bigint_add_u(&tmp2, &xh, &xl); /* 2: tmp2 <= xh+xl */
556 bigint_add_u(&tmp, &yh, &yl); /* 3: tmp <= yh+yl */
557 bigint_mul_u(&m, &tmp2, &tmp); /* 4: m <= tmp2*tmp */
558 bigint_mul_u(&tmp, &xh, &yh); /* 5: tmp <= xh*yh */
559 bigint_sub_u(&m, &m, dest); /* 6: m <= m-dest */
560 bigint_sub_u(&m, &m, &tmp); /* 7: m <= m-tmp */
561 bigint_add_scale_u(dest, &m, n * sizeof(bigint_word_t)); /* 8: dest <= dest+m**n*/
562 bigint_add_scale_u(dest, &tmp, 2 * n * sizeof(bigint_word_t)); /* 9: dest <= dest+tmp**(2*n) */
565 /******************************************************************************/
567 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
570 s |= GET_SIGN(b)?1:0;
572 case 0: /* both positive */
573 bigint_mul_u(dest, a,b);
576 case 1: /* a positive, b negative */
577 bigint_mul_u(dest, a,b);
580 case 2: /* a negative, b positive */
581 bigint_mul_u(dest, a,b);
584 case 3: /* both negative */
585 bigint_mul_u(dest, a,b);
588 default: /* how can this happen?*/
593 /******************************************************************************/
597 unsigned square_depth = 0;
601 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
602 void bigint_square(bigint_t* dest, const bigint_t* a){
603 if(a->length_W * sizeof(bigint_word_t) <= 4){
605 memcpy(&r, a->wordv, a->length_W * sizeof(bigint_word_t));
607 memcpy(dest->wordv, &r, 2 * a->length_W * sizeof(bigint_word_t));
609 dest->length_W = 2 * a->length_W;
613 if(dest->wordv == a->wordv){
615 bigint_word_t d_b[a->length_W*2];
617 bigint_square(&d, a);
618 bigint_copy(dest, &d);
628 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
629 bigint_word_t buffer[2*n+1];
633 xh.wordv = &(a->wordv[n]);
634 xh.length_W = a->length_W-n;
638 /* (xh * b**n + xl)**2 = xh**2 * b**2n + 2 * xh * xl * b**n + xl**2 */
640 if(square_depth == 1){
641 cli_putstr("\r\nDBG (a): xl: "); bigint_print_hex(&xl);
642 cli_putstr("\r\nDBG (b): xh: "); bigint_print_hex(&xh);
645 bigint_square(dest, &xl);
647 if(square_depth == 1){
648 cli_putstr("\r\nDBG (1): xl**2: "); bigint_print_hex(dest);
651 bigint_square(&tmp, &xh);
653 if(square_depth == 1){
654 cli_putstr("\r\nDBG (2): xh**2: "); bigint_print_hex(&tmp);
657 bigint_add_scale_u(dest, &tmp, 2 * n * sizeof(bigint_word_t));
659 if(square_depth == 1){
660 cli_putstr("\r\nDBG (3): xl**2 + xh**2*n**2: "); bigint_print_hex(dest);
663 bigint_mul_u(&tmp, &xl, &xh);
665 if(square_depth == 1){
666 cli_putstr("\r\nDBG (4): xl*xh: "); bigint_print_hex(&tmp);
669 bigint_shiftleft(&tmp, 1);
671 if(square_depth == 1){
672 cli_putstr("\r\nDBG (5): xl*xh*2: "); bigint_print_hex(&tmp);
675 bigint_add_scale_u(dest, &tmp, n * sizeof(bigint_word_t));
677 if(square_depth == 1){
678 cli_putstr("\r\nDBG (6): x**2: "); bigint_print_hex(dest);
685 /******************************************************************************/
686 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
688 bigint_word_t tmp_b[b->length_W + 1];
689 const uint16_t word_shift = bitscale / BIGINT_WORD_SIZE;
691 if(a->length_W < b->length_W + word_shift){
693 cli_putstr("\r\nDBG: *bang*\r\n");
699 bigint_copy(&tmp, b);
700 bigint_shiftleft(&tmp, bitscale % BIGINT_WORD_SIZE);
703 x.wordv = &(a->wordv[word_shift]);
704 x.length_W = a->length_W - word_shift;
706 bigint_sub_u(&x, &x, &tmp);
711 /******************************************************************************/
713 void bigint_reduce(bigint_t* a, const bigint_t* r){
714 // bigint_adjust((bigint_t*)r);
715 uint8_t rfbs = GET_FBS(r);
717 cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
719 if(r->length_W==0 || a->length_W==0){
722 if((r->length_W*sizeof(bigint_word_t)<=4) && (a->length_W*sizeof(bigint_word_t)<=4)){
724 memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
725 memcpy(&q, r->wordv, r->length_W*sizeof(bigint_word_t));
727 memcpy(a->wordv, &p, a->length_W*sizeof(bigint_word_t));
729 // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a);
733 while(a->length_W > r->length_W){
734 shift = (a->length_W - r->length_W) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1;
736 if((a->wordv[a->length_W-1] & ((1LL<<GET_FBS(a)) - 1)) > r->wordv[r->length_W-1]){
738 cli_putstr("\r\n ~ [a] = ");
739 cli_hexdump_rev(&a->wordv[a->length_W-1], 4);
740 cli_putstr(" [r] = ");
741 cli_hexdump_rev(&r->wordv[r->length_W-1], 4);
745 // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2);
746 // cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_W, 2);
747 // cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_W, 2);
749 bigint_sub_u_bitscale(a, r, shift);
750 // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a);
752 while((GET_FBS(a) > rfbs) && (a->length_W == r->length_W)){
753 shift = GET_FBS(a)-rfbs-1;
754 // cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2);
755 bigint_sub_u_bitscale(a, r, shift);
756 // cli_putstr("\r\nDBG: (2) = "); bigint_print_hex(a);
758 while(bigint_cmp_u(a,r)>=0){
760 // cli_putstr("\r\nDBG: (3) = "); bigint_print_hex(a);
763 // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
764 // cli_putstr("\r\n");
767 /******************************************************************************/
769 /* calculate dest = a**exp % r */
770 /* using square&multiply */
771 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
772 if(a->length_W==0 || r->length_W==0){
777 bigint_word_t t, base_b[MAX(a->length_W,r->length_W)], res_b[r->length_W*2];
780 // uint16_t *xaddr = &i;
781 // cli_putstr("\r\npre-alloc (");
782 // cli_hexdump_rev(&xaddr, 4);
783 // cli_putstr(") ...");
786 bigint_copy(&base, a);
787 // cli_putstr("\r\npost-copy");
788 bigint_reduce(&base, r);
793 if(exp->length_W == 0){
794 bigint_copy(dest, &res);
798 t=exp->wordv[exp->length_W - 1];
799 for(i=exp->length_W; i > 0; --i){
800 t = exp->wordv[i - 1];
801 for(j=BIGINT_WORD_SIZE; j > 0; --j){
803 if(t & (1<<(BIGINT_WORD_SIZE-1))){
808 bigint_square(&res, &res);
809 bigint_reduce(&res, r);
810 if(t & (1 << (BIGINT_WORD_SIZE - 1))){
811 bigint_mul_u(&res, &res, &base);
812 bigint_reduce(&res, r);
821 bigint_copy(dest, &res);
824 /******************************************************************************/
826 #define cli_putstr(a)
827 #define cli_putstr_P(a)
828 #define bigint_print_hex(a)
829 #define cli_hexdump_rev(a,b)
830 #define uart_flush(a)
831 #define printf_P(...)
833 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
834 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
836 printf_P(PSTR("\nDBG: gcdext( "));
838 printf_P(PSTR(", "));
840 printf_P(PSTR(")\n"));
841 if(x->length_W == 0 || y->length_W == 0){
842 printf_P(PSTR("\nDBG: got zero in gcd <%s %s %d>\n"), __FILE__, __func__, __LINE__);
844 bigint_set_zero(gcd);
854 if(x->length_W == 1 && x->wordv[0] == 1){
871 if(y->length_W == 1 && y->wordv[0] == 1){
889 while(x->wordv[i] == 0 && y->wordv[i] == 0){
892 bigint_word_t g_b[i + 2], x_b[x->length_W - i], y_b[y->length_W - i];
893 bigint_word_t u_b[x->length_W - i], v_b[y->length_W - i];
894 bigint_word_t a_b[y->length_W + 2], c_b[y->length_W + 2];
895 bigint_word_t b_b[x->length_W + 2], d_b[x->length_W + 2];
896 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
901 memset(g_b, 0, i * sizeof(bigint_word_t));
905 x_.info = y_.info = 0;
906 x_.length_W = x->length_W - i;
907 y_.length_W = y->length_W - i;
908 memcpy(x_.wordv, x->wordv + i, x_.length_W * sizeof(bigint_word_t));
909 memcpy(y_.wordv, y->wordv + i, y_.length_W * sizeof(bigint_word_t));
910 for(i = 0; (x_.wordv[0] & (1 << i)) == 0 && (y_.wordv[0] & (1 << i)) == 0; ++i){
917 bigint_shiftleft(&g, i);
918 bigint_shiftright(&x_, i);
919 bigint_shiftright(&y_, i);
929 bigint_copy(&u, &x_);
930 bigint_copy(&v, &y_);
937 bigint_set_zero(&b_);
938 bigint_set_zero(&c_);
939 printf_P(PSTR("\nloop: x_ = "));
940 bigint_print_hex(&x_);
941 printf_P(PSTR("; y_ = "));
942 bigint_print_hex(&y_);
944 printf_P(PSTR("\nDBG (gcdext) 0"));
945 while((u.wordv[0] & 1) == 0){
946 printf_P(PSTR("\nDBG (gcdext) 0.1"));
947 bigint_shiftright(&u, 1);
948 if((a_.wordv[0] & 1) || (b_.wordv[0] & 1)){
949 bigint_add_s(&a_, &a_, &y_);
950 bigint_sub_s(&b_, &b_, &x_);
952 bigint_shiftright(&a_, 1);
953 bigint_shiftright(&b_, 1);
954 printf_P(PSTR(" a_ = "));
955 bigint_print_hex(&a_);
956 printf_P(PSTR("; b_ = "));
957 bigint_print_hex(&b_);
959 while((v.wordv[0] & 1) == 0){
960 printf_P(PSTR("\nDBG (gcdext) 0.2"));
961 bigint_shiftright(&v, 1);
962 if((c_.wordv[0] & 1) || (d_.wordv[0] & 1)){
963 bigint_add_s(&c_, &c_, &y_);
964 bigint_sub_s(&d_, &d_, &x_);
966 printf_P(PSTR(" c* = "));
967 bigint_print_hex(&c_);
968 bigint_shiftright(&c_, 1);
969 bigint_shiftright(&d_, 1);
970 printf_P(PSTR(" c_ = "));
971 bigint_print_hex(&c_);
972 printf_P(PSTR("; d_ = "));
973 bigint_print_hex(&d_);
975 if(bigint_cmp_u(&u, &v) >= 0){
976 printf_P(PSTR("\nDBG (gcdext) 0.3"));
977 bigint_sub_u(&u, &u, &v);
978 bigint_sub_s(&a_, &a_, &c_);
979 bigint_sub_s(&b_, &b_, &d_);
980 printf_P(PSTR(" a_ = "));
981 bigint_print_hex(&a_);
982 printf_P(PSTR("; b_ = "));
983 bigint_print_hex(&b_);
985 printf_P(PSTR("\nDBG (gcdext) 0.4"));
986 bigint_sub_u(&v, &v, &u);
987 bigint_sub_s(&c_, &c_, &a_);
988 bigint_sub_s(&d_, &d_, &b_);
989 printf_P(PSTR(" c_ = "));
990 bigint_print_hex(&c_);
991 printf_P(PSTR("; d_ = "));
992 bigint_print_hex(&d_);
996 bigint_mul_s(gcd, &v, &g);
1002 bigint_copy(b, &d_);
1006 /******************************************************************************/
1008 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
1009 bigint_gcdext(NULL, dest, NULL, a, m);
1010 while(dest->info&BIGINT_NEG_MASK){
1011 bigint_add_s(dest, dest, m);
1015 /******************************************************************************/
1017 void bigint_changeendianess(bigint_t* a){
1019 p = (uint8_t*)(a->wordv);
1020 q = p + a->length_W * sizeof(bigint_word_t) - 1;
1029 /******************************************************************************/