math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / math / mp.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
d3409d5e 3 * Simple multiprecision arithmetic
d03ab969 4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
d03ab969 9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
45c0fd36 16 *
d03ab969 17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
45c0fd36 21 *
d03ab969 22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
d9a8ae10 28#ifndef CATACOMB_MP_H
29#define CATACOMB_MP_H
d03ab969 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
d3409d5e 37#include <assert.h>
d03ab969 38#include <string.h>
39
d3409d5e 40#include <mLib/sub.h>
41
d9a8ae10 42#ifndef CATACOMB_MPW_H
d3409d5e 43# include "mpw.h"
d03ab969 44#endif
45
d34decd2 46#ifndef CATACOMB_ARENA_H
47# include "arena.h"
48#endif
49
50#ifndef CATACOMB_MPARENA_H
51# include "mparena.h"
52#endif
53
d9a8ae10 54#ifndef CATACOMB_MPX_H
d03ab969 55# include "mpx.h"
56#endif
57
d03ab969 58/*----- Data structures ---------------------------------------------------*/
59
3bc0a0fe 60/* --- A multiprecision integer --- */
61
d03ab969 62typedef struct mp {
3bc0a0fe 63 mpw *v, *vl; /* Vector of digits, current limit */
64 size_t sz; /* Size of digit buffer in words */
65 mparena *a; /* Arena for buffer allocation */
66 unsigned f; /* Flags (see below) */
67 unsigned ref; /* Reference counter */
d03ab969 68} mp;
69
3bc0a0fe 70#define MP_NEG 1u /* Negative (signed magnitude) */
71#define MP_BURN 2u /* Secret (viral flag) */
72#define MP_CONST 4u /* Uses strange memory allocation */
73#define MP_UNDEF 8u /* Contains nothing interesting */
74#define MP_DESTROYED 16u /* Has been destroyed */
75
76/* --- A factor for simultaneous exponentation --- *
77 *
78 * Used by the Montgomery and Barrett exponentiators.
79 */
80
81typedef struct mp_expfactor {
82 mp *base;
83 mp *exp;
84} mp_expfactor;
d03ab969 85
d3409d5e 86/*----- Useful constants --------------------------------------------------*/
d03ab969 87
d3409d5e 88extern mp mp_const[];
d03ab969 89
d3409d5e 90#define MP_ZERO (&mp_const[0])
45c0fd36
MW
91#define MP_ONE (&mp_const[1])
92#define MP_TWO (&mp_const[2])
d3409d5e 93#define MP_THREE (&mp_const[3])
45c0fd36
MW
94#define MP_FOUR (&mp_const[4])
95#define MP_FIVE (&mp_const[5])
96#define MP_TEN (&mp_const[6])
d34decd2 97#define MP_256 (&mp_const[7])
45c0fd36 98#define MP_MONE (&mp_const[8])
d03ab969 99
d3409d5e 100#define MP_NEW ((mp *)0)
d34decd2 101#define MP_NEWSEC (&mp_const[9])
d03ab969 102
d34decd2 103/*----- Trivial macros ----------------------------------------------------*/
d03ab969 104
d34decd2 105/* --- @MP_LEN@ --- *
d03ab969 106 *
d34decd2 107 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 108 *
d34decd2 109 * Returns: Length of the integer, in words.
d03ab969 110 */
111
d34decd2 112#define MP_LEN(m) ((m)->vl - ((m)->v))
d03ab969 113
d34decd2 114/*----- Memory management and reference counting --------------------------*/
d3409d5e 115
d34decd2 116/* --- @mp_new@ --- *
d03ab969 117 *
d34decd2 118 * Arguments: @size_t sz@ = size of vector required
119 * @unsigned f@ = flags to set
d03ab969 120 *
d34decd2 121 * Returns: Pointer to a new MP structure.
d03ab969 122 *
d34decd2 123 * Use: Allocates a new multiprecision integer. The data space is
124 * allocated from either the standard global or secret arena,
125 * depending on the initial flags requested.
d03ab969 126 */
127
d34decd2 128extern mp *mp_new(size_t /*sz*/, unsigned /*f*/);
d03ab969 129
d34decd2 130/* --- @mp_create@ --- *
d03ab969 131 *
d34decd2 132 * Arguments: @size_t sz@ = size of vector required
d03ab969 133 *
d34decd2 134 * Returns: Pointer to pristine new MP structure with enough memory
135 * bolted onto it.
136 *
137 * Use: Creates a new multiprecision integer with indeterminate
138 * contents. The integer has a single reference.
d03ab969 139 */
140
d34decd2 141extern mp *mp_create(size_t /*sz*/);
d3409d5e 142
d34decd2 143/* --- @mp_createsecure@ --- *
d03ab969 144 *
d3409d5e 145 * Arguments: @size_t sz@ = size of vector required
d03ab969 146 *
d3409d5e 147 * Returns: Pointer to pristine new MP structure with enough memory
148 * bolted onto it.
d03ab969 149 *
d3409d5e 150 * Use: Creates a new multiprecision integer with indeterminate
d34decd2 151 * contents. The integer has a single reference. The integer's
152 * data space is allocated from the secure arena. Its burn flag
153 * is set.
d03ab969 154 */
155
d34decd2 156extern mp *mp_createsecure(size_t /*sz*/);
d03ab969 157
d3409d5e 158/* --- @mp_build@ --- *
d03ab969 159 *
d3409d5e 160 * Arguments: @mp *m@ = pointer to an MP block to fill in
161 * @mpw *v@ = pointer to a word array
162 * @mpw *vl@ = pointer just past end of array
d03ab969 163 *
164 * Returns: ---
165 *
d3409d5e 166 * Use: Creates a multiprecision integer representing some smallish
167 * number. You must provide storage for the number and dispose
168 * of it when you've finished with it. The number is marked as
169 * constant while it exists.
d03ab969 170 */
171
d3409d5e 172extern void mp_build(mp */*m*/, mpw */*v*/, mpw */*vl*/);
d03ab969 173
d3409d5e 174/* --- @mp_destroy@ --- *
d03ab969 175 *
d3409d5e 176 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 177 *
178 * Returns: ---
179 *
d3409d5e 180 * Use: Destroys a multiprecision integer. The reference count isn't
181 * checked. Don't use this function if you don't know what
182 * you're doing: use @mp_drop@ instead.
d03ab969 183 */
184
d3409d5e 185extern void mp_destroy(mp */*m*/);
d03ab969 186
d3409d5e 187/* --- @mp_copy@ --- *
d03ab969 188 *
d3409d5e 189 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 190 *
d3409d5e 191 * Returns: A copy of the given multiprecision integer.
192 *
193 * Use: Copies the given integer. In fact you just get another
194 * reference to the same old one again.
d03ab969 195 */
196
d3409d5e 197extern mp *mp_copy(mp */*m*/);
d03ab969 198
d3409d5e 199#define MP_COPY(m) ((m)->ref++, (m))
200
201/* --- @mp_drop@ --- *
d03ab969 202 *
d3409d5e 203 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 204 *
205 * Returns: ---
206 *
d3409d5e 207 * Use: Drops a reference to an integer which isn't wanted any more.
208 * If there are no more references, the integer is destroyed.
d03ab969 209 */
210
d3409d5e 211extern void mp_drop(mp */*m*/);
d03ab969 212
d3409d5e 213#define MP_DROP(m) do { \
214 mp *_mm = (m); \
d34decd2 215 _mm->ref--; \
216 if (_mm->ref == 0 && !(_mm->f & MP_CONST)) \
d3409d5e 217 mp_destroy(_mm); \
218} while (0)
45c0fd36 219
d3409d5e 220/* --- @mp_split@ --- *
d03ab969 221 *
d3409d5e 222 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 223 *
d3409d5e 224 * Returns: A reference to the same integer, possibly with a different
225 * address.
d03ab969 226 *
d3409d5e 227 * Use: Splits off a modifiable version of the integer referred to.
d03ab969 228 */
229
d3409d5e 230extern mp *mp_split(mp */*m*/);
231
232#define MP_SPLIT(m) do { \
d34decd2 233 mp *_m = (m); \
234 if ((_m->f & MP_CONST) || _m->ref > 1) { \
235 size_t _len = MP_LEN(_m); \
236 mp *_mm = mp_new(_len, _m->f); \
237 if (!(_m->f & MP_UNDEF)) \
238 memcpy(_mm->v, _m->v, MPWS(_len)); \
239 _m->ref--; \
240 _m = _mm; \
d3409d5e 241 } \
d34decd2 242 (m) = _m; \
d3409d5e 243} while (0)
d03ab969 244
d3409d5e 245/* --- @mp_resize@ --- *
d03ab969 246 *
d3409d5e 247 * Arguments: @mp *m@ = pointer to a multiprecision integer
248 * @size_t sz@ = new size
d03ab969 249 *
d3409d5e 250 * Returns: ---
d03ab969 251 *
d3409d5e 252 * Use: Resizes the vector containing the integer's digits. The new
253 * size must be at least as large as the current integer's
d34decd2 254 * length. This isn't really intended for client use.
d03ab969 255 */
256
d3409d5e 257extern void mp_resize(mp */*m*/, size_t /*sz*/);
258
259#define MP_RESIZE(m, ssz) do { \
260 mp *_m = (m); \
261 size_t _sz = (ssz); \
d34decd2 262 mparena *_a = (_m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; \
263 mpw *_v; \
d3409d5e 264 size_t _len = MP_LEN(_m); \
d34decd2 265 assert(((void)"can't make size less than length", _sz >= _len)); \
266 _v = mpalloc(_a, _sz); \
d9a8ae10 267 if (!(_m->f & MP_UNDEF)) \
268 memcpy(_v, _m->v, MPWS(_len)); \
d3409d5e 269 if (_m->f & MP_BURN) \
270 memset(_m->v, 0, MPWS(_m->sz)); \
d34decd2 271 mpfree(_m->a, _m->v); \
272 _m->a = _a; \
d3409d5e 273 _m->v = _v; \
274 _m->vl = _v + _len; \
d3409d5e 275} while (0)
d03ab969 276
d3409d5e 277/* --- @mp_ensure@ --- *
d03ab969 278 *
d3409d5e 279 * Arguments: @mp *m@ = pointer to a multiprecision integer
280 * @size_t sz@ = required size
d03ab969 281 *
282 * Returns: ---
283 *
d3409d5e 284 * Use: Ensures that the integer has enough space for @sz@ digits.
285 * The value is not changed.
d03ab969 286 */
287
d3409d5e 288extern void mp_ensure(mp */*m*/, size_t /*sz*/);
289
290#define MP_ENSURE(m, ssz) do { \
d34decd2 291 mp *_m = (m); \
d3409d5e 292 size_t _ssz = (ssz); \
d34decd2 293 size_t _len = MP_LEN(_m); \
294 if (_ssz >= _len) { \
295 if (_ssz > _m->sz) \
296 mp_resize(_m, _ssz); \
297 if (!(_m->f & MP_UNDEF) && _ssz > _len) \
298 memset(_m->vl, 0, MPWS(_ssz - _len)); \
299 _m->vl = _m->v + _ssz; \
300 } \
d3409d5e 301} while (0)
d03ab969 302
d34decd2 303/* --- @mp_dest@ --- *
d03ab969 304 *
d34decd2 305 * Arguments: @mp *m@ = a suggested destination integer
306 * @size_t sz@ = size required for result, in digits
307 * @unsigned f@ = various flags
308 *
309 * Returns: A pointer to an appropriate destination.
310 *
311 * Use: Converts a suggested destination into a real destination with
312 * the required properties. If the real destination is @d@,
313 * then the following properties will hold:
314 *
315 * * @d@ will have exactly one reference.
d03ab969 316 *
d34decd2 317 * * If @m@ is not @MP_NEW@, then the contents of @m@ will not
318 * change, unless @f@ has the @MP_UNDEF@ flag set.
d03ab969 319 *
d34decd2 320 * * If @m@ is not @MP_NEW@, then he reference count of @m@ on
321 * entry is equal to the sum of the counts of @d@ and @m@ on
322 * exit.
323 *
324 * * The size of @d@ will be at least @sz@.
325 *
326 * * If @f@ has the @MP_BURN@ flag set, then @d@ will be
327 * allocated from @MPARENA_SECURE@.
328 *
329 * Understanding this function is crucial to using Catacomb's
330 * multiprecision integer library effectively.
d03ab969 331 */
332
d34decd2 333extern mp *mp_dest(mp */*m*/, size_t /*sz*/, unsigned /*f*/);
d03ab969 334
d34decd2 335#define MP_DEST(m, ssz, f) do { \
d3409d5e 336 mp *_m = (m); \
d34decd2 337 size_t _ssz = (ssz); \
338 unsigned _f = (f); \
339 _m = mp_dest(_m, _ssz, _f); \
d3409d5e 340 (m) = _m; \
341} while (0)
342
343/*----- Size manipulation -------------------------------------------------*/
344
345/* --- @mp_shrink@ --- *
d03ab969 346 *
d3409d5e 347 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 348 *
349 * Returns: ---
350 *
d3409d5e 351 * Use: Reduces the recorded length of an integer. This doesn't
352 * reduce the amount of memory used, although it can improve
353 * performance a bit. To reduce memory, use @mp_minimize@
354 * instead. This can't change the value of an integer, and is
355 * therefore safe to use even when there are multiple
356 * references.
d03ab969 357 */
358
d3409d5e 359extern void mp_shrink(mp */*m*/);
d03ab969 360
d3409d5e 361#define MP_SHRINK(m) do { \
362 mp *_mm = (m); \
363 MPX_SHRINK(_mm->v, _mm->vl); \
a69a3efd 364 if (MP_ZEROP(_mm)) \
d3409d5e 365 _mm->f &= ~MP_NEG; \
366} while (0)
367
368/* --- @mp_minimize@ --- *
d03ab969 369 *
d3409d5e 370 * Arguments: @mp *m@ = pointer to a multiprecision integer
d03ab969 371 *
372 * Returns: ---
373 *
d3409d5e 374 * Use: Reduces the amount of memory an integer uses. It's best to
375 * do this to numbers which aren't going to change in the
376 * future.
d03ab969 377 */
378
d3409d5e 379extern void mp_minimize(mp */*m*/);
d03ab969 380
d3409d5e 381/*----- Bit scanning ------------------------------------------------------*/
d03ab969 382
d9a8ae10 383#ifndef CATACOMB_MPSCAN_H
d3409d5e 384# include "mpscan.h"
385#endif
386
387/* --- @mp_scan@ --- *
d03ab969 388 *
d3409d5e 389 * Arguments: @mpscan *sc@ = pointer to bitscanner block
390 * @const mp *m@ = pointer to a multiprecision integer
d03ab969 391 *
392 * Returns: ---
393 *
d3409d5e 394 * Use: Initializes a bitscanner on a multiprecision integer.
d03ab969 395 */
396
d3409d5e 397extern void mp_scan(mpscan */*sc*/, const mp */*m*/);
398
399#define MP_SCAN(sc, m) do { \
a790733d 400 const mp *_mm = (m); \
d3409d5e 401 mpscan *_sc = (sc); \
402 MPSCAN_INITX(_sc, _mm->v, _mm->vl); \
403} while (0)
404
5fbe3846 405/* --- @mp_rscan@ --- *
406 *
407 * Arguments: @mpscan *sc@ = pointer to bitscanner block
408 * @const mp *m@ = pointer to a multiprecision integer
409 *
410 * Returns: ---
411 *
412 * Use: Initializes a reverse bitscanner on a multiprecision
413 * integer.
414 */
415
416extern void mp_rscan(mpscan */*sc*/, const mp */*m*/);
417
418#define MP_RSCAN(sc, m) do { \
419 const mp *_mm = (m); \
420 mpscan *_sc = (sc); \
421 MPSCAN_RINITX(_sc, _mm->v, _mm->vl); \
422} while (0)
423
d3409d5e 424/* --- Other bitscanning aliases --- */
425
426#define mp_step mpscan_step
427#define mp_bit mpscan_bit
5fbe3846 428#define mp_rstep mpscan_rstep
429#define mp_rbit mpscan_rbit
d3409d5e 430
431#define MP_STEP MPSCAN_STEP
432#define MP_BIT MPSCAN_BIT
5fbe3846 433#define MP_RSTEP MPSCAN_RSTEP
434#define MP_RBIT MPSCAN_RBIT
d3409d5e 435
436/*----- Loading and storing -----------------------------------------------*/
d03ab969 437
d3409d5e 438/* --- @mp_octets@ --- *
d03ab969 439 *
d3409d5e 440 * Arguments: @const mp *m@ = a multiprecision integer
d03ab969 441 *
d3409d5e 442 * Returns: The number of octets required to represent @m@.
d03ab969 443 *
d3409d5e 444 * Use: Calculates the external storage required for a multiprecision
445 * integer.
d03ab969 446 */
447
24e1e862 448extern size_t mp_octets(const mp */*m*/);
449
f09e814a 450/* --- @mp_octets2c@ --- *
451 *
452 * Arguments: @const mp *m@ = a multiprecision integer
453 *
454 * Returns: The number of octets required to represent @m@.
455 *
456 * Use: Calculates the external storage required for a multiprecision
457 * integer represented as two's complement.
458 */
459
460extern size_t mp_octets2c(const mp */*m*/);
461
24e1e862 462/* --- @mp_bits@ --- *
463 *
464 * Arguments: @const mp *m@ = a multiprecision integer
465 *
466 * Returns: The number of bits required to represent @m@.
467 *
468 * Use: Calculates the external storage required for a multiprecision
469 * integer.
470 */
471
472extern unsigned long mp_bits(const mp */*m*/);
d03ab969 473
d3409d5e 474/* --- @mp_loadl@ --- *
d03ab969 475 *
d3409d5e 476 * Arguments: @mp *d@ = destination
477 * @const void *pv@ = pointer to source data
478 * @size_t sz@ = size of the source data
d03ab969 479 *
d3409d5e 480 * Returns: Resulting multiprecision number.
d03ab969 481 *
d3409d5e 482 * Use: Loads a multiprecision number from an array of octets. The
483 * first byte in the array is the least significant. More
484 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
485 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
d03ab969 486 */
487
d3409d5e 488extern mp *mp_loadl(mp */*d*/, const void */*pv*/, size_t /*sz*/);
d03ab969 489
d3409d5e 490/* --- @mp_storel@ --- *
491 *
492 * Arguments: @const mp *m@ = source
493 * @void *pv@ = pointer to output array
494 * @size_t sz@ = size of the output array
495 *
496 * Returns: ---
497 *
498 * Use: Stores a multiprecision number in an array of octets. The
499 * first byte in the array is the least significant. If the
500 * array is too small to represent the number, high-order bits
501 * are truncated; if the array is too large, high order bytes
502 * are filled with zeros. More formally, if the number is
503 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
504 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
505 */
d03ab969 506
d3409d5e 507extern void mp_storel(const mp */*m*/, void */*pv*/, size_t /*sz*/);
d03ab969 508
d3409d5e 509/* --- @mp_loadb@ --- *
d03ab969 510 *
d3409d5e 511 * Arguments: @mp *d@ = destination
512 * @const void *pv@ = pointer to source data
513 * @size_t sz@ = size of the source data
d03ab969 514 *
d3409d5e 515 * Returns: Resulting multiprecision number.
d03ab969 516 *
d3409d5e 517 * Use: Loads a multiprecision number from an array of octets. The
518 * last byte in the array is the least significant. More
519 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
520 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
d03ab969 521 */
522
d3409d5e 523extern mp *mp_loadb(mp */*d*/, const void */*pv*/, size_t /*sz*/);
d03ab969 524
d3409d5e 525/* --- @mp_storeb@ --- *
d03ab969 526 *
d3409d5e 527 * Arguments: @const mp *m@ = source
528 * @void *pv@ = pointer to output array
529 * @size_t sz@ = size of the output array
d03ab969 530 *
531 * Returns: ---
532 *
d3409d5e 533 * Use: Stores a multiprecision number in an array of octets. The
534 * last byte in the array is the least significant. If the
535 * array is too small to represent the number, high-order bits
536 * are truncated; if the array is too large, high order bytes
537 * are filled with zeros. More formally, if the number is
538 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
539 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
d03ab969 540 */
541
d3409d5e 542extern void mp_storeb(const mp */*m*/, void */*pv*/, size_t /*sz*/);
d03ab969 543
f09e814a 544/* --- @mp_loadl2c@ --- *
d03ab969 545 *
d3409d5e 546 * Arguments: @mp *d@ = destination
f09e814a 547 * @const void *pv@ = pointer to source data
548 * @size_t sz@ = size of the source data
549 *
550 * Returns: Resulting multiprecision number.
d03ab969 551 *
f09e814a 552 * Use: Loads a multiprecision number from an array of octets as
553 * two's complement. The first byte in the array is the least
554 * significant.
d3409d5e 555 */
556
f09e814a 557extern mp *mp_loadl2c(mp */*d*/, const void */*pv*/, size_t /*sz*/);
d3409d5e 558
f09e814a 559/* --- @mp_storel2c@ --- *
560 *
561 * Arguments: @const mp *m@ = source
562 * @void *pv@ = pointer to output array
563 * @size_t sz@ = size of the output array
564 *
565 * Returns: ---
566 *
567 * Use: Stores a multiprecision number in an array of octets as two's
568 * complement. The first byte in the array is the least
569 * significant. If the array is too small to represent the
570 * number, high-order bits are truncated; if the array is too
571 * large, high order bytes are sign-extended.
572 */
573
574extern void mp_storel2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
575
576/* --- @mp_loadb2c@ --- *
d3409d5e 577 *
578 * Arguments: @mp *d@ = destination
f09e814a 579 * @const void *pv@ = pointer to source data
580 * @size_t sz@ = size of the source data
d03ab969 581 *
f09e814a 582 * Returns: Resulting multiprecision number.
583 *
584 * Use: Loads a multiprecision number from an array of octets as
585 * two's complement. The last byte in the array is the least
586 * significant.
d03ab969 587 */
588
f09e814a 589extern mp *mp_loadb2c(mp */*d*/, const void */*pv*/, size_t /*sz*/);
590
591/* --- @mp_storeb2c@ --- *
592 *
593 * Arguments: @const mp *m@ = source
594 * @void *pv@ = pointer to output array
595 * @size_t sz@ = size of the output array
596 *
597 * Returns: ---
598 *
599 * Use: Stores a multiprecision number in an array of octets, as
600 * two's complement. The last byte in the array is the least
601 * significant. If the array is too small to represent the
602 * number, high-order bits are truncated; if the array is too
603 * large, high order bytes are sign-extended.
604 */
605
606extern void mp_storeb2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
607
397041a9 608/*----- Bit operations ----------------------------------------------------*/
d03ab969 609
397041a9 610/* --- @mp_not@ --- *
d03ab969 611 *
d3409d5e 612 * Arguments: @mp *d@ = destination
d9a8ae10 613 * @mp *a@ = source
d03ab969 614 *
397041a9 615 * Returns: The bitwise complement of the source.
45c0fd36 616 */
d3409d5e 617
397041a9 618extern mp *mp_not(mp */*d*/, mp */*a*/);
d3409d5e 619
397041a9 620/* --- @mp_bitop@ --- *
d3409d5e 621 *
622 * Arguments: @mp *d@ = destination
397041a9 623 * @mp *a, *b@ = sources
d03ab969 624 *
397041a9 625 * Returns: The result of the given bitwise operation. These functions
626 * don't handle negative numbers at all sensibly. For that, use
627 * the @...2c@ variants. The functions are named after the
628 * truth tables they generate:
629 *
630 * a: 0011
631 * b: 0101
632 * @mpx_bitXXXX@
d03ab969 633 */
634
397041a9 635#define MP_BITDECL(string) \
636 extern mp *mp_bit##string(mp */*d*/, mp */*a*/, mp */*b*/);
637MPX_DOBIN(MP_BITDECL)
f09e814a 638
397041a9 639/* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
f09e814a 640 *
397041a9 641 * Synonyms for the commonly-used functions.
f09e814a 642 */
643
45c0fd36
MW
644#define mp_and mp_bit0001
645#define mp_or mp_bit0111
397041a9 646#define mp_nand mp_bit1110
45c0fd36
MW
647#define mp_nor mp_bit1000
648#define mp_xor mp_bit0110
f09e814a 649
397041a9 650/* --- @mp_testbit@ --- *
f09e814a 651 *
652 * Arguments: @mp *x@ = a large integer
09d00c6b 653 * @unsigned long n@ = which bit to test
f09e814a 654 *
397041a9 655 * Returns: Nonzero if the bit is set, zero if not.
d3409d5e 656 */
657
397041a9 658extern int mp_testbit(mp */*x*/, unsigned long /*n*/);
d3409d5e 659
09d00c6b 660/* --- @mp_setbit@, @mp_clearbit@ --- *
661 *
662 * Arguments: @mp *d@ = a destination
663 * @mp *x@ = a large integer
664 * @unsigned long n@ = which bit to modify
665 *
666 * Returns: The argument @x@, with the appropriate bit set or cleared.
667 */
668
669extern mp *mp_setbit(mp */*d*/, mp */*x*/, unsigned long /*n*/);
670extern mp *mp_clearbit(mp */*d*/, mp */*x*/, unsigned long /*n*/);
671
81578196 672/* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- *
0f32e0f8 673 *
674 * Arguments: @mp *d@ = destination
397041a9 675 * @mp *a@ = source
676 * @size_t n@ = number of bits to move
f09e814a 677 *
397041a9 678 * Returns: Result, @a@ shifted left or right by @n@.
81578196 679 *
680 * Use: Bitwise shift operators. @mp_lslc@ fills the bits introduced
681 * on the right with ones instead of zeroes: it's used
682 * internally by @mp_lsl2c@, though it may be useful on its
683 * own.
f09e814a 684 */
685
397041a9 686extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/);
81578196 687extern mp *mp_lslc(mp */*d*/, mp */*a*/, size_t /*n*/);
397041a9 688extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
f09e814a 689
397041a9 690/* --- @mp_not2c@ --- *
f09e814a 691 *
692 * Arguments: @mp *d@ = destination
693 * @mp *a@ = source
694 *
397041a9 695 * Returns: The sign-extended complement of the argument.
696 */
f09e814a 697
397041a9 698extern mp *mp_not2c(mp */*d*/, mp */*a*/);
0f32e0f8 699
f09e814a 700/* --- @mp_bitop2c@ --- *
701 *
702 * Arguments: @mp *d@ = destination
703 * @mp *a, *b@ = sources
704 *
705 * Returns: The result of the given bitwise operation. Negative numbers
706 * are treated as two's complement, sign-extended infinitely to
707 * the left. The functions are named after the truth tables
708 * they generate:
709 *
710 * a: 0011
711 * b: 0101
712 * @mpx_bitXXXX@
713 */
714
715#define MP_BIT2CDECL(string) \
716 extern mp *mp_bit##string##2c(mp */*d*/, mp */*a*/, mp */*b*/);
717MPX_DOBIN(MP_BIT2CDECL)
718
719/* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
720 *
721 * Synonyms for the commonly-used functions.
722 */
723
724#define mp_and2c mp_bit00012c
45c0fd36 725#define mp_or2c mp_bit01112c
f09e814a 726#define mp_nand2c mp_bit11102c
727#define mp_nor2c mp_bit10002c
728#define mp_xor2c mp_bit01102c
729
397041a9 730/* --- @mp_lsl2c@, @mp_lsr2c@ --- *
f09e814a 731 *
732 * Arguments: @mp *d@ = destination
733 * @mp *a@ = source
397041a9 734 * @size_t n@ = number of bits to move
f09e814a 735 *
397041a9 736 * Returns: Result, @a@ shifted left or right by @n@. Handles the
737 * pretence of sign-extension for negative numbers.
f09e814a 738 */
739
397041a9 740extern mp *mp_lsl2c(mp */*d*/, mp */*a*/, size_t /*n*/);
741extern mp *mp_lsr2c(mp */*d*/, mp */*a*/, size_t /*n*/);
742
743/* --- @mp_testbit2c@ --- *
744 *
745 * Arguments: @mp *x@ = a large integer
746 * @unsigned long n@ = which bit to test
747 *
748 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
749 * complement representation.
750 */
751
752extern int mp_testbit2c(mp */*x*/, unsigned long /*n*/);
753
754/* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
755 *
756 * Arguments: @mp *d@ = a destination
757 * @mp *x@ = a large integer
758 * @unsigned long n@ = which bit to modify
759 *
760 * Returns: The argument @x@, with the appropriate bit set or cleared.
761 * Fakes up two's complement representation.
762 */
763
764extern mp *mp_setbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
765extern mp *mp_clearbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
766
767/*----- Comparisons -------------------------------------------------------*/
768
769/* --- @mp_eq@ --- *
770 *
771 * Arguments: @const mp *a, *b@ = two numbers
772 *
773 * Returns: Nonzero if the numbers are equal.
774 */
775
776extern int mp_eq(const mp */*a*/, const mp */*b*/);
777
778#define MP_EQ(a, b) \
779 ((((a)->f ^ (b)->f) & MP_NEG) == 0 && \
780 mpx_ueq((a)->v, (a)->vl, (b)->v, (b)->vl))
781
782/* --- @mp_cmp@ --- *
783 *
784 * Arguments: @const mp *a, *b@ = two numbers
785 *
786 * Returns: Less than, equal to or greater than zero, according to
787 * whether @a@ is less than, equal to or greater than @b@.
788 */
789
790extern int mp_cmp(const mp */*a*/, const mp */*b*/);
791
792#define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
793
3cd24abb 794/* --- Other handy macros --- */
795
a69a3efd 796#define MP_NEGP(x) ((x)->f & MP_NEG)
797#define MP_ZEROP(x) (!MP_LEN(x))
798#define MP_POSP(x) (!MP_NEGP(x) && !MP_ZEROP(x))
799#define MP_ODDP(x) (!MP_ZEROP(x) && ((x)->v[0] & 1u))
800#define MP_EVENP(x) (!MP_ODDP(x))
3cd24abb 801
397041a9 802/*----- Arithmetic operations ---------------------------------------------*/
803
804/* --- @mp_neg@ --- *
805 *
806 * Arguments: @mp *d@ = destination
807 * @mp *a@ = argument
808 *
809 * Returns: The negation of the argument.
810 *
811 * Use: Negates its argument.
812 */
813
814extern mp *mp_neg(mp */*d*/, mp */*a*/);
f09e814a 815
d3409d5e 816/* --- @mp_add@ --- *
d03ab969 817 *
d3409d5e 818 * Arguments: @mp *d@ = destination
d9a8ae10 819 * @mp *a, *b@ = sources
d3409d5e 820 *
821 * Returns: Result, @a@ added to @b@.
d03ab969 822 */
823
d9a8ae10 824extern mp *mp_add(mp */*d*/, mp */*a*/, mp */*b*/);
d03ab969 825
d3409d5e 826/* --- @mp_sub@ --- *
827 *
828 * Arguments: @mp *d@ = destination
d9a8ae10 829 * @mp *a, *b@ = sources
d3409d5e 830 *
831 * Returns: Result, @b@ subtracted from @a@.
832 */
d03ab969 833
d9a8ae10 834extern mp *mp_sub(mp */*d*/, mp */*a*/, mp */*b*/);
d3409d5e 835
836/* --- @mp_mul@ --- *
d03ab969 837 *
d3409d5e 838 * Arguments: @mp *d@ = destination
d9a8ae10 839 * @mp *a, *b@ = sources
d03ab969 840 *
d3409d5e 841 * Returns: Result, @a@ multiplied by @b@.
842 */
843
d9a8ae10 844extern mp *mp_mul(mp */*d*/, mp */*a*/, mp */*b*/);
d3409d5e 845
846/* --- @mp_sqr@ --- *
847 *
848 * Arguments: @mp *d@ = destination
d9a8ae10 849 * @mp *a@ = source
d3409d5e 850 *
851 * Returns: Result, @a@ squared.
852 */
853
d9a8ae10 854extern mp *mp_sqr(mp */*d*/, mp */*a*/);
d3409d5e 855
856/* --- @mp_div@ --- *
d03ab969 857 *
d3409d5e 858 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
d9a8ae10 859 * @mp *a, *b@ = sources
d3409d5e 860 *
861 * Use: Calculates the quotient and remainder when @a@ is divided by
862 * @b@.
d03ab969 863 */
864
d9a8ae10 865extern void mp_div(mp **/*qq*/, mp **/*rr*/, mp */*a*/, mp */*b*/);
d3409d5e 866
f4535c64 867/* --- @mp_exp@ --- *
868 *
869 * Arguments: @mp *d@ = fake destination
870 * @mp *a@ = base
871 * @mp *e@ = exponent
872 *
873 * Returns: Result, %$a^e$%.
874 */
875
876extern mp *mp_exp(mp */*d*/, mp */*a*/, mp */*e*/);
877
22a073c0 878/* --- @mp_odd@ --- *
879 *
880 * Arguments: @mp *d@ = pointer to destination integer
881 * @mp *m@ = pointer to source integer
882 * @size_t *s@ = where to store the power of 2
883 *
884 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
885 *
886 * Use: Computes a power of two and an odd integer which, when
887 * multiplied, give a specified result. This sort of thing is
888 * useful in number theory quite often.
889 */
890
891extern mp *mp_odd(mp */*d*/, mp */*m*/, size_t */*s*/);
892
d3409d5e 893/*----- More advanced algorithms ------------------------------------------*/
d03ab969 894
22a073c0 895/* --- @mp_sqrt@ --- *
896 *
897 * Arguments: @mp *d@ = pointer to destination integer
898 * @mp *a@ = (nonnegative) integer to take square root of
899 *
900 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
901 *
902 * Use: Computes integer square roots.
903 *
904 * The current implementation isn't very good: it uses the
905 * Newton-Raphson method to find an approximation to %$a$%. If
906 * there's any demand for a better version, I'll write one.
907 */
908
909extern mp *mp_sqrt(mp */*d*/, mp */*a*/);
910
d3409d5e 911/* --- @mp_gcd@ --- *
d03ab969 912 *
d3409d5e 913 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
914 * @mp *a, *b@ = sources (must be nonzero)
d03ab969 915 *
916 * Returns: ---
917 *
d3409d5e 918 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
919 * @ax + by = gcd(a, b)@. This is useful for computing modular
d9a8ae10 920 * inverses. Neither @a@ nor @b@ may be zero.
d03ab969 921 */
922
d3409d5e 923extern void mp_gcd(mp **/*gcd*/, mp **/*xx*/, mp **/*yy*/,
924 mp */*a*/, mp */*b*/);
925
b817bfc6 926/* -- @mp_modinv@ --- *
927 *
928 * Arguments: @mp *d@ = destination
929 * @mp *x@ = argument
930 * @mp *p@ = modulus
931 *
932 * Returns: The inverse %$x^{-1} \bmod p$%.
933 *
934 * Use: Computes a modular inverse. An assertion fails if %$p$%
935 * has no inverse.
936 */
937
938extern mp *mp_modinv(mp */*d*/, mp */*x*/, mp */*p*/);
939
5b00a0ea 940/* --- @mp_jacobi@ --- *
941 *
6791ed17
MW
942 * Arguments: @mp *a@ = an integer
943 * @mp *n@ = another integer
5b00a0ea 944 *
945 * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
946 *
6791ed17
MW
947 * Use: Computes the Kronecker symbol %$\jacobi{a}{n}$%. If @n@ is
948 * prime, this is the Legendre symbol and is equal to 1 if and
949 * only if @a@ is a quadratic residue mod @n@. The result is
950 * zero if and only if @a@ and @n@ have a common factor greater
951 * than one.
952 *
953 * If @n@ is composite, then this computes the Kronecker symbol
954 *
955 * %$\jacobi{a}{n}=\jacobi{a}{u}\prod_i\jacobi{a}{p_i}^{e_i}$%
956 *
957 * where %$n = u p_0^{e_0} \ldots p_{n-1}^{e_{n-1}}$% is the
958 * prime factorization of %$n$%. The missing bits are:
959 *
960 * * %$\jacobi{a}{1} = 1$%;
961 * * %$\jacobi{a}{-1} = 1$% if @a@ is negative, or 1 if
962 * positive;
963 * * %$\jacobi{a}{0} = 0$%;
075af359 964 * * %$\jacobi{a}{2}$ is 0 if @a@ is even, 1 if @a@ is
6791ed17
MW
965 * congruent to 1 or 7 (mod 8), or %$-1$% otherwise.
966 *
967 * If %$n$% is positive and odd, then this is the Jacobi
968 * symbol. (The Kronecker symbol is a consistant domain
969 * extension; the Jacobi symbol was implemented first, and the
970 * name stuck.)
5b00a0ea 971 */
972
22a073c0 973extern int mp_jacobi(mp */*a*/, mp */*n*/);
974
975/* --- @mp_modsqrt@ --- *
976 *
977 * Arguments: @mp *d@ = destination integer
978 * @mp *a@ = source integer
979 * @mp *p@ = modulus (must be prime)
980 *
981 * Returns: If %$a$% is a quadratic residue, a square root of %$a$%; else
982 * a null pointer.
983 *
984 * Use: Returns an integer %$x$% such that %$x^2 \equiv a \pmod{p}$%,
985 * if one exists; else a null pointer. This function will not
986 * work if %$p$% is composite: you must factor the modulus, take
987 * a square root mod each factor, and recombine the results
988 * using the Chinese Remainder Theorem.
222c8a43
MW
989 *
990 * We guarantee that the square root returned is the smallest
991 * one (i.e., the `positive' square root).
22a073c0 992 */
993
994extern mp *mp_modsqrt(mp */*d*/, mp */*a*/, mp */*p*/);
5b00a0ea 995
ffec4880
MW
996/* --- @mp_modexp@ --- *
997 *
998 * Arguments: @mp *d@ = fake destination
999 * @mp *x@ = base of exponentiation
1000 * @mp *e@ = exponent
1001 * @mp *n@ = modulus (must be positive)
1002 *
1003 * Returns: The value %$x^e \bmod n$%.
1004 */
1005
1006extern mp *mp_modexp(mp */*d*/, mp */*x*/, mp */*e*/, mp */*n*/);
1007
d3409d5e 1008/*----- Test harness support ----------------------------------------------*/
1009
1010#include <mLib/testrig.h>
1011
d9a8ae10 1012#ifndef CATACOMB_MPTEXT_H
d3409d5e 1013# include "mptext.h"
1014#endif
1015
1016extern const test_type type_mp;
d03ab969 1017
1018/*----- That's all, folks -------------------------------------------------*/
1019
1020#ifdef __cplusplus
1021 }
1022#endif
1023
1024#endif