X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/ab0350a65e5ac7f1f96e0e2df18c504dd8a441ce..67a452f7a9d69723741b064f08d70462bc8f00e7:/mpbarrett-mexp.c diff --git a/mpbarrett-mexp.c b/mpbarrett-mexp.c index e97c064..2cfda96 100644 --- a/mpbarrett-mexp.c +++ b/mpbarrett-mexp.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: mpbarrett-mexp.c,v 1.1 2001/06/16 12:58:34 mdw Exp $ + * $Id$ * * Multiple simultaneous exponentiations * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,26 +15,18 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: mpbarrett-mexp.c,v $ - * Revision 1.1 2001/06/16 12:58:34 mdw - * Added simultaneous exponentiation with Barrett reduction. - * - */ - /*----- Header files ------------------------------------------------------*/ #include "mp.h" @@ -49,7 +41,7 @@ * * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context * @mp *d@ = fake destination - * @mp_expfactor *f@ = pointer to array of factors + * @const mp_expfactor *f@ = pointer to array of factors * @size_t n@ = number of factors supplied * * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the @@ -59,23 +51,31 @@ * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$% */ -mp *mpbarrett_mexp(mpbarrett *mb, mp *d, mp_expfactor *f, size_t n) +mp *mpbarrett_mexp(mpbarrett *mb, mp *d, const mp_expfactor *f, size_t n) { + mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor)); mp *a = MP_ONE; mp *spare; + mp *g = MP_NEW; size_t i; spare = MP_NEW; for (i = 0; i < n; i++) { - if (f[i].exp->f & MP_BURN) { + if (f[i].exp->f & MP_BURN) spare = MP_NEWSEC; - break; - } + if (MP_NEGP(f[i].exp)) + ff[i].base = mp_modinv(MP_NEW, f[i].base, mb->m); + else + ff[i].base = MP_COPY(f[i].base); + ff[i].exp = f[i].exp; } - - EXP_SIMUL(a, f, n); + mp_drop(g); + EXP_SIMUL(a, ff, n); mp_drop(d); mp_drop(spare); + for (i = 0; i < n; i++) + mp_drop(ff[i].base); + xfree(ff); return (a); } @@ -170,7 +170,7 @@ int main(int argc, char *argv[]) sub_init(); test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett"); return (0); -} +} #endif