Implementation of the Digital Signature Algorithm.
[u/mdw/catacomb] / mpx.h
1 /* -*-c-*-
2 *
3 * $Id: mpx.h,v 1.4 1999/11/17 18:04:43 mdw Exp $
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 $
33 * Revision 1.4 1999/11/17 18:04:43 mdw
34 * Add two's complement support. Fix a bug in MPX_UMLAN.
35 *
36 * Revision 1.3 1999/11/13 01:51:29 mdw
37 * Minor interface changes. Should be stable now.
38 *
39 * Revision 1.2 1999/11/11 17:47:55 mdw
40 * Minor changes for different `mptypes.h' format.
41 *
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
71 #ifndef MPW_H
72 # include "mpw.h"
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 { \
86 const mpw *_vv = (v), *_vvl = (vl); \
87 while (_vvl > _vv && !_vvl[-1]) \
88 _vvl--; \
89 (vl) = (mpw *)_vvl; \
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); \
103 MPX_SHRINK(_v, _vl); \
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
124 * @const mpw *v, *vl@ = pointer to array of words
125 *
126 * Use: Calculates the number of octets in a multiprecision value.
127 */
128
129 #define MPX_OCTETS(o, v, vl) do { \
130 const mpw *_v = (v), *_vl = (vl); \
131 MPX_SHRINK(_v, _vl); \
132 if (_v == _vl) \
133 (o) = 0; \
134 else { \
135 size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \
136 mpw _w = _vl[-1]; \
137 unsigned _k = MPW_BITS / 2; \
138 while (_k >= 8) { \
139 if (_w >> _k) { \
140 _w >>= _k; \
141 _o += _k >> 3; \
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
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; \
164 if (_av == _dv) { \
165 if (_dvl > _avl) \
166 memset(_dv, 0, MPWS(_dn - _an)); \
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
182 #define MPX_ZERO(v, vl) do { \
183 mpw *_v = (v), *_vl = (vl); \
184 if (_v < _vl) \
185 memset(_v, 0, MPWS(_vl - _v)); \
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
193 * @void *p@ = pointer to octet array
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
203 extern void mpx_storel(const mpw */*v*/, const mpw */*vl*/,
204 void */*p*/, size_t /*sz*/);
205
206 /* --- @mpx_loadl@ --- *
207 *
208 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
209 * @const void *p@ = pointer to octet array
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
219 extern void mpx_loadl(mpw */*v*/, mpw */*vl*/,
220 const void */*p*/, size_t /*sz*/);
221
222 /* --- @mpx_storeb@ --- *
223 *
224 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
225 * @void *p@ = pointer to octet array
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
235 extern void mpx_storeb(const mpw */*v*/, const mpw */*vl*/,
236 void */*p*/, size_t /*sz*/);
237
238 /* --- @mpx_loadb@ --- *
239 *
240 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
241 * @const void *p@ = pointer to octet array
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
251 extern void mpx_loadb(mpw */*v*/, mpw */*vl*/,
252 const void */*p*/, size_t /*sz*/);
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
267 extern 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
282 extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/,
283 const mpw */*av*/, const mpw */*avl*/,
284 size_t /*n*/);
285
286 /*----- Unsigned arithmetic -----------------------------------------------*/
287
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
298 extern void mpx_2c(mpw */*dv*/, mpw */*dvl*/,
299 const mpw */*v*/, const mpw */*vl*/);
300
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
316 extern 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
335 extern void mpx_uadd(mpw */*dv*/, mpw */*dvl*/,
336 const mpw */*av*/, const mpw */*avl*/,
337 const mpw */*bv*/, const mpw */*bvl*/);
338
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
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
369 * happens more or less for free, although that's more a side-
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
375 extern void mpx_usub(mpw */*dv*/, mpw */*dvl*/,
376 const mpw */*av*/, const mpw */*avl*/,
377 const mpw */*bv*/, const mpw */*bvl*/);
378
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
415 extern void mpx_umul(mpw */*dv*/, mpw */*dvl*/,
416 const mpw */*av*/, const mpw */*avl*/,
417 const mpw */*bv*/, const mpw */*bvl*/);
418
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; \
440 _x = (mpd)_m * (mpd)*_av++ + _c; \
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); \
463 mpw _cc = 0; \
464 mpd _m = (m); \
465 \
466 while (_av < _avl) { \
467 mpd _x; \
468 if (_dv >= _dvl) \
469 break; \
470 _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc; \
471 *_dv++ = MPW(_x); \
472 _cc = _x >> MPW_BITS; \
473 } \
474 MPX_UADDN(_dv, _dvl, _cc); \
475 } while (0)
476
477 /* --- @mpx_usqr@ --- *
478 *
479 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
480 * @const mpw *av, *av@ = source vector base and limit
481 *
482 * Returns: ---
483 *
484 * Use: Performs unsigned integer squaring. The result vector must
485 * not overlap the source vector in any way.
486 */
487
488 extern void mpx_usqr(mpw */*dv*/, mpw */*dvl*/,
489 const mpw */*av*/, const mpw */*avl*/);
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
496 * @mpw *sv, *svl@ = scratch workspace
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
505 * does make some sense really. The remainder must have
506 * headroom for at least two extra words. The scratch space
507 * must be at least one word larger than the divisor.
508 */
509
510 extern void mpx_udiv(mpw */*qv*/, mpw */*qvl*/, mpw */*rv*/, mpw */*rvl*/,
511 const mpw */*dv*/, const mpw */*dvl*/,
512 mpw */*sv*/, mpw */*svl*/);
513
514 /*----- That's all, folks -------------------------------------------------*/
515
516 #ifdef __cplusplus
517 }
518 #endif
519
520 #endif