3 * $Id: mpmont-mexp.c,v 1.6 2001/06/16 13:00:20 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: mpmont-mexp.c,v $
33 * Revision 1.6 2001/06/16 13:00:20 mdw
34 * Use the generic exponentiation functions.
36 * Revision 1.5 2000/10/08 12:11:22 mdw
37 * Use @MP_EQ@ instead of @MP_CMP@.
39 * Revision 1.4 2000/06/17 11:45:09 mdw
40 * Major memory management overhaul. Added arena support. Use the secure
41 * arena for secret integers. Replace and improve the MP management macros
42 * (e.g., replace MP_MODIFY by MP_DEST).
44 * Revision 1.3 1999/12/10 23:18:39 mdw
45 * Change interface for suggested destinations.
47 * Revision 1.2 1999/11/21 11:35:10 mdw
48 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
49 * @mpmont_mul@ for squaring in exponentiation.
51 * Revision 1.1 1999/11/19 13:19:29 mdw
52 * Simultaneous exponentiation support.
56 /*----- Header files ------------------------------------------------------*/
62 #include "mpmont-exp.h"
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @mpmont_mexpr@ --- *
68 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
69 * @mp *d@ = fake destination
70 * @mp_expfactor *f@ = pointer to array of factors
71 * @size_t n@ = number of factors supplied
73 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
74 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
77 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
79 * except that the %$g_i$% and result are in Montgomery form.
82 mp
*mpmont_mexpr(mpmont
*mm
, mp
*d
, mp_expfactor
*f
, size_t n
)
84 mp
*a
= mp_copy(mm
->r
);
89 for (i
= 0; i
< n
; i
++) {
90 if (f
[i
].exp
->f
& MP_BURN
) {
102 /* --- @mpmont_mexp@ --- *
104 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
105 * @mp *d@ = fake destination
106 * @mp_expfactor *f@ = pointer to array of factors
107 * @size_t n@ = number of factors supplied
109 * Returns: Product of bases raised to exponents, all mod @m@.
111 * Use: Convenient interface over @mpmont_mexpr@.
114 mp
*mpmont_mexp(mpmont
*mm
, mp
*d
, mp_expfactor
*f
, size_t n
)
116 mp_expfactor
*v
= xmalloc(sizeof(*v
) * n
);
119 for (i
= 0; i
< n
; i
++) {
120 v
[i
].base
= mpmont_mul(mm
, MP_NEW
, f
[i
].base
, mm
->r2
);
123 d
= mpmont_mexpr(mm
, d
, v
, n
);
124 for (i
= 0; i
< n
; i
++)
127 d
= mpmont_reduce(mm
, d
, d
);
131 /*----- Test rig ----------------------------------------------------------*/
135 #include <mLib/testrig.h>
137 static int verify(size_t n
, dstr
*v
)
139 mp
*m
= *(mp
**)v
[0].buf
;
140 mp_expfactor
*f
= xmalloc(n
* sizeof(*f
));
147 for (i
= 0; i
< n
; i
++) {
148 f
[i
].base
= *(mp
**)v
[j
++].buf
;
149 f
[i
].exp
= *(mp
**)v
[j
++].buf
;
152 rr
= *(mp
**)v
[j
].buf
;
153 mpmont_create(&mm
, m
);
154 r
= mpmont_mexp(&mm
, MP_NEW
, f
, n
);
156 fputs("\n*** mexp failed\n", stderr
);
157 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
158 for (i
= 0; i
< n
; i
++) {
159 fprintf(stderr
, "\ng_%u = ", i
);
160 mp_writefile(f
[i
].base
, stderr
, 10);
161 fprintf(stderr
, "\ne_%u = ", i
);
162 mp_writefile(f
[i
].exp
, stderr
, 10);
164 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
165 fputs("\nR = ", stderr
); mp_writefile(rr
, stderr
, 10);
170 for (i
= 0; i
< n
; i
++) {
178 assert(mparena_count(MPARENA_GLOBAL
) == 0);
182 static int t1(dstr
*v
) { return verify(1, v
); }
183 static int t2(dstr
*v
) { return verify(2, v
); }
184 static int t3(dstr
*v
) { return verify(3, v
); }
185 static int t4(dstr
*v
) { return verify(4, v
); }
186 static int t5(dstr
*v
) { return verify(5, v
); }
188 static test_chunk tests
[] = {
189 { "mexp-1", t1
, { &type_mp
,
192 { "mexp-2", t2
, { &type_mp
,
196 { "mexp-3", t3
, { &type_mp
,
201 { "mexp-4", t4
, { &type_mp
,
207 { "mexp-5", t5
, { &type_mp
,
217 int main(int argc
, char *argv
[])
220 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
226 /*----- That's all, folks -------------------------------------------------*/