3 * $Id: mpbarrett-mexp.c,v 1.1 2001/06/16 12:58:34 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * $Log: mpbarrett-mexp.c,v $
33 * Revision 1.1 2001/06/16 12:58:34 mdw
34 * Added simultaneous exponentiation with Barrett reduction.
38 /*----- Header files ------------------------------------------------------*/
41 #include "mpbarrett.h"
44 #include "mpbarrett-exp.h"
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- @mpbarrett_mexp@ --- *
50 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
51 * @mp *d@ = fake destination
52 * @mp_expfactor *f@ = pointer to array of factors
53 * @size_t n@ = number of factors supplied
55 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
56 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
59 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
62 mp
*mpbarrett_mexp(mpbarrett
*mb
, mp
*d
, mp_expfactor
*f
, size_t n
)
69 for (i
= 0; i
< n
; i
++) {
70 if (f
[i
].exp
->f
& MP_BURN
) {
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 -------------------------------------------------*/