3 * $Id: exp.h,v 1.1.4.1 2004/03/20 00:13:31 mdw Exp $
5 * Generalized exponentiation
7 * (c) 2001 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 --------------------------------------------------*
33 * Revision 1.1.4.1 2004/03/20 00:13:31 mdw
34 * Projective coordinates for prime curves
36 * Revision 1.1 2001/06/16 13:00:59 mdw
37 * New generic exponentation code. Includes sliding-window simultaneous
43 # error "Multiple inclusion of <catacomb/exp.h>"
46 #define CATACOMB_EXP_H
52 /*----- Header files ------------------------------------------------------*/
56 #include <mLib/alloc.h>
62 /*----- Data structures ---------------------------------------------------*/
64 typedef struct exp_simulscan {
70 typedef struct exp_simul {
76 /*----- Macros provided ---------------------------------------------------*/
78 /* --- Parameters --- */
80 #ifndef EXP_WINSZ /* Sliding window size */
81 # define EXP_WINSZ 4 /* Predefine if you need to */
84 /* --- These are determined from the window size --- */
86 #define EXP_TABSZ (1 << EXP_WINSZ)
87 #define EXP_THRESH (((MPW_BITS / EXP_WINSZ) << 2) + 1)
89 /* --- Required operations --- *
91 * The macros here are independent of the underlying group elements. You
92 * must provide the necessary group operations and other definitions. The
93 * group operation is assumed to be written multiplicatively.
95 * @EXP_TYPE@ The type of a group element, e.g., @mp *@.
97 * @EXP_COPY(d, x)@ Makes @d@ be a copy of @x@.
99 * @EXP_DROP(x)@ Discards the element @x@, reclaiming any
102 * @EXP_MUL(a, x)@ Multiplies @a@ by @x@ (writing the result
105 * @EXP_FIX(x)@ Makes @x@ be a canonical representation of
106 * its value. All multiplications have the
107 * right argument canonical.
109 * @EXP_SQR(a)@ Multiplies @a@ by itself.
111 * @EXP_SETMUL(d, x, y)@ Sets @d@ to be the product of @x@ and @y@.
112 * The value @d@ has not been initialized.
114 * @EXP_SETSQR(d, x)@ Sets @d@ to be the square of @x@.
116 * Only @EXP_TYPE@, @EXP_MUL@ and @EXP_SQR@ are required for simple
117 * exponentation. Sliding window and simultaneous exponentation require all
122 # error "EXP_TYPE not defined for <catacomb/exp.h>"
125 /* --- @EXP_SIMPLE@ --- *
127 * Arguments: @a@ = the result object, initially a multiplicative identity
128 * @g@ = the object to exponentiate
129 * @x@ = the exponent, as a multiprecision integer
131 * Use: Performs a simple left-to-right exponentiation. At the end
132 * of the code, the answer is left in @a@; @g@ and @x@ are
136 #define EXP_SIMPLE(a, g, x) do { \
140 /* --- Begin scanning --- */ \
143 if (!MP_RSTEP(&sc)) \
144 goto exp_simple_exit; \
145 while (!MP_RBIT(&sc)) \
148 /* --- Do the main body of the work --- */ \
155 if (!MP_RSTEP(&sc)) \
156 goto exp_simple_done; \
161 while (sq--) EXP_SQR(a); \
164 /* --- Do a final round of squaring --- */ \
167 while (sq--) EXP_SQR(a); \
171 /* --- @EXP_WINDOW@ --- *
173 * Arguments: @a@ = the result object, initially a multiplicative identity
174 * @g@ = the object to exponentiate
175 * @x@ = the exponent, as a multiprecision integer
177 * Use: Performs a sliding-window exponentiation. At the end of the
178 * code, the answer is left in @a@; @g@ and @x@ are unchanged.
181 #define EXP_WINDOW(a, g, x) do { \
184 unsigned i, sq = 0; \
187 /* --- Get going --- */ \
190 if (!MP_RSTEP(&sc)) \
191 goto exp_window_exit; \
193 /* --- Do the precomputation --- */ \
198 v = xmalloc(EXP_TABSZ * sizeof(EXP_TYPE)); \
200 for (i = 1; i < EXP_TABSZ; i++) { \
201 EXP_SETMUL(v[i], v[i - 1], g2); \
206 /* --- Skip top-end zero bits --- * \
208 * If the initial step worked, there must be a set bit somewhere, so \
209 * keep stepping until I find it. \
212 while (!MP_RBIT(&sc)) \
215 /* --- Now for the main work --- */ \
221 /* --- The next bit is set, so read a window index --- * \
223 * Reset @i@ to zero and increment @sq@. Then, until either I read \
224 * @WINSZ@ bits or I run out of bits, scan in a bit: if it's clear, \
225 * bump the @z@ counter; if it's set, push a set bit into @i@, \
226 * shift it over by @z@ bits, bump @sq@ by @z + 1@ and clear @z@. \
227 * By the end of this palaver, @i@ is an index to the precomputed \
233 while (l < EXP_WINSZ && MP_RSTEP(&sc)) { \
238 i = ((i << 1) | 1) << z; \
244 /* --- Do the squaring --- * \
246 * Remember that @sq@ carries over from the zero-skipping stuff \
250 while (sq--) EXP_SQR(a); \
252 /* --- Do the multiply --- */ \
256 /* --- Now grind along through the rest of the bits --- */ \
260 if (!MP_RSTEP(&sc)) \
261 goto exp_window_done; \
268 /* --- Do a final round of squaring --- */ \
271 while (sq--) EXP_SQR(a); \
272 for (i = 0; i < EXP_TABSZ; i++) \
278 /* --- @EXP_SIMUL@ --- *
280 * Arguments: @a@ = the result object, initially a multiplicative identity
281 * @f@ = pointer to a vector of base/exp pairs
282 * @n@ = the number of base/exp pairs
284 * Use: Performs a simultaneous sliding-window exponentiation. The
285 * @f@ table is an array of structures containing members @base@
286 * of type @EXP_TYPE@, and @exp@ of type @mp *@.
289 #define EXP_SIMUL(a, f, n) do { \
290 size_t i, j, jj, k; \
291 size_t vn = 1 << (EXP_WINSZ * n), m = (1 << n) - 1; \
292 EXP_TYPE *v = xmalloc(vn * sizeof(EXP_TYPE)); \
296 /* --- Fill in the precomputed table --- */ \
299 for (i = 0; i < n; i++) { \
300 EXP_COPY(v[j], f[n - 1 - i].base); \
306 for (; i < k; i++) { \
307 EXP_SETSQR(v[j], v[jj]); \
311 for (i = 1; i < vn; i <<= 1) { \
312 for (j = 1; j < i; j++) { \
313 EXP_SETMUL(v[j + i], v[j], v[i]); \
318 /* --- Set up the bitscanners --- * \
320 * Got to use custom scanners, to keep them all in sync. \
325 e.s = xmalloc(n * sizeof(*e.s)); \
327 for (i = 0; i < n; i++) { \
328 MP_SHRINK(f[i].exp); \
329 e.s[i].len = MP_LEN(f[i].exp); \
330 e.s[i].v = f[i].exp->v; \
331 if (e.s[i].len > e.o) \
335 /* --- Skip as far as a nonzero column in the exponent matrix --- */ \
339 goto exp_simul_done; \
340 i = exp_simulnext(&e, 0); \
341 } while (!(i & m)); \
343 /* --- Now for the main work --- */ \
349 /* --- Just read a nonzero column, so read a window index --- * \
351 * Clear high bits of @i@ and increment @sq@. Then, until either I \
352 * read @WINSZ@ columns or I run out, scan in a column and append \
353 * it to @i@. If it's zero, bump the @z@ counter; if it's nonzero, \
354 * bump @sq@ by @z + 1@ and clear @z@. By the end of this palaver, \
355 * @i@ is an index to the precomputed value in @v@, followed by \
356 * @n * z@ zero bits. \
360 while (l < EXP_WINSZ && (e.o || e.b)) { \
362 i = exp_simulnext(&e, i); \
371 /* --- Do the squaring --- * \
373 * Remember that @sq@ carries over from the zero-skipping stuff \
377 while (sq--) EXP_SQR(a); \
379 /* --- Do the multiply --- */ \
384 /* --- Now grind along through the rest of the bits --- */ \
389 goto exp_simul_done; \
390 if ((i = exp_simulnext(&e, 0)) != 0) \
396 /* --- Do a final round of squaring --- */ \
399 while (sq--) EXP_SQR(a); \
400 for (i = 1; i < vn; i++) \
405 /*----- Functions provided ------------------------------------------------*/
407 /* --- @exp_simulnext@ --- *
409 * Arguments: @exp_simul *e@ = pointer to state structure
410 * @size_t x@ = a current accumulator
412 * Returns: The next column of bits.
414 * Use: Scans the next column of bits for a simultaneous
418 extern size_t exp_simulnext(exp_simul */*e*/, size_t /*x*/);
420 /*----- That's all, folks -------------------------------------------------*/