3 * $Id: mpmont-mexp.c,v 1.7 2002/01/13 13:49:14 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.7 2002/01/13 13:49:14 mdw
34 * Make @const@-correct.
36 * Revision 1.6 2001/06/16 13:00:20 mdw
37 * Use the generic exponentiation functions.
39 * Revision 1.5 2000/10/08 12:11:22 mdw
40 * Use @MP_EQ@ instead of @MP_CMP@.
42 * Revision 1.4 2000/06/17 11:45:09 mdw
43 * Major memory management overhaul. Added arena support. Use the secure
44 * arena for secret integers. Replace and improve the MP management macros
45 * (e.g., replace MP_MODIFY by MP_DEST).
47 * Revision 1.3 1999/12/10 23:18:39 mdw
48 * Change interface for suggested destinations.
50 * Revision 1.2 1999/11/21 11:35:10 mdw
51 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
52 * @mpmont_mul@ for squaring in exponentiation.
54 * Revision 1.1 1999/11/19 13:19:29 mdw
55 * Simultaneous exponentiation support.
59 /*----- Header files ------------------------------------------------------*/
65 #include "mpmont-exp.h"
67 /*----- Main code ---------------------------------------------------------*/
69 /* --- @mpmont_mexpr@ --- *
71 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
72 * @mp *d@ = fake destination
73 * @mp_expfactor *f@ = pointer to array of factors
74 * @size_t n@ = number of factors supplied
76 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
77 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
80 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
82 * except that the %$g_i$% and result are in Montgomery form.
85 mp
*mpmont_mexpr(mpmont
*mm
, mp
*d
, mp_expfactor
*f
, size_t n
)
87 mp
*a
= mp_copy(mm
->r
);
92 for (i
= 0; i
< n
; i
++) {
93 if (f
[i
].exp
->f
& MP_BURN
) {
105 /* --- @mpmont_mexp@ --- *
107 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
108 * @mp *d@ = fake destination
109 * @const mp_expfactor *f@ = pointer to array of factors
110 * @size_t n@ = number of factors supplied
112 * Returns: Product of bases raised to exponents, all mod @m@.
114 * Use: Convenient interface over @mpmont_mexpr@.
117 mp
*mpmont_mexp(mpmont
*mm
, mp
*d
, const mp_expfactor
*f
, size_t n
)
119 mp_expfactor
*v
= xmalloc(sizeof(*v
) * n
);
122 for (i
= 0; i
< n
; i
++) {
123 v
[i
].base
= mpmont_mul(mm
, MP_NEW
, f
[i
].base
, mm
->r2
);
126 d
= mpmont_mexpr(mm
, d
, v
, n
);
127 for (i
= 0; i
< n
; i
++)
130 d
= mpmont_reduce(mm
, d
, d
);
134 /*----- Test rig ----------------------------------------------------------*/
138 #include <mLib/testrig.h>
140 static int verify(size_t n
, dstr
*v
)
142 mp
*m
= *(mp
**)v
[0].buf
;
143 mp_expfactor
*f
= xmalloc(n
* sizeof(*f
));
150 for (i
= 0; i
< n
; i
++) {
151 f
[i
].base
= *(mp
**)v
[j
++].buf
;
152 f
[i
].exp
= *(mp
**)v
[j
++].buf
;
155 rr
= *(mp
**)v
[j
].buf
;
156 mpmont_create(&mm
, m
);
157 r
= mpmont_mexp(&mm
, MP_NEW
, f
, n
);
159 fputs("\n*** mexp failed\n", stderr
);
160 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
161 for (i
= 0; i
< n
; i
++) {
162 fprintf(stderr
, "\ng_%u = ", i
);
163 mp_writefile(f
[i
].base
, stderr
, 10);
164 fprintf(stderr
, "\ne_%u = ", i
);
165 mp_writefile(f
[i
].exp
, stderr
, 10);
167 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
168 fputs("\nR = ", stderr
); mp_writefile(rr
, stderr
, 10);
173 for (i
= 0; i
< n
; i
++) {
181 assert(mparena_count(MPARENA_GLOBAL
) == 0);
185 static int t1(dstr
*v
) { return verify(1, v
); }
186 static int t2(dstr
*v
) { return verify(2, v
); }
187 static int t3(dstr
*v
) { return verify(3, v
); }
188 static int t4(dstr
*v
) { return verify(4, v
); }
189 static int t5(dstr
*v
) { return verify(5, v
); }
191 static test_chunk tests
[] = {
192 { "mexp-1", t1
, { &type_mp
,
195 { "mexp-2", t2
, { &type_mp
,
199 { "mexp-3", t3
, { &type_mp
,
204 { "mexp-4", t4
, { &type_mp
,
210 { "mexp-5", t5
, { &type_mp
,
220 int main(int argc
, char *argv
[])
223 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
229 /*----- That's all, folks -------------------------------------------------*/