From ab0350a65e5ac7f1f96e0e2df18c504dd8a441ce Mon Sep 17 00:00:00 2001 From: mdw Date: Sat, 16 Jun 2001 12:58:47 +0000 Subject: [PATCH] Added simultaneous exponentiation with Barrett reduction. --- mpbarrett-mexp.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ mpbarrett.h | 22 ++++++- 2 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 mpbarrett-mexp.c diff --git a/mpbarrett-mexp.c b/mpbarrett-mexp.c new file mode 100644 index 0000000..e97c064 --- /dev/null +++ b/mpbarrett-mexp.c @@ -0,0 +1,177 @@ +/* -*-c-*- + * + * $Id: mpbarrett-mexp.c,v 1.1 2001/06/16 12:58:34 mdw Exp $ + * + * Multiple simultaneous exponentiations + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * 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" +#include "mpbarrett.h" + +#define EXP_WINSZ 3 +#include "mpbarrett-exp.h" + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @mpbarrett_mexp@ --- * + * + * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context + * @mp *d@ = fake destination + * @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 + * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result + * is: + * + * %$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 *a = MP_ONE; + mp *spare; + size_t i; + + spare = MP_NEW; + for (i = 0; i < n; i++) { + if (f[i].exp->f & MP_BURN) { + spare = MP_NEWSEC; + break; + } + } + + EXP_SIMUL(a, f, n); + mp_drop(d); + mp_drop(spare); + return (a); +} + +/*----- Test rig ----------------------------------------------------------*/ + +#ifdef TEST_RIG + +#include + +static int verify(size_t n, dstr *v) +{ + mp *m = *(mp **)v[0].buf; + mp_expfactor *f = xmalloc(n * sizeof(*f)); + mp *r, *rr; + size_t i, j; + mpbarrett mb; + int ok = 1; + + j = 1; + for (i = 0; i < n; i++) { + f[i].base = *(mp **)v[j++].buf; + f[i].exp = *(mp **)v[j++].buf; + } + + rr = *(mp **)v[j].buf; + mpbarrett_create(&mb, m); + r = mpbarrett_mexp(&mb, MP_NEW, f, n); + if (!MP_EQ(r, rr)) { + fputs("\n*** mexp failed\n", stderr); + fputs("m = ", stderr); mp_writefile(m, stderr, 10); + for (i = 0; i < n; i++) { + fprintf(stderr, "\ng_%u = ", i); + mp_writefile(f[i].base, stderr, 10); + fprintf(stderr, "\ne_%u = ", i); + mp_writefile(f[i].exp, stderr, 10); + } + fputs("\nr = ", stderr); mp_writefile(r, stderr, 10); + fputs("\nR = ", stderr); mp_writefile(rr, stderr, 10); + fputc('\n', stderr); + ok = 0; + } + + for (i = 0; i < n; i++) { + MP_DROP(f[i].base); + MP_DROP(f[i].exp); + } + MP_DROP(m); + MP_DROP(r); + MP_DROP(rr); + mpbarrett_destroy(&mb); + assert(mparena_count(MPARENA_GLOBAL) == 0); + return (ok); +} + +static int t1(dstr *v) { return verify(1, v); } +static int t2(dstr *v) { return verify(2, v); } +static int t3(dstr *v) { return verify(3, v); } +static int t4(dstr *v) { return verify(4, v); } +static int t5(dstr *v) { return verify(5, v); } + +static test_chunk tests[] = { + { "mexp-1", t1, { &type_mp, + &type_mp, &type_mp, + &type_mp, 0 } }, + { "mexp-2", t2, { &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, 0 } }, + { "mexp-3", t3, { &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, 0 } }, + { "mexp-4", t4, { &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, 0 } }, + { "mexp-5", t5, { &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, &type_mp, + &type_mp, 0 } }, + { 0, 0, { 0 } } +}; + +int main(int argc, char *argv[]) +{ + sub_init(); + test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett"); + return (0); +} + +#endif + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/mpbarrett.h b/mpbarrett.h index aa1971e..eaec41d 100644 --- a/mpbarrett.h +++ b/mpbarrett.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpbarrett.h,v 1.2 2000/10/08 12:03:44 mdw Exp $ + * $Id: mpbarrett.h,v 1.3 2001/06/16 12:58:47 mdw Exp $ * * Barrett modular reduction * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpbarrett.h,v $ + * Revision 1.3 2001/06/16 12:58:47 mdw + * Added simultaneous exponentiation with Barrett reduction. + * * Revision 1.2 2000/10/08 12:03:44 mdw * (mpbarrett_reduce): Cope with negative numbers. * @@ -129,6 +132,23 @@ extern mp *mpbarrett_reduce(mpbarrett */*mb*/, mp */*d*/, mp */*m*/); extern mp *mpbarrett_exp(mpbarrett */*mb*/, mp */*d*/, mp */*a*/, mp */*e*/); +/* --- @mpbarrett_mexp@ --- * + * + * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context + * @mp *d@ = fake destination + * @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 + * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result + * is: + * + * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$% + */ + +extern mp *mpbarrett_mexp(mpbarrett */*mb*/, mp */*d*/, + mp_expfactor */*f*/, size_t /*n*/); + /*----- That's all, folks -------------------------------------------------*/ #ifdef __cplusplus -- 2.11.0