Major memory management overhaul. Added arena support. Use the secure
[u/mdw/catacomb] / mp.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
d34decd2 3 * $Id: mp.h,v 1.7 2000/06/17 11:45:09 mdw Exp $
d03ab969 4 *
d3409d5e 5 * Simple multiprecision arithmetic
d03ab969 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 $
d34decd2 33 * Revision 1.7 2000/06/17 11:45:09 mdw
34 * Major memory management overhaul. Added arena support. Use the secure
35 * arena for secret integers. Replace and improve the MP management macros
36 * (e.g., replace MP_MODIFY by MP_DEST).
37 *
d9a8ae10 38 * Revision 1.6 1999/12/10 23:19:46 mdw
39 * Minor bugfixes. New interface for suggested destinations.
40 *
5b00a0ea 41 * Revision 1.5 1999/11/22 20:50:37 mdw
42 * Add support for computing Jacobi symbols.
43 *
24e1e862 44 * Revision 1.4 1999/11/21 22:13:02 mdw
45 * Add mp version of MPX_BITS.
46 *
a790733d 47 * Revision 1.3 1999/11/19 13:19:14 mdw
48 * Fix const annotation.
49 *
d3409d5e 50 * Revision 1.2 1999/11/17 18:02:16 mdw
51 * New multiprecision integer arithmetic suite.
d03ab969 52 *
53 */
54
d9a8ae10 55#ifndef CATACOMB_MP_H
56#define CATACOMB_MP_H
d03ab969 57
58#ifdef __cplusplus
59 extern "C" {
60#endif
61
62/*----- Header files ------------------------------------------------------*/
63
d3409d5e 64#include <assert.h>
d03ab969 65#include <string.h>
66
d3409d5e 67#include <mLib/sub.h>
68
d9a8ae10 69#ifndef CATACOMB_MPW_H
d3409d5e 70# include "mpw.h"
d03ab969 71#endif
72
d34decd2 73#ifndef CATACOMB_ARENA_H
74# include "arena.h"
75#endif
76
77#ifndef CATACOMB_MPARENA_H
78# include "mparena.h"
79#endif
80
d9a8ae10 81#ifndef CATACOMB_MPX_H
d03ab969 82# include "mpx.h"
83#endif
84
d03ab969 85/*----- Data structures ---------------------------------------------------*/
86
87typedef struct mp {
d3409d5e 88 mpw *v, *vl;
89 size_t sz;
d34decd2 90 mparena *a;
d3409d5e 91 unsigned f;
92 unsigned ref;
d03ab969 93} mp;
94
d3409d5e 95#define MP_NEG 1u
96#define MP_BURN 2u
97#define MP_CONST 4u
98#define MP_UNDEF 8u
d9a8ae10 99#define MP_DESTROYED 16u
d03ab969 100
d3409d5e 101/*----- Useful constants --------------------------------------------------*/
d03ab969 102
d3409d5e 103extern mp mp_const[];
d03ab969 104
d3409d5e 105#define MP_ZERO (&mp_const[0])
106#define MP_ONE (&mp_const[1])
107#define MP_TWO (&mp_const[2])
108#define MP_THREE (&mp_const[3])
109#define MP_FOUR (&mp_const[4])
110#define MP_FIVE (&mp_const[5])
111#define MP_TEN (&mp_const[6])
d34decd2 112#define MP_256 (&mp_const[7])
113#define MP_MONE (&mp_const[8])
d03ab969 114
d3409d5e 115#define MP_NEW ((mp *)0)
d34decd2 116#define MP_NEWSEC (&mp_const[9])
d03ab969 117
d34decd2 118/*----- Trivial macros ----------------------------------------------------*/
d03ab969 119
d34decd2 120/* --- @MP_LEN@ --- *
d03ab969 121 *
d34decd2 122 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 123 *
d34decd2 124 * Returns: Length of the integer, in words.
d03ab969 125 */
126
d34decd2 127#define MP_LEN(m) ((m)->vl - ((m)->v))
d03ab969 128
d34decd2 129/*----- Memory management and reference counting --------------------------*/
d3409d5e 130
d34decd2 131/* --- @mp_new@ --- *
d03ab969 132 *
d34decd2 133 * Arguments: @size_t sz@ = size of vector required
134 * @unsigned f@ = flags to set
d03ab969 135 *
d34decd2 136 * Returns: Pointer to a new MP structure.
d03ab969 137 *
d34decd2 138 * Use: Allocates a new multiprecision integer. The data space is
139 * allocated from either the standard global or secret arena,
140 * depending on the initial flags requested.
d03ab969 141 */
142
d34decd2 143extern mp *mp_new(size_t /*sz*/, unsigned /*f*/);
d03ab969 144
d34decd2 145/* --- @mp_create@ --- *
d03ab969 146 *
d34decd2 147 * Arguments: @size_t sz@ = size of vector required
d03ab969 148 *
d34decd2 149 * Returns: Pointer to pristine new MP structure with enough memory
150 * bolted onto it.
151 *
152 * Use: Creates a new multiprecision integer with indeterminate
153 * contents. The integer has a single reference.
d03ab969 154 */
155
d34decd2 156extern mp *mp_create(size_t /*sz*/);
d3409d5e 157
d34decd2 158/* --- @mp_createsecure@ --- *
d03ab969 159 *
d3409d5e 160 * Arguments: @size_t sz@ = size of vector required
d03ab969 161 *
d3409d5e 162 * Returns: Pointer to pristine new MP structure with enough memory
163 * bolted onto it.
d03ab969 164 *
d3409d5e 165 * Use: Creates a new multiprecision integer with indeterminate
d34decd2 166 * contents. The integer has a single reference. The integer's
167 * data space is allocated from the secure arena. Its burn flag
168 * is set.
d03ab969 169 */
170
d34decd2 171extern mp *mp_createsecure(size_t /*sz*/);
d03ab969 172
d3409d5e 173/* --- @mp_build@ --- *
d03ab969 174 *
d3409d5e 175 * Arguments: @mp *m@ = pointer to an MP block to fill in
176 * @mpw *v@ = pointer to a word array
177 * @mpw *vl@ = pointer just past end of array
d03ab969 178 *
179 * Returns: ---
180 *
d3409d5e 181 * Use: Creates a multiprecision integer representing some smallish
182 * number. You must provide storage for the number and dispose
183 * of it when you've finished with it. The number is marked as
184 * constant while it exists.
d03ab969 185 */
186
d3409d5e 187extern void mp_build(mp */*m*/, mpw */*v*/, mpw */*vl*/);
d03ab969 188
d3409d5e 189/* --- @mp_destroy@ --- *
d03ab969 190 *
d3409d5e 191 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 192 *
193 * Returns: ---
194 *
d3409d5e 195 * Use: Destroys a multiprecision integer. The reference count isn't
196 * checked. Don't use this function if you don't know what
197 * you're doing: use @mp_drop@ instead.
d03ab969 198 */
199
d3409d5e 200extern void mp_destroy(mp */*m*/);
d03ab969 201
d3409d5e 202/* --- @mp_copy@ --- *
d03ab969 203 *
d3409d5e 204 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 205 *
d3409d5e 206 * Returns: A copy of the given multiprecision integer.
207 *
208 * Use: Copies the given integer. In fact you just get another
209 * reference to the same old one again.
d03ab969 210 */
211
d3409d5e 212extern mp *mp_copy(mp */*m*/);
d03ab969 213
d3409d5e 214#define MP_COPY(m) ((m)->ref++, (m))
215
216/* --- @mp_drop@ --- *
d03ab969 217 *
d3409d5e 218 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 219 *
220 * Returns: ---
221 *
d3409d5e 222 * Use: Drops a reference to an integer which isn't wanted any more.
223 * If there are no more references, the integer is destroyed.
d03ab969 224 */
225
d3409d5e 226extern void mp_drop(mp */*m*/);
d03ab969 227
d3409d5e 228#define MP_DROP(m) do { \
229 mp *_mm = (m); \
d34decd2 230 _mm->ref--; \
231 if (_mm->ref == 0 && !(_mm->f & MP_CONST)) \
d3409d5e 232 mp_destroy(_mm); \
233} while (0)
234
235/* --- @mp_split@ --- *
d03ab969 236 *
d3409d5e 237 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 238 *
d3409d5e 239 * Returns: A reference to the same integer, possibly with a different
240 * address.
d03ab969 241 *
d3409d5e 242 * Use: Splits off a modifiable version of the integer referred to.
d03ab969 243 */
244
d3409d5e 245extern mp *mp_split(mp */*m*/);
246
247#define MP_SPLIT(m) do { \
d34decd2 248 mp *_m = (m); \
249 if ((_m->f & MP_CONST) || _m->ref > 1) { \
250 size_t _len = MP_LEN(_m); \
251 mp *_mm = mp_new(_len, _m->f); \
252 if (!(_m->f & MP_UNDEF)) \
253 memcpy(_mm->v, _m->v, MPWS(_len)); \
254 _m->ref--; \
255 _m = _mm; \
d3409d5e 256 } \
d34decd2 257 (m) = _m; \
d3409d5e 258} while (0)
d03ab969 259
d3409d5e 260/* --- @mp_resize@ --- *
d03ab969 261 *
d3409d5e 262 * Arguments: @mp *m@ = pointer to a multiprecision integer
263 * @size_t sz@ = new size
d03ab969 264 *
d3409d5e 265 * Returns: ---
d03ab969 266 *
d3409d5e 267 * Use: Resizes the vector containing the integer's digits. The new
268 * size must be at least as large as the current integer's
d34decd2 269 * length. This isn't really intended for client use.
d03ab969 270 */
271
d3409d5e 272extern void mp_resize(mp */*m*/, size_t /*sz*/);
273
274#define MP_RESIZE(m, ssz) do { \
275 mp *_m = (m); \
276 size_t _sz = (ssz); \
d34decd2 277 mparena *_a = (_m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; \
278 mpw *_v; \
d3409d5e 279 size_t _len = MP_LEN(_m); \
d34decd2 280 assert(((void)"can't make size less than length", _sz >= _len)); \
281 _v = mpalloc(_a, _sz); \
d9a8ae10 282 if (!(_m->f & MP_UNDEF)) \
283 memcpy(_v, _m->v, MPWS(_len)); \
d3409d5e 284 if (_m->f & MP_BURN) \
285 memset(_m->v, 0, MPWS(_m->sz)); \
d34decd2 286 mpfree(_m->a, _m->v); \
287 _m->a = _a; \
d3409d5e 288 _m->v = _v; \
289 _m->vl = _v + _len; \
d3409d5e 290} while (0)
d03ab969 291
d3409d5e 292/* --- @mp_ensure@ --- *
d03ab969 293 *
d3409d5e 294 * Arguments: @mp *m@ = pointer to a multiprecision integer
295 * @size_t sz@ = required size
d03ab969 296 *
297 * Returns: ---
298 *
d3409d5e 299 * Use: Ensures that the integer has enough space for @sz@ digits.
300 * The value is not changed.
d03ab969 301 */
302
d3409d5e 303extern void mp_ensure(mp */*m*/, size_t /*sz*/);
304
305#define MP_ENSURE(m, ssz) do { \
d34decd2 306 mp *_m = (m); \
d3409d5e 307 size_t _ssz = (ssz); \
d34decd2 308 size_t _len = MP_LEN(_m); \
309 if (_ssz >= _len) { \
310 if (_ssz > _m->sz) \
311 mp_resize(_m, _ssz); \
312 if (!(_m->f & MP_UNDEF) && _ssz > _len) \
313 memset(_m->vl, 0, MPWS(_ssz - _len)); \
314 _m->vl = _m->v + _ssz; \
315 } \
d3409d5e 316} while (0)
d03ab969 317
d34decd2 318/* --- @mp_dest@ --- *
d03ab969 319 *
d34decd2 320 * Arguments: @mp *m@ = a suggested destination integer
321 * @size_t sz@ = size required for result, in digits
322 * @unsigned f@ = various flags
323 *
324 * Returns: A pointer to an appropriate destination.
325 *
326 * Use: Converts a suggested destination into a real destination with
327 * the required properties. If the real destination is @d@,
328 * then the following properties will hold:
329 *
330 * * @d@ will have exactly one reference.
d03ab969 331 *
d34decd2 332 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
333 * change, unless @f@ has the @MP_UNDEF@ flag set.
d03ab969 334 *
d34decd2 335 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
336 * entry is equal to the sum of the counts of @d@ and @m@ on
337 * exit.
338 *
339 * * The size of @d@ will be at least @sz@.
340 *
341 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
342 * allocated from @MPARENA_SECURE@.
343 *
344 * Understanding this function is crucial to using Catacomb's
345 * multiprecision integer library effectively.
d03ab969 346 */
347
d34decd2 348extern mp *mp_dest(mp */*m*/, size_t /*sz*/, unsigned /*f*/);
d03ab969 349
d34decd2 350#define MP_DEST(m, ssz, f) do { \
d3409d5e 351 mp *_m = (m); \
d34decd2 352 size_t _ssz = (ssz); \
353 unsigned _f = (f); \
354 _m = mp_dest(_m, _ssz, _f); \
d3409d5e 355 (m) = _m; \
356} while (0)
357
358/*----- Size manipulation -------------------------------------------------*/
359
360/* --- @mp_shrink@ --- *
d03ab969 361 *
d3409d5e 362 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 363 *
364 * Returns: ---
365 *
d3409d5e 366 * Use: Reduces the recorded length of an integer. This doesn't
367 * reduce the amount of memory used, although it can improve
368 * performance a bit. To reduce memory, use @mp_minimize@
369 * instead. This can't change the value of an integer, and is
370 * therefore safe to use even when there are multiple
371 * references.
d03ab969 372 */
373
d3409d5e 374extern void mp_shrink(mp */*m*/);
d03ab969 375
d3409d5e 376#define MP_SHRINK(m) do { \
377 mp *_mm = (m); \
378 MPX_SHRINK(_mm->v, _mm->vl); \
379 if (!MP_LEN(_mm)) \
380 _mm->f &= ~MP_NEG; \
381} while (0)
382
383/* --- @mp_minimize@ --- *
d03ab969 384 *
d3409d5e 385 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 386 *
387 * Returns: ---
388 *
d3409d5e 389 * Use: Reduces the amount of memory an integer uses. It's best to
390 * do this to numbers which aren't going to change in the
391 * future.
d03ab969 392 */
393
d3409d5e 394extern void mp_minimize(mp */*m*/);
d03ab969 395
d3409d5e 396/*----- Bit scanning ------------------------------------------------------*/
d03ab969 397
d9a8ae10 398#ifndef CATACOMB_MPSCAN_H
d3409d5e 399# include "mpscan.h"
400#endif
401
402/* --- @mp_scan@ --- *
d03ab969 403 *
d3409d5e 404 * Arguments: @mpscan *sc@ = pointer to bitscanner block
405 * @const mp *m@ = pointer to a multiprecision integer
d03ab969 406 *
407 * Returns: ---
408 *
d3409d5e 409 * Use: Initializes a bitscanner on a multiprecision integer.
d03ab969 410 */
411
d3409d5e 412extern void mp_scan(mpscan */*sc*/, const mp */*m*/);
413
414#define MP_SCAN(sc, m) do { \
a790733d 415 const mp *_mm = (m); \
d3409d5e 416 mpscan *_sc = (sc); \
417 MPSCAN_INITX(_sc, _mm->v, _mm->vl); \
418} while (0)
419
420/* --- Other bitscanning aliases --- */
421
422#define mp_step mpscan_step
423#define mp_bit mpscan_bit
424
425#define MP_STEP MPSCAN_STEP
426#define MP_BIT MPSCAN_BIT
427
428/*----- Loading and storing -----------------------------------------------*/
d03ab969 429
d3409d5e 430/* --- @mp_octets@ --- *
d03ab969 431 *
d3409d5e 432 * Arguments: @const mp *m@ = a multiprecision integer
d03ab969 433 *
d3409d5e 434 * Returns: The number of octets required to represent @m@.
d03ab969 435 *
d3409d5e 436 * Use: Calculates the external storage required for a multiprecision
437 * integer.
d03ab969 438 */
439
24e1e862 440extern size_t mp_octets(const mp */*m*/);
441
442/* --- @mp_bits@ --- *
443 *
444 * Arguments: @const mp *m@ = a multiprecision integer
445 *
446 * Returns: The number of bits required to represent @m@.
447 *
448 * Use: Calculates the external storage required for a multiprecision
449 * integer.
450 */
451
452extern unsigned long mp_bits(const mp */*m*/);
d03ab969 453
d3409d5e 454/* --- @mp_loadl@ --- *
d03ab969 455 *
d3409d5e 456 * Arguments: @mp *d@ = destination
457 * @const void *pv@ = pointer to source data
458 * @size_t sz@ = size of the source data
d03ab969 459 *
d3409d5e 460 * Returns: Resulting multiprecision number.
d03ab969 461 *
d3409d5e 462 * Use: Loads a multiprecision number from an array of octets. The
463 * first byte in the array is the least significant. More
464 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
465 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
d03ab969 466 */
467
d3409d5e 468extern mp *mp_loadl(mp */*d*/, const void */*pv*/, size_t /*sz*/);
d03ab969 469
d3409d5e 470/* --- @mp_storel@ --- *
471 *
472 * Arguments: @const mp *m@ = source
473 * @void *pv@ = pointer to output array
474 * @size_t sz@ = size of the output array
475 *
476 * Returns: ---
477 *
478 * Use: Stores a multiprecision number in an array of octets. The
479 * first byte in the array is the least significant. If the
480 * array is too small to represent the number, high-order bits
481 * are truncated; if the array is too large, high order bytes
482 * are filled with zeros. More formally, if the number is
483 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
484 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
485 */
d03ab969 486
d3409d5e 487extern void mp_storel(const mp */*m*/, void */*pv*/, size_t /*sz*/);
d03ab969 488
d3409d5e 489/* --- @mp_loadb@ --- *
d03ab969 490 *
d3409d5e 491 * Arguments: @mp *d@ = destination
492 * @const void *pv@ = pointer to source data
493 * @size_t sz@ = size of the source data
d03ab969 494 *
d3409d5e 495 * Returns: Resulting multiprecision number.
d03ab969 496 *
d3409d5e 497 * Use: Loads a multiprecision number from an array of octets. The
498 * last byte in the array is the least significant. More
499 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
500 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
d03ab969 501 */
502
d3409d5e 503extern mp *mp_loadb(mp */*d*/, const void */*pv*/, size_t /*sz*/);
d03ab969 504
d3409d5e 505/* --- @mp_storeb@ --- *
d03ab969 506 *
d3409d5e 507 * Arguments: @const mp *m@ = source
508 * @void *pv@ = pointer to output array
509 * @size_t sz@ = size of the output array
d03ab969 510 *
511 * Returns: ---
512 *
d3409d5e 513 * Use: Stores a multiprecision number in an array of octets. The
514 * last byte in the array is the least significant. If the
515 * array is too small to represent the number, high-order bits
516 * are truncated; if the array is too large, high order bytes
517 * are filled with zeros. More formally, if the number is
518 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
519 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
d03ab969 520 */
521
d3409d5e 522extern void mp_storeb(const mp */*m*/, void */*pv*/, size_t /*sz*/);
d03ab969 523
d3409d5e 524/*----- Simple arithmetic -------------------------------------------------*/
d03ab969 525
d3409d5e 526/* --- @mp_2c@ --- *
d03ab969 527 *
d3409d5e 528 * Arguments: @mp *d@ = destination
529 * @mp *a@ = source
d03ab969 530 *
d3409d5e 531 * Returns: Result, @a@ converted to two's complement notation.
532 */
533
534extern mp *mp_2c(mp */*d*/, mp */*a*/);
535
536/* --- @mp_sm@ --- *
537 *
538 * Arguments: @mp *d@ = destination
539 * @mp *a@ = source
d03ab969 540 *
d3409d5e 541 * Returns: Result, @a@ converted to the native signed-magnitude
542 * notation.
d03ab969 543 */
544
d3409d5e 545extern mp *mp_sm(mp */*d*/, mp */*a*/);
d03ab969 546
d3409d5e 547/* --- @mp_lsl@ --- *
d03ab969 548 *
d3409d5e 549 * Arguments: @mp *d@ = destination
d9a8ae10 550 * @mp *a@ = source
d3409d5e 551 * @size_t n@ = number of bits to move
d03ab969 552 *
d3409d5e 553 * Returns: Result, @a@ shifted left by @n@.
554 */
555
d9a8ae10 556extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/);
d3409d5e 557
558/* --- @mp_lsr@ --- *
559 *
560 * Arguments: @mp *d@ = destination
d9a8ae10 561 * @mp *a@ = source
d3409d5e 562 * @size_t n@ = number of bits to move
d03ab969 563 *
d3409d5e 564 * Returns: Result, @a@ shifted left by @n@.
d03ab969 565 */
566
d9a8ae10 567extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
d03ab969 568
d3409d5e 569/* --- @mp_cmp@ --- *
d03ab969 570 *
d3409d5e 571 * Arguments: @const mp *a, *b@ = two numbers
d03ab969 572 *
d3409d5e 573 * Returns: Less than, equal to or greater than zero, according to
574 * whether @a@ is less than, equal to or greater than @b@.
575 */
576
577extern int mp_cmp(const mp */*a*/, const mp */*b*/);
578
579#define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
580
581/* --- @mp_add@ --- *
d03ab969 582 *
d3409d5e 583 * Arguments: @mp *d@ = destination
d9a8ae10 584 * @mp *a, *b@ = sources
d3409d5e 585 *
586 * Returns: Result, @a@ added to @b@.
d03ab969 587 */
588
d9a8ae10 589extern mp *mp_add(mp */*d*/, mp */*a*/, mp */*b*/);
d03ab969 590
d3409d5e 591/* --- @mp_sub@ --- *
592 *
593 * Arguments: @mp *d@ = destination
d9a8ae10 594 * @mp *a, *b@ = sources
d3409d5e 595 *
596 * Returns: Result, @b@ subtracted from @a@.
597 */
d03ab969 598
d9a8ae10 599extern mp *mp_sub(mp */*d*/, mp */*a*/, mp */*b*/);
d3409d5e 600
601/* --- @mp_mul@ --- *
d03ab969 602 *
d3409d5e 603 * Arguments: @mp *d@ = destination
d9a8ae10 604 * @mp *a, *b@ = sources
d03ab969 605 *
d3409d5e 606 * Returns: Result, @a@ multiplied by @b@.
607 */
608
d9a8ae10 609extern mp *mp_mul(mp */*d*/, mp */*a*/, mp */*b*/);
d3409d5e 610
611/* --- @mp_sqr@ --- *
612 *
613 * Arguments: @mp *d@ = destination
d9a8ae10 614 * @mp *a@ = source
d3409d5e 615 *
616 * Returns: Result, @a@ squared.
617 */
618
d9a8ae10 619extern mp *mp_sqr(mp */*d*/, mp */*a*/);
d3409d5e 620
621/* --- @mp_div@ --- *
d03ab969 622 *
d3409d5e 623 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
d9a8ae10 624 * @mp *a, *b@ = sources
d3409d5e 625 *
626 * Use: Calculates the quotient and remainder when @a@ is divided by
627 * @b@.
d03ab969 628 */
629
d9a8ae10 630extern void mp_div(mp **/*qq*/, mp **/*rr*/, mp */*a*/, mp */*b*/);
d3409d5e 631
632/*----- More advanced algorithms ------------------------------------------*/
d03ab969 633
d3409d5e 634/* --- @mp_gcd@ --- *
d03ab969 635 *
d3409d5e 636 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
637 * @mp *a, *b@ = sources (must be nonzero)
d03ab969 638 *
639 * Returns: ---
640 *
d3409d5e 641 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
642 * @ax + by = gcd(a, b)@. This is useful for computing modular
d9a8ae10 643 * inverses. Neither @a@ nor @b@ may be zero.
d03ab969 644 */
645
d3409d5e 646extern void mp_gcd(mp **/*gcd*/, mp **/*xx*/, mp **/*yy*/,
647 mp */*a*/, mp */*b*/);
648
5b00a0ea 649/* --- @mp_jacobi@ --- *
650 *
651 * Arguments: @mp *a@ = an integer less than @n@
652 * @mp *n@ = an odd integer
653 *
654 * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
655 *
656 * Use: Computes the Jacobi symbol. If @n@ is prime, this is the
657 * Legendre symbol and is equal to 1 if and only if @a@ is a
658 * quadratic residue mod @n@. The result is zero if and only if
659 * @a@ and @n@ have a common factor greater than one.
660 */
661
662int mp_jacobi(mp */*a*/, mp */*n*/);
663
d3409d5e 664/*----- Test harness support ----------------------------------------------*/
665
666#include <mLib/testrig.h>
667
d9a8ae10 668#ifndef CATACOMB_MPTEXT_H
d3409d5e 669# include "mptext.h"
670#endif
671
672extern const test_type type_mp;
d03ab969 673
674/*----- That's all, folks -------------------------------------------------*/
675
676#ifdef __cplusplus
677 }
678#endif
679
680#endif