3 * $Id: exp.h,v 1.3 2004/03/22 02:19:10 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.3 2004/03/22 02:19:10 mdw
34 * Rationalise the sliding-window threshold. Drop guarantee that right
35 * arguments to EC @add@ are canonical, and fix up projective implementations
38 * Revision 1.2 2004/03/21 22:52:06 mdw
39 * Merge and close elliptic curve branch.
41 * Revision 1.1.4.1 2004/03/20 00:13:31 mdw
42 * Projective coordinates for prime curves
44 * Revision 1.1 2001/06/16 13:00:59 mdw
45 * New generic exponentation code. Includes sliding-window simultaneous
51 # error "Multiple inclusion of <catacomb/exp.h>"
54 #define CATACOMB_EXP_H
60 /*----- Header files ------------------------------------------------------*/
64 #include <mLib/alloc.h>
70 /*----- Data structures ---------------------------------------------------*/
72 typedef struct exp_simulscan
{
78 typedef struct exp_simul
{
84 /*----- Macros provided ---------------------------------------------------*/
86 /* --- Parameters --- */
88 #ifndef EXP_WINSZ /* Sliding window size */
89 # define EXP_WINSZ 4 /* Predefine if you need to */
92 /* --- These are determined from the window size --- *
94 * Given a %$k$%-bit exponent, I expect to do %$k/2$% multiplies if I use the
95 * simple way. If I use an n-bit sliding window, then I do %$2^n$%
96 * multiplies up front, but I only do %$(2^n - 1)/2^n k/n$% multiplies for
97 * the exponentiation. This is a win when
99 * %$k \ge \frac{n 2^{n+1}}{n - 2}$%
102 #define EXP_TABSZ (1 << EXP_WINSZ)
104 ((EXP_WINSZ * (2 << EXP_WINSZ))/((EXP_WINSZ - 2) * MPW_BITS))
106 /* --- Required operations --- *
108 * The macros here are independent of the underlying group elements. You
109 * must provide the necessary group operations and other definitions. The
110 * group operation is assumed to be written multiplicatively.
112 * @EXP_TYPE@ The type of a group element, e.g., @mp *@.
114 * @EXP_COPY(d, x)@ Makes @d@ be a copy of @x@.
116 * @EXP_DROP(x)@ Discards the element @x@, reclaiming any
119 * @EXP_MUL(a, x)@ Multiplies @a@ by @x@ (writing the result
122 * @EXP_FIX(x)@ Makes @x@ be a canonical representation of
123 * its value. All multiplications have the
124 * right argument canonical.
126 * @EXP_SQR(a)@ Multiplies @a@ by itself.
128 * @EXP_SETMUL(d, x, y)@ Sets @d@ to be the product of @x@ and @y@.
129 * The value @d@ has not been initialized.
131 * @EXP_SETSQR(d, x)@ Sets @d@ to be the square of @x@.
133 * Only @EXP_TYPE@, @EXP_MUL@ and @EXP_SQR@ are required for simple
134 * exponentation. Sliding window and simultaneous exponentation require all
139 # error "EXP_TYPE not defined for <catacomb/exp.h>"
142 /* --- @EXP_SIMPLE@ --- *
144 * Arguments: @a@ = the result object, initially a multiplicative identity
145 * @g@ = the object to exponentiate
146 * @x@ = the exponent, as a multiprecision integer
148 * Use: Performs a simple left-to-right exponentiation. At the end
149 * of the code, the answer is left in @a@; @g@ and @x@ are
153 #define EXP_SIMPLE(a, g, x) do { \
157 /* --- Begin scanning --- */ \
160 if (!MP_RSTEP(&sc)) \
161 goto exp_simple_exit; \
162 while (!MP_RBIT(&sc)) \
165 /* --- Do the main body of the work --- */ \
172 if (!MP_RSTEP(&sc)) \
173 goto exp_simple_done; \
178 while (sq--) EXP_SQR(a); \
181 /* --- Do a final round of squaring --- */ \
184 while (sq--) EXP_SQR(a); \
188 /* --- @EXP_WINDOW@ --- *
190 * Arguments: @a@ = the result object, initially a multiplicative identity
191 * @g@ = the object to exponentiate
192 * @x@ = the exponent, as a multiprecision integer
194 * Use: Performs a sliding-window exponentiation. At the end of the
195 * code, the answer is left in @a@; @g@ and @x@ are unchanged.
198 #define EXP_WINDOW(a, g, x) do { \
201 unsigned i, sq = 0; \
204 /* --- Get going --- */ \
207 if (!MP_RSTEP(&sc)) \
208 goto exp_window_exit; \
210 /* --- Do the precomputation --- */ \
215 v = xmalloc(EXP_TABSZ * sizeof(EXP_TYPE)); \
217 for (i = 1; i < EXP_TABSZ; i++) { \
218 EXP_SETMUL(v[i], v[i - 1], g2); \
223 /* --- Skip top-end zero bits --- * \
225 * If the initial step worked, there must be a set bit somewhere, so \
226 * keep stepping until I find it. \
229 while (!MP_RBIT(&sc)) \
232 /* --- Now for the main work --- */ \
238 /* --- The next bit is set, so read a window index --- * \
240 * Reset @i@ to zero and increment @sq@. Then, until either I read \
241 * @WINSZ@ bits or I run out of bits, scan in a bit: if it's clear, \
242 * bump the @z@ counter; if it's set, push a set bit into @i@, \
243 * shift it over by @z@ bits, bump @sq@ by @z + 1@ and clear @z@. \
244 * By the end of this palaver, @i@ is an index to the precomputed \
250 while (l < EXP_WINSZ && MP_RSTEP(&sc)) { \
255 i = ((i << 1) | 1) << z; \
261 /* --- Do the squaring --- * \
263 * Remember that @sq@ carries over from the zero-skipping stuff \
267 while (sq--) EXP_SQR(a); \
269 /* --- Do the multiply --- */ \
273 /* --- Now grind along through the rest of the bits --- */ \
277 if (!MP_RSTEP(&sc)) \
278 goto exp_window_done; \
285 /* --- Do a final round of squaring --- */ \
288 while (sq--) EXP_SQR(a); \
289 for (i = 0; i < EXP_TABSZ; i++) \
295 /* --- @EXP_SIMUL@ --- *
297 * Arguments: @a@ = the result object, initially a multiplicative identity
298 * @f@ = pointer to a vector of base/exp pairs
299 * @n@ = the number of base/exp pairs
301 * Use: Performs a simultaneous sliding-window exponentiation. The
302 * @f@ table is an array of structures containing members @base@
303 * of type @EXP_TYPE@, and @exp@ of type @mp *@.
306 #define EXP_SIMUL(a, f, n) do { \
307 size_t i, j, jj, k; \
308 size_t vn = 1 << (EXP_WINSZ * n), m = (1 << n) - 1; \
309 EXP_TYPE *v = xmalloc(vn * sizeof(EXP_TYPE)); \
313 /* --- Fill in the precomputed table --- */ \
316 for (i = 0; i < n; i++) { \
317 EXP_COPY(v[j], f[n - 1 - i].base); \
323 for (; i < k; i++) { \
324 EXP_SETSQR(v[j], v[jj]); \
328 for (i = 1; i < vn; i <<= 1) { \
329 for (j = 1; j < i; j++) { \
330 EXP_SETMUL(v[j + i], v[j], v[i]); \
335 /* --- Set up the bitscanners --- * \
337 * Got to use custom scanners, to keep them all in sync. \
342 e.s = xmalloc(n * sizeof(*e.s)); \
344 for (i = 0; i < n; i++) { \
345 MP_SHRINK(f[i].exp); \
346 e.s[i].len = MP_LEN(f[i].exp); \
347 e.s[i].v = f[i].exp->v; \
348 if (e.s[i].len > e.o) \
352 /* --- Skip as far as a nonzero column in the exponent matrix --- */ \
356 goto exp_simul_done; \
357 i = exp_simulnext(&e, 0); \
358 } while (!(i & m)); \
360 /* --- Now for the main work --- */ \
366 /* --- Just read a nonzero column, so read a window index --- * \
368 * Clear high bits of @i@ and increment @sq@. Then, until either I \
369 * read @WINSZ@ columns or I run out, scan in a column and append \
370 * it to @i@. If it's zero, bump the @z@ counter; if it's nonzero, \
371 * bump @sq@ by @z + 1@ and clear @z@. By the end of this palaver, \
372 * @i@ is an index to the precomputed value in @v@, followed by \
373 * @n * z@ zero bits. \
377 while (l < EXP_WINSZ && (e.o || e.b)) { \
379 i = exp_simulnext(&e, i); \
388 /* --- Do the squaring --- * \
390 * Remember that @sq@ carries over from the zero-skipping stuff \
394 while (sq--) EXP_SQR(a); \
396 /* --- Do the multiply --- */ \
401 /* --- Now grind along through the rest of the bits --- */ \
406 goto exp_simul_done; \
407 if ((i = exp_simulnext(&e, 0)) != 0) \
413 /* --- Do a final round of squaring --- */ \
416 while (sq--) EXP_SQR(a); \
417 for (i = 1; i < vn; i++) \
422 /*----- Functions provided ------------------------------------------------*/
424 /* --- @exp_simulnext@ --- *
426 * Arguments: @exp_simul *e@ = pointer to state structure
427 * @size_t x@ = a current accumulator
429 * Returns: The next column of bits.
431 * Use: Scans the next column of bits for a simultaneous
435 extern size_t exp_simulnext(exp_simul */
*e*/
, size_t /*x*/);
437 /*----- That's all, folks -------------------------------------------------*/