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){
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_B==0 && b->length_B==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, word_alloc;
378 bigint_wordplus_t t=0;
384 for(i=0;i<=byteshift/sizeof(bigint_word_t); ++i){
385 a->wordv[a->length_B+i] = 0;
388 memmove(((uint8_t*)a->wordv)+byteshift, a->wordv, a->length_B*sizeof(bigint_word_t));
389 memset(a->wordv, 0, byteshift);
391 p = (bigint_word_t*)(((uint8_t*)a->wordv)+byteshift);
392 word_alloc = a->length_B+(byteshift+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t)+1;
393 a->wordv[word_alloc-1]=0;
395 for(i=0; i<a->length_B; ++i){
396 t |= ((bigint_wordplus_t)p[i])<<bitshift;
397 p[i] = (bigint_word_t)t;
398 t >>= BIGINT_WORD_SIZE;
400 p[i] = (bigint_word_t)t;
402 a->length_B = word_alloc;
406 /******************************************************************************/
408 void bigint_shiftright(bigint_t* a, uint16_t shift){
412 bigint_wordplus_t t=0;
415 if(byteshift >= a->length_B*sizeof(bigint_word_t)){ /* we would shift out more than we have */
419 if(byteshift == a->length_B*sizeof(bigint_word_t)-1 && bitshift>GET_FBS(a)){
424 memmove(a->wordv, (uint8_t*)a->wordv+byteshift, a->length_B-byteshift);
425 memset((uint8_t*)a->wordv+a->length_B-byteshift, 0, byteshift);
427 byteshift /= sizeof(bigint_word_t);
429 /* shift to the right */
430 for(i=a->length_B-byteshift-1; i>0; --i){
431 t |= ((bigint_wordplus_t)(a->wordv[i]))<<(BIGINT_WORD_SIZE-bitshift);
432 a->wordv[i] = (bigint_word_t)(t>>BIGINT_WORD_SIZE);
433 t <<= BIGINT_WORD_SIZE;
435 t |= ((bigint_wordplus_t)(a->wordv[0]))<<(BIGINT_WORD_SIZE-bitshift);
436 a->wordv[0] = (bigint_word_t)(t>>BIGINT_WORD_SIZE);
438 a->length_B -= ((shift/8)+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
442 /******************************************************************************/
444 void bigint_xor(bigint_t* dest, const bigint_t* a){
446 for(i=0; i<a->length_B; ++i){
447 dest->wordv[i] ^= a->wordv[i];
452 /******************************************************************************/
454 void bigint_set_zero(bigint_t* a){
458 /******************************************************************************/
460 /* using the Karatsuba-Algorithm */
461 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
462 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
463 if(a->length_B==0 || b->length_B==0){
464 bigint_set_zero(dest);
467 if(dest==a || dest==b){
469 bigint_word_t d_b[a->length_B+b->length_B];
471 bigint_mul_u(&d, a, b);
472 bigint_copy(dest, &d);
475 if(a->length_B==1 || b->length_B==1){
479 bigint_wordplus_t t=0;
481 bigint_word_t x = a->wordv[0];
482 for(i=0; i < b->length_B; ++i){
483 t += ((bigint_wordplus_t)b->wordv[i])*((bigint_wordplus_t)x);
484 dest->wordv[i] = (bigint_word_t)t;
485 t>>=BIGINT_WORD_SIZE;
487 dest->wordv[i] = (bigint_word_t)t;
488 dest->length_B = i+1;
493 if(a->length_B * sizeof(bigint_word_t) <= 4 && b->length_B * sizeof(bigint_word_t) <= 4){
496 memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t));
497 memcpy(&q, b->wordv, b->length_B*sizeof(bigint_word_t));
498 r = (uint64_t)p * (uint64_t)q;
499 memcpy(dest->wordv, &r, (dest->length_B = a->length_B + b->length_B)*sizeof(bigint_word_t));
503 bigint_set_zero(dest);
504 /* split a in xh & xl; split b in yh & yl */
505 const uint16_t n = (MAX(a->length_B, b->length_B)+1)/2;
506 bigint_t xl, xh, yl, yh;
510 bigint_set_zero(&xh);
511 xl.length_B = a->length_B;
517 xh.wordv = &(a->wordv[n]);
518 xh.length_B = a->length_B-n;
522 bigint_set_zero(&yh);
523 yl.length_B = b->length_B;
529 yh.wordv = &(b->wordv[n]);
530 yh.length_B = b->length_B-n;
533 /* now we have split up a and b */
534 /* remember we want to do:
535 * x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl
536 * 5 9 2 4 3 7 5 6 1 8 1
538 bigint_word_t tmp_b[2*n+2], m_b[2*(n+1)];
539 bigint_t tmp, tmp2, m;
541 tmp2.wordv = &(tmp_b[n+1]);
544 bigint_mul_u(dest, &xl, &yl); /* 1: dest <= xl*yl */
545 bigint_add_u(&tmp2, &xh, &xl); /* 2: tmp2 <= xh+xl */
546 bigint_add_u(&tmp, &yh, &yl); /* 3: tmp <= yh+yl */
547 bigint_mul_u(&m, &tmp2, &tmp); /* 4: m <= tmp2*tmp */
548 bigint_mul_u(&tmp, &xh, &yh); /* 5: h <= xh*yh */
549 bigint_sub_u(&m, &m, dest); /* 6: m <= m-dest */
550 bigint_sub_u(&m, &m, &tmp); /* 7: m <= m-h */
551 bigint_add_scale_u(dest, &m, n*sizeof(bigint_word_t)); /* 8: dest <= dest+m**n*/
552 bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t)); /* 9: dest <= dest+tmp**(2*n) */
555 /******************************************************************************/
557 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
560 s |= GET_SIGN(b)?1:0;
562 case 0: /* both positive */
563 bigint_mul_u(dest, a,b);
566 case 1: /* a positive, b negative */
567 bigint_mul_u(dest, a,b);
570 case 2: /* a negative, b positive */
571 bigint_mul_u(dest, a,b);
574 case 3: /* both negative */
575 bigint_mul_u(dest, a,b);
578 default: /* how can this happen?*/
583 /******************************************************************************/
586 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
587 void bigint_square(bigint_t* dest, const bigint_t* a){
588 if(a->length_B*sizeof(bigint_word_t)<=4){
590 memcpy(&r, a->wordv, a->length_B*sizeof(bigint_word_t));
592 memcpy(dest->wordv, &r, 2*a->length_B*sizeof(bigint_word_t));
594 dest->length_B=2*a->length_B;
600 bigint_word_t d_b[a->length_B*2];
602 bigint_square(&d, a);
603 bigint_copy(dest, &d);
608 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
609 bigint_word_t buffer[2*n+1];
613 xh.wordv = &(a->wordv[n]);
614 xh.length_B = a->length_B-n;
619 /* (xh * b**n + xl)**2 = xh**2 * b**2n + 2 * xh * xl * b**n + xl**2 */
621 // cli_putstr("\r\nDBG (a): xl: "); bigint_print_hex(&xl);
622 // cli_putstr("\r\nDBG (b): xh: "); bigint_print_hex(&xh);
623 bigint_square(dest, &xl);
624 // cli_putstr("\r\nDBG (1): xl**2: "); bigint_print_hex(dest);
625 bigint_square(&tmp, &xh);
626 // cli_putstr("\r\nDBG (2): xh**2: "); bigint_print_hex(&tmp);
627 bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t));
628 // cli_putstr("\r\nDBG (3): xl**2 + xh**2*n**2: "); bigint_print_hex(dest);
629 bigint_mul_u(&tmp, &xl, &xh);
630 // cli_putstr("\r\nDBG (4): xl*xh: "); bigint_print_hex(&tmp);
631 bigint_shiftleft(&tmp, 1);
632 // cli_putstr("\r\nDBG (5): xl*xh*2: "); bigint_print_hex(&tmp);
633 bigint_add_scale_u(dest, &tmp, n*sizeof(bigint_word_t));
634 // cli_putstr("\r\nDBG (6): x**2: "); bigint_print_hex(dest);
635 // cli_putstr("\r\n");
638 /******************************************************************************/
639 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
641 bigint_word_t tmp_b[b->length_B + 1];
642 const uint16_t word_shift = bitscale / BIGINT_WORD_SIZE;
644 if(a->length_B < b->length_B + word_shift){
646 cli_putstr("\r\nDBG: *bang*\r\n");
652 bigint_copy(&tmp, b);
653 bigint_shiftleft(&tmp, bitscale % BIGINT_WORD_SIZE);
656 x.wordv = &(a->wordv[word_shift]);
657 x.length_B = a->length_B - word_shift;
659 bigint_sub_u(&x, &x, &tmp);
664 /******************************************************************************/
666 void bigint_reduce(bigint_t* a, const bigint_t* r){
667 // bigint_adjust((bigint_t*)r);
668 uint8_t rfbs = GET_FBS(r);
670 cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
672 if(r->length_B==0 || a->length_B==0){
675 if((r->length_B*sizeof(bigint_word_t)<=4) && (a->length_B*sizeof(bigint_word_t)<=4)){
677 memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t));
678 memcpy(&q, r->wordv, r->length_B*sizeof(bigint_word_t));
680 memcpy(a->wordv, &p, a->length_B*sizeof(bigint_word_t));
682 // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a);
686 while(a->length_B > r->length_B){
687 shift = (a->length_B - r->length_B) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1;
689 if((a->wordv[a->length_B-1] & ((1LL<<GET_FBS(a)) - 1)) > r->wordv[r->length_B-1]){
691 cli_putstr("\r\n ~ [a] = ");
692 cli_hexdump_rev(&a->wordv[a->length_B-1], 4);
693 cli_putstr(" [r] = ");
694 cli_hexdump_rev(&r->wordv[r->length_B-1], 4);
698 // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2);
699 // cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_B, 2);
700 // cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_B, 2);
702 bigint_sub_u_bitscale(a, r, shift);
703 // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a);
705 while((GET_FBS(a) > rfbs) && (a->length_B == r->length_B)){
706 shift = GET_FBS(a)-rfbs-1;
707 // cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2);
708 bigint_sub_u_bitscale(a, r, shift);
709 // cli_putstr("\r\nDBG: (2) = "); bigint_print_hex(a);
711 while(bigint_cmp_u(a,r)>=0){
713 // cli_putstr("\r\nDBG: (3) = "); bigint_print_hex(a);
716 // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
717 // cli_putstr("\r\n");
720 /******************************************************************************/
722 /* calculate dest = a**exp % r */
723 /* using square&multiply */
724 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
725 if(a->length_B==0 || r->length_B==0){
730 bigint_word_t t, base_b[MAX(a->length_B,r->length_B)], res_b[r->length_B*2];
733 // uint16_t *xaddr = &i;
734 // cli_putstr("\r\npre-alloc (");
735 // cli_hexdump_rev(&xaddr, 4);
736 // cli_putstr(") ...");
739 bigint_copy(&base, a);
740 // cli_putstr("\r\npost-copy");
741 bigint_reduce(&base, r);
746 if(exp->length_B == 0){
747 bigint_copy(dest, &res);
751 t=exp->wordv[exp->length_B - 1];
752 for(i=exp->length_B; i > 0; --i){
753 t = exp->wordv[i - 1];
754 for(j=BIGINT_WORD_SIZE; j > 0; --j){
756 if(t & (1<<(BIGINT_WORD_SIZE-1))){
761 bigint_square(&res, &res);
762 bigint_reduce(&res, r);
763 if(t & (1<<(BIGINT_WORD_SIZE-1))){
764 bigint_mul_u(&res, &res, &base);
765 bigint_reduce(&res, r);
774 bigint_copy(dest, &res);
777 /******************************************************************************/
779 #define cli_putstr(a)
780 #define bigint_print_hex(a)
781 #define cli_hexdump_rev(a,b)
782 #define uart_flush(a)
784 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
785 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
786 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
788 if(x->length_B==0 || y->length_B==0){
791 if(x->length_B==1 && x->wordv[0]==1){
805 if(y->length_B==1 && y->wordv[0]==1){
820 while(x->wordv[i]==0 && y->wordv[i]==0){
823 bigint_word_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i];
824 bigint_word_t u_b[x->length_B-i], v_b[y->length_B-i];
825 bigint_word_t a_b[y->length_B+2], c_b[y->length_B+2];
826 bigint_word_t b_b[x->length_B+2], d_b[x->length_B+2];
831 memset(g_b, 0, i*sizeof(bigint_word_t));
835 x_.info = y_.info = 0;
836 x_.length_B = x->length_B-i;
837 y_.length_B = y->length_B-i;
838 memcpy(x_.wordv, x->wordv+i, x_.length_B*sizeof(bigint_word_t));
839 memcpy(y_.wordv, y->wordv+i, y_.length_B*sizeof(bigint_word_t));
840 for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
847 bigint_shiftleft(&g, i);
848 bigint_shiftright(&x_, i);
849 bigint_shiftright(&y_, i);
859 bigint_copy(&u, &x_);
860 bigint_copy(&v, &y_);
867 bigint_set_zero(&b_);
868 bigint_set_zero(&c_);
870 cli_putstr("\r\nDBG (gcdext) 0");
871 while((u.wordv[0]&1)==0){
872 cli_putstr("\r\nDBG (gcdext) 0.1");
873 bigint_shiftright(&u, 1);
874 if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
875 bigint_add_s(&a_, &a_, &y_);
876 bigint_sub_s(&b_, &b_, &x_);
878 bigint_shiftright(&a_, 1);
879 bigint_shiftright(&b_, 1);
881 while((v.wordv[0]&1)==0){
882 cli_putstr("\r\nDBG (gcdext) 0.2");
883 bigint_shiftright(&v, 1);
884 if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
885 bigint_add_s(&c_, &c_, &y_);
886 bigint_sub_s(&d_, &d_, &x_);
888 bigint_shiftright(&c_, 1);
889 bigint_shiftright(&d_, 1);
892 if(bigint_cmp_u(&u, &v)>=0){
893 bigint_sub_u(&u, &u, &v);
894 bigint_sub_s(&a_, &a_, &c_);
895 bigint_sub_s(&b_, &b_, &d_);
897 bigint_sub_u(&v, &v, &u);
898 bigint_sub_s(&c_, &c_, &a_);
899 bigint_sub_s(&d_, &d_, &b_);
903 bigint_mul_s(gcd, &v, &g);
913 /******************************************************************************/
915 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
916 bigint_gcdext(NULL, dest, NULL, a, m);
917 while(dest->info&BIGINT_NEG_MASK){
918 bigint_add_s(dest, dest, m);
922 /******************************************************************************/
924 void bigint_changeendianess(bigint_t* a){
926 p = (uint8_t*)(a->wordv);
927 q = ((uint8_t*)p)+a->length_B*sizeof(bigint_word_t)-1;
936 /******************************************************************************/