~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/tommath.h

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2
 
 *
3
 
 * LibTomMath is a library that provides multiple-precision
4
 
 * integer arithmetic as well as number theoretic functionality.
5
 
 *
6
 
 * The library was designed directly after the MPI library by
7
 
 * Michael Fromberger but has been written from scratch with
8
 
 * additional optimizations in place.
9
 
 *
10
 
 * The library is free for all purposes without any express
11
 
 * guarantee it works.
12
 
 *
13
 
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14
 
 */
15
 
#ifndef BN_H_
16
 
#define BN_H_
17
 
 
18
 
#include <stdio.h>
19
 
#include <string.h>
20
 
#include <stdlib.h>
21
 
#include <ctype.h>
22
 
#include <limits.h>
23
 
 
24
 
#define NO_LTM_TOOM 1
25
 
 
26
 
#undef MIN
27
 
#define MIN(x,y) ((x)<(y)?(x):(y))
28
 
#undef MAX
29
 
#define MAX(x,y) ((x)>(y)?(x):(y))
30
 
 
31
 
#ifdef __cplusplus
32
 
extern "C" {
33
 
 
34
 
/* C++ compilers don't like assigning void * to mp_digit * */
35
 
#define  OPT_CAST(x)  (x *)
36
 
 
37
 
#else
38
 
 
39
 
/* C on the other hand doesn't care */
40
 
#define  OPT_CAST(x)
41
 
 
42
 
#endif
43
 
 
44
 
/* some default configurations.
45
 
 *
46
 
 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
47
 
 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
48
 
 *
49
 
 * At the very least a mp_digit must be able to hold 7 bits
50
 
 * [any size beyond that is ok provided it doesn't overflow the data type]
51
 
 */
52
 
#ifdef MP_8BIT
53
 
   typedef unsigned char      mp_digit;
54
 
   typedef unsigned short     mp_word;
55
 
#elif defined(MP_16BIT)
56
 
   typedef unsigned short     mp_digit;
57
 
   typedef unsigned long      mp_word;
58
 
#elif defined(MP_64BIT)
59
 
   /* for GCC only on supported platforms */
60
 
#ifndef CRYPT
61
 
   typedef unsigned long long ulong64;
62
 
   typedef signed long long   long64;
63
 
#endif
64
 
 
65
 
   typedef ulong64            mp_digit;
66
 
   typedef unsigned long      mp_word __attribute__ ((mode(TI)));
67
 
 
68
 
   #define DIGIT_BIT          60
69
 
#else
70
 
   /* this is the default case, 28-bit digits */
71
 
   
72
 
   /* this is to make porting into LibTomCrypt easier :-) */
73
 
#ifndef CRYPT
74
 
   #if defined(_MSC_VER) || defined(__BORLANDC__) 
75
 
      typedef unsigned __int64   ulong64;
76
 
      typedef signed __int64     long64;
77
 
   #else
78
 
      typedef unsigned long long ulong64;
79
 
      typedef signed long long   long64;
80
 
   #endif
81
 
#endif
82
 
 
83
 
   typedef unsigned long      mp_digit;
84
 
   typedef ulong64            mp_word;
85
 
 
86
 
#ifdef MP_31BIT   
87
 
   /* this is an extension that uses 31-bit digits */
88
 
   #define DIGIT_BIT          31
89
 
#else
90
 
   /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
91
 
   #define DIGIT_BIT          28
92
 
   #define MP_28BIT
93
 
#endif   
94
 
#endif
95
 
 
96
 
/* define heap macros */
97
 
#ifndef CRYPT
98
 
   /* default to libc stuff */
99
 
   #ifndef XMALLOC 
100
 
       #define XMALLOC  malloc
101
 
       #define XFREE    free
102
 
       #define XREALLOC realloc
103
 
       #define XCALLOC  calloc
104
 
   #else
105
 
      /* prototypes for our heap functions */
106
 
      extern void *XMALLOC(size_t n);
107
 
      extern void *REALLOC(void *p, size_t n);
108
 
      extern void *XCALLOC(size_t n, size_t s);
109
 
      extern void XFREE(void *p);
110
 
   #endif
111
 
#endif
112
 
 
113
 
 
114
 
/* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
115
 
#ifndef DIGIT_BIT
116
 
   #define DIGIT_BIT     ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))  /* bits per digit */
117
 
#endif
118
 
 
119
 
#define MP_DIGIT_BIT     DIGIT_BIT
120
 
#define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
121
 
#define MP_DIGIT_MAX     MP_MASK
122
 
 
123
 
/* equalities */
124
 
#define MP_LT        -1   /* less than */
125
 
#define MP_EQ         0   /* equal to */
126
 
#define MP_GT         1   /* greater than */
127
 
 
128
 
#define MP_ZPOS       0   /* positive integer */
129
 
#define MP_NEG        1   /* negative */
130
 
 
131
 
#define MP_OKAY       0   /* ok result */
132
 
#define MP_MEM        -2  /* out of mem */
133
 
#define MP_VAL        -3  /* invalid input */
134
 
#define MP_RANGE      MP_VAL
135
 
 
136
 
#define MP_YES        1   /* yes response */
137
 
#define MP_NO         0   /* no response */
138
 
 
139
 
/* Primality generation flags */
140
 
#define LTM_PRIME_BBS      0x0001 /* BBS style prime */
141
 
#define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */
142
 
#define LTM_PRIME_2MSB_OFF 0x0004 /* force 2nd MSB to 0 */
143
 
#define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */
144
 
 
145
 
typedef int           mp_err;
146
 
 
147
 
/* you'll have to tune these... */
148
 
extern int KARATSUBA_MUL_CUTOFF,
149
 
           KARATSUBA_SQR_CUTOFF,
150
 
           TOOM_MUL_CUTOFF,
151
 
           TOOM_SQR_CUTOFF;
152
 
 
153
 
/* define this to use lower memory usage routines (exptmods mostly) */
154
 
/* #define MP_LOW_MEM */
155
 
 
156
 
/* default precision */
157
 
#ifndef MP_PREC
158
 
   #ifdef MP_LOW_MEM
159
 
      #define MP_PREC                 64     /* default digits of precision */
160
 
   #else
161
 
      #define MP_PREC                 8      /* default digits of precision */
162
 
   #endif   
163
 
#endif
164
 
 
165
 
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
166
 
#define MP_WARRAY               (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
167
 
 
168
 
/* the infamous mp_int structure */
169
 
typedef struct  {
170
 
    int used, alloc, sign;
171
 
    mp_digit *dp;
172
 
} mp_int;
173
 
 
174
 
/* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
175
 
typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
176
 
 
177
 
 
178
 
#define USED(m)    ((m)->used)
179
 
#define DIGIT(m,k) ((m)->dp[(k)])
180
 
#define SIGN(m)    ((m)->sign)
181
 
 
182
 
/* error code to char* string */
183
 
char *mp_error_to_string(int code);
184
 
 
185
 
/* ---> init and deinit bignum functions <--- */
186
 
/* init a bignum */
187
 
int mp_init(mp_int *a);
188
 
 
189
 
/* free a bignum */
190
 
void mp_clear(mp_int *a);
191
 
 
192
 
/* init a null terminated series of arguments */
193
 
int mp_init_multi(mp_int *mp, ...);
194
 
 
195
 
/* clear a null terminated series of arguments */
196
 
void mp_clear_multi(mp_int *mp, ...);
197
 
 
198
 
/* exchange two ints */
199
 
void mp_exch(mp_int *a, mp_int *b);
200
 
 
201
 
/* shrink ram required for a bignum */
202
 
int mp_shrink(mp_int *a);
203
 
 
204
 
/* grow an int to a given size */
205
 
int mp_grow(mp_int *a, int size);
206
 
 
207
 
/* init to a given number of digits */
208
 
int mp_init_size(mp_int *a, int size);
209
 
 
210
 
/* ---> Basic Manipulations <--- */
211
 
#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
212
 
#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
213
 
#define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
214
 
 
215
 
/* set to zero */
216
 
void mp_zero(mp_int *a);
217
 
 
218
 
/* set to a digit */
219
 
void mp_set(mp_int *a, mp_digit b);
220
 
 
221
 
/* set a 32-bit const */
222
 
int mp_set_int(mp_int *a, unsigned long b);
223
 
 
224
 
/* get a 32-bit value */
225
 
unsigned long mp_get_int(mp_int * a);
226
 
 
227
 
/* initialize and set a digit */
228
 
int mp_init_set (mp_int * a, mp_digit b);
229
 
 
230
 
/* initialize and set 32-bit value */
231
 
int mp_init_set_int (mp_int * a, unsigned long b);
232
 
 
233
 
/* copy, b = a */
234
 
int mp_copy(mp_int *a, mp_int *b);
235
 
 
236
 
/* inits and copies, a = b */
237
 
int mp_init_copy(mp_int *a, mp_int *b);
238
 
 
239
 
/* trim unused digits */
240
 
void mp_clamp(mp_int *a);
241
 
 
242
 
/* ---> digit manipulation <--- */
243
 
 
244
 
/* right shift by "b" digits */
245
 
void mp_rshd(mp_int *a, int b);
246
 
 
247
 
/* left shift by "b" digits */
248
 
int mp_lshd(mp_int *a, int b);
249
 
 
250
 
/* c = a / 2**b */
251
 
int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
252
 
 
253
 
/* b = a/2 */
254
 
int mp_div_2(mp_int *a, mp_int *b);
255
 
 
256
 
/* c = a * 2**b */
257
 
int mp_mul_2d(mp_int *a, int b, mp_int *c);
258
 
 
259
 
/* b = a*2 */
260
 
int mp_mul_2(mp_int *a, mp_int *b);
261
 
 
262
 
/* c = a mod 2**d */
263
 
int mp_mod_2d(mp_int *a, int b, mp_int *c);
264
 
 
265
 
/* computes a = 2**b */
266
 
int mp_2expt(mp_int *a, int b);
267
 
 
268
 
/* Counts the number of lsbs which are zero before the first zero bit */
269
 
int mp_cnt_lsb(mp_int *a);
270
 
 
271
 
/* I Love Earth! */
272
 
 
273
 
/* makes a pseudo-random int of a given size */
274
 
int mp_rand(mp_int *a, int digits);
275
 
 
276
 
/* ---> binary operations <--- */
277
 
/* c = a XOR b  */
278
 
int mp_xor(mp_int *a, mp_int *b, mp_int *c);
279
 
 
280
 
/* c = a OR b */
281
 
int mp_or(mp_int *a, mp_int *b, mp_int *c);
282
 
 
283
 
/* c = a AND b */
284
 
int mp_and(mp_int *a, mp_int *b, mp_int *c);
285
 
 
286
 
/* ---> Basic arithmetic <--- */
287
 
 
288
 
/* b = -a */
289
 
int mp_neg(mp_int *a, mp_int *b);
290
 
 
291
 
/* b = |a| */
292
 
int mp_abs(mp_int *a, mp_int *b);
293
 
 
294
 
/* compare a to b */
295
 
int mp_cmp(mp_int *a, mp_int *b);
296
 
 
297
 
/* compare |a| to |b| */
298
 
int mp_cmp_mag(mp_int *a, mp_int *b);
299
 
 
300
 
/* c = a + b */
301
 
int mp_add(mp_int *a, mp_int *b, mp_int *c);
302
 
 
303
 
/* c = a - b */
304
 
int mp_sub(mp_int *a, mp_int *b, mp_int *c);
305
 
 
306
 
/* c = a * b */
307
 
int mp_mul(mp_int *a, mp_int *b, mp_int *c);
308
 
 
309
 
/* b = a*a  */
310
 
int mp_sqr(mp_int *a, mp_int *b);
311
 
 
312
 
/* a/b => cb + d == a */
313
 
int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
314
 
 
315
 
/* c = a mod b, 0 <= c < b  */
316
 
int mp_mod(mp_int *a, mp_int *b, mp_int *c);
317
 
 
318
 
/* ---> single digit functions <--- */
319
 
 
320
 
/* compare against a single digit */
321
 
int mp_cmp_d(mp_int *a, mp_digit b);
322
 
 
323
 
/* c = a + b */
324
 
int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
325
 
 
326
 
/* c = a - b */
327
 
int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
328
 
 
329
 
/* c = a * b */
330
 
int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
331
 
 
332
 
/* a/b => cb + d == a */
333
 
int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
334
 
 
335
 
/* a/3 => 3c + d == a */
336
 
int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
337
 
 
338
 
/* c = a**b */
339
 
int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
340
 
 
341
 
/* c = a mod b, 0 <= c < b  */
342
 
int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
343
 
 
344
 
/* ---> number theory <--- */
345
 
 
346
 
/* d = a + b (mod c) */
347
 
int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
348
 
 
349
 
/* d = a - b (mod c) */
350
 
int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
351
 
 
352
 
/* d = a * b (mod c) */
353
 
int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
354
 
 
355
 
/* c = a * a (mod b) */
356
 
int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
357
 
 
358
 
/* c = 1/a (mod b) */
359
 
int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
360
 
 
361
 
/* c = (a, b) */
362
 
int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
363
 
 
364
 
/* produces value such that U1*a + U2*b = U3 */
365
 
int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
366
 
 
367
 
/* c = [a, b] or (a*b)/(a, b) */
368
 
int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
369
 
 
370
 
/* finds one of the b'th root of a, such that |c|**b <= |a|
371
 
 *
372
 
 * returns error if a < 0 and b is even
373
 
 */
374
 
int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
375
 
 
376
 
/* special sqrt algo */
377
 
int mp_sqrt(mp_int *arg, mp_int *ret);
378
 
 
379
 
/* is number a square? */
380
 
int mp_is_square(mp_int *arg, int *ret);
381
 
 
382
 
/* computes the jacobi c = (a | n) (or Legendre if b is prime)  */
383
 
int mp_jacobi(mp_int *a, mp_int *n, int *c);
384
 
 
385
 
/* used to setup the Barrett reduction for a given modulus b */
386
 
int mp_reduce_setup(mp_int *a, mp_int *b);
387
 
 
388
 
/* Barrett Reduction, computes a (mod b) with a precomputed value c
389
 
 *
390
 
 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
391
 
 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
392
 
 */
393
 
int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
394
 
 
395
 
/* setups the montgomery reduction */
396
 
int mp_montgomery_setup(mp_int *a, mp_digit *mp);
397
 
 
398
 
/* computes a = B**n mod b without division or multiplication useful for
399
 
 * normalizing numbers in a Montgomery system.
400
 
 */
401
 
int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
402
 
 
403
 
/* computes x/R == x (mod N) via Montgomery Reduction */
404
 
int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
405
 
 
406
 
/* returns 1 if a is a valid DR modulus */
407
 
int mp_dr_is_modulus(mp_int *a);
408
 
 
409
 
/* sets the value of "d" required for mp_dr_reduce */
410
 
void mp_dr_setup(mp_int *a, mp_digit *d);
411
 
 
412
 
/* reduces a modulo b using the Diminished Radix method */
413
 
int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
414
 
 
415
 
/* returns true if a can be reduced with mp_reduce_2k */
416
 
int mp_reduce_is_2k(mp_int *a);
417
 
 
418
 
/* determines k value for 2k reduction */
419
 
int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
420
 
 
421
 
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
422
 
int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
423
 
 
424
 
/* d = a**b (mod c) */
425
 
int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
426
 
 
427
 
/* ---> Primes <--- */
428
 
 
429
 
/* number of primes */
430
 
#ifdef MP_8BIT
431
 
   #define PRIME_SIZE      31
432
 
#else
433
 
   #define PRIME_SIZE      256
434
 
#endif
435
 
 
436
 
/* table of first PRIME_SIZE primes */
437
 
extern const mp_digit __prime_tab[];
438
 
 
439
 
/* result=1 if a is divisible by one of the first PRIME_SIZE primes */
440
 
int mp_prime_is_divisible(mp_int *a, int *result);
441
 
 
442
 
/* performs one Fermat test of "a" using base "b".
443
 
 * Sets result to 0 if composite or 1 if probable prime
444
 
 */
445
 
int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
446
 
 
447
 
/* performs one Miller-Rabin test of "a" using base "b".
448
 
 * Sets result to 0 if composite or 1 if probable prime
449
 
 */
450
 
int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
451
 
 
452
 
/* This gives [for a given bit size] the number of trials required
453
 
 * such that Miller-Rabin gives a prob of failure lower than 2^-96 
454
 
 */
455
 
int mp_prime_rabin_miller_trials(int size);
456
 
 
457
 
/* performs t rounds of Miller-Rabin on "a" using the first
458
 
 * t prime bases.  Also performs an initial sieve of trial
459
 
 * division.  Determines if "a" is prime with probability
460
 
 * of error no more than (1/4)**t.
461
 
 *
462
 
 * Sets result to 1 if probably prime, 0 otherwise
463
 
 */
464
 
int mp_prime_is_prime(mp_int *a, int t, int *result);
465
 
 
466
 
/* finds the next prime after the number "a" using "t" trials
467
 
 * of Miller-Rabin.
468
 
 *
469
 
 * bbs_style = 1 means the prime must be congruent to 3 mod 4
470
 
 */
471
 
int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
472
 
 
473
 
/* makes a truly random prime of a given size (bytes),
474
 
 * call with bbs = 1 if you want it to be congruent to 3 mod 4 
475
 
 *
476
 
 * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
477
 
 * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
478
 
 * so it can be NULL
479
 
 *
480
 
 * The prime generated will be larger than 2^(8*size).
481
 
 */
482
 
#define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
483
 
 
484
 
/* makes a truly random prime of a given size (bits),
485
 
 *
486
 
 * Flags are as follows:
487
 
 * 
488
 
 *   LTM_PRIME_BBS      - make prime congruent to 3 mod 4
489
 
 *   LTM_PRIME_SAFE     - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
490
 
 *   LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
491
 
 *   LTM_PRIME_2MSB_ON  - make the 2nd highest bit one
492
 
 *
493
 
 * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
494
 
 * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
495
 
 * so it can be NULL
496
 
 *
497
 
 */
498
 
int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
499
 
 
500
 
/* ---> radix conversion <--- */
501
 
int mp_count_bits(mp_int *a);
502
 
 
503
 
int mp_unsigned_bin_size(mp_int *a);
504
 
int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
505
 
int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
506
 
 
507
 
int mp_signed_bin_size(mp_int *a);
508
 
int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
509
 
int mp_to_signed_bin(mp_int *a, unsigned char *b);
510
 
 
511
 
int mp_read_radix(mp_int *a, char *str, int radix);
512
 
int mp_toradix(mp_int *a, char *str, int radix);
513
 
int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
514
 
int mp_radix_size(mp_int *a, int radix, int *size);
515
 
 
516
 
int mp_fread(mp_int *a, int radix, FILE *stream);
517
 
int mp_fwrite(mp_int *a, int radix, FILE *stream);
518
 
 
519
 
#define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
520
 
#define mp_raw_size(mp)           mp_signed_bin_size(mp)
521
 
#define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str))
522
 
#define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
523
 
#define mp_mag_size(mp)           mp_unsigned_bin_size(mp)
524
 
#define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str))
525
 
 
526
 
#define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
527
 
#define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
528
 
#define mp_todecimal(M, S) mp_toradix((M), (S), 10)
529
 
#define mp_tohex(M, S)     mp_toradix((M), (S), 16)
530
 
 
531
 
/* lowlevel functions, do not call! */
532
 
int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
533
 
int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
534
 
#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
535
 
int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
536
 
int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
537
 
int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
538
 
int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
539
 
int fast_s_mp_sqr(mp_int *a, mp_int *b);
540
 
int s_mp_sqr(mp_int *a, mp_int *b);
541
 
int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
542
 
int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
543
 
int mp_karatsuba_sqr(mp_int *a, mp_int *b);
544
 
int mp_toom_sqr(mp_int *a, mp_int *b);
545
 
int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
546
 
int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
547
 
int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
548
 
int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
549
 
void bn_reverse(unsigned char *s, int len);
550
 
 
551
 
extern const char *mp_s_rmap;
552
 
 
553
 
#ifdef __cplusplus
554
 
   }
555
 
#endif
556
 
 
557
 
#endif
558