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*)(((intptr_t)(a)) ^ ((intptr_t)(b))); \
57 b = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b))); \
58 a = (void*)(((intptr_t)(a)) ^ ((intptr_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_W!=0 && a->wordv[a->length_W-1]==0){
72 uint8_t i = BIGINT_WORD_SIZE-1;
73 t = a->wordv[a->length_W-1];
74 while((t&(1L<<(BIGINT_WORD_SIZE-1)))==0 && i){
81 /******************************************************************************/
83 uint16_t bigint_length_b(const bigint_t* a){
84 if(!a->length_W || a->length_W==0){
87 return (a->length_W-1) * BIGINT_WORD_SIZE + GET_FBS(a);
90 /******************************************************************************/
92 uint16_t bigint_length_B(const bigint_t* a){
93 return a->length_W * sizeof(bigint_word_t);
96 /******************************************************************************/
98 uint32_t bigint_get_first_set_bit(const bigint_t* a){
100 return (uint32_t)(-1);
102 return (a->length_W-1)*sizeof(bigint_word_t)*8+GET_FBS(a);
106 /******************************************************************************/
108 uint32_t bigint_get_last_set_bit(const bigint_t* a){
113 return (uint32_t)(-1);
115 while(a->wordv[r]==0 && r<a->length_W){
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_W*sizeof(bigint_word_t));
132 dest->length_W = src->length_W;
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_W < b->length_W){
145 for(i=0; i<b->length_W; ++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_W; ++i){
154 dest->wordv[i] = (bigint_word_t)t;
155 t>>=BIGINT_WORD_SIZE;
158 dest->wordv[i++] = (bigint_word_t)t;
164 /******************************************************************************/
166 /* this should be implemented in assembly */
167 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
168 if(a->length_W == 0){
172 bigint_add_u(dest, dest, a);
176 #if BIGINT_WORD_SIZE == 8
177 memset(dest->wordv + dest->length_W, 0, MAX(dest->length_W, a->length_W + scale) - dest->length_W);
178 x.wordv = dest->wordv + scale;
179 x.length_W = dest->length_W - scale;
180 if((int16_t)x.length_W < 0){
186 bigint_add_u(&x, &x, a);
187 dest->length_W = x.length_W + scale;
192 uint16_t word_shift = scale / sizeof(bigint_word_t), byte_shift = scale % sizeof(bigint_word_t);
193 bigint_word_t bv[a->length_W + 1];
195 bv[0] = bv[a->length_W] = 0;
196 memcpy((uint8_t*)bv + byte_shift, a->wordv, a->length_W * sizeof(bigint_word_t));
197 s.length_W = a->length_W + 1;
199 memset(dest->wordv + dest->length_W, 0, (MAX(dest->length_W, s.length_W + word_shift) - dest->length_W) * sizeof(bigint_word_t));
200 x.wordv = dest->wordv + word_shift;
201 x.length_W = dest->length_W - word_shift;
202 if((int16_t)x.length_W < 0){
208 bigint_add_u(&x, &x, &s);
209 dest->length_W = x.length_W + word_shift;
218 bigint_wordplus_t t=0;
219 scale_w = (scale+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
220 if(scale>dest->length_W*sizeof(bigint_word_t)){
221 memset(((uint8_t*)dest->wordv)+dest->length_W*sizeof(bigint_word_t), 0, scale-dest->length_W*sizeof(bigint_word_t));
223 // a->wordv = (const uint32_t*)(((uint8_t*)a->wordv)+(scale&3));
224 dst = dest->wordv + (scale&(sizeof(bigint_word_t)-1));
225 for(i=scale/sizeof(bigint_word_t); i<a->length_W+scale_w; ++i,++j){
227 if(dest->length_W>i){
230 dst[i] = (bigint_word_t)t;
231 t>>=BIGINT_WORD_SIZE;
234 if(dest->length_W>i){
237 dst[i] = (bigint_word_t)t;
238 t>>=BIGINT_WORD_SIZE;
241 if(dest->length_W < i){
248 /******************************************************************************/
250 /* this should be implemented in assembly */
251 void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
254 bigint_wordplus_signed_t t=0LL;
255 uint16_t i, min, max;
256 min = MIN(a->length_W, b->length_W);
257 max = MAX(a->length_W, b->length_W);
258 r = bigint_cmp_u(a,b);
260 bigint_set_zero(dest);
264 bigint_copy(dest, a);
269 bigint_copy(dest, b);
274 bigint_sub_u(dest, b, a);
278 for(i=0; i<max; ++i){
284 dest->wordv[i]=(bigint_word_t)t;
296 /******************************************************************************/
298 int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
299 if(a->length_W > b->length_W){
302 if(a->length_W < b->length_W){
311 if(a->wordv[i] != b->wordv[i]){
312 if(a->wordv[i] > b->wordv[i]){
322 /******************************************************************************/
324 void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
327 s |= GET_SIGN(b)?1:0;
329 case 0: /* both positive */
330 bigint_add_u(dest, a,b);
333 case 1: /* a positive, b negative */
334 bigint_sub_u(dest, a, b);
336 case 2: /* a negative, b positive */
337 bigint_sub_u(dest, b, a);
339 case 3: /* both negative */
340 bigint_add_u(dest, a, b);
343 default: /* how can this happen?*/
348 /******************************************************************************/
350 void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
353 s |= GET_SIGN(b)?1:0;
355 case 0: /* both positive */
356 bigint_sub_u(dest, a,b);
358 case 1: /* a positive, b negative */
359 bigint_add_u(dest, a, b);
362 case 2: /* a negative, b positive */
363 bigint_add_u(dest, a, b);
366 case 3: /* both negative */
367 bigint_sub_u(dest, b, a);
369 default: /* how can this happen?*/
375 /******************************************************************************/
377 int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
379 if(a->length_W==0 && b->length_W==0){
383 s |= GET_SIGN(b)?1:0;
385 case 0: /* both positive */
386 return bigint_cmp_u(a, b);
388 case 1: /* a positive, b negative */
391 case 2: /* a negative, b positive */
394 case 3: /* both negative */
395 return bigint_cmp_u(b, a);
397 default: /* how can this happen?*/
400 return 0; /* just to satisfy the compiler */
403 /******************************************************************************/
405 void bigint_shiftleft(bigint_t* a, uint16_t shift){
406 uint16_t byteshift, word_alloc, words_to_shift;
410 bigint_wordplus_t t=0;
416 for(i=0;i<=byteshift/sizeof(bigint_word_t); ++i){
417 a->wordv[a->length_W+i] = 0;
420 memmove(((uint8_t*)a->wordv) + byteshift, a->wordv, a->length_W * sizeof(bigint_word_t));
421 memset(a->wordv, 0, byteshift);
423 p = a->wordv + byteshift / sizeof(bigint_word_t);
424 words_to_shift = a->length_W + (byteshift % sizeof(bigint_word_t)?1:0);
425 word_alloc = a->length_W + (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t) + 1;
426 a->wordv[word_alloc-1]=0;
428 for(i=0; i < words_to_shift; ++i){
429 t |= ((bigint_wordplus_t)p[i])<<bitshift;
430 p[i] = (bigint_word_t)t;
431 t >>= BIGINT_WORD_SIZE;
433 p[i] = (bigint_word_t)t;
435 a->length_W = word_alloc;
439 /******************************************************************************/
441 void bigint_shiftright(bigint_t* a, uint16_t shift){
445 bigint_wordplus_t t=0;
448 if(byteshift >= a->length_W * sizeof(bigint_word_t)){ /* we would shift out more than we have */
452 if(byteshift == a->length_W * sizeof(bigint_word_t) - 1 && bitshift > GET_FBS(a)){
457 memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_W * sizeof(bigint_word_t) - byteshift);
458 memset((uint8_t*)a->wordv + a->length_W * sizeof(bigint_word_t) - byteshift, 0, byteshift);
460 byteshift /= sizeof(bigint_word_t);
461 a->length_W -= (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
462 if(bitshift != 0 && a->length_W){
463 /* shift to the right */
466 t |= ((bigint_wordplus_t)(a->wordv[i])) << (BIGINT_WORD_SIZE - bitshift);
467 a->wordv[i] = (bigint_word_t)(t >> BIGINT_WORD_SIZE);
468 t <<= BIGINT_WORD_SIZE;
474 /******************************************************************************/
476 void bigint_xor(bigint_t* dest, const bigint_t* a){
478 for(i=0; i<a->length_W; ++i){
479 dest->wordv[i] ^= a->wordv[i];
484 /******************************************************************************/
486 void bigint_set_zero(bigint_t* a){
490 /******************************************************************************/
492 /* using the Karatsuba-Algorithm */
493 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
494 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
495 if(a->length_W==0 || b->length_W==0){
496 bigint_set_zero(dest);
499 if(dest==a || dest==b){
501 bigint_word_t d_b[a->length_W+b->length_W];
503 bigint_mul_u(&d, a, b);
504 bigint_copy(dest, &d);
507 if(a->length_W==1 || b->length_W==1){
511 bigint_wordplus_t t=0;
513 bigint_word_t x = a->wordv[0];
514 for(i=0; i < b->length_W; ++i){
515 t += ((bigint_wordplus_t)b->wordv[i])*((bigint_wordplus_t)x);
516 dest->wordv[i] = (bigint_word_t)t;
517 t>>=BIGINT_WORD_SIZE;
519 dest->wordv[i] = (bigint_word_t)t;
520 dest->length_W = i+1;
525 if(a->length_W * sizeof(bigint_word_t) <= 4 && b->length_W * sizeof(bigint_word_t) <= 4){
528 memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
529 memcpy(&q, b->wordv, b->length_W*sizeof(bigint_word_t));
530 r = (uint64_t)p * (uint64_t)q;
531 memcpy(dest->wordv, &r, (dest->length_W = a->length_W + b->length_W)*sizeof(bigint_word_t));
535 bigint_set_zero(dest);
536 /* split a in xh & xl; split b in yh & yl */
537 const uint16_t n = (MAX(a->length_W, b->length_W)+1)/2;
538 bigint_t xl, xh, yl, yh;
542 bigint_set_zero(&xh);
543 xl.length_W = a->length_W;
549 xh.wordv = &(a->wordv[n]);
550 xh.length_W = a->length_W-n;
554 bigint_set_zero(&yh);
555 yl.length_W = b->length_W;
561 yh.wordv = &(b->wordv[n]);
562 yh.length_W = b->length_W-n;
565 /* now we have split up a and b */
566 /* remember we want to do:
567 * x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl
568 * 5 9 2 4 3 7 5 6 1 8 1
570 bigint_word_t tmp_b[2*n+2], m_b[2*(n+1)];
571 bigint_t tmp, tmp2, m;
573 tmp2.wordv = &(tmp_b[n+1]);
576 bigint_mul_u(dest, &xl, &yl); /* 1: dest <= xl*yl */
577 bigint_add_u(&tmp2, &xh, &xl); /* 2: tmp2 <= xh+xl */
578 bigint_add_u(&tmp, &yh, &yl); /* 3: tmp <= yh+yl */
579 bigint_mul_u(&m, &tmp2, &tmp); /* 4: m <= tmp2*tmp */
580 bigint_mul_u(&tmp, &xh, &yh); /* 5: h <= xh*yh */
581 bigint_sub_u(&m, &m, dest); /* 6: m <= m-dest */
582 bigint_sub_u(&m, &m, &tmp); /* 7: m <= m-h */
583 bigint_add_scale_u(dest, &m, n*sizeof(bigint_word_t)); /* 8: dest <= dest+m**n*/
584 bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t)); /* 9: dest <= dest+tmp**(2*n) */
587 /******************************************************************************/
589 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
592 s |= GET_SIGN(b)?1:0;
594 case 0: /* both positive */
595 bigint_mul_u(dest, a,b);
598 case 1: /* a positive, b negative */
599 bigint_mul_u(dest, a,b);
602 case 2: /* a negative, b positive */
603 bigint_mul_u(dest, a,b);
606 case 3: /* both negative */
607 bigint_mul_u(dest, a,b);
610 default: /* how can this happen?*/
615 /******************************************************************************/
618 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
619 void bigint_square(bigint_t* dest, const bigint_t* a){
620 if(a->length_W*sizeof(bigint_word_t)<=4){
622 memcpy(&r, a->wordv, a->length_W*sizeof(bigint_word_t));
624 memcpy(dest->wordv, &r, 2*a->length_W*sizeof(bigint_word_t));
626 dest->length_W=2*a->length_W;
632 bigint_word_t d_b[a->length_W*2];
634 bigint_square(&d, a);
635 bigint_copy(dest, &d);
640 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
641 bigint_word_t buffer[2*n+1];
645 xh.wordv = &(a->wordv[n]);
646 xh.length_W = a->length_W-n;
651 /* (xh * b**n + xl)**2 = xh**2 * b**2n + 2 * xh * xl * b**n + xl**2 */
653 // cli_putstr("\r\nDBG (a): xl: "); bigint_print_hex(&xl);
654 // cli_putstr("\r\nDBG (b): xh: "); bigint_print_hex(&xh);
655 bigint_square(dest, &xl);
656 // cli_putstr("\r\nDBG (1): xl**2: "); bigint_print_hex(dest);
657 bigint_square(&tmp, &xh);
658 // cli_putstr("\r\nDBG (2): xh**2: "); bigint_print_hex(&tmp);
659 bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t));
660 // cli_putstr("\r\nDBG (3): xl**2 + xh**2*n**2: "); bigint_print_hex(dest);
661 bigint_mul_u(&tmp, &xl, &xh);
662 // cli_putstr("\r\nDBG (4): xl*xh: "); bigint_print_hex(&tmp);
663 bigint_shiftleft(&tmp, 1);
664 // cli_putstr("\r\nDBG (5): xl*xh*2: "); bigint_print_hex(&tmp);
665 bigint_add_scale_u(dest, &tmp, n*sizeof(bigint_word_t));
666 // cli_putstr("\r\nDBG (6): x**2: "); bigint_print_hex(dest);
667 // cli_putstr("\r\n");
670 /******************************************************************************/
671 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
673 bigint_word_t tmp_b[b->length_W + 1];
674 const uint16_t word_shift = bitscale / BIGINT_WORD_SIZE;
676 if(a->length_W < b->length_W + word_shift){
678 cli_putstr("\r\nDBG: *bang*\r\n");
684 bigint_copy(&tmp, b);
685 bigint_shiftleft(&tmp, bitscale % BIGINT_WORD_SIZE);
688 x.wordv = &(a->wordv[word_shift]);
689 x.length_W = a->length_W - word_shift;
691 bigint_sub_u(&x, &x, &tmp);
696 /******************************************************************************/
698 void bigint_reduce(bigint_t* a, const bigint_t* r){
699 // bigint_adjust((bigint_t*)r);
700 uint8_t rfbs = GET_FBS(r);
702 cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
704 if(r->length_W==0 || a->length_W==0){
707 if((r->length_W*sizeof(bigint_word_t)<=4) && (a->length_W*sizeof(bigint_word_t)<=4)){
709 memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
710 memcpy(&q, r->wordv, r->length_W*sizeof(bigint_word_t));
712 memcpy(a->wordv, &p, a->length_W*sizeof(bigint_word_t));
714 // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a);
718 while(a->length_W > r->length_W){
719 shift = (a->length_W - r->length_W) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1;
721 if((a->wordv[a->length_W-1] & ((1LL<<GET_FBS(a)) - 1)) > r->wordv[r->length_W-1]){
723 cli_putstr("\r\n ~ [a] = ");
724 cli_hexdump_rev(&a->wordv[a->length_W-1], 4);
725 cli_putstr(" [r] = ");
726 cli_hexdump_rev(&r->wordv[r->length_W-1], 4);
730 // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2);
731 // cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_W, 2);
732 // cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_W, 2);
734 bigint_sub_u_bitscale(a, r, shift);
735 // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a);
737 while((GET_FBS(a) > rfbs) && (a->length_W == r->length_W)){
738 shift = GET_FBS(a)-rfbs-1;
739 // cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2);
740 bigint_sub_u_bitscale(a, r, shift);
741 // cli_putstr("\r\nDBG: (2) = "); bigint_print_hex(a);
743 while(bigint_cmp_u(a,r)>=0){
745 // cli_putstr("\r\nDBG: (3) = "); bigint_print_hex(a);
748 // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
749 // cli_putstr("\r\n");
752 /******************************************************************************/
754 /* calculate dest = a**exp % r */
755 /* using square&multiply */
756 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
757 if(a->length_W==0 || r->length_W==0){
762 bigint_word_t t, base_b[MAX(a->length_W,r->length_W)], res_b[r->length_W*2];
765 // uint16_t *xaddr = &i;
766 // cli_putstr("\r\npre-alloc (");
767 // cli_hexdump_rev(&xaddr, 4);
768 // cli_putstr(") ...");
771 bigint_copy(&base, a);
772 // cli_putstr("\r\npost-copy");
773 bigint_reduce(&base, r);
778 if(exp->length_W == 0){
779 bigint_copy(dest, &res);
783 t=exp->wordv[exp->length_W - 1];
784 for(i=exp->length_W; i > 0; --i){
785 t = exp->wordv[i - 1];
786 for(j=BIGINT_WORD_SIZE; j > 0; --j){
788 if(t & (1<<(BIGINT_WORD_SIZE-1))){
793 bigint_square(&res, &res);
794 bigint_reduce(&res, r);
795 if(t & (1 << (BIGINT_WORD_SIZE - 1))){
796 bigint_mul_u(&res, &res, &base);
797 bigint_reduce(&res, r);
806 bigint_copy(dest, &res);
809 /******************************************************************************/
811 #define cli_putstr(a)
812 #define bigint_print_hex(a)
813 #define cli_hexdump_rev(a,b)
814 #define uart_flush(a)
816 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
817 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
818 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
820 if(x->length_W==0 || y->length_W==0){
823 if(x->length_W==1 && x->wordv[0]==1){
837 if(y->length_W==1 && y->wordv[0]==1){
852 while(x->wordv[i]==0 && y->wordv[i]==0){
855 bigint_word_t g_b[i+2], x_b[x->length_W-i], y_b[y->length_W-i];
856 bigint_word_t u_b[x->length_W-i], v_b[y->length_W-i];
857 bigint_word_t a_b[y->length_W+2], c_b[y->length_W+2];
858 bigint_word_t b_b[x->length_W+2], d_b[x->length_W+2];
863 memset(g_b, 0, i*sizeof(bigint_word_t));
867 x_.info = y_.info = 0;
868 x_.length_W = x->length_W-i;
869 y_.length_W = y->length_W-i;
870 memcpy(x_.wordv, x->wordv+i, x_.length_W*sizeof(bigint_word_t));
871 memcpy(y_.wordv, y->wordv+i, y_.length_W*sizeof(bigint_word_t));
872 for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
879 bigint_shiftleft(&g, i);
880 bigint_shiftright(&x_, i);
881 bigint_shiftright(&y_, i);
891 bigint_copy(&u, &x_);
892 bigint_copy(&v, &y_);
899 bigint_set_zero(&b_);
900 bigint_set_zero(&c_);
902 cli_putstr("\r\nDBG (gcdext) 0");
903 while((u.wordv[0]&1)==0){
904 cli_putstr("\r\nDBG (gcdext) 0.1");
905 bigint_shiftright(&u, 1);
906 if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
907 bigint_add_s(&a_, &a_, &y_);
908 bigint_sub_s(&b_, &b_, &x_);
910 bigint_shiftright(&a_, 1);
911 bigint_shiftright(&b_, 1);
913 while((v.wordv[0]&1)==0){
914 cli_putstr("\r\nDBG (gcdext) 0.2");
915 bigint_shiftright(&v, 1);
916 if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
917 bigint_add_s(&c_, &c_, &y_);
918 bigint_sub_s(&d_, &d_, &x_);
920 bigint_shiftright(&c_, 1);
921 bigint_shiftright(&d_, 1);
924 if(bigint_cmp_u(&u, &v)>=0){
925 bigint_sub_u(&u, &u, &v);
926 bigint_sub_s(&a_, &a_, &c_);
927 bigint_sub_s(&b_, &b_, &d_);
929 bigint_sub_u(&v, &v, &u);
930 bigint_sub_s(&c_, &c_, &a_);
931 bigint_sub_s(&d_, &d_, &b_);
935 bigint_mul_s(gcd, &v, &g);
945 /******************************************************************************/
947 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
948 bigint_gcdext(NULL, dest, NULL, a, m);
949 while(dest->info&BIGINT_NEG_MASK){
950 bigint_add_s(dest, dest, m);
954 /******************************************************************************/
956 void bigint_changeendianess(bigint_t* a){
958 p = (uint8_t*)(a->wordv);
959 q = p + a->length_W * sizeof(bigint_word_t) - 1;
968 /******************************************************************************/