3 * $Id: mp.h,v 1.15 2002/10/15 19:18:31 mdw Exp $
5 * Simple multiprecision arithmetic
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.15 2002/10/15 19:18:31 mdw
34 * New operation to negate numbers.
36 * Revision 1.14 2002/10/15 00:19:40 mdw
37 * Bit setting and clearing functions.
39 * Revision 1.13 2002/10/06 22:52:50 mdw
40 * Pile of changes for supporting two's complement properly.
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.
46 * Revision 1.11 2001/04/03 19:36:05 mdw
47 * Add some simple bitwise operations so that Perl can use them.
49 * Revision 1.10 2000/10/08 12:03:16 mdw
50 * Provide @mp_eq@ and @MP_EQ@ for rapidly testing equality of two
53 * Revision 1.9 2000/07/29 17:03:31 mdw
54 * Add support for left-to-right bitscanning, for use in modular
57 * Revision 1.8 2000/06/22 19:02:01 mdw
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).
65 * Revision 1.6 1999/12/10 23:19:46 mdw
66 * Minor bugfixes. New interface for suggested destinations.
68 * Revision 1.5 1999/11/22 20:50:37 mdw
69 * Add support for computing Jacobi symbols.
71 * Revision 1.4 1999/11/21 22:13:02 mdw
72 * Add mp version of MPX_BITS.
74 * Revision 1.3 1999/11/19 13:19:14 mdw
75 * Fix const annotation.
77 * Revision 1.2 1999/11/17 18:02:16 mdw
78 * New multiprecision integer arithmetic suite.
89 /*----- Header files ------------------------------------------------------*/
96 #ifndef CATACOMB_MPW_H
100 #ifndef CATACOMB_ARENA_H
104 #ifndef CATACOMB_MPARENA_H
105 # include "mparena.h"
108 #ifndef CATACOMB_MPX_H
112 /*----- Data structures ---------------------------------------------------*/
114 /* --- A multiprecision integer --- */
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 */
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 */
130 /* --- A factor for simultaneous exponentation --- *
132 * Used by the Montgomery and Barrett exponentiators.
135 typedef struct mp_expfactor
{
140 /*----- Useful constants --------------------------------------------------*/
142 extern mp mp_const
[];
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])
154 #define MP_NEW ((mp *)0)
155 #define MP_NEWSEC (&mp_const[9])
157 /*----- Trivial macros ----------------------------------------------------*/
159 /* --- @MP_LEN@ --- *
161 * Arguments: @mp *m@ = pointer to a multiprecision integer
163 * Returns: Length of the integer, in words.
166 #define MP_LEN(m) ((m)->vl - ((m)->v))
168 /*----- Memory management and reference counting --------------------------*/
170 /* --- @mp_new@ --- *
172 * Arguments: @size_t sz@ = size of vector required
173 * @unsigned f@ = flags to set
175 * Returns: Pointer to a new MP structure.
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.
182 extern mp
*mp_new(size_t /*sz*/, unsigned /*f*/);
184 /* --- @mp_create@ --- *
186 * Arguments: @size_t sz@ = size of vector required
188 * Returns: Pointer to pristine new MP structure with enough memory
191 * Use: Creates a new multiprecision integer with indeterminate
192 * contents. The integer has a single reference.
195 extern mp
*mp_create(size_t /*sz*/);
197 /* --- @mp_createsecure@ --- *
199 * Arguments: @size_t sz@ = size of vector required
201 * Returns: Pointer to pristine new MP structure with enough memory
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
210 extern mp
*mp_createsecure(size_t /*sz*/);
212 /* --- @mp_build@ --- *
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
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.
226 extern void mp_build(mp */
*m*/
, mpw */
*v*/
, mpw */
*vl*/
);
228 /* --- @mp_destroy@ --- *
230 * Arguments: @mp *m@ = pointer to a multiprecision integer
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.
239 extern void mp_destroy(mp */
*m*/
);
241 /* --- @mp_copy@ --- *
243 * Arguments: @mp *m@ = pointer to a multiprecision integer
245 * Returns: A copy of the given multiprecision integer.
247 * Use: Copies the given integer. In fact you just get another
248 * reference to the same old one again.
251 extern mp
*mp_copy(mp */
*m*/
);
253 #define MP_COPY(m) ((m)->ref++, (m))
255 /* --- @mp_drop@ --- *
257 * Arguments: @mp *m@ = pointer to a multiprecision integer
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.
265 extern void mp_drop(mp */
*m*/
);
267 #define MP_DROP(m) do { \
270 if (_mm->ref == 0 && !(_mm->f & MP_CONST)) \
274 /* --- @mp_split@ --- *
276 * Arguments: @mp *m@ = pointer to a multiprecision integer
278 * Returns: A reference to the same integer, possibly with a different
281 * Use: Splits off a modifiable version of the integer referred to.
284 extern mp
*mp_split(mp */
*m*/
);
286 #define MP_SPLIT(m) do { \
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)); \
299 /* --- @mp_resize@ --- *
301 * Arguments: @mp *m@ = pointer to a multiprecision integer
302 * @size_t sz@ = new size
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.
311 extern void mp_resize(mp */
*m*/
, size_t /*sz*/);
313 #define MP_RESIZE(m, ssz) do { \
315 size_t _sz = (ssz); \
316 mparena *_a = (_m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; \
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); \
328 _m->vl = _v + _len; \
331 /* --- @mp_ensure@ --- *
333 * Arguments: @mp *m@ = pointer to a multiprecision integer
334 * @size_t sz@ = required size
338 * Use: Ensures that the integer has enough space for @sz@ digits.
339 * The value is not changed.
342 extern void mp_ensure(mp */
*m*/
, size_t /*sz*/);
344 #define MP_ENSURE(m, ssz) do { \
346 size_t _ssz = (ssz); \
347 size_t _len = MP_LEN(_m); \
348 if (_ssz >= _len) { \
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; \
357 /* --- @mp_dest@ --- *
359 * Arguments: @mp *m@ = a suggested destination integer
360 * @size_t sz@ = size required for result, in digits
361 * @unsigned f@ = various flags
363 * Returns: A pointer to an appropriate destination.
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:
369 * * @d@ will have exactly one reference.
371 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
372 * change, unless @f@ has the @MP_UNDEF@ flag set.
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
378 * * The size of @d@ will be at least @sz@.
380 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
381 * allocated from @MPARENA_SECURE@.
383 * Understanding this function is crucial to using Catacomb's
384 * multiprecision integer library effectively.
387 extern mp
*mp_dest(mp */
*m*/
, size_t /*sz*/, unsigned /*f*/);
389 #define MP_DEST(m, ssz, f) do { \
391 size_t _ssz = (ssz); \
393 _m = mp_dest(_m, _ssz, _f); \
397 /*----- Size manipulation -------------------------------------------------*/
399 /* --- @mp_shrink@ --- *
401 * Arguments: @mp *m@ = pointer to a multiprecision integer
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
413 extern void mp_shrink(mp */
*m*/
);
415 #define MP_SHRINK(m) do { \
417 MPX_SHRINK(_mm->v, _mm->vl); \
422 /* --- @mp_minimize@ --- *
424 * Arguments: @mp *m@ = pointer to a multiprecision integer
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
433 extern void mp_minimize(mp */
*m*/
);
435 /*----- Bit scanning ------------------------------------------------------*/
437 #ifndef CATACOMB_MPSCAN_H
441 /* --- @mp_scan@ --- *
443 * Arguments: @mpscan *sc@ = pointer to bitscanner block
444 * @const mp *m@ = pointer to a multiprecision integer
448 * Use: Initializes a bitscanner on a multiprecision integer.
451 extern void mp_scan(mpscan */
*sc*/
, const mp */
*m*/
);
453 #define MP_SCAN(sc, m) do { \
454 const mp *_mm = (m); \
455 mpscan *_sc = (sc); \
456 MPSCAN_INITX(_sc, _mm->v, _mm->vl); \
459 /* --- @mp_rscan@ --- *
461 * Arguments: @mpscan *sc@ = pointer to bitscanner block
462 * @const mp *m@ = pointer to a multiprecision integer
466 * Use: Initializes a reverse bitscanner on a multiprecision
470 extern void mp_rscan(mpscan */
*sc*/
, const mp */
*m*/
);
472 #define MP_RSCAN(sc, m) do { \
473 const mp *_mm = (m); \
474 mpscan *_sc = (sc); \
475 MPSCAN_RINITX(_sc, _mm->v, _mm->vl); \
478 /* --- Other bitscanning aliases --- */
480 #define mp_step mpscan_step
481 #define mp_bit mpscan_bit
482 #define mp_rstep mpscan_rstep
483 #define mp_rbit mpscan_rbit
485 #define MP_STEP MPSCAN_STEP
486 #define MP_BIT MPSCAN_BIT
487 #define MP_RSTEP MPSCAN_RSTEP
488 #define MP_RBIT MPSCAN_RBIT
490 /*----- Loading and storing -----------------------------------------------*/
492 /* --- @mp_octets@ --- *
494 * Arguments: @const mp *m@ = a multiprecision integer
496 * Returns: The number of octets required to represent @m@.
498 * Use: Calculates the external storage required for a multiprecision
502 extern size_t mp_octets(const mp */
*m*/
);
504 /* --- @mp_octets2c@ --- *
506 * Arguments: @const mp *m@ = a multiprecision integer
508 * Returns: The number of octets required to represent @m@.
510 * Use: Calculates the external storage required for a multiprecision
511 * integer represented as two's complement.
514 extern size_t mp_octets2c(const mp */
*m*/
);
516 /* --- @mp_bits@ --- *
518 * Arguments: @const mp *m@ = a multiprecision integer
520 * Returns: The number of bits required to represent @m@.
522 * Use: Calculates the external storage required for a multiprecision
526 extern unsigned long mp_bits(const mp */
*m*/
);
528 /* --- @mp_loadl@ --- *
530 * Arguments: @mp *d@ = destination
531 * @const void *pv@ = pointer to source data
532 * @size_t sz@ = size of the source data
534 * Returns: Resulting multiprecision number.
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}$%.
542 extern mp
*mp_loadl(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
544 /* --- @mp_storel@ --- *
546 * Arguments: @const mp *m@ = source
547 * @void *pv@ = pointer to output array
548 * @size_t sz@ = size of the output array
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}$%.
561 extern void mp_storel(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
563 /* --- @mp_loadb@ --- *
565 * Arguments: @mp *d@ = destination
566 * @const void *pv@ = pointer to source data
567 * @size_t sz@ = size of the source data
569 * Returns: Resulting multiprecision number.
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}$%.
577 extern mp
*mp_loadb(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
579 /* --- @mp_storeb@ --- *
581 * Arguments: @const mp *m@ = source
582 * @void *pv@ = pointer to output array
583 * @size_t sz@ = size of the output array
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$%.
596 extern void mp_storeb(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
598 /* --- @mp_loadl2c@ --- *
600 * Arguments: @mp *d@ = destination
601 * @const void *pv@ = pointer to source data
602 * @size_t sz@ = size of the source data
604 * Returns: Resulting multiprecision number.
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
611 extern mp
*mp_loadl2c(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
613 /* --- @mp_storel2c@ --- *
615 * Arguments: @const mp *m@ = source
616 * @void *pv@ = pointer to output array
617 * @size_t sz@ = size of the output array
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.
628 extern void mp_storel2c(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
630 /* --- @mp_loadb2c@ --- *
632 * Arguments: @mp *d@ = destination
633 * @const void *pv@ = pointer to source data
634 * @size_t sz@ = size of the source data
636 * Returns: Resulting multiprecision number.
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
643 extern mp
*mp_loadb2c(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
645 /* --- @mp_storeb2c@ --- *
647 * Arguments: @const mp *m@ = source
648 * @void *pv@ = pointer to output array
649 * @size_t sz@ = size of the output array
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.
660 extern void mp_storeb2c(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
662 /*----- Bit operations ----------------------------------------------------*/
664 /* --- @mp_not@ --- *
666 * Arguments: @mp *d@ = destination
669 * Returns: The bitwise complement of the source.
672 extern mp
*mp_not(mp */
*d*/
, mp */
*a*/
);
674 /* --- @mp_bitop@ --- *
676 * Arguments: @mp *d@ = destination
677 * @mp *a, *b@ = sources
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:
689 #define MP_BITDECL(string) \
690 extern mp *mp_bit##string(mp */*d*/, mp */*a*/, mp */*b*/);
691 MPX_DOBIN(MP_BITDECL
)
693 /* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
695 * Synonyms for the commonly-used functions.
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
704 /* --- @mp_testbit@ --- *
706 * Arguments: @mp *x@ = a large integer
707 * @unsigned long n@ = which bit to test
709 * Returns: Nonzero if the bit is set, zero if not.
712 extern int mp_testbit(mp */
*x*/
, unsigned long /*n*/);
714 /* --- @mp_setbit@, @mp_clearbit@ --- *
716 * Arguments: @mp *d@ = a destination
717 * @mp *x@ = a large integer
718 * @unsigned long n@ = which bit to modify
720 * Returns: The argument @x@, with the appropriate bit set or cleared.
723 extern mp
*mp_setbit(mp */
*d*/
, mp */
*x*/
, unsigned long /*n*/);
724 extern mp
*mp_clearbit(mp */
*d*/
, mp */
*x*/
, unsigned long /*n*/);
726 /* --- @mp_lsl@, @mp_lsr@ --- *
728 * Arguments: @mp *d@ = destination
730 * @size_t n@ = number of bits to move
732 * Returns: Result, @a@ shifted left or right by @n@.
735 extern mp
*mp_lsl(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
736 extern mp
*mp_lsr(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
738 /* --- @mp_not2c@ --- *
740 * Arguments: @mp *d@ = destination
743 * Returns: The sign-extended complement of the argument.
746 extern mp
*mp_not2c(mp */
*d*/
, mp */
*a*/
);
748 /* --- @mp_bitop2c@ --- *
750 * Arguments: @mp *d@ = destination
751 * @mp *a, *b@ = sources
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
763 #define MP_BIT2CDECL(string) \
764 extern mp *mp_bit##string##2c(mp */*d*/, mp */*a*/, mp */*b*/);
765 MPX_DOBIN(MP_BIT2CDECL
)
767 /* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
769 * Synonyms for the commonly-used functions.
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
778 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
780 * Arguments: @mp *d@ = destination
782 * @size_t n@ = number of bits to move
784 * Returns: Result, @a@ shifted left or right by @n@. Handles the
785 * pretence of sign-extension for negative numbers.
788 extern mp
*mp_lsl2c(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
789 extern mp
*mp_lsr2c(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
791 /* --- @mp_testbit2c@ --- *
793 * Arguments: @mp *x@ = a large integer
794 * @unsigned long n@ = which bit to test
796 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
797 * complement representation.
800 extern int mp_testbit2c(mp */
*x*/
, unsigned long /*n*/);
802 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
804 * Arguments: @mp *d@ = a destination
805 * @mp *x@ = a large integer
806 * @unsigned long n@ = which bit to modify
808 * Returns: The argument @x@, with the appropriate bit set or cleared.
809 * Fakes up two's complement representation.
812 extern mp
*mp_setbit2c(mp */
*d*/
, mp */
*x*/
, unsigned long /*n*/);
813 extern mp
*mp_clearbit2c(mp */
*d*/
, mp */
*x*/
, unsigned long /*n*/);
815 /*----- Comparisons -------------------------------------------------------*/
819 * Arguments: @const mp *a, *b@ = two numbers
821 * Returns: Nonzero if the numbers are equal.
824 extern int mp_eq(const mp */
*a*/
, const mp */
*b*/
);
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))
830 /* --- @mp_cmp@ --- *
832 * Arguments: @const mp *a, *b@ = two numbers
834 * Returns: Less than, equal to or greater than zero, according to
835 * whether @a@ is less than, equal to or greater than @b@.
838 extern int mp_cmp(const mp */
*a*/
, const mp */
*b*/
);
840 #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
842 /*----- Arithmetic operations ---------------------------------------------*/
844 /* --- @mp_neg@ --- *
846 * Arguments: @mp *d@ = destination
849 * Returns: The negation of the argument.
851 * Use: Negates its argument.
854 extern mp
*mp_neg(mp */
*d*/
, mp */
*a*/
);
856 /* --- @mp_add@ --- *
858 * Arguments: @mp *d@ = destination
859 * @mp *a, *b@ = sources
861 * Returns: Result, @a@ added to @b@.
864 extern mp
*mp_add(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
866 /* --- @mp_sub@ --- *
868 * Arguments: @mp *d@ = destination
869 * @mp *a, *b@ = sources
871 * Returns: Result, @b@ subtracted from @a@.
874 extern mp
*mp_sub(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
876 /* --- @mp_mul@ --- *
878 * Arguments: @mp *d@ = destination
879 * @mp *a, *b@ = sources
881 * Returns: Result, @a@ multiplied by @b@.
884 extern mp
*mp_mul(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
886 /* --- @mp_sqr@ --- *
888 * Arguments: @mp *d@ = destination
891 * Returns: Result, @a@ squared.
894 extern mp
*mp_sqr(mp */
*d*/
, mp */
*a*/
);
896 /* --- @mp_div@ --- *
898 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
899 * @mp *a, *b@ = sources
901 * Use: Calculates the quotient and remainder when @a@ is divided by
905 extern void mp_div(mp
**/
*qq*/
, mp
**/
*rr*/
, mp */
*a*/
, mp */
*b*/
);
907 /* --- @mp_odd@ --- *
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
913 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
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.
920 extern mp
*mp_odd(mp */
*d*/
, mp */
*m*/
, size_t */
*s*/
);
922 /*----- More advanced algorithms ------------------------------------------*/
924 /* --- @mp_sqrt@ --- *
926 * Arguments: @mp *d@ = pointer to destination integer
927 * @mp *a@ = (nonnegative) integer to take square root of
929 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
931 * Use: Computes integer square roots.
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.
938 extern mp
*mp_sqrt(mp */
*d*/
, mp */
*a*/
);
940 /* --- @mp_gcd@ --- *
942 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
943 * @mp *a, *b@ = sources (must be nonzero)
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.
952 extern void mp_gcd(mp
**/
*gcd*/
, mp
**/
*xx*/
, mp
**/
*yy*/
,
953 mp */
*a*/
, mp */
*b*/
);
955 /* --- @mp_jacobi@ --- *
957 * Arguments: @mp *a@ = an integer less than @n@
958 * @mp *n@ = an odd integer
960 * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
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.
968 extern int mp_jacobi(mp */
*a*/
, mp */
*n*/
);
970 /* --- @mp_modsqrt@ --- *
972 * Arguments: @mp *d@ = destination integer
973 * @mp *a@ = source integer
974 * @mp *p@ = modulus (must be prime)
976 * Returns: If %$a$% is a quadratic residue, a square root of %$a$%; else
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.
986 extern mp
*mp_modsqrt(mp */
*d*/
, mp */
*a*/
, mp */
*p*/
);
988 /*----- Test harness support ----------------------------------------------*/
990 #include <mLib/testrig.h>
992 #ifndef CATACOMB_MPTEXT_H
996 extern const test_type type_mp
;
998 /*----- That's all, folks -------------------------------------------------*/