3 * $Id: mp.h,v 1.5 1999/11/22 20:50:37 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.5 1999/11/22 20:50:37 mdw
34 * Add support for computing Jacobi symbols.
36 * Revision 1.4 1999/11/21 22:13:02 mdw
37 * Add mp version of MPX_BITS.
39 * Revision 1.3 1999/11/19 13:19:14 mdw
40 * Fix const annotation.
42 * Revision 1.2 1999/11/17 18:02:16 mdw
43 * New multiprecision integer arithmetic suite.
54 /*----- Header files ------------------------------------------------------*/
69 /*----- Data structures ---------------------------------------------------*/
83 /*----- Useful constants --------------------------------------------------*/
87 #define MP_ZERO (&mp_const[0])
88 #define MP_ONE (&mp_const[1])
89 #define MP_TWO (&mp_const[2])
90 #define MP_THREE (&mp_const[3])
91 #define MP_FOUR (&mp_const[4])
92 #define MP_FIVE (&mp_const[5])
93 #define MP_TEN (&mp_const[6])
94 #define MP_MONE (&mp_const[7])
96 #define MP_NEW ((mp *)0)
98 /*----- Memory allocation hooks -------------------------------------------*/
101 # include "mparena.h"
104 /* --- @MP_ARENA@ --- *
106 * This selects where memory is allocated from. Tweak to use more fancy
107 * things like custom arenas.
111 # define MP_ARENA MPARENA_GLOBAL
114 /* --- @MP_ALLOC@ --- *
116 * Arguments: @size_t sz@ = size required
118 * Returns: Pointer to an allocated vector of the requested size.
120 * Use: Hook for vector allocation.
124 # define MP_ALLOC(sz) mpalloc(MP_ARENA, (sz))
127 /* --- @MP_FREE@ --- *
129 * Arguments: @mpw *v@ = pointer to vector
133 * Use: Hook for vector deallocation.
137 # define MP_FREE(v) mpfree(MP_ARENA, (v))
140 /*----- Paranoia management -----------------------------------------------*/
142 /* --- @mp_burn@ --- *
144 * Arguments: @mp *m@ = pointer to a multiprecision integer
148 * Use: Marks the integer as `burn-after-use'. When the integer's
149 * memory is deallocated, it is deleted so that traces can't
150 * remain in the swap file. In theory.
153 extern void mp_burn(mp */
*m*/
);
155 /*----- Trivial macros ----------------------------------------------------*/
157 /* --- @MP_LEN@ --- *
159 * Arguments: @mp *m@ = pointer to a multiprecision integer
161 * Returns: Length of the integer, in words.
164 #define MP_LEN(m) ((m)->vl - ((m)->v))
166 /*----- Memory management and reference counting --------------------------*/
168 /* --- @mp_create@ --- *
170 * Arguments: @size_t sz@ = size of vector required
172 * Returns: Pointer to pristine new MP structure with enough memory
175 * Use: Creates a new multiprecision integer with indeterminate
176 * contents. The integer has a single reference.
179 extern mp
*mp_create(size_t /*sz*/);
181 /* --- @mp_build@ --- *
183 * Arguments: @mp *m@ = pointer to an MP block to fill in
184 * @mpw *v@ = pointer to a word array
185 * @mpw *vl@ = pointer just past end of array
189 * Use: Creates a multiprecision integer representing some smallish
190 * number. You must provide storage for the number and dispose
191 * of it when you've finished with it. The number is marked as
192 * constant while it exists.
195 extern void mp_build(mp */
*m*/
, mpw */
*v*/
, mpw */
*vl*/
);
197 /* --- @mp_destroy@ --- *
199 * Arguments: @mp *m@ = pointer to a multiprecision integer
203 * Use: Destroys a multiprecision integer. The reference count isn't
204 * checked. Don't use this function if you don't know what
205 * you're doing: use @mp_drop@ instead.
208 extern void mp_destroy(mp */
*m*/
);
210 /* --- @mp_copy@ --- *
212 * Arguments: @mp *m@ = pointer to a multiprecision integer
214 * Returns: A copy of the given multiprecision integer.
216 * Use: Copies the given integer. In fact you just get another
217 * reference to the same old one again.
220 extern mp
*mp_copy(mp */
*m*/
);
222 #define MP_COPY(m) ((m)->ref++, (m))
224 /* --- @mp_drop@ --- *
226 * Arguments: @mp *m@ = pointer to a multiprecision integer
230 * Use: Drops a reference to an integer which isn't wanted any more.
231 * If there are no more references, the integer is destroyed.
234 extern void mp_drop(mp */
*m*/
);
236 #define MP_DROP(m) do { \
240 else if (!(_mm->f & MP_CONST)) \
244 /* --- @mp_split@ --- *
246 * Arguments: @mp *m@ = pointer to a multiprecision integer
248 * Returns: A reference to the same integer, possibly with a different
251 * Use: Splits off a modifiable version of the integer referred to.
254 extern mp
*mp_split(mp */
*m*/
);
256 #define MP_SPLIT(m) do { \
258 if ((_mm->f & MP_CONST) || _mm->ref != 1) { \
259 mp *_dd = mp_create(_mm->sz); \
260 _dd->vl = _dd->v + MP_LEN(_mm); \
261 _dd->f = _mm->f & (MP_NEG | MP_BURN); \
262 memcpy(_dd->v, _mm->v, MPWS(MP_LEN(_mm))); \
269 /* --- @mp_resize@ --- *
271 * Arguments: @mp *m@ = pointer to a multiprecision integer
272 * @size_t sz@ = new size
276 * Use: Resizes the vector containing the integer's digits. The new
277 * size must be at least as large as the current integer's
278 * length. The integer's length is increased and new digits are
279 * filled with zeroes. This isn't really intended for client
283 extern void mp_resize(mp */
*m*/
, size_t /*sz*/);
285 #define MP_RESIZE(m, ssz) do { \
287 size_t _sz = (ssz); \
288 size_t _len = MP_LEN(_m); \
289 mpw *_v = MP_ALLOC(_sz); \
290 memcpy(_v, _m->v, MPWS(_len)); \
291 if (_m->f & MP_BURN) \
292 memset(_m->v, 0, MPWS(_m->sz)); \
295 _m->vl = _v + _len; \
299 /* --- @mp_ensure@ --- *
301 * Arguments: @mp *m@ = pointer to a multiprecision integer
302 * @size_t sz@ = required size
306 * Use: Ensures that the integer has enough space for @sz@ digits.
307 * The value is not changed.
310 extern void mp_ensure(mp */
*m*/
, size_t /*sz*/);
312 #define MP_ENSURE(m, ssz) do { \
314 size_t _ssz = (ssz); \
315 size_t _len = MP_LEN(_mm); \
316 if (_ssz > _mm->sz) \
317 MP_RESIZE(_mm, _ssz); \
318 if (!(_mm->f & MP_UNDEF) && _ssz > _len) { \
319 memset(_mm->vl, 0, MPWS(_ssz - _len)); \
320 _mm->vl = _mm->v + _ssz; \
324 /* --- @mp_modify@ --- *
326 * Arguments: @mp *m@ = pointer to a multiprecision integer
327 * @size_t sz@ = size required
329 * Returns: Pointer to the integer (possibly different).
331 * Use: Prepares an integer to be overwritten. It's split off from
332 * other references to the same integer, and sufficient space is
336 extern mp
*mp_modify(mp */
*m*/
, size_t /*sz*/);
338 #define MP_MODIFY(m, sz) do { \
342 _m = mp_create(_rq); \
345 MP_ENSURE(_m, _rq); \
347 _m->vl = _m->v + _rq; \
351 /*----- Size manipulation -------------------------------------------------*/
353 /* --- @mp_shrink@ --- *
355 * Arguments: @mp *m@ = pointer to a multiprecision integer
359 * Use: Reduces the recorded length of an integer. This doesn't
360 * reduce the amount of memory used, although it can improve
361 * performance a bit. To reduce memory, use @mp_minimize@
362 * instead. This can't change the value of an integer, and is
363 * therefore safe to use even when there are multiple
367 extern void mp_shrink(mp */
*m*/
);
369 #define MP_SHRINK(m) do { \
371 MPX_SHRINK(_mm->v, _mm->vl); \
376 /* --- @mp_minimize@ --- *
378 * Arguments: @mp *m@ = pointer to a multiprecision integer
382 * Use: Reduces the amount of memory an integer uses. It's best to
383 * do this to numbers which aren't going to change in the
387 extern void mp_minimize(mp */
*m*/
);
389 /*----- Bit scanning ------------------------------------------------------*/
395 /* --- @mp_scan@ --- *
397 * Arguments: @mpscan *sc@ = pointer to bitscanner block
398 * @const mp *m@ = pointer to a multiprecision integer
402 * Use: Initializes a bitscanner on a multiprecision integer.
405 extern void mp_scan(mpscan */
*sc*/
, const mp */
*m*/
);
407 #define MP_SCAN(sc, m) do { \
408 const mp *_mm = (m); \
409 mpscan *_sc = (sc); \
410 MPSCAN_INITX(_sc, _mm->v, _mm->vl); \
413 /* --- Other bitscanning aliases --- */
415 #define mp_step mpscan_step
416 #define mp_bit mpscan_bit
418 #define MP_STEP MPSCAN_STEP
419 #define MP_BIT MPSCAN_BIT
421 /*----- Loading and storing -----------------------------------------------*/
423 /* --- @mp_octets@ --- *
425 * Arguments: @const mp *m@ = a multiprecision integer
427 * Returns: The number of octets required to represent @m@.
429 * Use: Calculates the external storage required for a multiprecision
433 extern size_t mp_octets(const mp */
*m*/
);
435 /* --- @mp_bits@ --- *
437 * Arguments: @const mp *m@ = a multiprecision integer
439 * Returns: The number of bits required to represent @m@.
441 * Use: Calculates the external storage required for a multiprecision
445 extern unsigned long mp_bits(const mp */
*m*/
);
447 /* --- @mp_loadl@ --- *
449 * Arguments: @mp *d@ = destination
450 * @const void *pv@ = pointer to source data
451 * @size_t sz@ = size of the source data
453 * Returns: Resulting multiprecision number.
455 * Use: Loads a multiprecision number from an array of octets. The
456 * first byte in the array is the least significant. More
457 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
458 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
461 extern mp
*mp_loadl(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
463 /* --- @mp_storel@ --- *
465 * Arguments: @const mp *m@ = source
466 * @void *pv@ = pointer to output array
467 * @size_t sz@ = size of the output array
471 * Use: Stores a multiprecision number in an array of octets. The
472 * first byte in the array is the least significant. If the
473 * array is too small to represent the number, high-order bits
474 * are truncated; if the array is too large, high order bytes
475 * are filled with zeros. More formally, if the number is
476 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
477 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
480 extern void mp_storel(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
482 /* --- @mp_loadb@ --- *
484 * Arguments: @mp *d@ = destination
485 * @const void *pv@ = pointer to source data
486 * @size_t sz@ = size of the source data
488 * Returns: Resulting multiprecision number.
490 * Use: Loads a multiprecision number from an array of octets. The
491 * last byte in the array is the least significant. More
492 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
493 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
496 extern mp
*mp_loadb(mp */
*d*/
, const void */
*pv*/
, size_t /*sz*/);
498 /* --- @mp_storeb@ --- *
500 * Arguments: @const mp *m@ = source
501 * @void *pv@ = pointer to output array
502 * @size_t sz@ = size of the output array
506 * Use: Stores a multiprecision number in an array of octets. The
507 * last byte in the array is the least significant. If the
508 * array is too small to represent the number, high-order bits
509 * are truncated; if the array is too large, high order bytes
510 * are filled with zeros. More formally, if the number is
511 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
512 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
515 extern void mp_storeb(const mp */
*m*/
, void */
*pv*/
, size_t /*sz*/);
517 /*----- Simple arithmetic -------------------------------------------------*/
521 * Arguments: @mp *d@ = destination
524 * Returns: Result, @a@ converted to two's complement notation.
527 extern mp
*mp_2c(mp */
*d*/
, mp */
*a*/
);
531 * Arguments: @mp *d@ = destination
534 * Returns: Result, @a@ converted to the native signed-magnitude
538 extern mp
*mp_sm(mp */
*d*/
, mp */
*a*/
);
540 /* --- @mp_lsl@ --- *
542 * Arguments: @mp *d@ = destination
543 * @const mp *a@ = source
544 * @size_t n@ = number of bits to move
546 * Returns: Result, @a@ shifted left by @n@.
549 extern mp
*mp_lsl(mp */
*d*/
, const mp */
*a*/
, size_t /*n*/);
551 /* --- @mp_lsr@ --- *
553 * Arguments: @mp *d@ = destination
554 * @const mp *a@ = source
555 * @size_t n@ = number of bits to move
557 * Returns: Result, @a@ shifted left by @n@.
560 extern mp
*mp_lsr(mp */
*d*/
, const mp */
*a*/
, size_t /*n*/);
562 /* --- @mp_cmp@ --- *
564 * Arguments: @const mp *a, *b@ = two numbers
566 * Returns: Less than, equal to or greater than zero, according to
567 * whether @a@ is less than, equal to or greater than @b@.
570 extern int mp_cmp(const mp */
*a*/
, const mp */
*b*/
);
572 #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
574 /* --- @mp_add@ --- *
576 * Arguments: @mp *d@ = destination
577 * @const mp *a, *b@ = sources
579 * Returns: Result, @a@ added to @b@.
582 extern mp
*mp_add(mp */
*d*/
, const mp */
*a*/
, const mp */
*b*/
);
584 /* --- @mp_sub@ --- *
586 * Arguments: @mp *d@ = destination
587 * @const mp *a, *b@ = sources
589 * Returns: Result, @b@ subtracted from @a@.
592 extern mp
*mp_sub(mp */
*d*/
, const mp */
*a*/
, const mp */
*b*/
);
594 /* --- @mp_mul@ --- *
596 * Arguments: @mp *d@ = destination
597 * @const mp *a, *b@ = sources
599 * Returns: Result, @a@ multiplied by @b@.
602 extern mp
*mp_mul(mp */
*d*/
, const mp */
*a*/
, const mp */
*b*/
);
604 /* --- @mp_sqr@ --- *
606 * Arguments: @mp *d@ = destination
607 * @const mp *a@ = source
609 * Returns: Result, @a@ squared.
612 extern mp
*mp_sqr(mp */
*d*/
, const mp */
*a*/
);
614 /* --- @mp_div@ --- *
616 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
617 * @const mp *a, *b@ = sources
619 * Use: Calculates the quotient and remainder when @a@ is divided by
623 extern void mp_div(mp
**/
*qq*/
, mp
**/
*rr*/
,
624 const mp */
*a*/
, const mp */
*b*/
);
626 /*----- More advanced algorithms ------------------------------------------*/
628 /* --- @mp_gcd@ --- *
630 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
631 * @mp *a, *b@ = sources (must be nonzero)
635 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
636 * @ax + by = gcd(a, b)@. This is useful for computing modular
637 * inverses. Neither @a@ nor @b@ may be zero. Note that,
638 * unlike @mp_div@ for example, it is not possible to specify
639 * explicit destinations -- new MPs are always allocated.
642 extern void mp_gcd(mp
**/
*gcd*/
, mp
**/
*xx*/
, mp
**/
*yy*/
,
643 mp */
*a*/
, mp */
*b*/
);
645 /* --- @mp_jacobi@ --- *
647 * Arguments: @mp *a@ = an integer less than @n@
648 * @mp *n@ = an odd integer
650 * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
652 * Use: Computes the Jacobi symbol. If @n@ is prime, this is the
653 * Legendre symbol and is equal to 1 if and only if @a@ is a
654 * quadratic residue mod @n@. The result is zero if and only if
655 * @a@ and @n@ have a common factor greater than one.
658 int mp_jacobi(mp */
*a*/
, mp */
*n*/
);
660 /*----- Test harness support ----------------------------------------------*/
662 #include <mLib/testrig.h>
668 extern const test_type type_mp
;
670 /*----- That's all, folks -------------------------------------------------*/