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