3 * $Id: mp.h,v 1.11 2001/04/03 19:36:05 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.11 2001/04/03 19:36:05 mdw
34 * Add some simple bitwise operations so that Perl can use them.
36 * Revision 1.10 2000/10/08 12:03:16 mdw
37 * Provide @mp_eq@ and @MP_EQ@ for rapidly testing equality of two
40 * Revision 1.9 2000/07/29 17:03:31 mdw
41 * Add support for left-to-right bitscanning, for use in modular
44 * Revision 1.8 2000/06/22 19:02:01 mdw
47 * Revision 1.7 2000/06/17 11:45:09 mdw
48 * Major memory management overhaul. Added arena support. Use the secure
49 * arena for secret integers. Replace and improve the MP management macros
50 * (e.g., replace MP_MODIFY by MP_DEST).
52 * Revision 1.6 1999/12/10 23:19:46 mdw
53 * Minor bugfixes. New interface for suggested destinations.
55 * Revision 1.5 1999/11/22 20:50:37 mdw
56 * Add support for computing Jacobi symbols.
58 * Revision 1.4 1999/11/21 22:13:02 mdw
59 * Add mp version of MPX_BITS.
61 * Revision 1.3 1999/11/19 13:19:14 mdw
62 * Fix const annotation.
64 * Revision 1.2 1999/11/17 18:02:16 mdw
65 * New multiprecision integer arithmetic suite.
76 /*----- Header files ------------------------------------------------------*/
83 #ifndef CATACOMB_MPW_H
87 #ifndef CATACOMB_ARENA_H
91 #ifndef CATACOMB_MPARENA_H
95 #ifndef CATACOMB_MPX_H
99 /*----- Data structures ---------------------------------------------------*/
113 #define MP_DESTROYED 16u
115 /*----- Useful constants --------------------------------------------------*/
117 extern mp mp_const
[];
119 #define MP_ZERO (&mp_const[0])
120 #define MP_ONE (&mp_const[1])
121 #define MP_TWO (&mp_const[2])
122 #define MP_THREE (&mp_const[3])
123 #define MP_FOUR (&mp_const[4])
124 #define MP_FIVE (&mp_const[5])
125 #define MP_TEN (&mp_const[6])
126 #define MP_256 (&mp_const[7])
127 #define MP_MONE (&mp_const[8])
129 #define MP_NEW ((mp *)0)
130 #define MP_NEWSEC (&mp_const[9])
132 /*----- Trivial macros ----------------------------------------------------*/
134 /* --- @MP_LEN@ --- *
136 * Arguments: @mp *m@ = pointer to a multiprecision integer
138 * Returns: Length of the integer, in words.
141 #define MP_LEN(m) ((m)->vl - ((m)->v))
143 /*----- Memory management and reference counting --------------------------*/
145 /* --- @mp_new@ --- *
147 * Arguments: @size_t sz@ = size of vector required
148 * @unsigned f@ = flags to set
150 * Returns: Pointer to a new MP structure.
152 * Use: Allocates a new multiprecision integer. The data space is
153 * allocated from either the standard global or secret arena,
154 * depending on the initial flags requested.
157 extern mp
*mp_new(size_t /*sz*/, unsigned /*f*/);
159 /* --- @mp_create@ --- *
161 * Arguments: @size_t sz@ = size of vector required
163 * Returns: Pointer to pristine new MP structure with enough memory
166 * Use: Creates a new multiprecision integer with indeterminate
167 * contents. The integer has a single reference.
170 extern mp
*mp_create(size_t /*sz*/);
172 /* --- @mp_createsecure@ --- *
174 * Arguments: @size_t sz@ = size of vector required
176 * Returns: Pointer to pristine new MP structure with enough memory
179 * Use: Creates a new multiprecision integer with indeterminate
180 * contents. The integer has a single reference. The integer's
181 * data space is allocated from the secure arena. Its burn flag
185 extern mp
*mp_createsecure(size_t /*sz*/);
187 /* --- @mp_build@ --- *
189 * Arguments: @mp *m@ = pointer to an MP block to fill in
190 * @mpw *v@ = pointer to a word array
191 * @mpw *vl@ = pointer just past end of array
195 * Use: Creates a multiprecision integer representing some smallish
196 * number. You must provide storage for the number and dispose
197 * of it when you've finished with it. The number is marked as
198 * constant while it exists.
201 extern void mp_build(mp */
*m*/
, mpw */
*v*/
, mpw */
*vl*/
);
203 /* --- @mp_destroy@ --- *
205 * Arguments: @mp *m@ = pointer to a multiprecision integer
209 * Use: Destroys a multiprecision integer. The reference count isn't
210 * checked. Don't use this function if you don't know what
211 * you're doing: use @mp_drop@ instead.
214 extern void mp_destroy(mp */
*m*/
);
216 /* --- @mp_copy@ --- *
218 * Arguments: @mp *m@ = pointer to a multiprecision integer
220 * Returns: A copy of the given multiprecision integer.
222 * Use: Copies the given integer. In fact you just get another
223 * reference to the same old one again.
226 extern mp
*mp_copy(mp */
*m*/
);
228 #define MP_COPY(m) ((m)->ref++, (m))
230 /* --- @mp_drop@ --- *
232 * Arguments: @mp *m@ = pointer to a multiprecision integer
236 * Use: Drops a reference to an integer which isn't wanted any more.
237 * If there are no more references, the integer is destroyed.
240 extern void mp_drop(mp */
*m*/
);
242 #define MP_DROP(m) do { \
245 if (_mm->ref == 0 && !(_mm->f & MP_CONST)) \
249 /* --- @mp_split@ --- *
251 * Arguments: @mp *m@ = pointer to a multiprecision integer
253 * Returns: A reference to the same integer, possibly with a different
256 * Use: Splits off a modifiable version of the integer referred to.
259 extern mp
*mp_split(mp */
*m*/
);
261 #define MP_SPLIT(m) do { \
263 if ((_m->f & MP_CONST) || _m->ref > 1) { \
264 size_t _len = MP_LEN(_m); \
265 mp *_mm = mp_new(_len, _m->f); \
266 if (!(_m->f & MP_UNDEF)) \
267 memcpy(_mm->v, _m->v, MPWS(_len)); \
274 /* --- @mp_resize@ --- *
276 * Arguments: @mp *m@ = pointer to a multiprecision integer
277 * @size_t sz@ = new size
281 * Use: Resizes the vector containing the integer's digits. The new
282 * size must be at least as large as the current integer's
283 * length. This isn't really intended for client use.
286 extern void mp_resize(mp */
*m*/
, size_t /*sz*/);
288 #define MP_RESIZE(m, ssz) do { \
290 size_t _sz = (ssz); \
291 mparena *_a = (_m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; \
293 size_t _len = MP_LEN(_m); \
294 assert(((void)"can't make size less than length", _sz >= _len)); \
295 _v = mpalloc(_a, _sz); \
296 if (!(_m->f & MP_UNDEF)) \
297 memcpy(_v, _m->v, MPWS(_len)); \
298 if (_m->f & MP_BURN) \
299 memset(_m->v, 0, MPWS(_m->sz)); \
300 mpfree(_m->a, _m->v); \
303 _m->vl = _v + _len; \
306 /* --- @mp_ensure@ --- *
308 * Arguments: @mp *m@ = pointer to a multiprecision integer
309 * @size_t sz@ = required size
313 * Use: Ensures that the integer has enough space for @sz@ digits.
314 * The value is not changed.
317 extern void mp_ensure(mp */
*m*/
, size_t /*sz*/);
319 #define MP_ENSURE(m, ssz) do { \
321 size_t _ssz = (ssz); \
322 size_t _len = MP_LEN(_m); \
323 if (_ssz >= _len) { \
325 mp_resize(_m, _ssz); \
326 if (!(_m->f & MP_UNDEF) && _ssz > _len) \
327 memset(_m->vl, 0, MPWS(_ssz - _len)); \
328 _m->vl = _m->v + _ssz; \
332 /* --- @mp_dest@ --- *
334 * Arguments: @mp *m@ = a suggested destination integer
335 * @size_t sz@ = size required for result, in digits
336 * @unsigned f@ = various flags
338 * Returns: A pointer to an appropriate destination.
340 * Use: Converts a suggested destination into a real destination with
341 * the required properties. If the real destination is @d@,
342 * then the following properties will hold:
344 * * @d@ will have exactly one reference.
346 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
347 * change, unless @f@ has the @MP_UNDEF@ flag set.
349 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
350 * entry is equal to the sum of the counts of @d@ and @m@ on
353 * * The size of @d@ will be at least @sz@.
355 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
356 * allocated from @MPARENA_SECURE@.
358 * Understanding this function is crucial to using Catacomb's
359 * multiprecision integer library effectively.
362 extern mp
*mp_dest(mp */
*m*/
, size_t /*sz*/, unsigned /*f*/);
364 #define MP_DEST(m, ssz, f) do { \
366 size_t _ssz = (ssz); \
368 _m = mp_dest(_m, _ssz, _f); \
372 /*----- Size manipulation -------------------------------------------------*/
374 /* --- @mp_shrink@ --- *
376 * Arguments: @mp *m@ = pointer to a multiprecision integer
380 * Use: Reduces the recorded length of an integer. This doesn't
381 * reduce the amount of memory used, although it can improve
382 * performance a bit. To reduce memory, use @mp_minimize@
383 * instead. This can't change the value of an integer, and is
384 * therefore safe to use even when there are multiple
388 extern void mp_shrink(mp */
*m*/
);
390 #define MP_SHRINK(m) do { \
392 MPX_SHRINK(_mm->v, _mm->vl); \
397 /* --- @mp_minimize@ --- *
399 * Arguments: @mp *m@ = pointer to a multiprecision integer
403 * Use: Reduces the amount of memory an integer uses. It's best to
404 * do this to numbers which aren't going to change in the
408 extern void mp_minimize(mp */
*m*/
);
410 /*----- Bit scanning ------------------------------------------------------*/
412 #ifndef CATACOMB_MPSCAN_H
416 /* --- @mp_scan@ --- *
418 * Arguments: @mpscan *sc@ = pointer to bitscanner block
419 * @const mp *m@ = pointer to a multiprecision integer
423 * Use: Initializes a bitscanner on a multiprecision integer.
426 extern void mp_scan(mpscan */
*sc*/
, const mp */
*m*/
);
428 #define MP_SCAN(sc, m) do { \
429 const mp *_mm = (m); \
430 mpscan *_sc = (sc); \
431 MPSCAN_INITX(_sc, _mm->v, _mm->vl); \
434 /* --- @mp_rscan@ --- *
436 * Arguments: @mpscan *sc@ = pointer to bitscanner block
437 * @const mp *m@ = pointer to a multiprecision integer
441 * Use: Initializes a reverse bitscanner on a multiprecision
445 extern void mp_rscan(mpscan */
*sc*/
, const mp */
*m*/
);
447 #define MP_RSCAN(sc, m) do { \
448 const mp *_mm = (m); \
449 mpscan *_sc = (sc); \
450 MPSCAN_RINITX(_sc, _mm->v, _mm->vl); \
453 /* --- Other bitscanning aliases --- */
455 #define mp_step mpscan_step
456 #define mp_bit mpscan_bit
457 #define mp_rstep mpscan_rstep
458 #define mp_rbit mpscan_rbit
460 #define MP_STEP MPSCAN_STEP
461 #define MP_BIT MPSCAN_BIT
462 #define MP_RSTEP MPSCAN_RSTEP
463 #define MP_RBIT MPSCAN_RBIT
465 /*----- Loading and storing -----------------------------------------------*/
467 /* --- @mp_octets@ --- *
469 * Arguments: @const mp *m@ = a multiprecision integer
471 * Returns: The number of octets required to represent @m@.
473 * Use: Calculates the external storage required for a multiprecision
477 extern size_t mp_octets(const mp */
*m*/
);
479 /* --- @mp_bits@ --- *
481 * Arguments: @const mp *m@ = a multiprecision integer
483 * Returns: The number of bits required to represent @m@.
485 * Use: Calculates the external storage required for a multiprecision
489 extern unsigned long mp_bits(const mp */
*m*/
);
491 /* --- @mp_loadl@ --- *
493 * Arguments: @mp *d@ = destination
494 * @const void *pv@ = pointer to source data
495 * @size_t sz@ = size of the source data
497 * Returns: Resulting multiprecision number.
499 * Use: Loads a multiprecision number from an array of octets. The
500 * first byte in the array is the least significant. More
501 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
502 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
505 extern mp
*mp_loadl(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
507 /* --- @mp_storel@ --- *
509 * Arguments: @const mp *m@ = source
510 * @void *pv@ = pointer to output array
511 * @size_t sz@ = size of the output array
515 * Use: Stores a multiprecision number in an array of octets. The
516 * first byte in the array is the least significant. If the
517 * array is too small to represent the number, high-order bits
518 * are truncated; if the array is too large, high order bytes
519 * are filled with zeros. More formally, if the number is
520 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
521 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
524 extern void mp_storel(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
526 /* --- @mp_loadb@ --- *
528 * Arguments: @mp *d@ = destination
529 * @const void *pv@ = pointer to source data
530 * @size_t sz@ = size of the source data
532 * Returns: Resulting multiprecision number.
534 * Use: Loads a multiprecision number from an array of octets. The
535 * last byte in the array is the least significant. More
536 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
537 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
540 extern mp
*mp_loadb(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
542 /* --- @mp_storeb@ --- *
544 * Arguments: @const mp *m@ = source
545 * @void *pv@ = pointer to output array
546 * @size_t sz@ = size of the output array
550 * Use: Stores a multiprecision number in an array of octets. The
551 * last byte in the array is the least significant. If the
552 * array is too small to represent the number, high-order bits
553 * are truncated; if the array is too large, high order bytes
554 * are filled with zeros. More formally, if the number is
555 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
556 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
559 extern void mp_storeb(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
561 /*----- Simple arithmetic -------------------------------------------------*/
565 * Arguments: @mp *d@ = destination
568 * Returns: Result, @a@ converted to two's complement notation.
571 extern mp
*mp_2c(mp */
*d*/
, mp */
*a*/
);
575 * Arguments: @mp *d@ = destination
578 * Returns: Result, @a@ converted to the native signed-magnitude
582 extern mp
*mp_sm(mp */
*d*/
, mp */
*a*/
);
584 /* --- @mp_lsl@ --- *
586 * Arguments: @mp *d@ = destination
588 * @size_t n@ = number of bits to move
590 * Returns: Result, @a@ shifted left by @n@.
593 extern mp
*mp_lsl(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
595 /* --- @mp_lsr@ --- *
597 * Arguments: @mp *d@ = destination
599 * @size_t n@ = number of bits to move
601 * Returns: Result, @a@ shifted left by @n@.
604 extern mp
*mp_lsr(mp */
*d*/
, mp */
*a*/
, size_t /*n*/);
608 * Arguments: @const mp *a, *b@ = two numbers
610 * Returns: Nonzero if the numbers are equal.
613 extern int mp_eq(const mp */
*a*/
, const mp */
*b*/
);
615 #define MP_EQ(a, b) \
616 ((((a)->f ^ (b)->f) & MP_NEG) == 0 && \
617 mpx_ueq((a)->v, (a)->vl, (b)->v, (b)->vl))
619 /* --- @mp_cmp@ --- *
621 * Arguments: @const mp *a, *b@ = two numbers
623 * Returns: Less than, equal to or greater than zero, according to
624 * whether @a@ is less than, equal to or greater than @b@.
627 extern int mp_cmp(const mp */
*a*/
, const mp */
*b*/
);
629 #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
631 /* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- *
633 * Arguments: @mp *d@ = destination
634 * @mp *a, *b@ = sources
636 * Returns: The result of the obvious bitwise operation.
639 extern mp
*mp_and(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
640 extern mp
*mp_or(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
641 extern mp
*mp_xor(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
642 extern mp
*mp_not(mp */
*d*/
, mp */
*a*/
);
644 /* --- @mp_add@ --- *
646 * Arguments: @mp *d@ = destination
647 * @mp *a, *b@ = sources
649 * Returns: Result, @a@ added to @b@.
652 extern mp
*mp_add(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
654 /* --- @mp_sub@ --- *
656 * Arguments: @mp *d@ = destination
657 * @mp *a, *b@ = sources
659 * Returns: Result, @b@ subtracted from @a@.
662 extern mp
*mp_sub(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
664 /* --- @mp_mul@ --- *
666 * Arguments: @mp *d@ = destination
667 * @mp *a, *b@ = sources
669 * Returns: Result, @a@ multiplied by @b@.
672 extern mp
*mp_mul(mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
674 /* --- @mp_sqr@ --- *
676 * Arguments: @mp *d@ = destination
679 * Returns: Result, @a@ squared.
682 extern mp
*mp_sqr(mp */
*d*/
, mp */
*a*/
);
684 /* --- @mp_div@ --- *
686 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
687 * @mp *a, *b@ = sources
689 * Use: Calculates the quotient and remainder when @a@ is divided by
693 extern void mp_div(mp
**/
*qq*/
, mp
**/
*rr*/
, mp */
*a*/
, mp */
*b*/
);
695 /* --- @mp_odd@ --- *
697 * Arguments: @mp *d@ = pointer to destination integer
698 * @mp *m@ = pointer to source integer
699 * @size_t *s@ = where to store the power of 2
701 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
703 * Use: Computes a power of two and an odd integer which, when
704 * multiplied, give a specified result. This sort of thing is
705 * useful in number theory quite often.
708 extern mp
*mp_odd(mp */
*d*/
, mp */
*m*/
, size_t */
*s*/
);
710 /*----- More advanced algorithms ------------------------------------------*/
712 /* --- @mp_sqrt@ --- *
714 * Arguments: @mp *d@ = pointer to destination integer
715 * @mp *a@ = (nonnegative) integer to take square root of
717 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
719 * Use: Computes integer square roots.
721 * The current implementation isn't very good: it uses the
722 * Newton-Raphson method to find an approximation to %$a$%. If
723 * there's any demand for a better version, I'll write one.
726 extern mp
*mp_sqrt(mp */
*d*/
, mp */
*a*/
);
728 /* --- @mp_gcd@ --- *
730 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
731 * @mp *a, *b@ = sources (must be nonzero)
735 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
736 * @ax + by = gcd(a, b)@. This is useful for computing modular
737 * inverses. Neither @a@ nor @b@ may be zero.
740 extern void mp_gcd(mp
**/
*gcd*/
, mp
**/
*xx*/
, mp
**/
*yy*/
,
741 mp */
*a*/
, mp */
*b*/
);
743 /* --- @mp_jacobi@ --- *
745 * Arguments: @mp *a@ = an integer less than @n@
746 * @mp *n@ = an odd integer
748 * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
750 * Use: Computes the Jacobi symbol. If @n@ is prime, this is the
751 * Legendre symbol and is equal to 1 if and only if @a@ is a
752 * quadratic residue mod @n@. The result is zero if and only if
753 * @a@ and @n@ have a common factor greater than one.
756 extern int mp_jacobi(mp */
*a*/
, mp */
*n*/
);
758 /* --- @mp_modsqrt@ --- *
760 * Arguments: @mp *d@ = destination integer
761 * @mp *a@ = source integer
762 * @mp *p@ = modulus (must be prime)
764 * Returns: If %$a$% is a quadratic residue, a square root of %$a$%; else
767 * Use: Returns an integer %$x$% such that %$x^2 \equiv a \pmod{p}$%,
768 * if one exists; else a null pointer. This function will not
769 * work if %$p$% is composite: you must factor the modulus, take
770 * a square root mod each factor, and recombine the results
771 * using the Chinese Remainder Theorem.
774 extern mp
*mp_modsqrt(mp */
*d*/
, mp */
*a*/
, mp */
*p*/
);
776 /*----- Test harness support ----------------------------------------------*/
778 #include <mLib/testrig.h>
780 #ifndef CATACOMB_MPTEXT_H
784 extern const test_type type_mp
;
786 /*----- That's all, folks -------------------------------------------------*/