Unified · Split
| @@ -15,7 +15,6 @@ OFILES=\ · 14 unmodified lines | |||
| 15 | mpcmp.$O\ | 15 | mpcmp.$O\ |
| 16 | mpdigdiv.$O\ | 16 | mpdigdiv.$O\ |
| 17 | mpdiv.$O\ | 17 | mpdiv.$O\ |
| 18 | mpeuclid.$O\ | ||
| 19 | mpexp.$O\ | 18 | mpexp.$O\ |
| 20 | mpextendedgcd.$O\ | 19 | mpextendedgcd.$O\ |
| 21 | mpfactorial.$O\ | 20 | mpfactorial.$O\ |
| @@ -1,88 +0,0 @@ | |||
| 1 | #include "os.h" | ||
| 2 | #include <mp.h> | ||
| 3 | |||
| 4 | // extended euclid | ||
| 5 | // | ||
| 6 | // For a and b it solves, d = gcd(a,b) and finds x and y s.t. | ||
| 7 | // ax + by = d | ||
| 8 | // | ||
| 9 | // Handbook of Applied Cryptography, Menezes et al, 1997, pg 67 | ||
| 10 | |||
| 11 | void | ||
| 12 | mpeuclid(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y) | ||
| 13 | { | ||
| 14 | mpint *tmp, *x0, *x1, *x2, *y0, *y1, *y2, *q, *r; | ||
| 15 | |||
| 16 | assert((a->flags&b->flags) & MPnorm); | ||
| 17 | assert(((a->flags|b->flags|d->flags|x->flags|y->flags) & MPtimesafe) == 0); | ||
| 18 | |||
| 19 | if(a->sign<0 || b->sign<0) | ||
| 20 | sysfatal("mpeuclid: negative arg"); | ||
| 21 | |||
| 22 | if(mpcmp(a, b) < 0){ | ||
| 23 | tmp = a; | ||
| 24 | a = b; | ||
| 25 | b = tmp; | ||
| 26 | tmp = x; | ||
| 27 | x = y; | ||
| 28 | y = tmp; | ||
| 29 | } | ||
| 30 | |||
| 31 | if(b->top == 0){ | ||
| 32 | mpassign(a, d); | ||
| 33 | mpassign(mpone, x); | ||
| 34 | mpassign(mpzero, y); | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | a = mpcopy(a); | ||
| 39 | b = mpcopy(b); | ||
| 40 | x0 = mpnew(0); | ||
| 41 | x1 = mpcopy(mpzero); | ||
| 42 | x2 = mpcopy(mpone); | ||
| 43 | y0 = mpnew(0); | ||
| 44 | y1 = mpcopy(mpone); | ||
| 45 | y2 = mpcopy(mpzero); | ||
| 46 | q = mpnew(0); | ||
| 47 | r = mpnew(0); | ||
| 48 | |||
| 49 | while(b->top != 0 && b->sign > 0){ | ||
| 50 | // q = a/b | ||
| 51 | // r = a mod b | ||
| 52 | mpdiv(a, b, q, r); | ||
| 53 | // x0 = x2 - qx1 | ||
| 54 | mpmul(q, x1, x0); | ||
| 55 | mpsub(x2, x0, x0); | ||
| 56 | // y0 = y2 - qy1 | ||
| 57 | mpmul(q, y1, y0); | ||
| 58 | mpsub(y2, y0, y0); | ||
| 59 | // rotate values | ||
| 60 | tmp = a; | ||
| 61 | a = b; | ||
| 62 | b = r; | ||
| 63 | r = tmp; | ||
| 64 | tmp = x2; | ||
| 65 | x2 = x1; | ||
| 66 | x1 = x0; | ||
| 67 | x0 = tmp; | ||
| 68 | tmp = y2; | ||
| 69 | y2 = y1; | ||
| 70 | y1 = y0; | ||
| 71 | y0 = tmp; | ||
| 72 | } | ||
| 73 | |||
| 74 | mpassign(a, d); | ||
| 75 | mpassign(x2, x); | ||
| 76 | mpassign(y2, y); | ||
| 77 | |||
| 78 | mpfree(x0); | ||
| 79 | mpfree(x1); | ||
| 80 | mpfree(x2); | ||
| 81 | mpfree(y0); | ||
| 82 | mpfree(y1); | ||
| 83 | mpfree(y2); | ||
| 84 | mpfree(q); | ||
| 85 | mpfree(r); | ||
| 86 | mpfree(a); | ||
| 87 | mpfree(b); | ||
| 88 | } | ||