4cc02d06 |
1 | /* -*-c-*- |
2 | * |
22bab86c |
3 | * $Id: mpmont-mexp.c,v 1.5 2000/10/08 12:11:22 mdw Exp $ |
4cc02d06 |
4 | * |
d34decd2 |
5 | * Multiple simultaneous exponentiations |
4cc02d06 |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: mpmont-mexp.c,v $ |
22bab86c |
33 | * Revision 1.5 2000/10/08 12:11:22 mdw |
34 | * Use @MP_EQ@ instead of @MP_CMP@. |
35 | * |
d34decd2 |
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). |
40 | * |
ef5f4810 |
41 | * Revision 1.3 1999/12/10 23:18:39 mdw |
42 | * Change interface for suggested destinations. |
43 | * |
79a34029 |
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. |
47 | * |
4cc02d06 |
48 | * Revision 1.1 1999/11/19 13:19:29 mdw |
49 | * Simultaneous exponentiation support. |
50 | * |
51 | */ |
52 | |
53 | /*----- Header files ------------------------------------------------------*/ |
54 | |
55 | #include "mp.h" |
56 | #include "mpmont.h" |
57 | |
58 | /*----- Main code ---------------------------------------------------------*/ |
59 | |
60 | /* --- @mpmont_mexpr@ --- * |
61 | * |
62 | * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context |
ef5f4810 |
63 | * @mp *d@ = fake destination |
4cc02d06 |
64 | * @mpmont_factor *f@ = pointer to array of factors |
65 | * @size_t n@ = number of factors supplied |
66 | * |
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 |
69 | * is: |
70 | * |
71 | * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} R \bmod m$% |
72 | */ |
73 | |
74 | typedef struct scan { |
75 | size_t len; |
76 | mpw w; |
77 | } scan; |
78 | |
ef5f4810 |
79 | mp *mpmont_mexpr(mpmont *mm, mp *d, mpmont_factor *f, size_t n) |
4cc02d06 |
80 | { |
81 | size_t vn = 1 << n; |
82 | mp **v = xmalloc(vn * sizeof(mp *)); |
83 | scan *s; |
84 | size_t o; |
85 | unsigned b; |
86 | mp *a = MP_COPY(mm->r); |
87 | mp *spare = MP_NEW; |
88 | |
89 | /* --- Perform the precomputation --- */ |
90 | |
91 | { |
92 | size_t i, j; |
93 | size_t mask; |
94 | |
95 | /* --- Fill in the rest of the array --- * |
96 | * |
97 | * Zero never gets used. |
98 | */ |
99 | |
100 | j = 0; |
101 | mask = 0; |
102 | for (i = 1; i < vn; i++) { |
103 | |
104 | /* --- Check for a new bit entering --- * |
105 | * |
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. |
108 | */ |
109 | |
110 | if ((i & mask) == 0) { |
111 | v[i] = mpmont_mul(mm, MP_NEW, f[j++].base, mm->r2); |
112 | mask = i; |
113 | } |
114 | |
115 | /* --- Otherwise I can get away with a single multiplication --- * |
116 | * |
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 |
120 | * computed. |
121 | */ |
122 | |
123 | else |
124 | v[i] = mpmont_mul(mm, MP_NEW, v[mask], v[i & ~mask]); |
125 | } |
126 | } |
127 | |
128 | /* --- Set up the bitscanners --- * |
129 | * |
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. |
d34decd2 |
132 | * |
133 | * If any of the exponents are considered secret then make the accumulator |
134 | * automatically set the secret bit. |
4cc02d06 |
135 | */ |
136 | |
137 | { |
138 | size_t i; |
139 | |
140 | s = xmalloc(n * sizeof(scan)); |
141 | o = 0; |
142 | for (i = 0; i < n; i++) { |
143 | s[i].len = MP_LEN(f[i].exp); |
144 | if (s[i].len > o) |
145 | o = s[i].len; |
d34decd2 |
146 | if (f[i].exp->f & MP_BURN) |
147 | spare = MP_NEWSEC; |
4cc02d06 |
148 | } |
149 | b = 0; |
150 | } |
151 | |
152 | /* --- Now do the actual calculation --- */ |
153 | |
154 | b = 0; |
155 | for (;;) { |
156 | size_t i; |
157 | size_t j; |
158 | mp *dd; |
159 | |
160 | /* --- If no more bits, get some more --- */ |
161 | |
162 | if (!b) { |
163 | if (!o) |
164 | break; |
165 | o--; |
166 | b = MPW_BITS; |
167 | } |
168 | |
169 | /* --- Work out the next index --- */ |
170 | |
171 | j = 0; |
172 | b--; |
173 | for (i = 0; i < n; i++) { |
174 | if (o < s[i].len) |
175 | j |= (((f[i].exp->v[o] >> b) & 1) << i); |
176 | } |
177 | |
178 | /* --- Accumulate the result --- */ |
179 | |
180 | if (spare) { |
79a34029 |
181 | dd = mp_sqr(spare, a); |
182 | dd = mpmont_reduce(mm, dd, dd); |
4cc02d06 |
183 | spare = a; |
184 | a = dd; |
185 | } |
186 | |
187 | if (j) { |
188 | dd = mpmont_mul(mm, spare, a, v[j]); |
189 | spare = a; |
190 | a = dd; |
191 | } |
192 | } |
193 | |
194 | /* --- Tidy up afterwards --- */ |
195 | |
196 | { |
197 | size_t i; |
198 | for (i = 1; i < vn; i++) |
199 | MP_DROP(v[i]); |
200 | if (spare) |
201 | MP_DROP(spare); |
202 | free(v); |
203 | free(s); |
204 | } |
205 | |
ef5f4810 |
206 | if (d != MP_NEW) |
207 | MP_DROP(d); |
208 | |
4cc02d06 |
209 | return (a); |
210 | } |
211 | |
212 | /* --- @mpmont_mexp@ --- * |
213 | * |
214 | * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context |
ef5f4810 |
215 | * @mp *d@ = fake destination |
4cc02d06 |
216 | * @mpmont_factor *f@ = pointer to array of factors |
217 | * @size_t n@ = number of factors supplied |
218 | * |
219 | * Returns: Product of bases raised to exponents, all mod @m@. |
220 | * |
221 | * Use: Convenient interface over @mpmont_mexpr@. |
222 | */ |
223 | |
ef5f4810 |
224 | mp *mpmont_mexp(mpmont *mm, mp *d, mpmont_factor *f, size_t n) |
4cc02d06 |
225 | { |
ef5f4810 |
226 | d = mpmont_mexpr(mm, d, f, n); |
4cc02d06 |
227 | d = mpmont_reduce(mm, d, d); |
228 | return (d); |
229 | } |
230 | |
231 | /*----- Test rig ----------------------------------------------------------*/ |
232 | |
233 | #ifdef TEST_RIG |
234 | |
235 | #include <mLib/testrig.h> |
236 | |
237 | static int verify(size_t n, dstr *v) |
238 | { |
239 | mp *m = *(mp **)v[0].buf; |
240 | mpmont_factor *f = xmalloc(n * sizeof(*f)); |
241 | mp *r, *rr; |
242 | size_t i, j; |
243 | mpmont mm; |
244 | int ok = 1; |
245 | |
246 | j = 1; |
247 | for (i = 0; i < n; i++) { |
248 | f[i].base = *(mp **)v[j++].buf; |
249 | f[i].exp = *(mp **)v[j++].buf; |
250 | } |
251 | |
252 | rr = *(mp **)v[j].buf; |
253 | mpmont_create(&mm, m); |
ef5f4810 |
254 | r = mpmont_mexp(&mm, MP_NEW, f, n); |
22bab86c |
255 | if (!MP_EQ(r, rr)) { |
4cc02d06 |
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); |
263 | } |
264 | fputs("\nr = ", stderr); mp_writefile(r, stderr, 10); |
265 | fputs("\nR = ", stderr); mp_writefile(rr, stderr, 10); |
266 | fputc('\n', stderr); |
267 | ok = 0; |
268 | } |
269 | |
270 | for (i = 0; i < n; i++) { |
271 | MP_DROP(f[i].base); |
272 | MP_DROP(f[i].exp); |
273 | } |
274 | MP_DROP(m); |
275 | MP_DROP(r); |
276 | MP_DROP(rr); |
277 | mpmont_destroy(&mm); |
ef5f4810 |
278 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
4cc02d06 |
279 | return (ok); |
280 | } |
281 | |
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); } |
287 | |
288 | static test_chunk tests[] = { |
289 | { "mexp-1", t1, { &type_mp, |
290 | &type_mp, &type_mp, |
291 | &type_mp, 0 } }, |
292 | { "mexp-2", t2, { &type_mp, |
293 | &type_mp, &type_mp, |
294 | &type_mp, &type_mp, |
295 | &type_mp, 0 } }, |
296 | { "mexp-3", t3, { &type_mp, |
297 | &type_mp, &type_mp, |
298 | &type_mp, &type_mp, |
299 | &type_mp, &type_mp, |
300 | &type_mp, 0 } }, |
301 | { "mexp-4", t4, { &type_mp, |
302 | &type_mp, &type_mp, |
303 | &type_mp, &type_mp, |
304 | &type_mp, &type_mp, |
305 | &type_mp, &type_mp, |
306 | &type_mp, 0 } }, |
307 | { "mexp-5", t5, { &type_mp, |
308 | &type_mp, &type_mp, |
309 | &type_mp, &type_mp, |
310 | &type_mp, &type_mp, |
311 | &type_mp, &type_mp, |
312 | &type_mp, &type_mp, |
313 | &type_mp, 0 } }, |
314 | { 0, 0, { 0 } } |
315 | }; |
316 | |
317 | int main(int argc, char *argv[]) |
318 | { |
319 | sub_init(); |
320 | test_run(argc, argv, tests, SRCDIR "/tests/mpmont"); |
321 | return (0); |
322 | } |
323 | |
324 | #endif |
325 | |
326 | /*----- That's all, folks -------------------------------------------------*/ |