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 /*----- Header files ------------------------------------------------------*/
36 #include "mpmont-exp.h"
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @mpmont_mexpr@ --- *
42 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
43 * @mp *d@ = fake destination
44 * @const mp_expfactor *f@ = pointer to array of factors
45 * @size_t n@ = number of factors supplied
47 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
48 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
51 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
53 * except that the %$g_i$% and result are in Montgomery form.
56 static mp
*mexpr(mpmont
*mm
, mp
*d
, mp_expfactor
*f
, size_t n
)
58 mp
*a
= MP_COPY(mm
->r
);
62 for (i
= 0; i
< n
; i
++) {
64 if (f
[i
].exp
->f
& MP_BURN
)
66 if (MP_NEGP(f
[i
].exp
)) {
67 t
= mpmont_reduce(mm
, f
[i
].base
, f
[i
].base
);
68 t
= mp_modinv(t
, t
, mm
->m
);
69 f
[i
].base
= mpmont_mul(mm
, t
, t
, mm
->r2
);
75 for (i
= 0; i
< n
; i
++)
81 mp
*mpmont_mexpr(mpmont
*mm
, mp
*d
, const mp_expfactor
*f
, size_t n
)
83 mp_expfactor
*ff
= xmalloc(n
* sizeof(mp_expfactor
));
86 for (i
= 0; i
< n
; i
++) {
87 ff
[i
].base
= MP_COPY(f
[i
].base
);
90 return (mexpr(mm
, d
, ff
, n
));
93 /* --- @mpmont_mexp@ --- *
95 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
96 * @mp *d@ = fake destination
97 * @const mp_expfactor *f@ = pointer to array of factors
98 * @size_t n@ = number of factors supplied
100 * Returns: Product of bases raised to exponents, all mod @m@.
102 * Use: Convenient interface over @mpmont_mexpr@.
105 mp
*mpmont_mexp(mpmont
*mm
, mp
*d
, const mp_expfactor
*f
, size_t n
)
107 mp_expfactor
*ff
= xmalloc(n
* sizeof(mp_expfactor
));
110 for (i
= 0; i
< n
; i
++) {
111 ff
[i
].base
= mpmont_mul(mm
, MP_NEW
, f
[i
].base
, mm
->r2
);
112 ff
[i
].exp
= f
[i
].exp
;
114 d
= mexpr(mm
, d
, ff
, n
);
115 return (mpmont_reduce(mm
, d
, d
));
118 /*----- Test rig ----------------------------------------------------------*/
122 #include <mLib/testrig.h>
124 static int verify(size_t n
, dstr
*v
)
126 mp
*m
= *(mp
**)v
[0].buf
;
127 mp_expfactor
*f
= xmalloc(n
* sizeof(*f
));
134 for (i
= 0; i
< n
; i
++) {
135 f
[i
].base
= *(mp
**)v
[j
++].buf
;
136 f
[i
].exp
= *(mp
**)v
[j
++].buf
;
139 rr
= *(mp
**)v
[j
].buf
;
140 mpmont_create(&mm
, m
);
141 r
= mpmont_mexp(&mm
, MP_NEW
, f
, n
);
143 fputs("\n*** mexp failed\n", stderr
);
144 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
145 for (i
= 0; i
< n
; i
++) {
146 fprintf(stderr
, "\ng_%u = ", i
);
147 mp_writefile(f
[i
].base
, stderr
, 10);
148 fprintf(stderr
, "\ne_%u = ", i
);
149 mp_writefile(f
[i
].exp
, stderr
, 10);
151 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
152 fputs("\nR = ", stderr
); mp_writefile(rr
, stderr
, 10);
157 for (i
= 0; i
< n
; i
++) {
165 assert(mparena_count(MPARENA_GLOBAL
) == 0);
169 static int t1(dstr
*v
) { return verify(1, v
); }
170 static int t2(dstr
*v
) { return verify(2, v
); }
171 static int t3(dstr
*v
) { return verify(3, v
); }
172 static int t4(dstr
*v
) { return verify(4, v
); }
173 static int t5(dstr
*v
) { return verify(5, v
); }
175 static test_chunk tests
[] = {
176 { "mexp-1", t1
, { &type_mp
,
179 { "mexp-2", t2
, { &type_mp
,
183 { "mexp-3", t3
, { &type_mp
,
188 { "mexp-4", t4
, { &type_mp
,
194 { "mexp-5", t5
, { &type_mp
,
204 int main(int argc
, char *argv
[])
207 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
213 /*----- That's all, folks -------------------------------------------------*/