X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/5e1601ef0d8692dd25e337e37a7c98e119e08cf0..80be023065ced106a4078a36371c135a60d2bd6c:/mpcrt.c diff --git a/mpcrt.c b/mpcrt.c index 18c622f..8c5efbd 100644 --- a/mpcrt.c +++ b/mpcrt.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpcrt.c,v 1.4 2001/04/19 18:25:38 mdw Exp $ + * $Id$ * * Chinese Remainder Theorem computations (Gauss's algorithm) * @@ -27,23 +27,6 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: mpcrt.c,v $ - * Revision 1.4 2001/04/19 18:25:38 mdw - * Use mpmul for the multiplication. - * - * Revision 1.3 2000/10/08 12:11:22 mdw - * Use @MP_EQ@ instead of @MP_CMP@. - * - * Revision 1.2 1999/12/10 23:22:32 mdw - * Interface changes for suggested destinations. Use Barrett reduction. - * - * Revision 1.1 1999/11/22 20:50:57 mdw - * Add support for solving Chinese Remainder Theorem problems. - * - */ - /*----- Header files ------------------------------------------------------*/ #include "mp.h" @@ -88,7 +71,6 @@ void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n) else { mpmul mm; mpmul_init(&mm); - n = MP_COPY(v[0].m); for (i = 0; i < k; i++) mpmul_add(&mm, v[i].m); n = mpmul_done(&mm); @@ -111,7 +93,10 @@ void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n) */ if (!v[0].ni && !v[1].ni) { - mp_gcd(0, &v[0].ni, &v[1].ni, v[0].n, v[1].n); + mp *g = MP_NEW; + mp_gcd(&g, &v[0].ni, &v[1].ni, v[0].n, v[1].n); + assert(MP_EQ(g, MP_ONE)); + mp_drop(g); v[0].ni = mp_add(v[0].ni, v[0].ni, v[1].n); } else { int i, j; @@ -139,7 +124,7 @@ void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n) if (!v[i].n) mp_div(&v[i].n, 0, n, v[i].m); if (!v[i].ni) - mp_gcd(0, &v[i].ni, 0, v[i].n, v[i].m); + v[i].ni = mp_modinv(MP_NEW, v[i].n, v[i].m); if (!v[i].nni) v[i].nni = mp_mul(MP_NEW, v[i].n, v[i].ni); } @@ -260,8 +245,8 @@ static int verify(size_t n, dstr *v) mp_drop(a); mp_drop(b); mpcrt_destroy(&c); - free(m); - free(r); + xfree(m); + xfree(r); assert(mparena_count(MPARENA_GLOBAL) == 0); return (ok); }