3 * Multiple simultaneous exponentiations
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 #include "mpmont-exp.h"
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @mpmont_mexpr@ --- *
40 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
41 * @mp *d@ = fake destination
42 * @const mp_expfactor *f@ = pointer to array of factors
43 * @size_t n@ = number of factors supplied
45 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
46 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
49 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
51 * except that the %$g_i$% and result are in Montgomery form.
54 static mp
*mexpr(mpmont
*mm
, mp
*d
, mp_expfactor
*f
, size_t n
)
56 mp
*a
= MP_COPY(mm
->r
);
60 for (i
= 0; i
< n
; i
++) {
62 if (f
[i
].exp
->f
& MP_BURN
)
64 if (MP_NEGP(f
[i
].exp
)) {
65 t
= mpmont_reduce(mm
, f
[i
].base
, f
[i
].base
);
66 t
= mp_modinv(t
, t
, mm
->m
);
67 f
[i
].base
= mpmont_mul(mm
, t
, t
, mm
->r2
);
73 for (i
= 0; i
< n
; i
++)
79 mp
*mpmont_mexpr(mpmont
*mm
, mp
*d
, const mp_expfactor
*f
, size_t n
)
81 mp_expfactor
*ff
= xmalloc(n
* sizeof(mp_expfactor
));
84 for (i
= 0; i
< n
; i
++) {
85 ff
[i
].base
= MP_COPY(f
[i
].base
);
88 return (mexpr(mm
, d
, ff
, n
));
91 /* --- @mpmont_mexp@ --- *
93 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
94 * @mp *d@ = fake destination
95 * @const mp_expfactor *f@ = pointer to array of factors
96 * @size_t n@ = number of factors supplied
98 * Returns: Product of bases raised to exponents, all mod @m@.
100 * Use: Convenient interface over @mpmont_mexpr@.
103 mp
*mpmont_mexp(mpmont
*mm
, mp
*d
, const mp_expfactor
*f
, size_t n
)
105 mp_expfactor
*ff
= xmalloc(n
* sizeof(mp_expfactor
));
108 for (i
= 0; i
< n
; i
++) {
109 ff
[i
].base
= mpmont_mul(mm
, MP_NEW
, f
[i
].base
, mm
->r2
);
110 ff
[i
].exp
= f
[i
].exp
;
112 d
= mexpr(mm
, d
, ff
, n
);
113 return (mpmont_reduce(mm
, d
, d
));
116 /*----- Test rig ----------------------------------------------------------*/
120 #include <mLib/testrig.h>
122 static int verify(size_t n
, dstr
*v
)
124 mp
*m
= *(mp
**)v
[0].buf
;
125 mp_expfactor
*f
= xmalloc(n
* sizeof(*f
));
132 for (i
= 0; i
< n
; i
++) {
133 f
[i
].base
= *(mp
**)v
[j
++].buf
;
134 f
[i
].exp
= *(mp
**)v
[j
++].buf
;
137 rr
= *(mp
**)v
[j
].buf
;
138 mpmont_create(&mm
, m
);
139 r
= mpmont_mexp(&mm
, MP_NEW
, f
, n
);
141 fputs("\n*** mexp failed\n", stderr
);
142 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
143 for (i
= 0; i
< n
; i
++) {
144 fprintf(stderr
, "\ng_%lu = ", (unsigned long)i
);
145 mp_writefile(f
[i
].base
, stderr
, 10);
146 fprintf(stderr
, "\ne_%lu = ", (unsigned long)i
);
147 mp_writefile(f
[i
].exp
, stderr
, 10);
149 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
150 fputs("\nR = ", stderr
); mp_writefile(rr
, stderr
, 10);
155 for (i
= 0; i
< n
; i
++) {
163 assert(mparena_count(MPARENA_GLOBAL
) == 0);
167 static int t1(dstr
*v
) { return verify(1, v
); }
168 static int t2(dstr
*v
) { return verify(2, v
); }
169 static int t3(dstr
*v
) { return verify(3, v
); }
170 static int t4(dstr
*v
) { return verify(4, v
); }
171 static int t5(dstr
*v
) { return verify(5, v
); }
173 static test_chunk tests
[] = {
174 { "mexp-1", t1
, { &type_mp
,
177 { "mexp-2", t2
, { &type_mp
,
181 { "mexp-3", t3
, { &type_mp
,
186 { "mexp-4", t4
, { &type_mp
,
192 { "mexp-5", t5
, { &type_mp
,
202 int main(int argc
, char *argv
[])
205 test_run(argc
, argv
, tests
, SRCDIR
"/t/mpmont");
211 /*----- That's all, folks -------------------------------------------------*/