]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bigint/bigint.c
3e2f0eb295241651d679faa9f04f3df723db01d1
[avr-crypto-lib.git] / bigint / bigint.c
1 /* bigint.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19 /**
20  * \file                bigint.c
21  * \author              Daniel Otte
22  * \date                2010-02-22
23  * 
24  * \license         GPLv3 or later
25  * 
26  */
27  
28
29 #define STRING2(x) #x
30 #define STRING(x) STRING2(x)
31 #define STR_LINE STRING(__LINE__)
32
33 #include "bigint.h"
34 #include <string.h>
35 /*
36 #include "cli.h"
37 #include "bigint_io.h"
38 */
39 #ifndef MAX
40  #define MAX(a,b) (((a)>(b))?(a):(b))
41 #endif
42
43 #ifndef MIN
44  #define MIN(a,b) (((a)<(b))?(a):(b))
45 #endif
46
47 #define SET_FBS(a, v) do{(a)->info &=0xF8; (a)->info |= (v);}while(0)
48 #define GET_FBS(a)   ((a)->info&BIGINT_FBS_MASK)
49 #define SET_NEG(a)   (a)->info |= BIGINT_NEG_MASK
50 #define SET_POS(a)   (a)->info &= ~BIGINT_NEG_MASK
51 #define XCHG(a,b)    do{(a)^=(b); (b)^=(a); (a)^=(b);}while(0)
52 #define XCHG_PTR(a,b)    do{ a = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b))); \
53                                  b = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b))); \
54                                  a = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b)));}while(0)
55
56 #define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
57
58 /******************************************************************************/
59 void bigint_adjust(bigint_t* a){
60         while(a->length_B!=0 && a->wordv[a->length_B-1]==0){
61                 a->length_B--;
62         }
63         if(a->length_B==0){
64                 a->info=0;
65                 return;
66         }
67         uint8_t t;
68         uint8_t i = 0x07;
69         t = a->wordv[a->length_B-1];
70         while((t&0x80)==0 && i){
71                 t<<=1;
72                 i--;
73         }
74         SET_FBS(a, i);
75 }
76
77 /******************************************************************************/
78
79 void bigint_copy(bigint_t* dest, const bigint_t* src){
80         dest->length_B = src->length_B;
81         dest->info = src->info;
82         memcpy(dest->wordv, src->wordv, src->length_B);
83 }
84
85 /******************************************************************************/
86
87 /* this should be implemented in assembly */
88 void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
89         uint16_t t=0, i;
90         if(a->length_B < b->length_B){
91                 XCHG_PTR(a,b);
92         }
93         for(i=0; i<b->length_B; ++i){
94                 t = a->wordv[i] + b->wordv[i] + t;
95                 dest->wordv[i] = (uint8_t)t;
96                 t>>=8;
97         }
98         for(; i<a->length_B; ++i){
99                 t = a->wordv[i] + t;
100                 dest->wordv[i] = (uint8_t)t;
101                 t>>=8;
102         }
103         dest->wordv[i++] = t;
104         dest->length_B = i;
105         bigint_adjust(dest);
106 }
107
108 /******************************************************************************/
109
110 /* this should be implemented in assembly */
111 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
112         uint16_t i,j=0;
113         uint16_t t=0;
114         if(scale>dest->length_B)
115                 memset(dest->wordv+dest->length_B, 0, scale-dest->length_B);
116         for(i=scale; i<a->length_B+scale; ++i,++j){
117                 t = a->wordv[j] + t;
118                 if(dest->length_B>i){
119                         t += dest->wordv[i];
120                 }
121                 dest->wordv[i] = (uint8_t)t;
122                 t>>=8;
123         }
124         while(t){
125                 if(dest->length_B>i){
126                         t = dest->wordv[i] + t;
127                 }
128                 dest->wordv[i] = (uint8_t)t;
129                 t>>=8;
130                 ++i;
131         }
132         if(dest->length_B < i){
133                 dest->length_B = i;
134         }
135         bigint_adjust(dest);
136 }
137
138 /******************************************************************************/
139
140 /* this should be implemented in assembly */
141 void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
142         int8_t borrow=0;
143         int8_t  r;
144         int16_t t;
145         uint16_t i, min, max;
146         min = MIN(a->length_B, b->length_B);
147         max = MAX(a->length_B, b->length_B);
148         r = bigint_cmp_u(a,b);
149         if(r==0){
150                 dest->length_B = 0;
151                 dest->wordv[0] = 0;
152                 bigint_adjust(dest);
153                 return;
154         }
155         if(b->length_B==0){
156                 dest->length_B = a->length_B;
157                 memcpy(dest->wordv, a->wordv, a->length_B);
158                 dest->info = a->info;
159                 SET_POS(dest);
160                 return;
161         }
162         if(a->length_B==0){
163                         dest->length_B = b->length_B;
164                         memcpy(dest->wordv, b->wordv, b->length_B);
165                         dest->info = b->info;
166                         SET_NEG(dest);
167                         return;
168         }
169         if(r<0){
170                 bigint_sub_u(dest, b, a);
171                 SET_NEG(dest);
172         }else{
173                 for(i=0; i<min; ++i){
174                         t = a->wordv[i] - b->wordv[i] - borrow;
175                         if(t<0){
176                                 borrow = 1;
177                                 dest->wordv[i]=(uint8_t)t;
178                         }else{
179                                 borrow = 0;
180                                 dest->wordv[i]=(uint8_t)t;
181                         }
182                 }
183                 for(;i<max; ++i){
184                         t = a->wordv[i] - borrow;
185                         if(t<0){
186                                 borrow = 1;
187                                 dest->wordv[i]=(uint8_t)t;
188                         }else{
189                                 borrow = 0;
190                                 dest->wordv[i]=(uint8_t)t;
191                         }
192
193                 }
194                 SET_POS(dest);
195                 dest->length_B = i;
196                 bigint_adjust(dest);
197         }
198 }
199
200 /******************************************************************************/
201
202 int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
203         if(a->length_B > b->length_B){
204                 return 1;
205         }
206         if(a->length_B < b->length_B){
207                 return -1;
208         }
209         if(a->length_B==0){
210                 return 0;
211         }
212         uint16_t i;
213         i = a->length_B-1;
214         do{
215                 if(a->wordv[i]!=b->wordv[i]){
216                         if(a->wordv[i]>b->wordv[i]){
217                                 return 1;
218                         }else{
219                                 return -1;
220                         }
221                 }
222         }while(i--);
223         return 0;
224 }
225
226 /******************************************************************************/
227
228 void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
229         uint8_t s;
230         s  = GET_SIGN(a)?2:0;
231         s |= GET_SIGN(b)?1:0;
232         switch(s){
233                 case 0: /* both positive */
234                         bigint_add_u(dest, a,b);
235                         SET_POS(dest);
236                         break;
237                 case 1: /* a positive, b negative */
238                         bigint_sub_u(dest, a, b);
239                         break;
240                 case 2: /* a negative, b positive */
241                         bigint_sub_u(dest, b, a);
242                         break;
243                 case 3: /* both negative */
244                         bigint_add_u(dest, a, b);
245                         SET_NEG(dest);
246                         break;
247                 default: /* how can this happen?*/
248                         break;
249         }
250 }
251
252 /******************************************************************************/
253
254 void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
255         uint8_t s;
256         s  = GET_SIGN(a)?2:0;
257         s |= GET_SIGN(b)?1:0;
258         switch(s){
259                 case 0: /* both positive */
260                         bigint_sub_u(dest, a,b);
261                         break;
262                 case 1: /* a positive, b negative */
263                         bigint_add_u(dest, a, b);
264                         SET_POS(dest);
265                         break;
266                 case 2: /* a negative, b positive */
267                         bigint_add_u(dest, a, b);
268                         SET_NEG(dest);
269                         break;
270                 case 3: /* both negative */
271                         bigint_sub_u(dest, b, a);
272                         break;
273                 default: /* how can this happen?*/
274                                         break;
275         }
276
277 }
278
279 /******************************************************************************/
280
281 int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
282         uint8_t s;
283         if(a->length_B==0 && b->length_B==0){
284                 return 0;
285         }
286         s  = GET_SIGN(a)?2:0;
287         s |= GET_SIGN(b)?1:0;
288         switch(s){
289                 case 0: /* both positive */
290                         return bigint_cmp_u(a, b);
291                         break;
292                 case 1: /* a positive, b negative */
293                         return 1;
294                         break;
295                 case 2: /* a negative, b positive */
296                         return -1;
297                         break;
298                 case 3: /* both negative */
299                         return bigint_cmp_u(b, a);
300                         break;
301                 default: /* how can this happen?*/
302                                         break;
303         }
304         return 0; /* just to satisfy the compiler */
305 }
306
307 /******************************************************************************/
308
309 void bigint_shiftleft(bigint_t* a, uint16_t shift){
310         uint16_t byteshift;
311         uint16_t i;
312         uint8_t bitshift;
313         uint16_t t=0;
314         byteshift = (shift+3)/8;
315         bitshift = shift&7;
316         memmove(a->wordv+byteshift, a->wordv, a->length_B);
317         memset(a->wordv, 0, byteshift);
318         if(bitshift!=0){
319                 if(bitshift<=4){ /* shift to the left */
320                         for(i=byteshift; i<a->length_B+byteshift; ++i){
321                                 t |= (a->wordv[i])<<bitshift;
322                                 a->wordv[i] = (uint8_t)t;
323                                 t >>= 8;
324                         }
325                         a->wordv[i] = (uint8_t)t;
326                         byteshift++;
327                 }else{ /* shift to the right */
328                         for(i=a->length_B+byteshift-1; i>byteshift-1; --i){
329                                 t |= (a->wordv[i])<<(bitshift);
330                                 a->wordv[i] = (uint8_t)(t>>8);
331                                 t <<= 8;
332                         }
333                         t |= (a->wordv[i])<<(bitshift);
334                         a->wordv[i] = (uint8_t)(t>>8);
335                 }
336         }
337         a->length_B += byteshift;
338         bigint_adjust(a);
339 }
340
341 /******************************************************************************/
342
343 void bigint_shiftright(bigint_t* a, uint16_t shift){
344         uint16_t byteshift;
345         uint16_t i;
346         uint8_t bitshift;
347         uint16_t t=0;
348         byteshift = shift/8;
349         bitshift = shift&7;
350         if(byteshift >= a->length_B){ /* we would shift out more than we have */
351                 bigint_set_zero(a);
352                 return;
353         }
354         if(byteshift == a->length_B-1 && bitshift>GET_FBS(a)){
355                 bigint_set_zero(a);
356                 return;
357         }
358         if(byteshift){
359                 memmove(a->wordv, a->wordv+byteshift, a->length_B-byteshift);
360                 memset(a->wordv+a->length_B-byteshift, 0,  byteshift);
361         }
362         if(bitshift!=0){
363          /* shift to the right */
364                 for(i=a->length_B-byteshift-1; i>0; --i){
365                         t |= (a->wordv[i])<<(8-bitshift);
366                         a->wordv[i] = (uint8_t)(t>>8);
367                         t <<= 8;
368                 }
369                 t |= (a->wordv[0])<<(8-bitshift);
370                 a->wordv[0] = (uint8_t)(t>>8);
371         }
372         a->length_B -= byteshift;
373         bigint_adjust(a);
374 }
375
376 /******************************************************************************/
377
378 void bigint_xor(bigint_t* dest, const bigint_t* a){
379         uint16_t i;
380         for(i=0; i<a->length_B; ++i){
381                 dest->wordv[i] ^= a->wordv[i];
382         }
383         bigint_adjust(dest);
384 }
385
386 /******************************************************************************/
387
388 void bigint_set_zero(bigint_t* a){
389         a->length_B=0;
390 }
391
392 /******************************************************************************/
393
394 /* using the Karatsuba-Algorithm */
395 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
396 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
397         if(a->length_B==0 || b->length_B==0){
398                 bigint_set_zero(dest);
399                 return;
400         }
401         if(dest==a || dest==b){
402                 bigint_t d;
403                 uint8_t d_b[a->length_B+b->length_B];
404                 d.wordv = d_b;
405                 bigint_mul_u(&d, a, b);
406                 bigint_copy(dest, &d);
407                 return;
408         }
409         if(a->length_B==1 || b->length_B==1){
410                 if(a->length_B!=1){
411                         XCHG_PTR(a,b);
412                 }
413                 uint16_t i, t=0;
414                 uint8_t x = a->wordv[0];
415                 for(i=0; i<b->length_B; ++i){
416                         t += b->wordv[i]*x;
417                         dest->wordv[i] = (uint8_t)t;
418                         t>>=8;
419                 }
420                 dest->wordv[i] = (uint8_t)t;
421                 dest->length_B=i+1;
422                 bigint_adjust(dest);
423                 return;
424         }
425         if(a->length_B<=4 && b->length_B<=4){
426                 uint32_t p=0, q=0;
427                 uint64_t r;
428                 memcpy(&p, a->wordv, a->length_B);
429                 memcpy(&q, b->wordv, b->length_B);
430                 r = (uint64_t)p*(uint64_t)q;
431                 memcpy(dest->wordv, &r, a->length_B+b->length_B);
432                 dest->length_B =  a->length_B+b->length_B;
433                 bigint_adjust(dest);
434                 return;
435         }
436         bigint_set_zero(dest);
437         /* split a in xh & xl; split b in yh & yl */
438         uint16_t n;
439         n=(MAX(a->length_B, b->length_B)+1)/2;
440         bigint_t xl, xh, yl, yh;
441         xl.wordv = a->wordv;
442         yl.wordv = b->wordv;
443         if(a->length_B<=n){
444                 xh.info=0;
445                 xh.length_B = 0;
446                 xl.length_B = a->length_B;
447                 xl.info = 0;
448         }else{
449                 xl.length_B=n;
450                 xl.info = 0;
451                 bigint_adjust(&xl);
452                 xh.wordv = a->wordv+n;
453                 xh.length_B = a->length_B-n;
454                 xh.info = 0;
455         }
456         if(b->length_B<=n){
457                 yh.info=0;
458                 yh.length_B = 0;
459                 yl.length_B = b->length_B;
460                 yl.info = b->info;
461         }else{
462                 yl.length_B=n;
463                 yl.info = 0;
464                 bigint_adjust(&yl);
465                 yh.wordv = b->wordv+n;
466                 yh.length_B = b->length_B-n;
467                 yh.info = 0;
468         }
469         /* now we have split up a and b */
470         uint8_t  tmp_b[2*n+2], m_b[2*(n+1)];
471         bigint_t tmp, tmp2, m;
472         tmp.wordv = tmp_b;
473         tmp2.wordv = tmp_b+n+1;
474         m.wordv = m_b;
475
476         bigint_mul_u(dest, &xl, &yl);  /* dest <= xl*yl     */
477         bigint_add_u(&tmp2, &xh, &xl); /* tmp2 <= xh+xl     */
478         bigint_add_u(&tmp, &yh, &yl);  /* tmp  <= yh+yl     */
479         bigint_mul_u(&m, &tmp2, &tmp); /* m    <= tmp2*tmp  */
480         bigint_mul_u(&tmp, &xh, &yh);  /* h    <= xh*yh     */
481         bigint_sub_u(&m, &m, dest);    /* m    <= m-dest    */
482     bigint_sub_u(&m, &m, &tmp);    /* m    <= m-h       */
483         bigint_add_scale_u(dest, &m, n);
484         bigint_add_scale_u(dest, &tmp, 2*n);
485 }
486
487 /******************************************************************************/
488
489 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
490         uint8_t s;
491         s  = GET_SIGN(a)?2:0;
492         s |= GET_SIGN(b)?1:0;
493         switch(s){
494                 case 0: /* both positive */
495                         bigint_mul_u(dest, a,b);
496                         SET_POS(dest);
497                         break;
498                 case 1: /* a positive, b negative */
499                         bigint_mul_u(dest, a,b);
500                         SET_NEG(dest);
501                         break;
502                 case 2: /* a negative, b positive */
503                         bigint_mul_u(dest, a,b);
504                         SET_NEG(dest);
505                         break;
506                 case 3: /* both negative */
507                         bigint_mul_u(dest, a,b);
508                         SET_POS(dest);
509                         break;
510                 default: /* how can this happen?*/
511                         break;
512         }
513 }
514
515 /******************************************************************************/
516
517 /* square */
518 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
519 void bigint_square(bigint_t* dest, const bigint_t* a){
520         if(a->length_B<=4){
521                 uint64_t r=0;
522                 memcpy(&r, a->wordv, a->length_B);
523                 r = r*r;
524                 memcpy(dest->wordv, &r, 2*a->length_B);
525                 SET_POS(dest);
526                 dest->length_B=2*a->length_B;
527                 bigint_adjust(dest);
528                 return;
529         }
530         if(dest==a){
531                 bigint_t d;
532                 uint8_t d_b[a->length_B*2];
533                 d.wordv = d_b;
534                 bigint_square(&d, a);
535                 bigint_copy(dest, &d);
536                 return;
537         }
538         uint16_t n;
539         n=(a->length_B+1)/2;
540         bigint_t xh, xl, tmp; /* x-high, x-low, temp */
541         uint8_t buffer[2*n+1];
542         xl.wordv = a->wordv;
543         xl.length_B = n;
544         xh.wordv = a->wordv+n;
545         xh.length_B = a->length_B-n;
546         tmp.wordv = buffer;
547         bigint_square(dest, &xl);
548         bigint_square(&tmp, &xh);
549         bigint_add_scale_u(dest, &tmp, 2*n);
550         bigint_mul_u(&tmp, &xl, &xh);
551         bigint_shiftleft(&tmp, 1);
552         bigint_add_scale_u(dest, &tmp, n);
553 }
554
555 /******************************************************************************/
556
557 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
558         bigint_t tmp;
559         uint8_t tmp_b[b->length_B+1];
560         uint16_t i,j,byteshift=bitscale/8;
561         uint8_t borrow=0;
562         int16_t t;
563
564         if(a->length_B < b->length_B+byteshift){
565                 bigint_set_zero(a);
566                 return;
567         }
568
569         tmp.wordv = tmp_b;
570         bigint_copy(&tmp, b);
571         bigint_shiftleft(&tmp, bitscale&7);
572
573         for(j=0,i=byteshift; i<tmp.length_B+byteshift; ++i, ++j){
574                 t = a->wordv[i] - tmp.wordv[j] - borrow;
575                 a->wordv[i] = (uint8_t)t;
576                 if(t<0){
577                         borrow = 1;
578                 }else{
579                         borrow = 0;
580                 }
581         }
582         while(borrow){
583                 if(i+1 > a->length_B){
584                         bigint_set_zero(a);
585                         return;
586                 }
587                 a->wordv[i] -= borrow;
588                 if(a->wordv[i]!=0xff){
589                         borrow=0;
590                 }
591                 ++i;
592         }
593         bigint_adjust(a);
594 }
595
596 /******************************************************************************/
597
598 void bigint_reduce(bigint_t* a, const bigint_t* r){
599 //      bigint_adjust(r);
600         uint8_t rfbs = GET_FBS(r);
601
602         if(r->length_B==0 || a->length_B==0){
603                 return;
604         }
605         while(a->length_B > r->length_B){
606                 bigint_sub_u_bitscale(a, r, (a->length_B-r->length_B)*8+GET_FBS(a)-rfbs-1);
607         }
608         while((GET_FBS(a) > rfbs+1) && (a->length_B == r->length_B)){
609                 bigint_sub_u_bitscale(a, r, GET_FBS(a)-rfbs-1);
610         }
611         while(bigint_cmp_u(a,r)>=0){
612                 bigint_sub_u(a,a,r);
613         }
614 }
615
616 /******************************************************************************/
617
618 /* calculate dest = a**exp % r */
619 /* using square&multiply */
620 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
621         if(a->length_B==0 || r->length_B==0){
622                 return;
623         }
624
625         bigint_t res, base;
626         uint8_t base_b[MAX(a->length_B,r->length_B*2)], res_b[r->length_B*2];
627         uint16_t i;
628         uint8_t j, t;
629         res.wordv = res_b;
630         base.wordv = base_b;
631         bigint_copy(&base, a);
632         bigint_reduce(&base, r);
633         res.wordv[0]=1;
634         res.length_B=1;
635         res.info = 0;
636         bigint_adjust(&res);
637         for(i=0; i+1<exp->length_B; ++i){
638                 t=exp->wordv[i];
639                 for(j=0; j<8; ++j){
640                         if(t&1){
641                                 bigint_mul_u(&res, &res, &base);
642                                 bigint_reduce(&res, r);
643                         }
644                         bigint_square(&base, &base);
645                         bigint_reduce(&base, r);
646                         t>>=1;
647                 }
648         }
649         t=exp->wordv[i];
650         while(t){
651                 if(t&1){
652                         bigint_mul_u(&res, &res, &base);
653                         bigint_reduce(&res, r);
654                 }
655                 bigint_square(&base, &base);
656                 bigint_reduce(&base, r);
657                 t>>=1;
658         }
659         SET_POS(&res);
660         bigint_copy(dest, &res);
661 }
662
663 /******************************************************************************/
664 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
665 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
666          bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
667          volatile uint16_t i=0;
668          if(x->length_B==0 || y->length_B==0){
669                  return;
670          }
671          while(x->wordv[i]==0 && y->wordv[i]==0){
672                  ++i;
673          }
674          uint8_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i];
675          uint8_t u_b[x->length_B-i], v_b[y->length_B-i];
676          uint8_t a_b[y->length_B+2], c_b[y->length_B+2];
677          uint8_t b_b[x->length_B+2], d_b[x->length_B+2];
678
679          g.wordv = g_b;
680          x_.wordv = x_b;
681          y_.wordv = y_b;
682          memset(g_b, 0, i);
683          g_b[i]=1;
684          g.length_B = i+1;
685          g.info=0;
686          x_.info = y_.info = 0;
687          x_.length_B = x->length_B-i;
688          y_.length_B = y->length_B-i;
689          memcpy(x_.wordv, x->wordv+i, x_.length_B);
690          memcpy(y_.wordv, y->wordv+i, y_.length_B);
691          for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
692          }
693
694          bigint_adjust(&x_);
695          bigint_adjust(&y_);
696
697          if(i){
698                  bigint_shiftleft(&g, i);
699                  bigint_shiftright(&x_, i);
700                  bigint_shiftright(&y_, i);
701          }
702          u.wordv = u_b;
703          v.wordv = v_b;
704          a_.wordv = a_b;
705          b_.wordv = b_b;
706          c_.wordv = c_b;
707          d_.wordv = d_b;
708
709          bigint_copy(&u, &x_);
710          bigint_copy(&v, &y_);
711          a_.wordv[0] = 1;
712          a_.length_B = 1;
713          a_.info = 0;
714          d_.wordv[0] = 1;
715          d_.length_B = 1;
716          d_.info = 0;
717          bigint_set_zero(&b_);
718          bigint_set_zero(&c_);
719          do{
720                  while((u.wordv[0]&1)==0){
721                          bigint_shiftright(&u, 1);
722                          if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
723                                  bigint_add_s(&a_, &a_, &y_);
724                                  bigint_sub_s(&b_, &b_, &x_);
725                          }
726                          bigint_shiftright(&a_, 1);
727                          bigint_shiftright(&b_, 1);
728                  }
729                  while((v.wordv[0]&1)==0){
730                          bigint_shiftright(&v, 1);
731                          if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
732                                  bigint_add_s(&c_, &c_, &y_);
733                                  bigint_sub_s(&d_, &d_, &x_);
734                          }
735                          bigint_shiftright(&c_, 1);
736                          bigint_shiftright(&d_, 1);
737
738                  }
739                  if(bigint_cmp_u(&u, &v)>=0){
740                         bigint_sub_u(&u, &u, &v);
741                         bigint_sub_s(&a_, &a_, &c_);
742                         bigint_sub_s(&b_, &b_, &d_);
743                  }else{
744                         bigint_sub_u(&v, &v, &u);
745                         bigint_sub_s(&c_, &c_, &a_);
746                         bigint_sub_s(&d_, &d_, &b_);
747                  }
748          }while(u.length_B);
749          if(gcd){
750                  bigint_mul_s(gcd, &v, &g);
751          }
752          if(a){
753                 bigint_copy(a, &c_);
754          }
755          if(b){
756                  bigint_copy(b, &d_);
757          }
758 }
759
760 /******************************************************************************/
761
762 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
763         bigint_gcdext(NULL, dest, NULL, a, m);
764         while(dest->info&BIGINT_NEG_MASK){
765                 bigint_add_s(dest, dest, m);
766         }
767 }
768
769 /******************************************************************************/
770
771 void bigint_changeendianess(bigint_t* a){
772         uint8_t t, *p, *q;
773         p = a->wordv;
774         q = p+a->length_B-1;
775         while(p<q){
776                 t = *p;
777                 *p = *q;
778                 *q = t;
779                 ++p; --q;
780         }
781 }
782
783 /******************************************************************************/
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806