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"
44 #define MAX(a,b) (((a)>(b))?(a):(b))
48 #define MIN(a,b) (((a)<(b))?(a):(b))
51 #define SET_FBS(a, v) do{(a)->info &=~BIGINT_FBS_MASK; (a)->info |= (v);}while(0)
52 #define GET_FBS(a) ((a)->info&BIGINT_FBS_MASK)
53 #define SET_NEG(a) (a)->info |= BIGINT_NEG_MASK
54 #define SET_POS(a) (a)->info &= ~BIGINT_NEG_MASK
55 #define XCHG(a,b) do{(a)^=(b); (b)^=(a); (a)^=(b);}while(0)
56 #define XCHG_PTR(a,b) do{ a = (void*)(((bigint_ptr_int_t)(a)) ^ ((bigint_ptr_int_t)(b))); \
57 b = (void*)(((bigint_ptr_int_t)(a)) ^ ((bigint_ptr_int_t)(b))); \
58 a = (void*)(((bigint_ptr_int_t)(a)) ^ ((bigint_ptr_int_t)(b)));}while(0)
60 #define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
62 /******************************************************************************/
63 void bigint_adjust(bigint_t* a){
64 while(a->length_B!=0 && a->wordv[a->length_B-1]==0){
72 uint8_t i = BIGINT_WORD_SIZE-1;
73 t = a->wordv[a->length_B-1];
74 while((t&(1L<<(BIGINT_WORD_SIZE-1)))==0 && i){
81 /******************************************************************************/
83 uint16_t bigint_length_b(bigint_t* a){
84 if(!a->length_B || a->length_B==0){
87 return (a->length_B-1) * BIGINT_WORD_SIZE + GET_FBS(a);
90 /******************************************************************************/
92 uint16_t bigint_length_B(bigint_t* a){
93 return a->length_B * sizeof(bigint_word_t);
96 /******************************************************************************/
98 uint32_t bigint_get_first_set_bit(bigint_t* a){
100 return (uint32_t)(-1);
102 return (a->length_B-1)*sizeof(bigint_word_t)*8+GET_FBS(a);
106 /******************************************************************************/
108 uint32_t bigint_get_last_set_bit(bigint_t* a){
113 return (uint32_t)(-1);
115 while(a->wordv[r]==0 && r<a->length_B){
118 if(a->wordv[r] == 0){
119 return (uint32_t)(-1);
121 while((x&a->wordv[r])==0){
125 return r*BIGINT_WORD_SIZE+b;
128 /******************************************************************************/
130 void bigint_copy(bigint_t* dest, const bigint_t* src){
131 memcpy(dest->wordv, src->wordv, src->length_B*sizeof(bigint_word_t));
132 dest->length_B = src->length_B;
133 dest->info = src->info;
136 /******************************************************************************/
138 /* this should be implemented in assembly */
139 void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
141 bigint_wordplus_t t=0LL;
142 if(a->length_B < b->length_B){
145 for(i=0; i<b->length_B; ++i){
146 // t = (bigint_wordplus_t)(a->wordv[i]) + (bigint_wordplus_t)(b->wordv[i]) + t;
149 dest->wordv[i] = (bigint_word_t)t;
150 t>>=BIGINT_WORD_SIZE;
152 for(; i<a->length_B; ++i){
154 dest->wordv[i] = (bigint_word_t)t;
155 t>>=BIGINT_WORD_SIZE;
157 dest->wordv[i++] = (bigint_word_t)t;
162 /******************************************************************************/
164 /* this should be implemented in assembly */
165 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
167 #if BIGINT_WORD_SIZE == 8
168 memset(dest->wordv + dest->length_B, 0, MAX(dest->length_B, a->length_B + scale) - dest->length_B);
169 x.wordv = dest->wordv + scale;
170 x.length_B = dest->length_B - scale;
171 if((int16_t)x.length_B < 0)
174 bigint_add_u(&x, &x, a);
175 dest->length_B = x.length_B + scale;
179 #error unimplemented!
186 bigint_wordplus_t t=0;
187 scale_w = (scale+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
188 if(scale>dest->length_B*sizeof(bigint_word_t)){
189 memset(((uint8_t*)dest->wordv)+dest->length_B*sizeof(bigint_word_t), 0, scale-dest->length_B*sizeof(bigint_word_t));
191 // a->wordv = (const uint32_t*)(((uint8_t*)a->wordv)+(scale&3));
192 dst = dest->wordv + (scale&(sizeof(bigint_word_t)-1));
193 for(i=scale/sizeof(bigint_word_t); i<a->length_B+scale_w; ++i,++j){
195 if(dest->length_B>i){
198 dst[i] = (bigint_word_t)t;
199 t>>=BIGINT_WORD_SIZE;
202 if(dest->length_B>i){
205 dst[i] = (bigint_word_t)t;
206 t>>=BIGINT_WORD_SIZE;
209 if(dest->length_B < i){
216 /******************************************************************************/
218 /* this should be implemented in assembly */
219 void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
222 bigint_wordplus_signed_t t=0LL;
223 uint16_t i, min, max;
224 min = MIN(a->length_B, b->length_B);
225 max = MAX(a->length_B, b->length_B);
226 r = bigint_cmp_u(a,b);
228 bigint_set_zero(dest);
232 bigint_copy(dest, a);
237 bigint_copy(dest, b);
242 bigint_sub_u(dest, b, a);
246 for(i=0; i<max; ++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_B > b->length_B){
270 if(a->length_B < b->length_B){
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){
296 s |= GET_SIGN(b)?1:0;
298 case 0: /* both positive */
300 bigint_add_u(dest, a,b);
302 case 1: /* a positive, b negative */
303 d = bigint_cmp_u(a,b);
304 bigint_sub_u(dest, a, b);
306 case 2: /* a negative, b positive */
307 d = bigint_cmp_u(b,a);
308 bigint_sub_u(dest, b, a);
310 case 3: /* both negative */
312 bigint_add_u(dest, a, b);
314 default: /* how can this happen?*/
324 /******************************************************************************/
326 void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
329 s |= GET_SIGN(b)?1:0;
331 case 0: /* both positive */
332 bigint_sub_u(dest, a,b);
334 case 1: /* a positive, b negative */
335 bigint_add_u(dest, a, b);
338 case 2: /* a negative, b positive */
339 bigint_add_u(dest, a, b);
342 case 3: /* both negative */
343 bigint_sub_u(dest, b, a);
345 default: /* how can this happen?*/
351 /******************************************************************************/
353 int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
355 if(a->length_B==0 && b->length_B==0){
359 s |= GET_SIGN(b)?1:0;
361 case 0: /* both positive */
362 return bigint_cmp_u(a, b);
364 case 1: /* a positive, b negative */
367 case 2: /* a negative, b positive */
370 case 3: /* both negative */
371 return bigint_cmp_u(b, a);
373 default: /* how can this happen?*/
376 return 0; /* just to satisfy the compiler */
379 /******************************************************************************/
381 void bigint_shiftleft(bigint_t* a, uint16_t shift){
382 uint16_t byteshift, word_alloc;
386 bigint_wordplus_t t=0;
392 for(i=0;i<=byteshift/sizeof(bigint_word_t); ++i){
393 a->wordv[a->length_B+i] = 0;
396 memmove(((uint8_t*)a->wordv)+byteshift, a->wordv, a->length_B*sizeof(bigint_word_t));
397 memset(a->wordv, 0, byteshift);
399 p = (bigint_word_t*)(((uint8_t*)a->wordv)+byteshift);
400 word_alloc = a->length_B+(byteshift+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t)+1;
401 a->wordv[word_alloc-1]=0;
403 for(i=0; i<a->length_B; ++i){
404 t |= ((bigint_wordplus_t)p[i])<<bitshift;
405 p[i] = (bigint_word_t)t;
406 t >>= BIGINT_WORD_SIZE;
408 p[i] = (bigint_word_t)t;
410 a->length_B = word_alloc;
414 /******************************************************************************/
416 void bigint_shiftright(bigint_t* a, uint16_t shift){
420 bigint_wordplus_t t=0;
423 if(byteshift >= a->length_B*sizeof(bigint_word_t)){ /* we would shift out more than we have */
427 if(byteshift == a->length_B*sizeof(bigint_word_t)-1 && bitshift>GET_FBS(a)){
432 memmove(a->wordv, (uint8_t*)a->wordv+byteshift, a->length_B-byteshift);
433 memset((uint8_t*)a->wordv+a->length_B-byteshift, 0, byteshift);
435 byteshift /= sizeof(bigint_word_t);
437 /* shift to the right */
438 for(i=a->length_B-byteshift-1; i>0; --i){
439 t |= ((bigint_wordplus_t)(a->wordv[i]))<<(BIGINT_WORD_SIZE-bitshift);
440 a->wordv[i] = (bigint_word_t)(t>>BIGINT_WORD_SIZE);
441 t <<= BIGINT_WORD_SIZE;
443 t |= ((bigint_wordplus_t)(a->wordv[0]))<<(BIGINT_WORD_SIZE-bitshift);
444 a->wordv[0] = (bigint_word_t)(t>>BIGINT_WORD_SIZE);
446 a->length_B -= ((shift/8)+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
450 /******************************************************************************/
452 void bigint_xor(bigint_t* dest, const bigint_t* a){
454 for(i=0; i<a->length_B; ++i){
455 dest->wordv[i] ^= a->wordv[i];
460 /******************************************************************************/
462 void bigint_set_zero(bigint_t* a){
466 /******************************************************************************/
468 /* using the Karatsuba-Algorithm */
469 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
470 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
471 if(a->length_B==0 || b->length_B==0){
472 bigint_set_zero(dest);
475 if(dest==a || dest==b){
477 bigint_word_t d_b[a->length_B+b->length_B];
479 bigint_mul_u(&d, a, b);
480 bigint_copy(dest, &d);
483 if(a->length_B==1 || b->length_B==1){
487 bigint_wordplus_t t=0;
489 bigint_word_t x = a->wordv[0];
490 for(i=0; i < b->length_B; ++i){
491 t += ((bigint_wordplus_t)b->wordv[i])*((bigint_wordplus_t)x);
492 dest->wordv[i] = (bigint_word_t)t;
493 t>>=BIGINT_WORD_SIZE;
495 dest->wordv[i] = (bigint_word_t)t;
496 dest->length_B = i+1;
501 if(a->length_B * sizeof(bigint_word_t) <= 4 && b->length_B * sizeof(bigint_word_t) <= 4){
504 memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t));
505 memcpy(&q, b->wordv, b->length_B*sizeof(bigint_word_t));
506 r = (uint64_t)p * (uint64_t)q;
507 memcpy(dest->wordv, &r, (dest->length_B = a->length_B + b->length_B)*sizeof(bigint_word_t));
511 bigint_set_zero(dest);
512 /* split a in xh & xl; split b in yh & yl */
513 const uint16_t n = (MAX(a->length_B, b->length_B)+1)/2;
514 bigint_t xl, xh, yl, yh;
518 bigint_set_zero(&xh);
519 xl.length_B = a->length_B;
525 xh.wordv = &(a->wordv[n]);
526 xh.length_B = a->length_B-n;
530 bigint_set_zero(&yh);
531 yl.length_B = b->length_B;
537 yh.wordv = &(b->wordv[n]);
538 yh.length_B = b->length_B-n;
541 /* now we have split up a and b */
542 /* remember we want to do:
543 * x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl
544 * 5 9 2 4 3 7 5 6 1 8 1
546 bigint_word_t tmp_b[2*n+2], m_b[2*(n+1)];
547 bigint_t tmp, tmp2, m;
549 tmp2.wordv = &(tmp_b[n+1]);
552 bigint_mul_u(dest, &xl, &yl); /* 1: dest <= xl*yl */
553 bigint_add_u(&tmp2, &xh, &xl); /* 2: tmp2 <= xh+xl */
554 bigint_add_u(&tmp, &yh, &yl); /* 3: tmp <= yh+yl */
555 bigint_mul_u(&m, &tmp2, &tmp); /* 4: m <= tmp2*tmp */
556 bigint_mul_u(&tmp, &xh, &yh); /* 5: h <= xh*yh */
557 bigint_sub_u(&m, &m, dest); /* 6: m <= m-dest */
558 bigint_sub_u(&m, &m, &tmp); /* 7: m <= m-h */
559 bigint_add_scale_u(dest, &m, n*sizeof(bigint_word_t)); /* 8: dest <= dest+m**n*/
560 bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t)); /* 9: dest <= dest+tmp**(2*n) */
563 /******************************************************************************/
565 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
568 s |= GET_SIGN(b)?1:0;
570 case 0: /* both positive */
571 bigint_mul_u(dest, a,b);
574 case 1: /* a positive, b negative */
575 bigint_mul_u(dest, a,b);
578 case 2: /* a negative, b positive */
579 bigint_mul_u(dest, a,b);
582 case 3: /* both negative */
583 bigint_mul_u(dest, a,b);
586 default: /* how can this happen?*/
591 /******************************************************************************/
594 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
595 void bigint_square(bigint_t* dest, const bigint_t* a){
596 if(a->length_B*sizeof(bigint_word_t)<=4){
598 memcpy(&r, a->wordv, a->length_B*sizeof(bigint_word_t));
600 memcpy(dest->wordv, &r, 2*a->length_B*sizeof(bigint_word_t));
602 dest->length_B=2*a->length_B;
608 bigint_word_t d_b[a->length_B*2];
610 bigint_square(&d, a);
611 bigint_copy(dest, &d);
616 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
617 bigint_word_t buffer[2*n+1];
621 xh.wordv = &(a->wordv[n]);
622 xh.length_B = a->length_B-n;
627 /* (xh * b**n + xl)**2 = xh**2 * b**2n + 2 * xh * xl * b**n + xl**2 */
629 // cli_putstr("\r\nDBG (a): xl: "); bigint_print_hex(&xl);
630 // cli_putstr("\r\nDBG (b): xh: "); bigint_print_hex(&xh);
631 bigint_square(dest, &xl);
632 // cli_putstr("\r\nDBG (1): xl**2: "); bigint_print_hex(dest);
633 bigint_square(&tmp, &xh);
634 // cli_putstr("\r\nDBG (2): xh**2: "); bigint_print_hex(&tmp);
635 bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t));
636 // cli_putstr("\r\nDBG (3): xl**2 + xh**2*n**2: "); bigint_print_hex(dest);
637 bigint_mul_u(&tmp, &xl, &xh);
638 // cli_putstr("\r\nDBG (4): xl*xh: "); bigint_print_hex(&tmp);
639 bigint_shiftleft(&tmp, 1);
640 // cli_putstr("\r\nDBG (5): xl*xh*2: "); bigint_print_hex(&tmp);
641 bigint_add_scale_u(dest, &tmp, n*sizeof(bigint_word_t));
642 // cli_putstr("\r\nDBG (6): x**2: "); bigint_print_hex(dest);
643 // cli_putstr("\r\n");
646 /******************************************************************************/
647 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
649 bigint_word_t tmp_b[b->length_B + 1];
650 const uint16_t word_shift = bitscale / BIGINT_WORD_SIZE;
652 if(a->length_B < b->length_B + word_shift){
654 cli_putstr("\r\nDBG: *bang*\r\n");
660 bigint_copy(&tmp, b);
661 bigint_shiftleft(&tmp, bitscale % BIGINT_WORD_SIZE);
664 x.wordv = &(a->wordv[word_shift]);
665 x.length_B = a->length_B - word_shift;
667 bigint_sub_u(&x, &x, &tmp);
672 /******************************************************************************/
674 void bigint_reduce(bigint_t* a, const bigint_t* r){
675 // bigint_adjust((bigint_t*)r);
676 uint8_t rfbs = GET_FBS(r);
678 cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
680 if(r->length_B==0 || a->length_B==0){
683 if((r->length_B*sizeof(bigint_word_t)<=4) && (a->length_B*sizeof(bigint_word_t)<=4)){
685 memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t));
686 memcpy(&q, r->wordv, r->length_B*sizeof(bigint_word_t));
688 memcpy(a->wordv, &p, a->length_B*sizeof(bigint_word_t));
690 // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a);
694 while(a->length_B > r->length_B){
695 shift = (a->length_B - r->length_B) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1;
697 if((a->wordv[a->length_B-1] & ((1LL<<GET_FBS(a)) - 1)) > r->wordv[r->length_B-1]){
699 cli_putstr("\r\n ~ [a] = ");
700 cli_hexdump_rev(&a->wordv[a->length_B-1], 4);
701 cli_putstr(" [r] = ");
702 cli_hexdump_rev(&r->wordv[r->length_B-1], 4);
706 // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2);
707 // cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_B, 2);
708 // cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_B, 2);
710 bigint_sub_u_bitscale(a, r, shift);
711 // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a);
713 while((GET_FBS(a) > rfbs) && (a->length_B == r->length_B)){
714 shift = GET_FBS(a)-rfbs-1;
715 // cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2);
716 bigint_sub_u_bitscale(a, r, shift);
717 // cli_putstr("\r\nDBG: (2) = "); bigint_print_hex(a);
719 while(bigint_cmp_u(a,r)>=0){
721 // cli_putstr("\r\nDBG: (3) = "); bigint_print_hex(a);
724 // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
725 // cli_putstr("\r\n");
728 /******************************************************************************/
730 /* calculate dest = a**exp % r */
731 /* using square&multiply */
732 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
733 if(a->length_B==0 || r->length_B==0){
738 bigint_word_t t, base_b[MAX(a->length_B,r->length_B)], res_b[r->length_B*2];
741 // uint16_t *xaddr = &i;
742 // cli_putstr("\r\npre-alloc (");
743 // cli_hexdump_rev(&xaddr, 4);
744 // cli_putstr(") ...");
747 bigint_copy(&base, a);
748 // cli_putstr("\r\npost-copy");
749 bigint_reduce(&base, r);
754 if(exp->length_B == 0){
755 bigint_copy(dest, &res);
759 t=exp->wordv[exp->length_B - 1];
760 for(i=exp->length_B; i > 0; --i){
761 t = exp->wordv[i - 1];
762 for(j=BIGINT_WORD_SIZE; j > 0; --j){
764 if(t & (1<<(BIGINT_WORD_SIZE-1))){
769 bigint_square(&res, &res);
770 bigint_reduce(&res, r);
771 if(t & (1<<(BIGINT_WORD_SIZE-1))){
772 bigint_mul_u(&res, &res, &base);
773 bigint_reduce(&res, r);
782 bigint_copy(dest, &res);
785 /******************************************************************************/
787 #define cli_putstr(a)
788 #define bigint_print_hex(a)
789 #define cli_hexdump_rev(a,b)
790 #define uart_flush(a)
792 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
793 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
794 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
796 if(x->length_B==0 || y->length_B==0){
799 if(x->length_B==1 && x->wordv[0]==1){
813 if(y->length_B==1 && y->wordv[0]==1){
828 while(x->wordv[i]==0 && y->wordv[i]==0){
831 bigint_word_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i];
832 bigint_word_t u_b[x->length_B-i], v_b[y->length_B-i];
833 bigint_word_t a_b[y->length_B+2], c_b[y->length_B+2];
834 bigint_word_t b_b[x->length_B+2], d_b[x->length_B+2];
839 memset(g_b, 0, i*sizeof(bigint_word_t));
843 x_.info = y_.info = 0;
844 x_.length_B = x->length_B-i;
845 y_.length_B = y->length_B-i;
846 memcpy(x_.wordv, x->wordv+i, x_.length_B*sizeof(bigint_word_t));
847 memcpy(y_.wordv, y->wordv+i, y_.length_B*sizeof(bigint_word_t));
848 for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
855 bigint_shiftleft(&g, i);
856 bigint_shiftright(&x_, i);
857 bigint_shiftright(&y_, i);
867 bigint_copy(&u, &x_);
868 bigint_copy(&v, &y_);
875 bigint_set_zero(&b_);
876 bigint_set_zero(&c_);
878 cli_putstr("\r\nDBG (gcdext) 0");
879 while((u.wordv[0]&1)==0){
880 cli_putstr("\r\nDBG (gcdext) 0.1");
881 bigint_shiftright(&u, 1);
882 if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
883 bigint_add_s(&a_, &a_, &y_);
884 bigint_sub_s(&b_, &b_, &x_);
886 bigint_shiftright(&a_, 1);
887 bigint_shiftright(&b_, 1);
889 while((v.wordv[0]&1)==0){
890 cli_putstr("\r\nDBG (gcdext) 0.2");
891 bigint_shiftright(&v, 1);
892 if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
893 bigint_add_s(&c_, &c_, &y_);
894 bigint_sub_s(&d_, &d_, &x_);
896 bigint_shiftright(&c_, 1);
897 bigint_shiftright(&d_, 1);
900 if(bigint_cmp_u(&u, &v)>=0){
901 bigint_sub_u(&u, &u, &v);
902 bigint_sub_s(&a_, &a_, &c_);
903 bigint_sub_s(&b_, &b_, &d_);
905 bigint_sub_u(&v, &v, &u);
906 bigint_sub_s(&c_, &c_, &a_);
907 bigint_sub_s(&d_, &d_, &b_);
911 bigint_mul_s(gcd, &v, &g);
921 /******************************************************************************/
923 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
924 bigint_gcdext(NULL, dest, NULL, a, m);
925 while(dest->info&BIGINT_NEG_MASK){
926 bigint_add_s(dest, dest, m);
930 /******************************************************************************/
932 void bigint_changeendianess(bigint_t* a){
934 p = (uint8_t*)(a->wordv);
935 q = ((uint8_t*)p)+a->length_B*sizeof(bigint_word_t)-1;
944 /******************************************************************************/