3 * $Id: mp.h,v 1.13 2002/10/06 22:52:50 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.13 2002/10/06 22:52:50 mdw
34 * Pile of changes for supporting two's complement properly.
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.
40 * Revision 1.11 2001/04/03 19:36:05 mdw
41 * Add some simple bitwise operations so that Perl can use them.
43 * Revision 1.10 2000/10/08 12:03:16 mdw
44 * Provide @mp_eq@ and @MP_EQ@ for rapidly testing equality of two
47 * Revision 1.9 2000/07/29 17:03:31 mdw
48 * Add support for left-to-right bitscanning, for use in modular
51 * Revision 1.8 2000/06/22 19:02:01 mdw
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).
59 * Revision 1.6 1999/12/10 23:19:46 mdw
60 * Minor bugfixes. New interface for suggested destinations.
62 * Revision 1.5 1999/11/22 20:50:37 mdw
63 * Add support for computing Jacobi symbols.
65 * Revision 1.4 1999/11/21 22:13:02 mdw
66 * Add mp version of MPX_BITS.
68 * Revision 1.3 1999/11/19 13:19:14 mdw
69 * Fix const annotation.
71 * Revision 1.2 1999/11/17 18:02:16 mdw
72 * New multiprecision integer arithmetic suite.
83 /*----- Header files ------------------------------------------------------*/
90 #ifndef CATACOMB_MPW_H
94 #ifndef CATACOMB_ARENA_H
98 #ifndef CATACOMB_MPARENA_H
102 #ifndef CATACOMB_MPX_H
106 /*----- Data structures ---------------------------------------------------*/
108 /* --- A multiprecision integer --- */
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 */
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 */
124 /* --- A factor for simultaneous exponentation --- *
126 * Used by the Montgomery and Barrett exponentiators.
129 typedef struct mp_expfactor
{
134 /*----- Useful constants --------------------------------------------------*/
136 extern mp mp_const
[];
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])
148 #define MP_NEW ((mp *)0)
149 #define MP_NEWSEC (&mp_const[9])
151 /*----- Trivial macros ----------------------------------------------------*/
153 /* --- @MP_LEN@ --- *
155 * Arguments: @mp *m@ = pointer to a multiprecision integer
157 * Returns: Length of the integer, in words.
160 #define MP_LEN(m) ((m)->vl - ((m)->v))
162 /*----- Memory management and reference counting --------------------------*/
164 /* --- @mp_new@ --- *
166 * Arguments: @size_t sz@ = size of vector required
167 * @unsigned f@ = flags to set
169 * Returns: Pointer to a new MP structure.
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.
176 extern mp
*mp_new(size_t /*sz*/, unsigned /*f*/);
178 /* --- @mp_create@ --- *
180 * Arguments: @size_t sz@ = size of vector required
182 * Returns: Pointer to pristine new MP structure with enough memory
185 * Use: Creates a new multiprecision integer with indeterminate
186 * contents. The integer has a single reference.
189 extern mp
*mp_create(size_t /*sz*/);
191 /* --- @mp_createsecure@ --- *
193 * Arguments: @size_t sz@ = size of vector required
195 * Returns: Pointer to pristine new MP structure with enough memory
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
204 extern mp
*mp_createsecure(size_t /*sz*/);
206 /* --- @mp_build@ --- *
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
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.
220 extern void mp_build(mp */
*m*/
, mpw */
*v*/
, mpw */
*vl*/
);
222 /* --- @mp_destroy@ --- *
224 * Arguments: @mp *m@ = pointer to a multiprecision integer
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.
233 extern void mp_destroy(mp */
*m*/
);
235 /* --- @mp_copy@ --- *
237 * Arguments: @mp *m@ = pointer to a multiprecision integer
239 * Returns: A copy of the given multiprecision integer.
241 * Use: Copies the given integer. In fact you just get another
242 * reference to the same old one again.
245 extern mp
*mp_copy(mp */
*m*/
);
247 #define MP_COPY(m) ((m)->ref++, (m))
249 /* --- @mp_drop@ --- *
251 * Arguments: @mp *m@ = pointer to a multiprecision integer
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.
259 extern void mp_drop(mp */
*m*/
);
261 #define MP_DROP(m) do { \
264 if (_mm->ref == 0 && !(_mm->f & MP_CONST)) \
268 /* --- @mp_split@ --- *
270 * Arguments: @mp *m@ = pointer to a multiprecision integer
272 * Returns: A reference to the same integer, possibly with a different
275 * Use: Splits off a modifiable version of the integer referred to.
278 extern mp
*mp_split(mp */
*m*/
);
280 #define MP_SPLIT(m) do { \
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)); \
293 /* --- @mp_resize@ --- *
295 * Arguments: @mp *m@ = pointer to a multiprecision integer
296 * @size_t sz@ = new size
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.
305 extern void mp_resize(mp */
*m*/
, size_t /*sz*/);
307 #define MP_RESIZE(m, ssz) do { \
309 size_t _sz = (ssz); \
310 mparena *_a = (_m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; \
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); \
322 _m->vl = _v + _len; \
325 /* --- @mp_ensure@ --- *
327 * Arguments: @mp *m@ = pointer to a multiprecision integer
328 * @size_t sz@ = required size
332 * Use: Ensures that the integer has enough space for @sz@ digits.
333 * The value is not changed.
336 extern void mp_ensure(mp */
*m*/
, size_t /*sz*/);
338 #define MP_ENSURE(m, ssz) do { \
340 size_t _ssz = (ssz); \
341 size_t _len = MP_LEN(_m); \
342 if (_ssz >= _len) { \
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; \
351 /* --- @mp_dest@ --- *
353 * Arguments: @mp *m@ = a suggested destination integer
354 * @size_t sz@ = size required for result, in digits
355 * @unsigned f@ = various flags
357 * Returns: A pointer to an appropriate destination.
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:
363 * * @d@ will have exactly one reference.
365 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
366 * change, unless @f@ has the @MP_UNDEF@ flag set.
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
372 * * The size of @d@ will be at least @sz@.
374 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
375 * allocated from @MPARENA_SECURE@.
377 * Understanding this function is crucial to using Catacomb's
378 * multiprecision integer library effectively.
381 extern mp
*mp_dest(mp */
*m*/
, size_t /*sz*/, unsigned /*f*/);
383 #define MP_DEST(m, ssz, f) do { \
385 size_t _ssz = (ssz); \
387 _m = mp_dest(_m, _ssz, _f); \
391 /*----- Size manipulation -------------------------------------------------*/
393 /* --- @mp_shrink@ --- *
395 * Arguments: @mp *m@ = pointer to a multiprecision integer
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
407 extern void mp_shrink(mp */
*m*/
);
409 #define MP_SHRINK(m) do { \
411 MPX_SHRINK(_mm->v, _mm->vl); \
416 /* --- @mp_minimize@ --- *
418 * Arguments: @mp *m@ = pointer to a multiprecision integer
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
427 extern void mp_minimize(mp */
*m*/
);
429 /*----- Bit scanning ------------------------------------------------------*/
431 #ifndef CATACOMB_MPSCAN_H
435 /* --- @mp_scan@ --- *
437 * Arguments: @mpscan *sc@ = pointer to bitscanner block
438 * @const mp *m@ = pointer to a multiprecision integer
442 * Use: Initializes a bitscanner on a multiprecision integer.
445 extern void mp_scan(mpscan */
*sc*/
, const mp */
*m*/
);
447 #define MP_SCAN(sc, m) do { \
448 const mp *_mm = (m); \
449 mpscan *_sc = (sc); \
450 MPSCAN_INITX(_sc, _mm->v, _mm->vl); \
453 /* --- @mp_rscan@ --- *
455 * Arguments: @mpscan *sc@ = pointer to bitscanner block
456 * @const mp *m@ = pointer to a multiprecision integer
460 * Use: Initializes a reverse bitscanner on a multiprecision
464 extern void mp_rscan(mpscan */
*sc*/
, const mp */
*m*/
);
466 #define MP_RSCAN(sc, m) do { \
467 const mp *_mm = (m); \
468 mpscan *_sc = (sc); \
469 MPSCAN_RINITX(_sc, _mm->v, _mm->vl); \
472 /* --- Other bitscanning aliases --- */
474 #define mp_step mpscan_step
475 #define mp_bit mpscan_bit
476 #define mp_rstep mpscan_rstep
477 #define mp_rbit mpscan_rbit
479 #define MP_STEP MPSCAN_STEP
480 #define MP_BIT MPSCAN_BIT
481 #define MP_RSTEP MPSCAN_RSTEP
482 #define MP_RBIT MPSCAN_RBIT
484 /*----- Loading and storing -----------------------------------------------*/
486 /* --- @mp_octets@ --- *
488 * Arguments: @const mp *m@ = a multiprecision integer
490 * Returns: The number of octets required to represent @m@.
492 * Use: Calculates the external storage required for a multiprecision
496 extern size_t mp_octets(const mp */
*m*/
);
498 /* --- @mp_octets2c@ --- *
500 * Arguments: @const mp *m@ = a multiprecision integer
502 * Returns: The number of octets required to represent @m@.
504 * Use: Calculates the external storage required for a multiprecision
505 * integer represented as two's complement.
508 extern size_t mp_octets2c(const mp */
*m*/
);
510 /* --- @mp_bits@ --- *
512 * Arguments: @const mp *m@ = a multiprecision integer
514 * Returns: The number of bits required to represent @m@.
516 * Use: Calculates the external storage required for a multiprecision
520 extern unsigned long mp_bits(const mp */
*m*/
);
522 /* --- @mp_loadl@ --- *
524 * Arguments: @mp *d@ = destination
525 * @const void *pv@ = pointer to source data
526 * @size_t sz@ = size of the source data
528 * Returns: Resulting multiprecision number.
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}$%.
536 extern mp
*mp_loadl(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
538 /* --- @mp_storel@ --- *
540 * Arguments: @const mp *m@ = source
541 * @void *pv@ = pointer to output array
542 * @size_t sz@ = size of the output array
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}$%.
555 extern void mp_storel(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
557 /* --- @mp_loadb@ --- *
559 * Arguments: @mp *d@ = destination
560 * @const void *pv@ = pointer to source data
561 * @size_t sz@ = size of the source data
563 * Returns: Resulting multiprecision number.
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}$%.
571 extern mp
*mp_loadb(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
573 /* --- @mp_storeb@ --- *
575 * Arguments: @const mp *m@ = source
576 * @void *pv@ = pointer to output array
577 * @size_t sz@ = size of the output array
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$%.
590 extern void mp_storeb(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
592 /* --- @mp_loadl2c@ --- *
594 * Arguments: @mp *d@ = destination
595 * @const void *pv@ = pointer to source data
596 * @size_t sz@ = size of the source data
598 * Returns: Resulting multiprecision number.
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
605 extern mp
*mp_loadl2c(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
607 /* --- @mp_storel2c@ --- *
609 * Arguments: @const mp *m@ = source
610 * @void *pv@ = pointer to output array
611 * @size_t sz@ = size of the output array
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.
622 extern void mp_storel2c(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
624 /* --- @mp_loadb2c@ --- *
626 * Arguments: @mp *d@ = destination
627 * @const void *pv@ = pointer to source data
628 * @size_t sz@ = size of the source data
630 * Returns: Resulting multiprecision number.
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
637 extern mp
*mp_loadb2c(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
639 /* --- @mp_storeb2c@ --- *
641 * Arguments: @const mp *m@ = source
642 * @void *pv@ = pointer to output array
643 * @size_t sz@ = size of the output array
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.
654 extern void mp_storeb2c(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
656 /*----- Simple arithmetic -------------------------------------------------*/
658 /* --- @mp_lsl@, @mp_lsr@ --- *
660 * Arguments: @mp *d@ = destination
662 * @size_t n@ = number of bits to move
664 * Returns: Result, @a@ shifted left or right by @n@.
667 extern mp
*mp_lsl(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
668 extern mp
*mp_lsr(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
670 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
672 * Arguments: @mp *d@ = destination
674 * @size_t n@ = number of bits to move
676 * Returns: Result, @a@ shifted left or right by @n@. Handles the
677 * pretence of sign-extension for negative numbers.
680 extern mp
*mp_lsl2c(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
681 extern mp
*mp_lsr2c(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
683 /* --- @mp_testbit@ --- *
685 * Arguments: @mp *x@ = a large integer
686 * @size_t n@ = which bit to test
688 * Returns: Nonzero if the bit is set, zero if not.
691 extern int mp_testbit(mp */
*x*/
, size_t /*n*/);
693 /* --- @mp_testbit2c@ --- *
695 * Arguments: @mp *x@ = a large integer
696 * @size_t n@ = which bit to test
698 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
699 * complement representation.
702 extern int mp_testbit2c(mp */
*x*/
, size_t /*n*/);
706 * Arguments: @const mp *a, *b@ = two numbers
708 * Returns: Nonzero if the numbers are equal.
711 extern int mp_eq(const mp */
*a*/
, const mp */
*b*/
);
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))
717 /* --- @mp_cmp@ --- *
719 * Arguments: @const mp *a, *b@ = two numbers
721 * Returns: Less than, equal to or greater than zero, according to
722 * whether @a@ is less than, equal to or greater than @b@.
725 extern int mp_cmp(const mp */
*a*/
, const mp */
*b*/
);
727 #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
729 /* --- @mp_bitop@ --- *
731 * Arguments: @mp *d@ = destination
732 * @mp *a, *b@ = sources
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:
744 #define MP_BITDECL(string) \
745 extern mp *mp_bit##string(mp */*d*/, mp */*a*/, mp */*b*/);
746 MPX_DOBIN(MP_BITDECL
)
748 /* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
750 * Synonyms for the commonly-used functions.
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
759 /* --- @mp_not@ --- *
761 * Arguments: @mp *d@ = destination
764 * Returns: The bitwise complement of the source.
767 extern mp
*mp_not(mp */
*d*/
, mp */
*a*/
);
769 /* --- @mp_bitop2c@ --- *
771 * Arguments: @mp *d@ = destination
772 * @mp *a, *b@ = sources
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
784 #define MP_BIT2CDECL(string) \
785 extern mp *mp_bit##string##2c(mp */*d*/, mp */*a*/, mp */*b*/);
786 MPX_DOBIN(MP_BIT2CDECL
)
788 /* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
790 * Synonyms for the commonly-used functions.
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
799 /* --- @mp_not2c@ --- *
801 * Arguments: @mp *d@ = destination
804 * Returns: The sign-extended complement of the argument.
807 extern mp
*mp_not2c(mp */
*d*/
, mp */
*a*/
);
809 /* --- @mp_add@ --- *
811 * Arguments: @mp *d@ = destination
812 * @mp *a, *b@ = sources
814 * Returns: Result, @a@ added to @b@.
817 extern mp
*mp_add(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
819 /* --- @mp_sub@ --- *
821 * Arguments: @mp *d@ = destination
822 * @mp *a, *b@ = sources
824 * Returns: Result, @b@ subtracted from @a@.
827 extern mp
*mp_sub(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
829 /* --- @mp_mul@ --- *
831 * Arguments: @mp *d@ = destination
832 * @mp *a, *b@ = sources
834 * Returns: Result, @a@ multiplied by @b@.
837 extern mp
*mp_mul(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
839 /* --- @mp_sqr@ --- *
841 * Arguments: @mp *d@ = destination
844 * Returns: Result, @a@ squared.
847 extern mp
*mp_sqr(mp */
*d*/
, mp */
*a*/
);
849 /* --- @mp_div@ --- *
851 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
852 * @mp *a, *b@ = sources
854 * Use: Calculates the quotient and remainder when @a@ is divided by
858 extern void mp_div(mp
**/
*qq*/
, mp
**/
*rr*/
, mp */
*a*/
, mp */
*b*/
);
860 /* --- @mp_odd@ --- *
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
866 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
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.
873 extern mp
*mp_odd(mp */
*d*/
, mp */
*m*/
, size_t */
*s*/
);
875 /*----- More advanced algorithms ------------------------------------------*/
877 /* --- @mp_sqrt@ --- *
879 * Arguments: @mp *d@ = pointer to destination integer
880 * @mp *a@ = (nonnegative) integer to take square root of
882 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
884 * Use: Computes integer square roots.
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.
891 extern mp
*mp_sqrt(mp */
*d*/
, mp */
*a*/
);
893 /* --- @mp_gcd@ --- *
895 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
896 * @mp *a, *b@ = sources (must be nonzero)
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.
905 extern void mp_gcd(mp
**/
*gcd*/
, mp
**/
*xx*/
, mp
**/
*yy*/
,
906 mp */
*a*/
, mp */
*b*/
);
908 /* --- @mp_jacobi@ --- *
910 * Arguments: @mp *a@ = an integer less than @n@
911 * @mp *n@ = an odd integer
913 * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
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.
921 extern int mp_jacobi(mp */
*a*/
, mp */
*n*/
);
923 /* --- @mp_modsqrt@ --- *
925 * Arguments: @mp *d@ = destination integer
926 * @mp *a@ = source integer
927 * @mp *p@ = modulus (must be prime)
929 * Returns: If %$a$% is a quadratic residue, a square root of %$a$%; else
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.
939 extern mp
*mp_modsqrt(mp */
*d*/
, mp */
*a*/
, mp */
*p*/
);
941 /*----- Test harness support ----------------------------------------------*/
943 #include <mLib/testrig.h>
945 #ifndef CATACOMB_MPTEXT_H
949 extern const test_type type_mp
;
951 /*----- That's all, folks -------------------------------------------------*/