3 * $Id: mpmont-mexp.c,v 1.1 1999/11/19 13:19:29 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.1 1999/11/19 13:19:29 mdw
34 * Simultaneous exponentiation support.
38 /*----- Header files ------------------------------------------------------*/
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @mpmont_mexpr@ --- *
47 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
48 * @mpmont_factor *f@ = pointer to array of factors
49 * @size_t n@ = number of factors supplied
51 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
52 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
55 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} R \bmod m$%
63 mp
*mpmont_mexpr(mpmont
*mm
, mpmont_factor
*f
, size_t n
)
66 mp
**v
= xmalloc(vn
* sizeof(mp
*));
70 mp
*a
= MP_COPY(mm
->r
);
73 /* --- Perform the precomputation --- */
79 /* --- Fill in the rest of the array --- *
81 * Zero never gets used.
86 for (i
= 1; i
< vn
; i
++) {
88 /* --- Check for a new bit entering --- *
90 * If a bit gets set that wasn't set before, then all the lower bits
91 * are zeroes and I've got to introduce a new base into the array.
94 if ((i
& mask
) == 0) {
95 v
[i
] = mpmont_mul(mm
, MP_NEW
, f
[j
++].base
, mm
->r2
);
99 /* --- Otherwise I can get away with a single multiplication --- *
101 * In particular, if %$i$% has more than one bit set, then I only need
102 * to calculate %$v_i = v_{\mathit{mask}} v_{i - \mathit{mask}}$%.
103 * Since both are less than %$i$%, they must have already been
108 v
[i
] = mpmont_mul(mm
, MP_NEW
, v
[mask
], v
[i
& ~mask
]);
112 /* --- Set up the bitscanners --- *
114 * I must scan the exponents from left to right, which is a shame. It
115 * means that I can't use the standard @mpscan@ stuff, in particular.
121 s
= xmalloc(n
* sizeof(scan
));
123 for (i
= 0; i
< n
; i
++) {
124 s
[i
].len
= MP_LEN(f
[i
].exp
);
131 /* --- Now do the actual calculation --- */
139 /* --- If no more bits, get some more --- */
148 /* --- Work out the next index --- */
152 for (i
= 0; i
< n
; i
++) {
154 j
|= (((f
[i
].exp
->v
[o
] >> b
) & 1) << i
);
157 /* --- Accumulate the result --- */
160 dd
= mpmont_mul(mm
, spare
, a
, a
);
166 dd
= mpmont_mul(mm
, spare
, a
, v
[j
]);
172 /* --- Tidy up afterwards --- */
176 for (i
= 1; i
< vn
; i
++)
187 /* --- @mpmont_mexp@ --- *
189 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
190 * @mpmont_factor *f@ = pointer to array of factors
191 * @size_t n@ = number of factors supplied
193 * Returns: Product of bases raised to exponents, all mod @m@.
195 * Use: Convenient interface over @mpmont_mexpr@.
198 mp
*mpmont_mexp(mpmont
*mm
, mpmont_factor
*f
, size_t n
)
200 mp
*d
= mpmont_mexpr(mm
, f
, n
);
201 d
= mpmont_reduce(mm
, d
, d
);
205 /*----- Test rig ----------------------------------------------------------*/
209 #include <mLib/testrig.h>
211 static int verify(size_t n
, dstr
*v
)
213 mp
*m
= *(mp
**)v
[0].buf
;
214 mpmont_factor
*f
= xmalloc(n
* sizeof(*f
));
221 for (i
= 0; i
< n
; i
++) {
222 f
[i
].base
= *(mp
**)v
[j
++].buf
;
223 f
[i
].exp
= *(mp
**)v
[j
++].buf
;
226 rr
= *(mp
**)v
[j
].buf
;
227 mpmont_create(&mm
, m
);
228 r
= mpmont_mexp(&mm
, f
, n
);
229 if (MP_CMP(r
, !=, rr
)) {
230 fputs("\n*** mexp failed\n", stderr
);
231 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
232 for (i
= 0; i
< n
; i
++) {
233 fprintf(stderr
, "\ng_%u = ", i
);
234 mp_writefile(f
[i
].base
, stderr
, 10);
235 fprintf(stderr
, "\ne_%u = ", i
);
236 mp_writefile(f
[i
].exp
, stderr
, 10);
238 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
239 fputs("\nR = ", stderr
); mp_writefile(rr
, stderr
, 10);
244 for (i
= 0; i
< n
; i
++) {
255 static int t1(dstr
*v
) { return verify(1, v
); }
256 static int t2(dstr
*v
) { return verify(2, v
); }
257 static int t3(dstr
*v
) { return verify(3, v
); }
258 static int t4(dstr
*v
) { return verify(4, v
); }
259 static int t5(dstr
*v
) { return verify(5, v
); }
261 static test_chunk tests
[] = {
262 { "mexp-1", t1
, { &type_mp
,
265 { "mexp-2", t2
, { &type_mp
,
269 { "mexp-3", t3
, { &type_mp
,
274 { "mexp-4", t4
, { &type_mp
,
280 { "mexp-5", t5
, { &type_mp
,
290 int main(int argc
, char *argv
[])
293 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
299 /*----- That's all, folks -------------------------------------------------*/