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