5 * Multiple simultaneous exponentiations
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
33 #include "mpbarrett.h"
36 #include "mpbarrett-exp.h"
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @mpbarrett_mexp@ --- *
42 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
43 * @mp *d@ = fake destination
44 * @const mp_expfactor *f@ = pointer to array of factors
45 * @size_t n@ = number of factors supplied
47 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
48 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
51 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
54 mp *mpbarrett_mexp(mpbarrett *mb, mp *d, const mp_expfactor *f, size_t n)
56 mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor));
63 for (i = 0; i < n; i++) {
64 if (f[i].exp->f & MP_BURN)
66 if (MP_NEGP(f[i].exp))
67 ff[i].base = mp_modinv(MP_NEW, f[i].base, mb->m);
69 ff[i].base = MP_COPY(f[i].base);
76 for (i = 0; i < n; i++)
82 /*----- Test rig ----------------------------------------------------------*/
86 #include <mLib/testrig.h>
88 static int verify(size_t n, dstr *v)
90 mp *m = *(mp **)v[0].buf;
91 mp_expfactor *f = xmalloc(n * sizeof(*f));
98 for (i = 0; i < n; i++) {
99 f[i].base = *(mp **)v[j++].buf;
100 f[i].exp = *(mp **)v[j++].buf;
103 rr = *(mp **)v[j].buf;
104 mpbarrett_create(&mb, m);
105 r = mpbarrett_mexp(&mb, MP_NEW, f, n);
107 fputs("\n*** mexp failed\n", stderr);
108 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
109 for (i = 0; i < n; i++) {
110 fprintf(stderr, "\ng_%u = ", i);
111 mp_writefile(f[i].base, stderr, 10);
112 fprintf(stderr, "\ne_%u = ", i);
113 mp_writefile(f[i].exp, stderr, 10);
115 fputs("\nr = ", stderr); mp_writefile(r, stderr, 10);
116 fputs("\nR = ", stderr); mp_writefile(rr, stderr, 10);
121 for (i = 0; i < n; i++) {
128 mpbarrett_destroy(&mb);
129 assert(mparena_count(MPARENA_GLOBAL) == 0);
133 static int t1(dstr *v) { return verify(1, v); }
134 static int t2(dstr *v) { return verify(2, v); }
135 static int t3(dstr *v) { return verify(3, v); }
136 static int t4(dstr *v) { return verify(4, v); }
137 static int t5(dstr *v) { return verify(5, v); }
139 static test_chunk tests[] = {
140 { "mexp-1", t1, { &type_mp,
143 { "mexp-2", t2, { &type_mp,
147 { "mexp-3", t3, { &type_mp,
152 { "mexp-4", t4, { &type_mp,
158 { "mexp-5", t5, { &type_mp,
168 int main(int argc, char *argv[])
171 test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett");
177 /*----- That's all, folks -------------------------------------------------*/