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  * SPDX-License-Identifier: Unlicense
11  */
12 #ifndef BN_H_
13 #define BN_H_
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <limits.h>
18 
19 #include "tommath_class.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */
26 #if defined(_MSC_VER) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)
27 #   define MP_32BIT
28 #endif
29 
30 /* detect 64-bit mode if possible */
31 #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \
32     defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
33     defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
34     defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
35     defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
36     defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
37 #   if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
38 #      if defined(__GNUC__) && !defined(__hppa)
39 /* we support 128bit integers only via: __attribute__((mode(TI))) */
40 #         define MP_64BIT
41 #      else
42 /* otherwise we fall back to MP_32BIT even on 64bit platforms */
43 #         define MP_32BIT
44 #      endif
45 #   endif
46 #endif
47 
48 /* some default configurations.
49  *
50  * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
51  * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
52  *
53  * At the very least a mp_digit must be able to hold 7 bits
54  * [any size beyond that is ok provided it doesn't overflow the data type]
55  */
56 #ifdef MP_8BIT
57 typedef unsigned char        mp_digit;
58 typedef unsigned short       mp_word;
59 #   define MP_SIZEOF_MP_DIGIT 1
60 #   ifdef DIGIT_BIT
61 #      error You must not define DIGIT_BIT when using MP_8BIT
62 #   endif
63 #elif defined(MP_16BIT)
64 typedef unsigned short       mp_digit;
65 typedef unsigned int         mp_word;
66 #   define MP_SIZEOF_MP_DIGIT 2
67 #   ifdef DIGIT_BIT
68 #      error You must not define DIGIT_BIT when using MP_16BIT
69 #   endif
70 #elif defined(MP_64BIT)
71 /* for GCC only on supported platforms */
72 typedef unsigned long long   mp_digit;
73 typedef unsigned long        mp_word __attribute__((mode(TI)));
74 #   define DIGIT_BIT 60
75 #else
76 /* this is the default case, 28-bit digits */
77 
78 /* this is to make porting into LibTomCrypt easier :-) */
79 typedef unsigned int         mp_digit;
80 #ifdef _MSC_VER
81 typedef unsigned __int64 mp_word;
82 #else
83 typedef unsigned long long   mp_word; /* PATCHED */
84 #endif
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 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
97 #ifndef DIGIT_BIT
98 #   define DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1))  /* bits per digit */
99 typedef unsigned long mp_min_u32;
100 #else
101 typedef mp_digit mp_min_u32;
102 #endif
103 
104 #define MP_DIGIT_BIT     DIGIT_BIT
105 #define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
106 #define MP_DIGIT_MAX     MP_MASK
107 
108 /* equalities */
109 #define MP_LT        -1   /* less than */
110 #define MP_EQ         0   /* equal to */
111 #define MP_GT         1   /* greater than */
112 
113 #define MP_ZPOS       0   /* positive integer */
114 #define MP_NEG        1   /* negative */
115 
116 #define MP_OKAY       0   /* ok result */
117 #define MP_MEM        -2  /* out of mem */
118 #define MP_VAL        -3  /* invalid input */
119 #define MP_RANGE      MP_VAL
120 #define MP_ITER       -4  /* Max. iterations reached */
121 
122 #define MP_YES        1   /* yes response */
123 #define MP_NO         0   /* no response */
124 
125 /* Primality generation flags */
126 #define LTM_PRIME_BBS      0x0001 /* BBS style prime */
127 #define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */
128 #define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */
129 
130 typedef int           mp_err;
131 
132 /* you'll have to tune these... */
133 extern int KARATSUBA_MUL_CUTOFF,
134        KARATSUBA_SQR_CUTOFF,
135        TOOM_MUL_CUTOFF,
136        TOOM_SQR_CUTOFF;
137 
138 /* define this to use lower memory usage routines (exptmods mostly) */
139 /* #define MP_LOW_MEM */
140 
141 /* default precision */
142 #ifndef MP_PREC
143 #   ifndef MP_LOW_MEM
144 #      define MP_PREC 32        /* default digits of precision */
145 #   else
146 #      define MP_PREC 8         /* default digits of precision */
147 #   endif
148 #endif
149 
150 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
151 #define MP_WARRAY               (1u << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
152 
153 /* the infamous mp_int structure */
154 typedef struct  {
155    int used, alloc, sign;
156    mp_digit *dp;
157 } mp_int;
158 
159 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
160 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
161 
162 
163 #define USED(m)     ((m)->used)
164 #define DIGIT(m, k) ((m)->dp[(k)])
165 #define SIGN(m)     ((m)->sign)
166 
167 /* error code to char* string */
168 const char *mp_error_to_string(int code);
169 
170 /* ---> init and deinit bignum functions <--- */
171 /* init a bignum */
172 int mp_init(mp_int *a);
173 
174 /* free a bignum */
175 void mp_clear(mp_int *a);
176 
177 /* init a null terminated series of arguments */
178 int mp_init_multi(mp_int *mp, ...);
179 
180 /* clear a null terminated series of arguments */
181 void mp_clear_multi(mp_int *mp, ...);
182 
183 /* exchange two ints */
184 void mp_exch(mp_int *a, mp_int *b);
185 
186 /* shrink ram required for a bignum */
187 int mp_shrink(mp_int *a);
188 
189 /* grow an int to a given size */
190 int mp_grow(mp_int *a, int size);
191 
192 /* init to a given number of digits */
193 int mp_init_size(mp_int *a, int size);
194 
195 /* ---> Basic Manipulations <--- */
196 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
197 #define mp_iseven(a) ((((a)->used == 0) || (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
198 #define mp_isodd(a)  ((((a)->used > 0) && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
199 #define mp_isneg(a)  (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
200 
201 /* set to zero */
202 void mp_zero(mp_int *a);
203 
204 /* set to a digit */
205 void mp_set(mp_int *a, mp_digit b);
206 
207 /* set a double */
208 int mp_set_double(mp_int *a, double b);
209 
210 /* set a 32-bit const */
211 int mp_set_int(mp_int *a, unsigned long b);
212 
213 /* set a platform dependent unsigned long value */
214 int mp_set_long(mp_int *a, unsigned long b);
215 
216 /* set a platform dependent unsigned long long value */
217 /* int mp_set_long_long(mp_int *a, unsigned long long b); */
218 
219 /* get a double */
220 double mp_get_double(const mp_int *a);
221 
222 /* get a 32-bit value */
223 unsigned long mp_get_int(const mp_int *a);
224 
225 /* get a platform dependent unsigned long value */
226 unsigned long mp_get_long(const mp_int *a);
227 
228 /* get a platform dependent unsigned long long value */
229 /* unsigned long long mp_get_long_long(const mp_int *a); */
230 
231 /* initialize and set a digit */
232 int mp_init_set(mp_int *a, mp_digit b);
233 
234 /* initialize and set 32-bit value */
235 int mp_init_set_int(mp_int *a, unsigned long b);
236 
237 /* copy, b = a */
238 int mp_copy(const mp_int *a, mp_int *b);
239 
240 /* inits and copies, a = b */
241 int mp_init_copy(mp_int *a, const mp_int *b);
242 
243 /* trim unused digits */
244 void mp_clamp(mp_int *a);
245 
246 /* import binary data */
247 int mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails, const void *op);
248 
249 /* export binary data */
250 int mp_export(void *rop, size_t *countp, int order, size_t size, int endian, size_t nails, const mp_int *op);
251 
252 /* ---> digit manipulation <--- */
253 
254 /* right shift by "b" digits */
255 void mp_rshd(mp_int *a, int b);
256 
257 /* left shift by "b" digits */
258 int mp_lshd(mp_int *a, int b);
259 
260 /* c = a / 2**b, implemented as c = a >> b */
261 int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d);
262 
263 /* b = a/2 */
264 int mp_div_2(const mp_int *a, mp_int *b);
265 
266 /* c = a * 2**b, implemented as c = a << b */
267 int mp_mul_2d(const mp_int *a, int b, mp_int *c);
268 
269 /* b = a*2 */
270 int mp_mul_2(const mp_int *a, mp_int *b);
271 
272 /* c = a mod 2**b */
273 int mp_mod_2d(const mp_int *a, int b, mp_int *c);
274 
275 /* computes a = 2**b */
276 int mp_2expt(mp_int *a, int b);
277 
278 /* Counts the number of lsbs which are zero before the first zero bit */
279 int mp_cnt_lsb(const mp_int *a);
280 
281 /* I Love Earth! */
282 
283 /* makes a pseudo-random int of a given size */
284 int mp_rand(mp_int *a, int digits);
285 
286 #ifdef MP_PRNG_ENABLE_LTM_RNG
287 /* as last resort we will fall back to libtomcrypt's rng_get_bytes()
288  * in case you don't use libtomcrypt or use it w/o rng_get_bytes()
289  * you have to implement it somewhere else, as it's required */
290 extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
291 extern void (*ltm_rng_callback)(void);
292 #endif
293 
294 /* ---> binary operations <--- */
295 /* c = a XOR b  */
296 int mp_xor(const mp_int *a, const mp_int *b, mp_int *c);
297 
298 /* c = a OR b */
299 int mp_or(const mp_int *a, const mp_int *b, mp_int *c);
300 
301 /* c = a AND b */
302 int mp_and(const mp_int *a, const mp_int *b, mp_int *c);
303 
304 /* Checks the bit at position b and returns MP_YES
305    if the bit is 1, MP_NO if it is 0 and MP_VAL
306    in case of error */
307 int mp_get_bit(const mp_int *a, int b);
308 
309 /* c = a XOR b (two complement) */
310 int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c);
311 
312 /* c = a OR b (two complement) */
313 int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c);
314 
315 /* c = a AND b (two complement) */
316 int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c);
317 
318 /* right shift (two complement) */
319 int mp_tc_div_2d(const mp_int *a, int b, mp_int *c);
320 
321 /* ---> Basic arithmetic <--- */
322 
323 /* b = ~a */
324 int mp_complement(const mp_int *a, mp_int *b);
325 
326 /* b = -a */
327 int mp_neg(const mp_int *a, mp_int *b);
328 
329 /* b = |a| */
330 int mp_abs(const mp_int *a, mp_int *b);
331 
332 /* compare a to b */
333 int mp_cmp(const mp_int *a, const mp_int *b);
334 
335 /* compare |a| to |b| */
336 int mp_cmp_mag(const mp_int *a, const mp_int *b);
337 
338 /* c = a + b */
339 int mp_add(const mp_int *a, const mp_int *b, mp_int *c);
340 
341 /* c = a - b */
342 int mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
343 
344 /* c = a * b */
345 int mp_mul(const mp_int *a, const mp_int *b, mp_int *c);
346 
347 /* b = a*a  */
348 int mp_sqr(const mp_int *a, mp_int *b);
349 
350 /* a/b => cb + d == a */
351 int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
352 
353 /* c = a mod b, 0 <= c < b  */
354 int mp_mod(const mp_int *a, const mp_int *b, mp_int *c);
355 
356 /* ---> single digit functions <--- */
357 
358 /* compare against a single digit */
359 int mp_cmp_d(const mp_int *a, mp_digit b);
360 
361 /* c = a + b */
362 int mp_add_d(const mp_int *a, mp_digit b, mp_int *c);
363 
364 /* c = a - b */
365 int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c);
366 
367 /* c = a * b */
368 int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c);
369 
370 /* a/b => cb + d == a */
371 int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
372 
373 /* a/3 => 3c + d == a */
374 int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d);
375 
376 /* c = a**b */
377 int mp_expt_d(const mp_int *a, mp_digit b, mp_int *c);
378 int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast);
379 
380 /* c = a mod b, 0 <= c < b  */
381 int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c);
382 
383 /* ---> number theory <--- */
384 
385 /* d = a + b (mod c) */
386 int mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
387 
388 /* d = a - b (mod c) */
389 int mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
390 
391 /* d = a * b (mod c) */
392 int mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
393 
394 /* c = a * a (mod b) */
395 int mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c);
396 
397 /* c = 1/a (mod b) */
398 int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
399 
400 /* c = (a, b) */
401 int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c);
402 
403 /* produces value such that U1*a + U2*b = U3 */
404 int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
405 
406 /* c = [a, b] or (a*b)/(a, b) */
407 int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c);
408 
409 /* finds one of the b'th root of a, such that |c|**b <= |a|
410  *
411  * returns error if a < 0 and b is even
412  */
413 int mp_n_root(const mp_int *a, mp_digit b, mp_int *c);
414 int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast);
415 
416 /* special sqrt algo */
417 int mp_sqrt(const mp_int *arg, mp_int *ret);
418 
419 /* special sqrt (mod prime) */
420 int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret);
421 
422 /* is number a square? */
423 int mp_is_square(const mp_int *arg, int *ret);
424 
425 /* computes the jacobi c = (a | n) (or Legendre if b is prime)  */
426 int mp_jacobi(const mp_int *a, const mp_int *n, int *c);
427 
428 /* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */
429 int mp_kronecker(const mp_int *a, const mp_int *p, int *c);
430 
431 /* used to setup the Barrett reduction for a given modulus b */
432 int mp_reduce_setup(mp_int *a, const mp_int *b);
433 
434 /* Barrett Reduction, computes a (mod b) with a precomputed value c
435  *
436  * Assumes that 0 < x <= m*m, note if 0 > x > -(m*m) then you can merely
437  * compute the reduction as -1 * mp_reduce(mp_abs(x)) [pseudo code].
438  */
439 int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu);
440 
441 /* setups the montgomery reduction */
442 int mp_montgomery_setup(const mp_int *n, mp_digit *rho);
443 
444 /* computes a = B**n mod b without division or multiplication useful for
445  * normalizing numbers in a Montgomery system.
446  */
447 int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b);
448 
449 /* computes x/R == x (mod N) via Montgomery Reduction */
450 int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);
451 
452 /* returns 1 if a is a valid DR modulus */
453 int mp_dr_is_modulus(const mp_int *a);
454 
455 /* sets the value of "d" required for mp_dr_reduce */
456 void mp_dr_setup(const mp_int *a, mp_digit *d);
457 
458 /* reduces a modulo n using the Diminished Radix method */
459 int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k);
460 
461 /* returns true if a can be reduced with mp_reduce_2k */
462 int mp_reduce_is_2k(const mp_int *a);
463 
464 /* determines k value for 2k reduction */
465 int mp_reduce_2k_setup(const mp_int *a, mp_digit *d);
466 
467 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
468 int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d);
469 
470 /* returns true if a can be reduced with mp_reduce_2k_l */
471 int mp_reduce_is_2k_l(const mp_int *a);
472 
473 /* determines k value for 2k reduction */
474 int mp_reduce_2k_setup_l(const mp_int *a, mp_int *d);
475 
476 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
477 int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d);
478 
479 /* Y = G**X (mod P) */
480 int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y);
481 
482 /* ---> Primes <--- */
483 
484 /* number of primes */
485 #ifdef MP_8BIT
486 #  define PRIME_SIZE 31
487 #else
488 #  define PRIME_SIZE 256
489 #endif
490 
491 /* table of first PRIME_SIZE primes */
492 extern const mp_digit ltm_prime_tab[PRIME_SIZE];
493 
494 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
495 int mp_prime_is_divisible(const mp_int *a, int *result);
496 
497 /* performs one Fermat test of "a" using base "b".
498  * Sets result to 0 if composite or 1 if probable prime
499  */
500 int mp_prime_fermat(const mp_int *a, const mp_int *b, int *result);
501 
502 /* performs one Miller-Rabin test of "a" using base "b".
503  * Sets result to 0 if composite or 1 if probable prime
504  */
505 int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result);
506 
507 /* This gives [for a given bit size] the number of trials required
508  * such that Miller-Rabin gives a prob of failure lower than 2^-96
509  */
510 int mp_prime_rabin_miller_trials(int size);
511 
512 /* performs one strong Lucas-Selfridge test of "a".
513  * Sets result to 0 if composite or 1 if probable prime
514  */
515 int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result);
516 
517 /* performs one Frobenius test of "a" as described by Paul Underwood.
518  * Sets result to 0 if composite or 1 if probable prime
519  */
520 int mp_prime_frobenius_underwood(const mp_int *N, int *result);
521 
522 /* performs t random rounds of Miller-Rabin on "a" additional to
523  * bases 2 and 3.  Also performs an initial sieve of trial
524  * division.  Determines if "a" is prime with probability
525  * of error no more than (1/4)**t.
526  * Both a strong Lucas-Selfridge to complete the BPSW test
527  * and a separate Frobenius test are available at compile time.
528  * With t<0 a deterministic test is run for primes up to
529  * 318665857834031151167461. With t<13 (abs(t)-13) additional
530  * tests with sequential small primes are run starting at 43.
531  * Is Fips 186.4 compliant if called with t as computed by
532  * mp_prime_rabin_miller_trials();
533  *
534  * Sets result to 1 if probably prime, 0 otherwise
535  */
536 int mp_prime_is_prime(const mp_int *a, int t, int *result);
537 
538 /* finds the next prime after the number "a" using "t" trials
539  * of Miller-Rabin.
540  *
541  * bbs_style = 1 means the prime must be congruent to 3 mod 4
542  */
543 int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
544 
545 /* makes a truly random prime of a given size (bytes),
546  * call with bbs = 1 if you want it to be congruent to 3 mod 4
547  *
548  * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
549  * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
550  * so it can be NULL
551  *
552  * The prime generated will be larger than 2^(8*size).
553  */
554 #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)
555 
556 /* makes a truly random prime of a given size (bits),
557  *
558  * Flags are as follows:
559  *
560  *   LTM_PRIME_BBS      - make prime congruent to 3 mod 4
561  *   LTM_PRIME_SAFE     - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
562  *   LTM_PRIME_2MSB_ON  - make the 2nd highest bit one
563  *
564  * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
565  * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
566  * so it can be NULL
567  *
568  */
569 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
570 
571 /* ---> radix conversion <--- */
572 int mp_count_bits(const mp_int *a);
573 
574 int mp_unsigned_bin_size(const mp_int *a);
575 int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
576 int mp_to_unsigned_bin(const mp_int *a, unsigned char *b);
577 int mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen);
578 
579 int mp_signed_bin_size(const mp_int *a);
580 int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c);
581 int mp_to_signed_bin(const mp_int *a,  unsigned char *b);
582 int mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen);
583 
584 int mp_read_radix(mp_int *a, const char *str, int radix);
585 int mp_toradix(const mp_int *a, char *str, int radix);
586 int mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen);
587 int mp_radix_size(const mp_int *a, int radix, int *size);
588 
589 #ifndef LTM_NO_FILE
590 int mp_fread(mp_int *a, int radix, FILE *stream);
591 int mp_fwrite(const mp_int *a, int radix, FILE *stream);
592 #endif
593 
594 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
595 #define mp_raw_size(mp)           mp_signed_bin_size(mp)
596 #define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str))
597 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
598 #define mp_mag_size(mp)           mp_unsigned_bin_size(mp)
599 #define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str))
600 
601 #define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
602 #define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
603 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
604 #define mp_tohex(M, S)     mp_toradix((M), (S), 16)
605 
606 #ifdef __cplusplus
607 }
608 #endif
609 
610 #endif
611 
612 
613 /* ref:         $Format:%D$ */
614 /* git commit:  $Format:%H$ */
615 /* commit time: $Format:%ai$ */
616