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