Pile of changes for supporting two's complement properly.
[u/mdw/catacomb] / mp.h
1 /* -*-c-*-
2 *
3 * $Id: mp.h,v 1.13 2002/10/06 22:52:50 mdw Exp $
4 *
5 * Simple 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: mp.h,v $
33 * Revision 1.13 2002/10/06 22:52:50 mdw
34 * Pile of changes for supporting two's complement properly.
35 *
36 * Revision 1.12 2001/06/16 12:57:43 mdw
37 * Move the @mpmont_factor@ structure and rename it now that it's used for
38 * Barrett simultaneous exponentiation too.
39 *
40 * Revision 1.11 2001/04/03 19:36:05 mdw
41 * Add some simple bitwise operations so that Perl can use them.
42 *
43 * Revision 1.10 2000/10/08 12:03:16 mdw
44 * Provide @mp_eq@ and @MP_EQ@ for rapidly testing equality of two
45 * integers.
46 *
47 * Revision 1.9 2000/07/29 17:03:31 mdw
48 * Add support for left-to-right bitscanning, for use in modular
49 * exponentiation.
50 *
51 * Revision 1.8 2000/06/22 19:02:01 mdw
52 * Add new functions.
53 *
54 * Revision 1.7 2000/06/17 11:45:09 mdw
55 * Major memory management overhaul. Added arena support. Use the secure
56 * arena for secret integers. Replace and improve the MP management macros
57 * (e.g., replace MP_MODIFY by MP_DEST).
58 *
59 * Revision 1.6 1999/12/10 23:19:46 mdw
60 * Minor bugfixes. New interface for suggested destinations.
61 *
62 * Revision 1.5 1999/11/22 20:50:37 mdw
63 * Add support for computing Jacobi symbols.
64 *
65 * Revision 1.4 1999/11/21 22:13:02 mdw
66 * Add mp version of MPX_BITS.
67 *
68 * Revision 1.3 1999/11/19 13:19:14 mdw
69 * Fix const annotation.
70 *
71 * Revision 1.2 1999/11/17 18:02:16 mdw
72 * New multiprecision integer arithmetic suite.
73 *
74 */
75
76 #ifndef CATACOMB_MP_H
77 #define CATACOMB_MP_H
78
79 #ifdef __cplusplus
80 extern "C" {
81 #endif
82
83 /*----- Header files ------------------------------------------------------*/
84
85 #include <assert.h>
86 #include <string.h>
87
88 #include <mLib/sub.h>
89
90 #ifndef CATACOMB_MPW_H
91 # include "mpw.h"
92 #endif
93
94 #ifndef CATACOMB_ARENA_H
95 # include "arena.h"
96 #endif
97
98 #ifndef CATACOMB_MPARENA_H
99 # include "mparena.h"
100 #endif
101
102 #ifndef CATACOMB_MPX_H
103 # include "mpx.h"
104 #endif
105
106 /*----- Data structures ---------------------------------------------------*/
107
108 /* --- A multiprecision integer --- */
109
110 typedef struct mp {
111 mpw *v, *vl; /* Vector of digits, current limit */
112 size_t sz; /* Size of digit buffer in words */
113 mparena *a; /* Arena for buffer allocation */
114 unsigned f; /* Flags (see below) */
115 unsigned ref; /* Reference counter */
116 } mp;
117
118 #define MP_NEG 1u /* Negative (signed magnitude) */
119 #define MP_BURN 2u /* Secret (viral flag) */
120 #define MP_CONST 4u /* Uses strange memory allocation */
121 #define MP_UNDEF 8u /* Contains nothing interesting */
122 #define MP_DESTROYED 16u /* Has been destroyed */
123
124 /* --- A factor for simultaneous exponentation --- *
125 *
126 * Used by the Montgomery and Barrett exponentiators.
127 */
128
129 typedef struct mp_expfactor {
130 mp *base;
131 mp *exp;
132 } mp_expfactor;
133
134 /*----- Useful constants --------------------------------------------------*/
135
136 extern mp mp_const[];
137
138 #define MP_ZERO (&mp_const[0])
139 #define MP_ONE (&mp_const[1])
140 #define MP_TWO (&mp_const[2])
141 #define MP_THREE (&mp_const[3])
142 #define MP_FOUR (&mp_const[4])
143 #define MP_FIVE (&mp_const[5])
144 #define MP_TEN (&mp_const[6])
145 #define MP_256 (&mp_const[7])
146 #define MP_MONE (&mp_const[8])
147
148 #define MP_NEW ((mp *)0)
149 #define MP_NEWSEC (&mp_const[9])
150
151 /*----- Trivial macros ----------------------------------------------------*/
152
153 /* --- @MP_LEN@ --- *
154 *
155 * Arguments: @mp *m@ = pointer to a multiprecision integer
156 *
157 * Returns: Length of the integer, in words.
158 */
159
160 #define MP_LEN(m) ((m)->vl - ((m)->v))
161
162 /*----- Memory management and reference counting --------------------------*/
163
164 /* --- @mp_new@ --- *
165 *
166 * Arguments: @size_t sz@ = size of vector required
167 * @unsigned f@ = flags to set
168 *
169 * Returns: Pointer to a new MP structure.
170 *
171 * Use: Allocates a new multiprecision integer. The data space is
172 * allocated from either the standard global or secret arena,
173 * depending on the initial flags requested.
174 */
175
176 extern mp *mp_new(size_t /*sz*/, unsigned /*f*/);
177
178 /* --- @mp_create@ --- *
179 *
180 * Arguments: @size_t sz@ = size of vector required
181 *
182 * Returns: Pointer to pristine new MP structure with enough memory
183 * bolted onto it.
184 *
185 * Use: Creates a new multiprecision integer with indeterminate
186 * contents. The integer has a single reference.
187 */
188
189 extern mp *mp_create(size_t /*sz*/);
190
191 /* --- @mp_createsecure@ --- *
192 *
193 * Arguments: @size_t sz@ = size of vector required
194 *
195 * Returns: Pointer to pristine new MP structure with enough memory
196 * bolted onto it.
197 *
198 * Use: Creates a new multiprecision integer with indeterminate
199 * contents. The integer has a single reference. The integer's
200 * data space is allocated from the secure arena. Its burn flag
201 * is set.
202 */
203
204 extern mp *mp_createsecure(size_t /*sz*/);
205
206 /* --- @mp_build@ --- *
207 *
208 * Arguments: @mp *m@ = pointer to an MP block to fill in
209 * @mpw *v@ = pointer to a word array
210 * @mpw *vl@ = pointer just past end of array
211 *
212 * Returns: ---
213 *
214 * Use: Creates a multiprecision integer representing some smallish
215 * number. You must provide storage for the number and dispose
216 * of it when you've finished with it. The number is marked as
217 * constant while it exists.
218 */
219
220 extern void mp_build(mp */*m*/, mpw */*v*/, mpw */*vl*/);
221
222 /* --- @mp_destroy@ --- *
223 *
224 * Arguments: @mp *m@ = pointer to a multiprecision integer
225 *
226 * Returns: ---
227 *
228 * Use: Destroys a multiprecision integer. The reference count isn't
229 * checked. Don't use this function if you don't know what
230 * you're doing: use @mp_drop@ instead.
231 */
232
233 extern void mp_destroy(mp */*m*/);
234
235 /* --- @mp_copy@ --- *
236 *
237 * Arguments: @mp *m@ = pointer to a multiprecision integer
238 *
239 * Returns: A copy of the given multiprecision integer.
240 *
241 * Use: Copies the given integer. In fact you just get another
242 * reference to the same old one again.
243 */
244
245 extern mp *mp_copy(mp */*m*/);
246
247 #define MP_COPY(m) ((m)->ref++, (m))
248
249 /* --- @mp_drop@ --- *
250 *
251 * Arguments: @mp *m@ = pointer to a multiprecision integer
252 *
253 * Returns: ---
254 *
255 * Use: Drops a reference to an integer which isn't wanted any more.
256 * If there are no more references, the integer is destroyed.
257 */
258
259 extern void mp_drop(mp */*m*/);
260
261 #define MP_DROP(m) do { \
262 mp *_mm = (m); \
263 _mm->ref--; \
264 if (_mm->ref == 0 && !(_mm->f & MP_CONST)) \
265 mp_destroy(_mm); \
266 } while (0)
267
268 /* --- @mp_split@ --- *
269 *
270 * Arguments: @mp *m@ = pointer to a multiprecision integer
271 *
272 * Returns: A reference to the same integer, possibly with a different
273 * address.
274 *
275 * Use: Splits off a modifiable version of the integer referred to.
276 */
277
278 extern mp *mp_split(mp */*m*/);
279
280 #define MP_SPLIT(m) do { \
281 mp *_m = (m); \
282 if ((_m->f & MP_CONST) || _m->ref > 1) { \
283 size_t _len = MP_LEN(_m); \
284 mp *_mm = mp_new(_len, _m->f); \
285 if (!(_m->f & MP_UNDEF)) \
286 memcpy(_mm->v, _m->v, MPWS(_len)); \
287 _m->ref--; \
288 _m = _mm; \
289 } \
290 (m) = _m; \
291 } while (0)
292
293 /* --- @mp_resize@ --- *
294 *
295 * Arguments: @mp *m@ = pointer to a multiprecision integer
296 * @size_t sz@ = new size
297 *
298 * Returns: ---
299 *
300 * Use: Resizes the vector containing the integer's digits. The new
301 * size must be at least as large as the current integer's
302 * length. This isn't really intended for client use.
303 */
304
305 extern void mp_resize(mp */*m*/, size_t /*sz*/);
306
307 #define MP_RESIZE(m, ssz) do { \
308 mp *_m = (m); \
309 size_t _sz = (ssz); \
310 mparena *_a = (_m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; \
311 mpw *_v; \
312 size_t _len = MP_LEN(_m); \
313 assert(((void)"can't make size less than length", _sz >= _len)); \
314 _v = mpalloc(_a, _sz); \
315 if (!(_m->f & MP_UNDEF)) \
316 memcpy(_v, _m->v, MPWS(_len)); \
317 if (_m->f & MP_BURN) \
318 memset(_m->v, 0, MPWS(_m->sz)); \
319 mpfree(_m->a, _m->v); \
320 _m->a = _a; \
321 _m->v = _v; \
322 _m->vl = _v + _len; \
323 } while (0)
324
325 /* --- @mp_ensure@ --- *
326 *
327 * Arguments: @mp *m@ = pointer to a multiprecision integer
328 * @size_t sz@ = required size
329 *
330 * Returns: ---
331 *
332 * Use: Ensures that the integer has enough space for @sz@ digits.
333 * The value is not changed.
334 */
335
336 extern void mp_ensure(mp */*m*/, size_t /*sz*/);
337
338 #define MP_ENSURE(m, ssz) do { \
339 mp *_m = (m); \
340 size_t _ssz = (ssz); \
341 size_t _len = MP_LEN(_m); \
342 if (_ssz >= _len) { \
343 if (_ssz > _m->sz) \
344 mp_resize(_m, _ssz); \
345 if (!(_m->f & MP_UNDEF) && _ssz > _len) \
346 memset(_m->vl, 0, MPWS(_ssz - _len)); \
347 _m->vl = _m->v + _ssz; \
348 } \
349 } while (0)
350
351 /* --- @mp_dest@ --- *
352 *
353 * Arguments: @mp *m@ = a suggested destination integer
354 * @size_t sz@ = size required for result, in digits
355 * @unsigned f@ = various flags
356 *
357 * Returns: A pointer to an appropriate destination.
358 *
359 * Use: Converts a suggested destination into a real destination with
360 * the required properties. If the real destination is @d@,
361 * then the following properties will hold:
362 *
363 * * @d@ will have exactly one reference.
364 *
365 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
366 * change, unless @f@ has the @MP_UNDEF@ flag set.
367 *
368 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
369 * entry is equal to the sum of the counts of @d@ and @m@ on
370 * exit.
371 *
372 * * The size of @d@ will be at least @sz@.
373 *
374 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
375 * allocated from @MPARENA_SECURE@.
376 *
377 * Understanding this function is crucial to using Catacomb's
378 * multiprecision integer library effectively.
379 */
380
381 extern mp *mp_dest(mp */*m*/, size_t /*sz*/, unsigned /*f*/);
382
383 #define MP_DEST(m, ssz, f) do { \
384 mp *_m = (m); \
385 size_t _ssz = (ssz); \
386 unsigned _f = (f); \
387 _m = mp_dest(_m, _ssz, _f); \
388 (m) = _m; \
389 } while (0)
390
391 /*----- Size manipulation -------------------------------------------------*/
392
393 /* --- @mp_shrink@ --- *
394 *
395 * Arguments: @mp *m@ = pointer to a multiprecision integer
396 *
397 * Returns: ---
398 *
399 * Use: Reduces the recorded length of an integer. This doesn't
400 * reduce the amount of memory used, although it can improve
401 * performance a bit. To reduce memory, use @mp_minimize@
402 * instead. This can't change the value of an integer, and is
403 * therefore safe to use even when there are multiple
404 * references.
405 */
406
407 extern void mp_shrink(mp */*m*/);
408
409 #define MP_SHRINK(m) do { \
410 mp *_mm = (m); \
411 MPX_SHRINK(_mm->v, _mm->vl); \
412 if (!MP_LEN(_mm)) \
413 _mm->f &= ~MP_NEG; \
414 } while (0)
415
416 /* --- @mp_minimize@ --- *
417 *
418 * Arguments: @mp *m@ = pointer to a multiprecision integer
419 *
420 * Returns: ---
421 *
422 * Use: Reduces the amount of memory an integer uses. It's best to
423 * do this to numbers which aren't going to change in the
424 * future.
425 */
426
427 extern void mp_minimize(mp */*m*/);
428
429 /*----- Bit scanning ------------------------------------------------------*/
430
431 #ifndef CATACOMB_MPSCAN_H
432 # include "mpscan.h"
433 #endif
434
435 /* --- @mp_scan@ --- *
436 *
437 * Arguments: @mpscan *sc@ = pointer to bitscanner block
438 * @const mp *m@ = pointer to a multiprecision integer
439 *
440 * Returns: ---
441 *
442 * Use: Initializes a bitscanner on a multiprecision integer.
443 */
444
445 extern void mp_scan(mpscan */*sc*/, const mp */*m*/);
446
447 #define MP_SCAN(sc, m) do { \
448 const mp *_mm = (m); \
449 mpscan *_sc = (sc); \
450 MPSCAN_INITX(_sc, _mm->v, _mm->vl); \
451 } while (0)
452
453 /* --- @mp_rscan@ --- *
454 *
455 * Arguments: @mpscan *sc@ = pointer to bitscanner block
456 * @const mp *m@ = pointer to a multiprecision integer
457 *
458 * Returns: ---
459 *
460 * Use: Initializes a reverse bitscanner on a multiprecision
461 * integer.
462 */
463
464 extern void mp_rscan(mpscan */*sc*/, const mp */*m*/);
465
466 #define MP_RSCAN(sc, m) do { \
467 const mp *_mm = (m); \
468 mpscan *_sc = (sc); \
469 MPSCAN_RINITX(_sc, _mm->v, _mm->vl); \
470 } while (0)
471
472 /* --- Other bitscanning aliases --- */
473
474 #define mp_step mpscan_step
475 #define mp_bit mpscan_bit
476 #define mp_rstep mpscan_rstep
477 #define mp_rbit mpscan_rbit
478
479 #define MP_STEP MPSCAN_STEP
480 #define MP_BIT MPSCAN_BIT
481 #define MP_RSTEP MPSCAN_RSTEP
482 #define MP_RBIT MPSCAN_RBIT
483
484 /*----- Loading and storing -----------------------------------------------*/
485
486 /* --- @mp_octets@ --- *
487 *
488 * Arguments: @const mp *m@ = a multiprecision integer
489 *
490 * Returns: The number of octets required to represent @m@.
491 *
492 * Use: Calculates the external storage required for a multiprecision
493 * integer.
494 */
495
496 extern size_t mp_octets(const mp */*m*/);
497
498 /* --- @mp_octets2c@ --- *
499 *
500 * Arguments: @const mp *m@ = a multiprecision integer
501 *
502 * Returns: The number of octets required to represent @m@.
503 *
504 * Use: Calculates the external storage required for a multiprecision
505 * integer represented as two's complement.
506 */
507
508 extern size_t mp_octets2c(const mp */*m*/);
509
510 /* --- @mp_bits@ --- *
511 *
512 * Arguments: @const mp *m@ = a multiprecision integer
513 *
514 * Returns: The number of bits required to represent @m@.
515 *
516 * Use: Calculates the external storage required for a multiprecision
517 * integer.
518 */
519
520 extern unsigned long mp_bits(const mp */*m*/);
521
522 /* --- @mp_loadl@ --- *
523 *
524 * Arguments: @mp *d@ = destination
525 * @const void *pv@ = pointer to source data
526 * @size_t sz@ = size of the source data
527 *
528 * Returns: Resulting multiprecision number.
529 *
530 * Use: Loads a multiprecision number from an array of octets. The
531 * first byte in the array is the least significant. More
532 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
533 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
534 */
535
536 extern mp *mp_loadl(mp */*d*/, const void */*pv*/, size_t /*sz*/);
537
538 /* --- @mp_storel@ --- *
539 *
540 * Arguments: @const mp *m@ = source
541 * @void *pv@ = pointer to output array
542 * @size_t sz@ = size of the output array
543 *
544 * Returns: ---
545 *
546 * Use: Stores a multiprecision number in an array of octets. The
547 * first byte in the array is the least significant. If the
548 * array is too small to represent the number, high-order bits
549 * are truncated; if the array is too large, high order bytes
550 * are filled with zeros. More formally, if the number is
551 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
552 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
553 */
554
555 extern void mp_storel(const mp */*m*/, void */*pv*/, size_t /*sz*/);
556
557 /* --- @mp_loadb@ --- *
558 *
559 * Arguments: @mp *d@ = destination
560 * @const void *pv@ = pointer to source data
561 * @size_t sz@ = size of the source data
562 *
563 * Returns: Resulting multiprecision number.
564 *
565 * Use: Loads a multiprecision number from an array of octets. The
566 * last byte in the array is the least significant. More
567 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
568 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
569 */
570
571 extern mp *mp_loadb(mp */*d*/, const void */*pv*/, size_t /*sz*/);
572
573 /* --- @mp_storeb@ --- *
574 *
575 * Arguments: @const mp *m@ = source
576 * @void *pv@ = pointer to output array
577 * @size_t sz@ = size of the output array
578 *
579 * Returns: ---
580 *
581 * Use: Stores a multiprecision number in an array of octets. The
582 * last byte in the array is the least significant. If the
583 * array is too small to represent the number, high-order bits
584 * are truncated; if the array is too large, high order bytes
585 * are filled with zeros. More formally, if the number is
586 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
587 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
588 */
589
590 extern void mp_storeb(const mp */*m*/, void */*pv*/, size_t /*sz*/);
591
592 /* --- @mp_loadl2c@ --- *
593 *
594 * Arguments: @mp *d@ = destination
595 * @const void *pv@ = pointer to source data
596 * @size_t sz@ = size of the source data
597 *
598 * Returns: Resulting multiprecision number.
599 *
600 * Use: Loads a multiprecision number from an array of octets as
601 * two's complement. The first byte in the array is the least
602 * significant.
603 */
604
605 extern mp *mp_loadl2c(mp */*d*/, const void */*pv*/, size_t /*sz*/);
606
607 /* --- @mp_storel2c@ --- *
608 *
609 * Arguments: @const mp *m@ = source
610 * @void *pv@ = pointer to output array
611 * @size_t sz@ = size of the output array
612 *
613 * Returns: ---
614 *
615 * Use: Stores a multiprecision number in an array of octets as two's
616 * complement. The first byte in the array is the least
617 * significant. If the array is too small to represent the
618 * number, high-order bits are truncated; if the array is too
619 * large, high order bytes are sign-extended.
620 */
621
622 extern void mp_storel2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
623
624 /* --- @mp_loadb2c@ --- *
625 *
626 * Arguments: @mp *d@ = destination
627 * @const void *pv@ = pointer to source data
628 * @size_t sz@ = size of the source data
629 *
630 * Returns: Resulting multiprecision number.
631 *
632 * Use: Loads a multiprecision number from an array of octets as
633 * two's complement. The last byte in the array is the least
634 * significant.
635 */
636
637 extern mp *mp_loadb2c(mp */*d*/, const void */*pv*/, size_t /*sz*/);
638
639 /* --- @mp_storeb2c@ --- *
640 *
641 * Arguments: @const mp *m@ = source
642 * @void *pv@ = pointer to output array
643 * @size_t sz@ = size of the output array
644 *
645 * Returns: ---
646 *
647 * Use: Stores a multiprecision number in an array of octets, as
648 * two's complement. The last byte in the array is the least
649 * significant. If the array is too small to represent the
650 * number, high-order bits are truncated; if the array is too
651 * large, high order bytes are sign-extended.
652 */
653
654 extern void mp_storeb2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
655
656 /*----- Simple arithmetic -------------------------------------------------*/
657
658 /* --- @mp_lsl@, @mp_lsr@ --- *
659 *
660 * Arguments: @mp *d@ = destination
661 * @mp *a@ = source
662 * @size_t n@ = number of bits to move
663 *
664 * Returns: Result, @a@ shifted left or right by @n@.
665 */
666
667 extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/);
668 extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
669
670 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
671 *
672 * Arguments: @mp *d@ = destination
673 * @mp *a@ = source
674 * @size_t n@ = number of bits to move
675 *
676 * Returns: Result, @a@ shifted left or right by @n@. Handles the
677 * pretence of sign-extension for negative numbers.
678 */
679
680 extern mp *mp_lsl2c(mp */*d*/, mp */*a*/, size_t /*n*/);
681 extern mp *mp_lsr2c(mp */*d*/, mp */*a*/, size_t /*n*/);
682
683 /* --- @mp_testbit@ --- *
684 *
685 * Arguments: @mp *x@ = a large integer
686 * @size_t n@ = which bit to test
687 *
688 * Returns: Nonzero if the bit is set, zero if not.
689 */
690
691 extern int mp_testbit(mp */*x*/, size_t /*n*/);
692
693 /* --- @mp_testbit2c@ --- *
694 *
695 * Arguments: @mp *x@ = a large integer
696 * @size_t n@ = which bit to test
697 *
698 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
699 * complement representation.
700 */
701
702 extern int mp_testbit2c(mp */*x*/, size_t /*n*/);
703
704 /* --- @mp_eq@ --- *
705 *
706 * Arguments: @const mp *a, *b@ = two numbers
707 *
708 * Returns: Nonzero if the numbers are equal.
709 */
710
711 extern int mp_eq(const mp */*a*/, const mp */*b*/);
712
713 #define MP_EQ(a, b) \
714 ((((a)->f ^ (b)->f) & MP_NEG) == 0 && \
715 mpx_ueq((a)->v, (a)->vl, (b)->v, (b)->vl))
716
717 /* --- @mp_cmp@ --- *
718 *
719 * Arguments: @const mp *a, *b@ = two numbers
720 *
721 * Returns: Less than, equal to or greater than zero, according to
722 * whether @a@ is less than, equal to or greater than @b@.
723 */
724
725 extern int mp_cmp(const mp */*a*/, const mp */*b*/);
726
727 #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
728
729 /* --- @mp_bitop@ --- *
730 *
731 * Arguments: @mp *d@ = destination
732 * @mp *a, *b@ = sources
733 *
734 * Returns: The result of the given bitwise operation. These functions
735 * don't handle negative numbers at all sensibly. For that, use
736 * the @...2c@ variants. The functions are named after the
737 * truth tables they generate:
738 *
739 * a: 0011
740 * b: 0101
741 * @mpx_bitXXXX@
742 */
743
744 #define MP_BITDECL(string) \
745 extern mp *mp_bit##string(mp */*d*/, mp */*a*/, mp */*b*/);
746 MPX_DOBIN(MP_BITDECL)
747
748 /* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
749 *
750 * Synonyms for the commonly-used functions.
751 */
752
753 #define mp_and mp_bit0001
754 #define mp_or mp_bit0111
755 #define mp_nand mp_bit1110
756 #define mp_nor mp_bit1000
757 #define mp_xor mp_bit0110
758
759 /* --- @mp_not@ --- *
760 *
761 * Arguments: @mp *d@ = destination
762 * @mp *a@ = source
763 *
764 * Returns: The bitwise complement of the source.
765 */
766
767 extern mp *mp_not(mp */*d*/, mp */*a*/);
768
769 /* --- @mp_bitop2c@ --- *
770 *
771 * Arguments: @mp *d@ = destination
772 * @mp *a, *b@ = sources
773 *
774 * Returns: The result of the given bitwise operation. Negative numbers
775 * are treated as two's complement, sign-extended infinitely to
776 * the left. The functions are named after the truth tables
777 * they generate:
778 *
779 * a: 0011
780 * b: 0101
781 * @mpx_bitXXXX@
782 */
783
784 #define MP_BIT2CDECL(string) \
785 extern mp *mp_bit##string##2c(mp */*d*/, mp */*a*/, mp */*b*/);
786 MPX_DOBIN(MP_BIT2CDECL)
787
788 /* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
789 *
790 * Synonyms for the commonly-used functions.
791 */
792
793 #define mp_and2c mp_bit00012c
794 #define mp_or2c mp_bit01112c
795 #define mp_nand2c mp_bit11102c
796 #define mp_nor2c mp_bit10002c
797 #define mp_xor2c mp_bit01102c
798
799 /* --- @mp_not2c@ --- *
800 *
801 * Arguments: @mp *d@ = destination
802 * @mp *a@ = source
803 *
804 * Returns: The sign-extended complement of the argument.
805 */
806
807 extern mp *mp_not2c(mp */*d*/, mp */*a*/);
808
809 /* --- @mp_add@ --- *
810 *
811 * Arguments: @mp *d@ = destination
812 * @mp *a, *b@ = sources
813 *
814 * Returns: Result, @a@ added to @b@.
815 */
816
817 extern mp *mp_add(mp */*d*/, mp */*a*/, mp */*b*/);
818
819 /* --- @mp_sub@ --- *
820 *
821 * Arguments: @mp *d@ = destination
822 * @mp *a, *b@ = sources
823 *
824 * Returns: Result, @b@ subtracted from @a@.
825 */
826
827 extern mp *mp_sub(mp */*d*/, mp */*a*/, mp */*b*/);
828
829 /* --- @mp_mul@ --- *
830 *
831 * Arguments: @mp *d@ = destination
832 * @mp *a, *b@ = sources
833 *
834 * Returns: Result, @a@ multiplied by @b@.
835 */
836
837 extern mp *mp_mul(mp */*d*/, mp */*a*/, mp */*b*/);
838
839 /* --- @mp_sqr@ --- *
840 *
841 * Arguments: @mp *d@ = destination
842 * @mp *a@ = source
843 *
844 * Returns: Result, @a@ squared.
845 */
846
847 extern mp *mp_sqr(mp */*d*/, mp */*a*/);
848
849 /* --- @mp_div@ --- *
850 *
851 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
852 * @mp *a, *b@ = sources
853 *
854 * Use: Calculates the quotient and remainder when @a@ is divided by
855 * @b@.
856 */
857
858 extern void mp_div(mp **/*qq*/, mp **/*rr*/, mp */*a*/, mp */*b*/);
859
860 /* --- @mp_odd@ --- *
861 *
862 * Arguments: @mp *d@ = pointer to destination integer
863 * @mp *m@ = pointer to source integer
864 * @size_t *s@ = where to store the power of 2
865 *
866 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
867 *
868 * Use: Computes a power of two and an odd integer which, when
869 * multiplied, give a specified result. This sort of thing is
870 * useful in number theory quite often.
871 */
872
873 extern mp *mp_odd(mp */*d*/, mp */*m*/, size_t */*s*/);
874
875 /*----- More advanced algorithms ------------------------------------------*/
876
877 /* --- @mp_sqrt@ --- *
878 *
879 * Arguments: @mp *d@ = pointer to destination integer
880 * @mp *a@ = (nonnegative) integer to take square root of
881 *
882 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
883 *
884 * Use: Computes integer square roots.
885 *
886 * The current implementation isn't very good: it uses the
887 * Newton-Raphson method to find an approximation to %$a$%. If
888 * there's any demand for a better version, I'll write one.
889 */
890
891 extern mp *mp_sqrt(mp */*d*/, mp */*a*/);
892
893 /* --- @mp_gcd@ --- *
894 *
895 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
896 * @mp *a, *b@ = sources (must be nonzero)
897 *
898 * Returns: ---
899 *
900 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
901 * @ax + by = gcd(a, b)@. This is useful for computing modular
902 * inverses. Neither @a@ nor @b@ may be zero.
903 */
904
905 extern void mp_gcd(mp **/*gcd*/, mp **/*xx*/, mp **/*yy*/,
906 mp */*a*/, mp */*b*/);
907
908 /* --- @mp_jacobi@ --- *
909 *
910 * Arguments: @mp *a@ = an integer less than @n@
911 * @mp *n@ = an odd integer
912 *
913 * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
914 *
915 * Use: Computes the Jacobi symbol. If @n@ is prime, this is the
916 * Legendre symbol and is equal to 1 if and only if @a@ is a
917 * quadratic residue mod @n@. The result is zero if and only if
918 * @a@ and @n@ have a common factor greater than one.
919 */
920
921 extern int mp_jacobi(mp */*a*/, mp */*n*/);
922
923 /* --- @mp_modsqrt@ --- *
924 *
925 * Arguments: @mp *d@ = destination integer
926 * @mp *a@ = source integer
927 * @mp *p@ = modulus (must be prime)
928 *
929 * Returns: If %$a$% is a quadratic residue, a square root of %$a$%; else
930 * a null pointer.
931 *
932 * Use: Returns an integer %$x$% such that %$x^2 \equiv a \pmod{p}$%,
933 * if one exists; else a null pointer. This function will not
934 * work if %$p$% is composite: you must factor the modulus, take
935 * a square root mod each factor, and recombine the results
936 * using the Chinese Remainder Theorem.
937 */
938
939 extern mp *mp_modsqrt(mp */*d*/, mp */*a*/, mp */*p*/);
940
941 /*----- Test harness support ----------------------------------------------*/
942
943 #include <mLib/testrig.h>
944
945 #ifndef CATACOMB_MPTEXT_H
946 # include "mptext.h"
947 #endif
948
949 extern const test_type type_mp;
950
951 /*----- That's all, folks -------------------------------------------------*/
952
953 #ifdef __cplusplus
954 }
955 #endif
956
957 #endif