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