3 * $Id: exp.h,v 1.2 2004/03/21 22:52:06 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.2 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
36 * Revision 1.1.4.1 2004/03/20 00:13:31 mdw
37 * Projective coordinates for prime curves
39 * Revision 1.1 2001/06/16 13:00:59 mdw
40 * New generic exponentation code. Includes sliding-window simultaneous
46 # error "Multiple inclusion of <catacomb/exp.h>"
49 #define CATACOMB_EXP_H
55 /*----- Header files ------------------------------------------------------*/
59 #include <mLib/alloc.h>
65 /*----- Data structures ---------------------------------------------------*/
67 typedef struct exp_simulscan
{
73 typedef struct exp_simul
{
79 /*----- Macros provided ---------------------------------------------------*/
81 /* --- Parameters --- */
83 #ifndef EXP_WINSZ /* Sliding window size */
84 # define EXP_WINSZ 4 /* Predefine if you need to */
87 /* --- These are determined from the window size --- */
89 #define EXP_TABSZ (1 << EXP_WINSZ)
90 #define EXP_THRESH (((MPW_BITS / EXP_WINSZ) << 2) + 1)
92 /* --- Required operations --- *
94 * The macros here are independent of the underlying group elements. You
95 * must provide the necessary group operations and other definitions. The
96 * group operation is assumed to be written multiplicatively.
98 * @EXP_TYPE@ The type of a group element, e.g., @mp *@.
100 * @EXP_COPY(d, x)@ Makes @d@ be a copy of @x@.
102 * @EXP_DROP(x)@ Discards the element @x@, reclaiming any
105 * @EXP_MUL(a, x)@ Multiplies @a@ by @x@ (writing the result
108 * @EXP_FIX(x)@ Makes @x@ be a canonical representation of
109 * its value. All multiplications have the
110 * right argument canonical.
112 * @EXP_SQR(a)@ Multiplies @a@ by itself.
114 * @EXP_SETMUL(d, x, y)@ Sets @d@ to be the product of @x@ and @y@.
115 * The value @d@ has not been initialized.
117 * @EXP_SETSQR(d, x)@ Sets @d@ to be the square of @x@.
119 * Only @EXP_TYPE@, @EXP_MUL@ and @EXP_SQR@ are required for simple
120 * exponentation. Sliding window and simultaneous exponentation require all
125 # error "EXP_TYPE not defined for <catacomb/exp.h>"
128 /* --- @EXP_SIMPLE@ --- *
130 * Arguments: @a@ = the result object, initially a multiplicative identity
131 * @g@ = the object to exponentiate
132 * @x@ = the exponent, as a multiprecision integer
134 * Use: Performs a simple left-to-right exponentiation. At the end
135 * of the code, the answer is left in @a@; @g@ and @x@ are
139 #define EXP_SIMPLE(a, g, x) do { \
143 /* --- Begin scanning --- */ \
146 if (!MP_RSTEP(&sc)) \
147 goto exp_simple_exit; \
148 while (!MP_RBIT(&sc)) \
151 /* --- Do the main body of the work --- */ \
158 if (!MP_RSTEP(&sc)) \
159 goto exp_simple_done; \
164 while (sq--) EXP_SQR(a); \
167 /* --- Do a final round of squaring --- */ \
170 while (sq--) EXP_SQR(a); \
174 /* --- @EXP_WINDOW@ --- *
176 * Arguments: @a@ = the result object, initially a multiplicative identity
177 * @g@ = the object to exponentiate
178 * @x@ = the exponent, as a multiprecision integer
180 * Use: Performs a sliding-window exponentiation. At the end of the
181 * code, the answer is left in @a@; @g@ and @x@ are unchanged.
184 #define EXP_WINDOW(a, g, x) do { \
187 unsigned i, sq = 0; \
190 /* --- Get going --- */ \
193 if (!MP_RSTEP(&sc)) \
194 goto exp_window_exit; \
196 /* --- Do the precomputation --- */ \
201 v = xmalloc(EXP_TABSZ * sizeof(EXP_TYPE)); \
203 for (i = 1; i < EXP_TABSZ; i++) { \
204 EXP_SETMUL(v[i], v[i - 1], g2); \
209 /* --- Skip top-end zero bits --- * \
211 * If the initial step worked, there must be a set bit somewhere, so \
212 * keep stepping until I find it. \
215 while (!MP_RBIT(&sc)) \
218 /* --- Now for the main work --- */ \
224 /* --- The next bit is set, so read a window index --- * \
226 * Reset @i@ to zero and increment @sq@. Then, until either I read \
227 * @WINSZ@ bits or I run out of bits, scan in a bit: if it's clear, \
228 * bump the @z@ counter; if it's set, push a set bit into @i@, \
229 * shift it over by @z@ bits, bump @sq@ by @z + 1@ and clear @z@. \
230 * By the end of this palaver, @i@ is an index to the precomputed \
236 while (l < EXP_WINSZ && MP_RSTEP(&sc)) { \
241 i = ((i << 1) | 1) << z; \
247 /* --- Do the squaring --- * \
249 * Remember that @sq@ carries over from the zero-skipping stuff \
253 while (sq--) EXP_SQR(a); \
255 /* --- Do the multiply --- */ \
259 /* --- Now grind along through the rest of the bits --- */ \
263 if (!MP_RSTEP(&sc)) \
264 goto exp_window_done; \
271 /* --- Do a final round of squaring --- */ \
274 while (sq--) EXP_SQR(a); \
275 for (i = 0; i < EXP_TABSZ; i++) \
281 /* --- @EXP_SIMUL@ --- *
283 * Arguments: @a@ = the result object, initially a multiplicative identity
284 * @f@ = pointer to a vector of base/exp pairs
285 * @n@ = the number of base/exp pairs
287 * Use: Performs a simultaneous sliding-window exponentiation. The
288 * @f@ table is an array of structures containing members @base@
289 * of type @EXP_TYPE@, and @exp@ of type @mp *@.
292 #define EXP_SIMUL(a, f, n) do { \
293 size_t i, j, jj, k; \
294 size_t vn = 1 << (EXP_WINSZ * n), m = (1 << n) - 1; \
295 EXP_TYPE *v = xmalloc(vn * sizeof(EXP_TYPE)); \
299 /* --- Fill in the precomputed table --- */ \
302 for (i = 0; i < n; i++) { \
303 EXP_COPY(v[j], f[n - 1 - i].base); \
309 for (; i < k; i++) { \
310 EXP_SETSQR(v[j], v[jj]); \
314 for (i = 1; i < vn; i <<= 1) { \
315 for (j = 1; j < i; j++) { \
316 EXP_SETMUL(v[j + i], v[j], v[i]); \
321 /* --- Set up the bitscanners --- * \
323 * Got to use custom scanners, to keep them all in sync. \
328 e.s = xmalloc(n * sizeof(*e.s)); \
330 for (i = 0; i < n; i++) { \
331 MP_SHRINK(f[i].exp); \
332 e.s[i].len = MP_LEN(f[i].exp); \
333 e.s[i].v = f[i].exp->v; \
334 if (e.s[i].len > e.o) \
338 /* --- Skip as far as a nonzero column in the exponent matrix --- */ \
342 goto exp_simul_done; \
343 i = exp_simulnext(&e, 0); \
344 } while (!(i & m)); \
346 /* --- Now for the main work --- */ \
352 /* --- Just read a nonzero column, so read a window index --- * \
354 * Clear high bits of @i@ and increment @sq@. Then, until either I \
355 * read @WINSZ@ columns or I run out, scan in a column and append \
356 * it to @i@. If it's zero, bump the @z@ counter; if it's nonzero, \
357 * bump @sq@ by @z + 1@ and clear @z@. By the end of this palaver, \
358 * @i@ is an index to the precomputed value in @v@, followed by \
359 * @n * z@ zero bits. \
363 while (l < EXP_WINSZ && (e.o || e.b)) { \
365 i = exp_simulnext(&e, i); \
374 /* --- Do the squaring --- * \
376 * Remember that @sq@ carries over from the zero-skipping stuff \
380 while (sq--) EXP_SQR(a); \
382 /* --- Do the multiply --- */ \
387 /* --- Now grind along through the rest of the bits --- */ \
392 goto exp_simul_done; \
393 if ((i = exp_simulnext(&e, 0)) != 0) \
399 /* --- Do a final round of squaring --- */ \
402 while (sq--) EXP_SQR(a); \
403 for (i = 1; i < vn; i++) \
408 /*----- Functions provided ------------------------------------------------*/
410 /* --- @exp_simulnext@ --- *
412 * Arguments: @exp_simul *e@ = pointer to state structure
413 * @size_t x@ = a current accumulator
415 * Returns: The next column of bits.
417 * Use: Scans the next column of bits for a simultaneous
421 extern size_t exp_simulnext(exp_simul */
*e*/
, size_t /*x*/);
423 /*----- That's all, folks -------------------------------------------------*/