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