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