void mp_invert(MINT *x1, MINT *x0, MINT *c) { MINT u2, u3; MINT v2, v3; MINT zero; MINT q, r; MINT t; MINT x0_prime; static MINT *one = NULL; /* * Minimize calls to allocators. Don't use pointers for local * variables, for the one "initialized" multiple precision * variable, do it just once. */ if (one == NULL) one = mp_itom(1); zero.len = q.len = r.len = t.len = 0; x0_prime.len = u2.len = u3.len = 0; _mp_move(x0, &u3); _mp_move(x0, &x0_prime); v2.len = v3.len = 0; _mp_move(one, &v2); _mp_move(x1, &v3); while (mp_mcmp(&v3, &zero) != 0) { /* invariant: x0*u1 + x1*u2 = u3 */ /* invariant: x0*v1 + x2*v2 = v3 */ /* invariant: x(n+1) = x(n-1) % x(n) */ mp_mdiv(&u3, &v3, &q, &r); _mp_move(&v3, &u3); _mp_move(&r, &v3); mp_mult(&q, &v2, &t); mp_msub(&u2, &t, &t); _mp_move(&v2, &u2); _mp_move(&t, &v2); } /* now x0*u1 + x1*u2 == 1, therefore, (u2*x1) % x0 == 1 */ _mp_move(&u2, c); if (mp_mcmp(c, &zero) < 0) { mp_madd(&x0_prime, c, c); } _mp_xfree(&zero); _mp_xfree(&v2); _mp_xfree(&v3); _mp_xfree(&u2); _mp_xfree(&u3); _mp_xfree(&q); _mp_xfree(&r); _mp_xfree(&t); }
static void testmcmp(const MINT *mp1, const MINT *mp2, const char *tname) { if (mp_mcmp(mp1, mp2) == 0) printf("ok %d - %s\n", ++tnr, tname); else printf("not ok - %d %s\n", ++tnr, tname); }
int mcmp(MINT *a, MINT *b) { return (mp_mcmp(a, b)); }