3 * Generalized exponentiation
5 * (c) 2001 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,
29 # error "Multiple inclusion of <catacomb/exp.h>"
32 #define CATACOMB_EXP_H
38 /*----- Header files ------------------------------------------------------*/
42 #include <mLib/alloc.h>
48 /*----- Data structures ---------------------------------------------------*/
50 typedef struct exp_simulscan {
56 typedef struct exp_simul {
62 /*----- Macros provided ---------------------------------------------------*/
64 /* --- Parameters --- */
66 #ifndef EXP_WINSZ /* Sliding window size */
67 # define EXP_WINSZ 4 /* Predefine if you need to */
70 /* --- These are determined from the window size --- *
72 * Given a %$k$%-bit exponent, I expect to do %$k/2$% multiplies if I use the
73 * simple way. If I use an n-bit sliding window, then I do %$2^n$%
74 * multiplies up front, but I only do %$(2^n - 1)/2^n k/n$% multiplies for
75 * the exponentiation. This is a win when
77 * %$k \ge \frac{n 2^{n+1}}{n - 2}$%
80 #define EXP_TABSZ (1 << EXP_WINSZ)
82 ((EXP_WINSZ * (2 << EXP_WINSZ))/((EXP_WINSZ - 2) * MPW_BITS))
84 /* --- Required operations --- *
86 * The macros here are independent of the underlying group elements. You
87 * must provide the necessary group operations and other definitions. The
88 * group operation is assumed to be written multiplicatively.
90 * @EXP_TYPE@ The type of a group element, e.g., @mp *@.
92 * @EXP_COPY(d, x)@ Makes @d@ be a copy of @x@.
94 * @EXP_DROP(x)@ Discards the element @x@, reclaiming any
97 * @EXP_MUL(a, x)@ Multiplies @a@ by @x@ (writing the result
100 * @EXP_FIX(x)@ Makes @x@ be a canonical representation of
101 * its value. All multiplications have the
102 * right argument canonical.
104 * @EXP_SQR(a)@ Multiplies @a@ by itself.
106 * @EXP_SETMUL(d, x, y)@ Sets @d@ to be the product of @x@ and @y@.
107 * The value @d@ has not been initialized.
109 * @EXP_SETSQR(d, x)@ Sets @d@ to be the square of @x@.
111 * Only @EXP_TYPE@, @EXP_MUL@ and @EXP_SQR@ are required for simple
112 * exponentation. Sliding window and simultaneous exponentation require all
117 # error "EXP_TYPE not defined for <catacomb/exp.h>"
120 /* --- @EXP_SIMPLE@ --- *
122 * Arguments: @a@ = the result object, initially a multiplicative identity
123 * @g@ = the object to exponentiate
124 * @x@ = the exponent, as a multiprecision integer
126 * Use: Performs a simple left-to-right exponentiation. At the end
127 * of the code, the answer is left in @a@; @g@ and @x@ are
131 #define EXP_SIMPLE(a, g, x) do { \
135 /* --- Begin scanning --- */ \
138 if (!MP_RSTEP(&sc)) \
139 goto exp_simple_exit; \
140 while (!MP_RBIT(&sc)) \
143 /* --- Do the main body of the work --- */ \
150 if (!MP_RSTEP(&sc)) \
151 goto exp_simple_done; \
156 while (sq--) EXP_SQR(a); \
159 /* --- Do a final round of squaring --- */ \
162 while (sq--) EXP_SQR(a); \
166 /* --- @EXP_WINDOW@ --- *
168 * Arguments: @a@ = the result object, initially a multiplicative identity
169 * @g@ = the object to exponentiate
170 * @x@ = the exponent, as a multiprecision integer
172 * Use: Performs a sliding-window exponentiation. At the end of the
173 * code, the answer is left in @a@; @g@ and @x@ are unchanged.
176 #define EXP_WINDOW(a, g, x) do { \
179 unsigned i, sq = 0; \
182 /* --- Get going --- */ \
185 if (!MP_RSTEP(&sc)) \
186 goto exp_window_exit; \
188 /* --- Do the precomputation --- */ \
193 v = xmalloc(EXP_TABSZ * sizeof(EXP_TYPE)); \
195 for (i = 1; i < EXP_TABSZ; i++) { \
196 EXP_SETMUL(v[i], v[i - 1], g2); \
201 /* --- Skip top-end zero bits --- * \
203 * If the initial step worked, there must be a set bit somewhere, so \
204 * keep stepping until I find it. \
207 while (!MP_RBIT(&sc)) \
210 /* --- Now for the main work --- */ \
216 /* --- The next bit is set, so read a window index --- * \
218 * Reset @i@ to zero and increment @sq@. Then, until either I read \
219 * @WINSZ@ bits or I run out of bits, scan in a bit: if it's clear, \
220 * bump the @z@ counter; if it's set, push a set bit into @i@, \
221 * shift it over by @z@ bits, bump @sq@ by @z + 1@ and clear @z@. \
222 * By the end of this palaver, @i@ is an index to the precomputed \
228 while (l < EXP_WINSZ && MP_RSTEP(&sc)) { \
233 i = ((i << 1) | 1) << z; \
239 /* --- Do the squaring --- * \
241 * Remember that @sq@ carries over from the zero-skipping stuff \
245 while (sq--) EXP_SQR(a); \
247 /* --- Do the multiply --- */ \
251 /* --- Now grind along through the rest of the bits --- */ \
255 if (!MP_RSTEP(&sc)) \
256 goto exp_window_done; \
263 /* --- Do a final round of squaring --- */ \
266 while (sq--) EXP_SQR(a); \
267 for (i = 0; i < EXP_TABSZ; i++) \
273 /* --- @EXP_SIMUL@ --- *
275 * Arguments: @a@ = the result object, initially a multiplicative identity
276 * @f@ = pointer to a vector of base/exp pairs
277 * @n@ = the number of base/exp pairs
279 * Use: Performs a simultaneous sliding-window exponentiation. The
280 * @f@ table is an array of structures containing members @base@
281 * of type @EXP_TYPE@, and @exp@ of type @mp *@.
284 #define EXP_SIMUL(a, f, n) do { \
285 size_t i, j, jj, k; \
286 size_t vn = 1 << (EXP_WINSZ * n), m = (1 << n) - 1; \
287 EXP_TYPE *v = xmalloc(vn * sizeof(EXP_TYPE)); \
291 /* --- Fill in the precomputed table --- */ \
294 for (i = 0; i < n; i++) { \
295 EXP_COPY(v[j], f[n - 1 - i].base); \
301 for (; i < k; i++) { \
302 EXP_SETSQR(v[j], v[jj]); \
306 for (i = 1; i < vn; i <<= 1) { \
307 for (j = 1; j < i; j++) { \
308 EXP_SETMUL(v[j + i], v[j], v[i]); \
313 /* --- Set up the bitscanners --- * \
315 * Got to use custom scanners, to keep them all in sync. \
320 e.s = xmalloc(n * sizeof(*e.s)); \
322 for (i = 0; i < n; i++) { \
323 MP_SHRINK(f[i].exp); \
324 e.s[i].len = MP_LEN(f[i].exp); \
325 e.s[i].v = f[i].exp->v; \
326 if (e.s[i].len > e.o) \
330 /* --- Skip as far as a nonzero column in the exponent matrix --- */ \
334 goto exp_simul_done; \
335 i = exp_simulnext(&e, 0); \
336 } while (!(i & m)); \
338 /* --- Now for the main work --- */ \
344 /* --- Just read a nonzero column, so read a window index --- * \
346 * Clear high bits of @i@ and increment @sq@. Then, until either I \
347 * read @WINSZ@ columns or I run out, scan in a column and append \
348 * it to @i@. If it's zero, bump the @z@ counter; if it's nonzero, \
349 * bump @sq@ by @z + 1@ and clear @z@. By the end of this palaver, \
350 * @i@ is an index to the precomputed value in @v@, followed by \
351 * @n * z@ zero bits. \
355 while (l < EXP_WINSZ && (e.o || e.b)) { \
357 i = exp_simulnext(&e, i); \
366 /* --- Do the squaring --- * \
368 * Remember that @sq@ carries over from the zero-skipping stuff \
372 while (sq--) EXP_SQR(a); \
374 /* --- Do the multiply --- */ \
379 /* --- Now grind along through the rest of the bits --- */ \
384 goto exp_simul_done; \
385 if ((i = exp_simulnext(&e, 0)) != 0) \
391 /* --- Do a final round of squaring --- */ \
394 while (sq--) EXP_SQR(a); \
395 for (i = 1; i < vn; i++) \
401 /*----- Functions provided ------------------------------------------------*/
403 /* --- @exp_simulnext@ --- *
405 * Arguments: @exp_simul *e@ = pointer to state structure
406 * @size_t x@ = a current accumulator
408 * Returns: The next column of bits.
410 * Use: Scans the next column of bits for a simultaneous
414 extern size_t exp_simulnext(exp_simul */*e*/, size_t /*x*/);
416 /*----- That's all, folks -------------------------------------------------*/