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