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 /******************************************************************************/
687 void bigint_sub_u_bitscale(bigint_t *a, const bigint_t *b, uint16_t bitscale){
689 bigint_word_t tmp_b[b->length_W + 1];
690 const uint16_t word_shift = bitscale / BIGINT_WORD_SIZE;
692 if(a->length_W < b->length_W + word_shift){
694 cli_putstr("\r\nDBG: *bang*\r\n");
700 bigint_copy(&tmp, b);
701 bigint_shiftleft(&tmp, bitscale % BIGINT_WORD_SIZE);
704 x.wordv = &(a->wordv[word_shift]);
705 x.length_W = a->length_W - word_shift;
707 bigint_sub_u(&x, &x, &tmp);
712 /******************************************************************************/
714 void bigint_reduce(bigint_t *a, const bigint_t *r){
715 // bigint_adjust((bigint_t*)r);
716 uint8_t rfbs = GET_FBS(r);
718 cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
720 if(r->length_W==0 || a->length_W==0){
724 if(bigint_length_b(a) + 3 > bigint_length_b(r)){
725 if((r->length_W*sizeof(bigint_word_t)<=4) && (a->length_W*sizeof(bigint_word_t)<=4)){
727 memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
728 memcpy(&q, r->wordv, r->length_W*sizeof(bigint_word_t));
730 memcpy(a->wordv, &p, a->length_W*sizeof(bigint_word_t));
732 // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a);
736 while(a->length_W > r->length_W){
737 shift = (a->length_W - r->length_W) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1;
739 if((a->wordv[a->length_W-1] & ((1LL<<GET_FBS(a)) - 1)) > r->wordv[r->length_W-1]){
741 cli_putstr("\r\n ~ [a] = ");
742 cli_hexdump_rev(&a->wordv[a->length_W-1], 4);
743 cli_putstr(" [r] = ");
744 cli_hexdump_rev(&r->wordv[r->length_W-1], 4);
748 // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2);
749 // cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_W, 2);
750 // cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_W, 2);
752 bigint_sub_u_bitscale(a, r, shift);
753 // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a);
755 while((GET_FBS(a) > rfbs) && (a->length_W == r->length_W)){
756 shift = GET_FBS(a)-rfbs-1;
757 // cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2);
758 bigint_sub_u_bitscale(a, r, shift);
759 // cli_putstr("\r\nDBG: (2) = "); bigint_print_hex(a);
762 while(bigint_cmp_u(a,r)>=0){
764 // cli_putstr("\r\nDBG: (3) = "); bigint_print_hex(a);
767 // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
768 // cli_putstr("\r\n");
771 /******************************************************************************/
773 /* calculate dest = a**exp % r */
774 /* using square&multiply */
775 void bigint_expmod_u_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r){
776 if(a->length_W==0 || r->length_W==0){
781 bigint_word_t t, base_b[MAX(a->length_W,r->length_W)], res_b[r->length_W*2];
784 // uint16_t *xaddr = &i;
785 // cli_putstr("\r\npre-alloc (");
786 // cli_hexdump_rev(&xaddr, 4);
787 // cli_putstr(") ...");
790 bigint_copy(&base, a);
791 // cli_putstr("\r\npost-copy");
792 bigint_reduce(&base, r);
797 if(exp->length_W == 0){
798 bigint_copy(dest, &res);
802 t=exp->wordv[exp->length_W - 1];
803 for(i=exp->length_W; i > 0; --i){
804 t = exp->wordv[i - 1];
805 for(j=BIGINT_WORD_SIZE; j > 0; --j){
807 if(t & (1<<(BIGINT_WORD_SIZE-1))){
812 bigint_square(&res, &res);
813 bigint_reduce(&res, r);
814 if(t & (1 << (BIGINT_WORD_SIZE - 1))){
815 bigint_mul_u(&res, &res, &base);
816 bigint_reduce(&res, r);
825 bigint_copy(dest, &res);
828 /******************************************************************************/
830 #define cli_putstr(a)
831 #define cli_putstr_P(a)
832 #define bigint_print_hex(a)
833 #define cli_hexdump_rev(a,b)
834 #define uart_flush(a)
835 #define printf_P(...)
837 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
838 void bigint_gcdext(bigint_t *gcd, bigint_t *a, bigint_t *b, const bigint_t *x, const bigint_t *y){
840 printf_P(PSTR("\nDBG: gcdext( "));
842 printf_P(PSTR(", "));
844 printf_P(PSTR(")\n"));
845 if(x->length_W == 0 || y->length_W == 0){
846 printf_P(PSTR("\nDBG: got zero in gcd <%s %s %d>\n"), __FILE__, __func__, __LINE__);
848 bigint_set_zero(gcd);
858 if(x->length_W == 1 && x->wordv[0] == 1){
875 if(y->length_W == 1 && y->wordv[0] == 1){
893 while(x->wordv[i] == 0 && y->wordv[i] == 0){
896 bigint_word_t g_b[i + 2], x_b[x->length_W - i], y_b[y->length_W - i];
897 bigint_word_t u_b[x->length_W - i], v_b[y->length_W - i];
898 bigint_word_t a_b[y->length_W + 2], c_b[y->length_W + 2];
899 bigint_word_t b_b[x->length_W + 2], d_b[x->length_W + 2];
900 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
905 memset(g_b, 0, i * sizeof(bigint_word_t));
909 x_.info = y_.info = 0;
910 x_.length_W = x->length_W - i;
911 y_.length_W = y->length_W - i;
912 memcpy(x_.wordv, x->wordv + i, x_.length_W * sizeof(bigint_word_t));
913 memcpy(y_.wordv, y->wordv + i, y_.length_W * sizeof(bigint_word_t));
914 for(i = 0; (x_.wordv[0] & (1 << i)) == 0 && (y_.wordv[0] & (1 << i)) == 0; ++i){
921 bigint_shiftleft(&g, i);
922 bigint_shiftright(&x_, i);
923 bigint_shiftright(&y_, i);
933 bigint_copy(&u, &x_);
934 bigint_copy(&v, &y_);
941 bigint_set_zero(&b_);
942 bigint_set_zero(&c_);
943 printf_P(PSTR("\nloop: x_ = "));
944 bigint_print_hex(&x_);
945 printf_P(PSTR("; y_ = "));
946 bigint_print_hex(&y_);
948 printf_P(PSTR("\nDBG (gcdext) 0"));
949 while((u.wordv[0] & 1) == 0){
950 printf_P(PSTR("\nDBG (gcdext) 0.1"));
951 bigint_shiftright(&u, 1);
952 if((a_.wordv[0] & 1) || (b_.wordv[0] & 1)){
953 bigint_add_s(&a_, &a_, &y_);
954 bigint_sub_s(&b_, &b_, &x_);
956 bigint_shiftright(&a_, 1);
957 bigint_shiftright(&b_, 1);
958 printf_P(PSTR(" a_ = "));
959 bigint_print_hex(&a_);
960 printf_P(PSTR("; b_ = "));
961 bigint_print_hex(&b_);
963 while((v.wordv[0] & 1) == 0){
964 printf_P(PSTR("\nDBG (gcdext) 0.2"));
965 bigint_shiftright(&v, 1);
966 if((c_.wordv[0] & 1) || (d_.wordv[0] & 1)){
967 bigint_add_s(&c_, &c_, &y_);
968 bigint_sub_s(&d_, &d_, &x_);
970 printf_P(PSTR(" c* = "));
971 bigint_print_hex(&c_);
972 bigint_shiftright(&c_, 1);
973 bigint_shiftright(&d_, 1);
974 printf_P(PSTR(" c_ = "));
975 bigint_print_hex(&c_);
976 printf_P(PSTR("; d_ = "));
977 bigint_print_hex(&d_);
979 if(bigint_cmp_u(&u, &v) >= 0){
980 printf_P(PSTR("\nDBG (gcdext) 0.3"));
981 bigint_sub_u(&u, &u, &v);
982 bigint_sub_s(&a_, &a_, &c_);
983 bigint_sub_s(&b_, &b_, &d_);
984 printf_P(PSTR(" a_ = "));
985 bigint_print_hex(&a_);
986 printf_P(PSTR("; b_ = "));
987 bigint_print_hex(&b_);
989 printf_P(PSTR("\nDBG (gcdext) 0.4"));
990 bigint_sub_u(&v, &v, &u);
991 bigint_sub_s(&c_, &c_, &a_);
992 bigint_sub_s(&d_, &d_, &b_);
993 printf_P(PSTR(" c_ = "));
994 bigint_print_hex(&c_);
995 printf_P(PSTR("; d_ = "));
996 bigint_print_hex(&d_);
1000 bigint_mul_s(gcd, &v, &g);
1003 bigint_copy(a, &c_);
1006 bigint_copy(b, &d_);
1010 /******************************************************************************/
1012 void bigint_inverse(bigint_t *dest, const bigint_t *a, const bigint_t *m){
1013 bigint_gcdext(NULL, dest, NULL, a, m);
1014 while(dest->info&BIGINT_NEG_MASK){
1015 bigint_add_s(dest, dest, m);
1019 /******************************************************************************/
1021 void bigint_changeendianess(bigint_t *a){
1023 p = (uint8_t*)(a->wordv);
1024 q = p + a->length_W * sizeof(bigint_word_t) - 1;
1033 /******************************************************************************/
1038 /******************************************************************************/
1040 void bigint_expmod_u(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r){
1041 bigint_expmod_u_sam(dest, a, exp, r);