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