3 * $Id: mpmont-mexp.c,v 1.5 2000/10/08 12:11:22 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.5 2000/10/08 12:11:22 mdw
34 * Use @MP_EQ@ instead of @MP_CMP@.
36 * Revision 1.4 2000/06/17 11:45:09 mdw
37 * Major memory management overhaul. Added arena support. Use the secure
38 * arena for secret integers. Replace and improve the MP management macros
39 * (e.g., replace MP_MODIFY by MP_DEST).
41 * Revision 1.3 1999/12/10 23:18:39 mdw
42 * Change interface for suggested destinations.
44 * Revision 1.2 1999/11/21 11:35:10 mdw
45 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
46 * @mpmont_mul@ for squaring in exponentiation.
48 * Revision 1.1 1999/11/19 13:19:29 mdw
49 * Simultaneous exponentiation support.
53 /*----- Header files ------------------------------------------------------*/
58 /*----- Main code ---------------------------------------------------------*/
60 /* --- @mpmont_mexpr@ --- *
62 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
63 * @mp *d@ = fake destination
64 * @mpmont_factor *f@ = pointer to array of factors
65 * @size_t n@ = number of factors supplied
67 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
68 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
71 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} R \bmod m$%
79 mp
*mpmont_mexpr(mpmont
*mm
, mp
*d
, mpmont_factor
*f
, size_t n
)
82 mp
**v
= xmalloc(vn
* sizeof(mp
*));
86 mp
*a
= MP_COPY(mm
->r
);
89 /* --- Perform the precomputation --- */
95 /* --- Fill in the rest of the array --- *
97 * Zero never gets used.
102 for (i
= 1; i
< vn
; i
++) {
104 /* --- Check for a new bit entering --- *
106 * If a bit gets set that wasn't set before, then all the lower bits
107 * are zeroes and I've got to introduce a new base into the array.
110 if ((i
& mask
) == 0) {
111 v
[i
] = mpmont_mul(mm
, MP_NEW
, f
[j
++].base
, mm
->r2
);
115 /* --- Otherwise I can get away with a single multiplication --- *
117 * In particular, if %$i$% has more than one bit set, then I only need
118 * to calculate %$v_i = v_{\mathit{mask}} v_{i - \mathit{mask}}$%.
119 * Since both are less than %$i$%, they must have already been
124 v
[i
] = mpmont_mul(mm
, MP_NEW
, v
[mask
], v
[i
& ~mask
]);
128 /* --- Set up the bitscanners --- *
130 * I must scan the exponents from left to right, which is a shame. It
131 * means that I can't use the standard @mpscan@ stuff, in particular.
133 * If any of the exponents are considered secret then make the accumulator
134 * automatically set the secret bit.
140 s
= xmalloc(n
* sizeof(scan
));
142 for (i
= 0; i
< n
; i
++) {
143 s
[i
].len
= MP_LEN(f
[i
].exp
);
146 if (f
[i
].exp
->f
& MP_BURN
)
152 /* --- Now do the actual calculation --- */
160 /* --- If no more bits, get some more --- */
169 /* --- Work out the next index --- */
173 for (i
= 0; i
< n
; i
++) {
175 j
|= (((f
[i
].exp
->v
[o
] >> b
) & 1) << i
);
178 /* --- Accumulate the result --- */
181 dd
= mp_sqr(spare
, a
);
182 dd
= mpmont_reduce(mm
, dd
, dd
);
188 dd
= mpmont_mul(mm
, spare
, a
, v
[j
]);
194 /* --- Tidy up afterwards --- */
198 for (i
= 1; i
< vn
; i
++)
212 /* --- @mpmont_mexp@ --- *
214 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
215 * @mp *d@ = fake destination
216 * @mpmont_factor *f@ = pointer to array of factors
217 * @size_t n@ = number of factors supplied
219 * Returns: Product of bases raised to exponents, all mod @m@.
221 * Use: Convenient interface over @mpmont_mexpr@.
224 mp
*mpmont_mexp(mpmont
*mm
, mp
*d
, mpmont_factor
*f
, size_t n
)
226 d
= mpmont_mexpr(mm
, d
, f
, n
);
227 d
= mpmont_reduce(mm
, d
, d
);
231 /*----- Test rig ----------------------------------------------------------*/
235 #include <mLib/testrig.h>
237 static int verify(size_t n
, dstr
*v
)
239 mp
*m
= *(mp
**)v
[0].buf
;
240 mpmont_factor
*f
= xmalloc(n
* sizeof(*f
));
247 for (i
= 0; i
< n
; i
++) {
248 f
[i
].base
= *(mp
**)v
[j
++].buf
;
249 f
[i
].exp
= *(mp
**)v
[j
++].buf
;
252 rr
= *(mp
**)v
[j
].buf
;
253 mpmont_create(&mm
, m
);
254 r
= mpmont_mexp(&mm
, MP_NEW
, f
, n
);
256 fputs("\n*** mexp failed\n", stderr
);
257 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
258 for (i
= 0; i
< n
; i
++) {
259 fprintf(stderr
, "\ng_%u = ", i
);
260 mp_writefile(f
[i
].base
, stderr
, 10);
261 fprintf(stderr
, "\ne_%u = ", i
);
262 mp_writefile(f
[i
].exp
, stderr
, 10);
264 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
265 fputs("\nR = ", stderr
); mp_writefile(rr
, stderr
, 10);
270 for (i
= 0; i
< n
; i
++) {
278 assert(mparena_count(MPARENA_GLOBAL
) == 0);
282 static int t1(dstr
*v
) { return verify(1, v
); }
283 static int t2(dstr
*v
) { return verify(2, v
); }
284 static int t3(dstr
*v
) { return verify(3, v
); }
285 static int t4(dstr
*v
) { return verify(4, v
); }
286 static int t5(dstr
*v
) { return verify(5, v
); }
288 static test_chunk tests
[] = {
289 { "mexp-1", t1
, { &type_mp
,
292 { "mexp-2", t2
, { &type_mp
,
296 { "mexp-3", t3
, { &type_mp
,
301 { "mexp-4", t4
, { &type_mp
,
307 { "mexp-5", t5
, { &type_mp
,
317 int main(int argc
, char *argv
[])
320 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
326 /*----- That's all, folks -------------------------------------------------*/