Use a Karatsuba-based reduction for large moduli.
[u/mdw/catacomb] / mpx.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
a86e33af 3 * $Id: mpx.h,v 1.6 1999/12/10 23:23:51 mdw Exp $
d03ab969 4 *
5 * Low level multiprecision arithmetic
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: mpx.h,v $
a86e33af 33 * Revision 1.6 1999/12/10 23:23:51 mdw
34 * Karatsuba-Ofman multiplication algorithm.
35 *
dd517851 36 * Revision 1.5 1999/11/20 22:23:27 mdw
37 * Add function versions of some low-level macros with wider use.
38 *
7c13f461 39 * Revision 1.4 1999/11/17 18:04:43 mdw
40 * Add two's complement support. Fix a bug in MPX_UMLAN.
41 *
3c9ede17 42 * Revision 1.3 1999/11/13 01:51:29 mdw
43 * Minor interface changes. Should be stable now.
44 *
b9b1c853 45 * Revision 1.2 1999/11/11 17:47:55 mdw
46 * Minor changes for different `mptypes.h' format.
47 *
d03ab969 48 * Revision 1.1 1999/09/03 08:41:12 mdw
49 * Initial import.
50 *
51 */
52
a86e33af 53#ifndef CATACOMB_MPX_H
54#define CATACOMB_MPX_H
d03ab969 55
56#ifdef __cplusplus
57 extern "C" {
58#endif
59
60/*----- The idea ----------------------------------------------------------*
61 *
62 * This file provides functions and macros which work on vectors of words as
63 * unsigned multiprecision integers. The interface works in terms of base
64 * and limit pointers (i.e., a pointer to the start of a vector, and a
65 * pointer just past its end) rather than base pointer and length, because
66 * that requires more arithmetic and state to work on.
67 *
68 * The interfaces are slightly bizarre in other ways. Try to use the
69 * higher-level functions where you can: they're rather better designed to
70 * actually be friendly and useful.
71 */
72
73/*----- Header files ------------------------------------------------------*/
74
75#include <string.h>
76
a86e33af 77#ifndef CATACOMB_MPW_H
3c9ede17 78# include "mpw.h"
d03ab969 79#endif
80
81/*----- General manipulation ----------------------------------------------*/
82
83/* --- @MPX_SHRINK@ --- *
84 *
85 * Arguments: @const mpw *v@ = pointer to vector of words
86 * @const mpw *vl@ = (updated) current limit of vector
87 *
88 * Use: Shrinks down the limit of a multiprecision integer vector.
89 */
90
91#define MPX_SHRINK(v, vl) do { \
3c9ede17 92 const mpw *_vv = (v), *_vvl = (vl); \
93 while (_vvl > _vv && !_vvl[-1]) \
94 _vvl--; \
95 (vl) = (mpw *)_vvl; \
d03ab969 96} while (0)
97
98/* --- @MPX_BITS@ --- *
99 *
100 * Arguments: @unsigned long b@ = result variable
101 * @const mpw *v@ = pointer to array of words
102 * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@)
103 *
104 * Use: Calculates the number of bits in a multiprecision value.
105 */
106
107#define MPX_BITS(b, v, vl) do { \
108 const mpw *_v = (v), *_vl = (vl); \
3c9ede17 109 MPX_SHRINK(_v, _vl); \
d03ab969 110 if (_v == _vl) \
111 (b) = 0; \
112 else { \
113 unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \
114 mpw _w = _vl[-1]; \
115 unsigned _k = MPW_BITS / 2; \
116 while (_k) { \
117 if (_w >> _k) { \
118 _w >>= _k; \
119 _b += _k; \
120 } \
121 _k >>= 1; \
122 } \
123 (b) = _b; \
124 } \
125} while (0)
126
127/* --- @MPX_OCTETS@ --- *
128 *
129 * Arguments: @size_t o@ = result variable
3c9ede17 130 * @const mpw *v, *vl@ = pointer to array of words
d03ab969 131 *
132 * Use: Calculates the number of octets in a multiprecision value.
133 */
134
3c9ede17 135#define MPX_OCTETS(o, v, vl) do { \
d03ab969 136 const mpw *_v = (v), *_vl = (vl); \
3c9ede17 137 MPX_SHRINK(_v, _vl); \
d03ab969 138 if (_v == _vl) \
139 (o) = 0; \
140 else { \
3c9ede17 141 size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
d03ab969 142 mpw _w = _vl[-1]; \
143 unsigned _k = MPW_BITS / 2; \
3c9ede17 144 while (_k >= 8) { \
d03ab969 145 if (_w >> _k) { \
146 _w >>= _k; \
3c9ede17 147 _o += _k >> 3; \
d03ab969 148 } \
149 _k >>= 1; \
150 } \
151 if (_w) \
152 _o++; \
153 (o) = _o; \
154 } \
155} while (0)
156
157/* --- @MPX_COPY@ --- *
158 *
159 * Arguments: @dv, dvl@ = destination vector base and limit
160 * @av, avl@ = source vector base and limit
161 *
162 * Use: Copies a multiprecision integer.
163 */
164
3c9ede17 165#define MPX_COPY(dv, dvl, av, avl) do { \
166 mpw *_dv = (dv), *_dvl = (dvl); \
167 size_t _dn = _dvl - _dv; \
168 const mpw *_av = (av), *_avl = (avl); \
169 size_t _an = _avl - _av; \
d03ab969 170 if (_av == _dv) { \
171 if (_dvl > _avl) \
3c9ede17 172 memset(_dv, 0, MPWS(_dn - _an)); \
d03ab969 173 } else if (_an >= _dn) \
174 memmove(_dv, _av, MPWS(_dn)); \
175 else { \
176 memmove(_dv, _av, MPWS(_an)); \
177 memset(_dv + _an, 0, MPWS(_dn - _an)); \
178 } \
179} while (0)
180
181/* --- @MPX_ZERO@ --- *
182 *
183 * Arguments: @v, vl@ = base and limit of vector to clear
184 *
185 * Use: Zeroes the area between the two vector pointers.
186 */
187
3c9ede17 188#define MPX_ZERO(v, vl) do { \
d03ab969 189 mpw *_v = (v), *_vl = (vl); \
3c9ede17 190 if (_v < _vl) \
191 memset(_v, 0, MPWS(_vl - _v)); \
d03ab969 192} while (0)
193
194/*----- Loading and storing -----------------------------------------------*/
195
196/* --- @mpx_storel@ --- *
197 *
198 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
3c9ede17 199 * @void *p@ = pointer to octet array
d03ab969 200 * @size_t sz@ = size of octet array
201 *
202 * Returns: ---
203 *
204 * Use: Stores an MP in an octet array, least significant octet
205 * first. High-end octets are silently discarded if there
206 * isn't enough space for them.
207 */
208
209extern void mpx_storel(const mpw */*v*/, const mpw */*vl*/,
3c9ede17 210 void */*p*/, size_t /*sz*/);
d03ab969 211
212/* --- @mpx_loadl@ --- *
213 *
214 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
3c9ede17 215 * @const void *p@ = pointer to octet array
d03ab969 216 * @size_t sz@ = size of octet array
217 *
218 * Returns: ---
219 *
220 * Use: Loads an MP in an octet array, least significant octet
221 * first. High-end octets are ignored if there isn't enough
222 * space for them.
223 */
224
225extern void mpx_loadl(mpw */*v*/, mpw */*vl*/,
3c9ede17 226 const void */*p*/, size_t /*sz*/);
d03ab969 227
228/* --- @mpx_storeb@ --- *
229 *
230 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
3c9ede17 231 * @void *p@ = pointer to octet array
d03ab969 232 * @size_t sz@ = size of octet array
233 *
234 * Returns: ---
235 *
236 * Use: Stores an MP in an octet array, most significant octet
237 * first. High-end octets are silently discarded if there
238 * isn't enough space for them.
239 */
240
241extern void mpx_storeb(const mpw */*v*/, const mpw */*vl*/,
3c9ede17 242 void */*p*/, size_t /*sz*/);
d03ab969 243
244/* --- @mpx_loadb@ --- *
245 *
246 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
3c9ede17 247 * @const void *p@ = pointer to octet array
d03ab969 248 * @size_t sz@ = size of octet array
249 *
250 * Returns: ---
251 *
252 * Use: Loads an MP in an octet array, most significant octet
253 * first. High-end octets are ignored if there isn't enough
254 * space for them.
255 */
256
257extern void mpx_loadb(mpw */*v*/, mpw */*vl*/,
3c9ede17 258 const void */*p*/, size_t /*sz*/);
d03ab969 259
260/*----- Logical shifting --------------------------------------------------*/
261
262/* --- @mpx_lsl@ --- *
263 *
264 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
265 * @const mpw *av, *avl@ = source vector base and limit
266 * @size_t n@ = number of bit positions to shift by
267 *
268 * Returns: ---
269 *
270 * Use: Performs a logical shift left operation on an integer.
271 */
272
273extern void mpx_lsl(mpw */*dv*/, mpw */*dvl*/,
274 const mpw */*av*/, const mpw */*avl*/,
275 size_t /*n*/);
276
277/* --- @mpx_lsr@ --- *
278 *
279 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
280 * @const mpw *av, *avl@ = source vector base and limit
281 * @size_t n@ = number of bit positions to shift by
282 *
283 * Returns: ---
284 *
285 * Use: Performs a logical shift right operation on an integer.
286 */
287
288extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/,
289 const mpw */*av*/, const mpw */*avl*/,
290 size_t /*n*/);
291
292/*----- Unsigned arithmetic -----------------------------------------------*/
293
7c13f461 294/* --- @mpx_2c@ --- *
295 *
296 * Arguments: @mpw *dv, *dvl@ = destination vector
297 * @const mpw *v, *vl@ = source vector
298 *
299 * Returns: ---
300 *
301 * Use: Calculates the two's complement of @v@.
302 */
303
304extern void mpx_2c(mpw */*dv*/, mpw */*dvl*/,
305 const mpw */*v*/, const mpw */*vl*/);
306
d03ab969 307/* --- @mpx_ucmp@ --- *
308 *
309 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
310 * @const mpw *bv, *bvl@ = second argument vector base and limit
311 *
312 * Returns: Less than, equal to, or greater than zero depending on
313 * whether @a@ is less than, equal to or greater than @b@,
314 * respectively.
315 *
316 * Use: Performs an unsigned integer comparison.
317 */
318
319#define MPX_UCMP(av, avl, op, dv, dvl) \
320 (mpx_ucmp((av), (avl), (dv), (dvl)) op 0)
321
322extern int mpx_ucmp(const mpw */*av*/, const mpw */*avl*/,
323 const mpw */*bv*/, const mpw */*bvl*/);
324
325/* --- @mpx_uadd@ --- *
326 *
327 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
328 * @const mpw *av, *avl@ = first addend vector base and limit
329 * @const mpw *bv, *bvl@ = second addend vector base and limit
330 *
331 * Returns: ---
332 *
333 * Use: Performs unsigned integer addition. If the result overflows
334 * the destination vector, high-order bits are discarded. This
335 * means that two's complement addition happens more or less for
336 * free, although that's more a side-effect than anything else.
337 * The result vector may be equal to either or both source
338 * vectors, but may not otherwise overlap them.
339 */
340
341extern void mpx_uadd(mpw */*dv*/, mpw */*dvl*/,
342 const mpw */*av*/, const mpw */*avl*/,
343 const mpw */*bv*/, const mpw */*bvl*/);
344
dd517851 345/* --- @mpx_uaddn@ --- *
346 *
347 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
348 * @mpw n@ = other addend
3c9ede17 349 *
dd517851 350 * Returns: ---
3c9ede17 351 *
352 * Use: Adds a small integer to a multiprecision number.
353 */
354
355#define MPX_UADDN(dv, dvl, n) do { \
356 mpw *_ddv = (dv), *_ddvl = (dvl); \
357 mpw _c = (n); \
358 \
359 while (_c && _ddv < _ddvl) { \
360 mpd _x = (mpd)*_ddv + (mpd)_c; \
361 *_ddv++ = MPW(_x); \
362 _c = _x >> MPW_BITS; \
363 } \
364} while (0)
365
dd517851 366extern void mpx_uaddn(mpw */*dv*/, mpw */*dvl*/, mpw /*n*/);
367
d03ab969 368/* --- @mpx_usub@ --- *
369 *
370 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
371 * @const mpw *av, *avl@ = first argument vector base and limit
372 * @const mpw *bv, *bvl@ = second argument vector base and limit
373 *
374 * Returns: ---
375 *
376 * Use: Performs unsigned integer subtraction. If the result
377 * overflows the destination vector, high-order bits are
378 * discarded. This means that two's complement subtraction
3c9ede17 379 * happens more or less for free, although that's more a side-
d03ab969 380 * effect than anything else. The result vector may be equal to
381 * either or both source vectors, but may not otherwise overlap
382 * them.
383 */
384
385extern void mpx_usub(mpw */*dv*/, mpw */*dvl*/,
386 const mpw */*av*/, const mpw */*avl*/,
387 const mpw */*bv*/, const mpw */*bvl*/);
388
dd517851 389/* --- @mpx_usubn@ --- *
3c9ede17 390 *
dd517851 391 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
392 * @n@ = subtrahend
393 *
394 * Returns: ---
3c9ede17 395 *
396 * Use: Subtracts a small integer from a multiprecision number.
397 */
398
399#define MPX_USUBN(dv, dvl, n) do { \
400 mpw *_ddv = (dv), *_ddvl = (dvl); \
401 mpw _c = (n); \
402 \
403 while (_ddv < _ddvl) { \
404 mpd _x = (mpd)*_ddv - (mpd)_c; \
405 *_ddv++ = MPW(_x); \
406 if (_x >> MPW_BITS) \
407 _c = 1; \
408 else \
409 break; \
410 } \
411} while (0)
412
dd517851 413extern void mpx_usubn(mpw */*dv*/, mpw */*dvl*/, mpw /*n*/);
414
3c9ede17 415/* --- @mpx_umul@ --- *
416 *
417 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
418 * @const mpw *av, *avl@ = multiplicand vector base and limit
419 * @const mpw *bv, *bvl@ = multiplier vector base and limit
420 *
421 * Returns: ---
422 *
423 * Use: Performs unsigned integer multiplication. If the result
424 * overflows the desination vector, high-order bits are
425 * discarded. The result vector may not overlap the argument
426 * vectors in any way.
427 */
428
429extern void mpx_umul(mpw */*dv*/, mpw */*dvl*/,
430 const mpw */*av*/, const mpw */*avl*/,
431 const mpw */*bv*/, const mpw */*bvl*/);
432
dd517851 433/* --- @mpx_umuln@ --- *
d03ab969 434 *
dd517851 435 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
436 * @const mpw *av, *avl@ = multiplicand vector base and limit
437 * @mpw m@ = multiplier
438 *
439 * Returns: ---
d03ab969 440 *
441 * Use: Multiplies a multiprecision integer by a single-word value.
442 * The destination and source may be equal. The destination
443 * is completely cleared after use.
444 */
445
446#define MPX_UMULN(dv, dvl, av, avl, m) do { \
447 mpw *_dv = (dv), *_dvl = (dvl); \
448 const mpw *_av = (av), *_avl = (avl); \
449 mpw _c = 0; \
450 mpd _m = (m); \
451 \
452 while (_av < _avl) { \
453 mpd _x; \
454 if (_dv >= _dvl) \
455 break; \
3c9ede17 456 _x = (mpd)_m * (mpd)*_av++ + _c; \
d03ab969 457 *_dv++ = MPW(_x); \
458 _c = _x >> MPW_BITS; \
459 } \
460 if (_dv < _dvl) { \
461 *_dv++ = MPW(_c); \
462 MPX_ZERO(_dv, _dvl); \
463 } \
464} while (0)
465
dd517851 466extern void mpx_umuln(mpw */*dv*/, mpw */*dvl*/,
467 const mpw */*av*/, const mpw */*avl*/, mpw m);
468
469/* --- @mpx_umlan@ --- *
d03ab969 470 *
dd517851 471 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
472 * @const mpw *av, *avl@ = multiplicand vector base and limit
473 * @mpw m@ = multiplier
474 *
475 * Returns: ---
d03ab969 476 *
477 * Use: Multiplies a multiprecision integer by a single-word value
478 * and adds the result to an accumulator.
479 */
480
481#define MPX_UMLAN(dv, dvl, av, avl, m) do { \
482 mpw *_dv = (dv), *_dvl = (dvl); \
483 const mpw *_av = (av), *_avl = (avl); \
7c13f461 484 mpw _cc = 0; \
d03ab969 485 mpd _m = (m); \
486 \
487 while (_av < _avl) { \
488 mpd _x; \
489 if (_dv >= _dvl) \
490 break; \
7c13f461 491 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \
d03ab969 492 *_dv++ = MPW(_x); \
7c13f461 493 _cc = _x >> MPW_BITS; \
d03ab969 494 } \
7c13f461 495 MPX_UADDN(_dv, _dvl, _cc); \
d03ab969 496} while (0)
497
dd517851 498extern void mpx_umlan(mpw */*dv*/, mpw */*dvl*/,
499 const mpw */*av*/, const mpw */*avl*/, mpw m);
500
3c9ede17 501/* --- @mpx_usqr@ --- *
d03ab969 502 *
503 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
3c9ede17 504 * @const mpw *av, *av@ = source vector base and limit
d03ab969 505 *
506 * Returns: ---
507 *
3c9ede17 508 * Use: Performs unsigned integer squaring. The result vector must
509 * not overlap the source vector in any way.
d03ab969 510 */
511
3c9ede17 512extern void mpx_usqr(mpw */*dv*/, mpw */*dvl*/,
513 const mpw */*av*/, const mpw */*avl*/);
d03ab969 514
a86e33af 515/* --- @mpx_kmul@ --- *
516 *
517 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
518 * @const mpw *av, *avl@ = pointer to first argument
519 * @const mpw *bv, *bvl@ = pointer to second argument
520 * @mpw *sv, *svl@ = pointer to scratch workspace
521 *
522 * Returns: ---
523 *
524 * Use: Multiplies two multiprecision integers using Karatsuba's
525 * algorithm. This is rather faster than traditional long
526 * multiplication (e.g., @mpx_umul@) on large numbers, although
527 * more expensive on small ones.
528 *
529 * The destination and scratch buffers must be twice as large as
530 * the larger argument.
531 */
532
533#define KARATSUBA_CUTOFF 16
534#define KARATSUBA_SLOP 32
535
536extern void mpx_kmul(mpw */*dv*/, mpw */*dvl*/,
537 const mpw */*av*/, const mpw */*avl*/,
538 const mpw */*bv*/, const mpw */*bvl*/,
539 mpw */*sv*/, mpw */*svl*/);
540
d03ab969 541/* --- @mpx_udiv@ --- *
542 *
543 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
544 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
545 * @const mpw *dv, *dvl@ = divisor vector base and limit
3c9ede17 546 * @mpw *sv, *svl@ = scratch workspace
d03ab969 547 *
548 * Returns: ---
549 *
550 * Use: Performs unsigned integer division. If the result overflows
551 * the quotient vector, high-order bits are discarded. (Clearly
552 * the remainder vector can't overflow.) The various vectors
553 * may not overlap in any way. Yes, I know it's a bit odd
554 * requiring the dividend to be in the result position but it
3c9ede17 555 * does make some sense really. The remainder must have
556 * headroom for at least two extra words. The scratch space
7c13f461 557 * must be at least one word larger than the divisor.
d03ab969 558 */
559
560extern void mpx_udiv(mpw */*qv*/, mpw */*qvl*/, mpw */*rv*/, mpw */*rvl*/,
3c9ede17 561 const mpw */*dv*/, const mpw */*dvl*/,
562 mpw */*sv*/, mpw */*svl*/);
d03ab969 563
564/*----- That's all, folks -------------------------------------------------*/
565
566#ifdef __cplusplus
567 }
568#endif
569
570#endif