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