3 * $Id: mpmont-mexp.c,v 1.3 1999/12/10 23:18:39 mdw Exp $
5 * Multiplle 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.3 1999/12/10 23:18:39 mdw
34 * Change interface for suggested destinations.
36 * Revision 1.2 1999/11/21 11:35:10 mdw
37 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
38 * @mpmont_mul@ for squaring in exponentiation.
40 * Revision 1.1 1999/11/19 13:19:29 mdw
41 * Simultaneous exponentiation support.
45 /*----- Header files ------------------------------------------------------*/
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @mpmont_mexpr@ --- *
54 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
55 * @mp *d@ = fake destination
56 * @mpmont_factor *f@ = pointer to array of factors
57 * @size_t n@ = number of factors supplied
59 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
60 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
63 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} R \bmod m$%
71 mp
*mpmont_mexpr(mpmont
*mm
, mp
*d
, mpmont_factor
*f
, size_t n
)
74 mp
**v
= xmalloc(vn
* sizeof(mp
*));
78 mp
*a
= MP_COPY(mm
->r
);
81 /* --- Perform the precomputation --- */
87 /* --- Fill in the rest of the array --- *
89 * Zero never gets used.
94 for (i
= 1; i
< vn
; i
++) {
96 /* --- Check for a new bit entering --- *
98 * If a bit gets set that wasn't set before, then all the lower bits
99 * are zeroes and I've got to introduce a new base into the array.
102 if ((i
& mask
) == 0) {
103 v
[i
] = mpmont_mul(mm
, MP_NEW
, f
[j
++].base
, mm
->r2
);
107 /* --- Otherwise I can get away with a single multiplication --- *
109 * In particular, if %$i$% has more than one bit set, then I only need
110 * to calculate %$v_i = v_{\mathit{mask}} v_{i - \mathit{mask}}$%.
111 * Since both are less than %$i$%, they must have already been
116 v
[i
] = mpmont_mul(mm
, MP_NEW
, v
[mask
], v
[i
& ~mask
]);
120 /* --- Set up the bitscanners --- *
122 * I must scan the exponents from left to right, which is a shame. It
123 * means that I can't use the standard @mpscan@ stuff, in particular.
129 s
= xmalloc(n
* sizeof(scan
));
131 for (i
= 0; i
< n
; i
++) {
132 s
[i
].len
= MP_LEN(f
[i
].exp
);
139 /* --- Now do the actual calculation --- */
147 /* --- If no more bits, get some more --- */
156 /* --- Work out the next index --- */
160 for (i
= 0; i
< n
; i
++) {
162 j
|= (((f
[i
].exp
->v
[o
] >> b
) & 1) << i
);
165 /* --- Accumulate the result --- */
168 dd
= mp_sqr(spare
, a
);
169 dd
= mpmont_reduce(mm
, dd
, dd
);
175 dd
= mpmont_mul(mm
, spare
, a
, v
[j
]);
181 /* --- Tidy up afterwards --- */
185 for (i
= 1; i
< vn
; i
++)
199 /* --- @mpmont_mexp@ --- *
201 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
202 * @mp *d@ = fake destination
203 * @mpmont_factor *f@ = pointer to array of factors
204 * @size_t n@ = number of factors supplied
206 * Returns: Product of bases raised to exponents, all mod @m@.
208 * Use: Convenient interface over @mpmont_mexpr@.
211 mp
*mpmont_mexp(mpmont
*mm
, mp
*d
, mpmont_factor
*f
, size_t n
)
213 d
= mpmont_mexpr(mm
, d
, f
, n
);
214 d
= mpmont_reduce(mm
, d
, d
);
218 /*----- Test rig ----------------------------------------------------------*/
222 #include <mLib/testrig.h>
224 static int verify(size_t n
, dstr
*v
)
226 mp
*m
= *(mp
**)v
[0].buf
;
227 mpmont_factor
*f
= xmalloc(n
* sizeof(*f
));
234 for (i
= 0; i
< n
; i
++) {
235 f
[i
].base
= *(mp
**)v
[j
++].buf
;
236 f
[i
].exp
= *(mp
**)v
[j
++].buf
;
239 rr
= *(mp
**)v
[j
].buf
;
240 mpmont_create(&mm
, m
);
241 r
= mpmont_mexp(&mm
, MP_NEW
, f
, n
);
242 if (MP_CMP(r
, !=, rr
)) {
243 fputs("\n*** mexp failed\n", stderr
);
244 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
245 for (i
= 0; i
< n
; i
++) {
246 fprintf(stderr
, "\ng_%u = ", i
);
247 mp_writefile(f
[i
].base
, stderr
, 10);
248 fprintf(stderr
, "\ne_%u = ", i
);
249 mp_writefile(f
[i
].exp
, stderr
, 10);
251 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
252 fputs("\nR = ", stderr
); mp_writefile(rr
, stderr
, 10);
257 for (i
= 0; i
< n
; i
++) {
265 assert(mparena_count(MPARENA_GLOBAL
) == 0);
269 static int t1(dstr
*v
) { return verify(1, v
); }
270 static int t2(dstr
*v
) { return verify(2, v
); }
271 static int t3(dstr
*v
) { return verify(3, v
); }
272 static int t4(dstr
*v
) { return verify(4, v
); }
273 static int t5(dstr
*v
) { return verify(5, v
); }
275 static test_chunk tests
[] = {
276 { "mexp-1", t1
, { &type_mp
,
279 { "mexp-2", t2
, { &type_mp
,
283 { "mexp-3", t3
, { &type_mp
,
288 { "mexp-4", t4
, { &type_mp
,
294 { "mexp-5", t5
, { &type_mp
,
304 int main(int argc
, char *argv
[])
307 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
313 /*----- That's all, folks -------------------------------------------------*/