michael@0: /* michael@0: * mpi.h michael@0: * michael@0: * Arbitrary precision integer arithmetic library michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef _H_MPI_ michael@0: #define _H_MPI_ michael@0: michael@0: #include "mpi-config.h" michael@0: michael@0: #if MP_DEBUG michael@0: #undef MP_IOFUNC michael@0: #define MP_IOFUNC 1 michael@0: #endif michael@0: michael@0: #if MP_IOFUNC michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #if defined(BSDI) michael@0: #undef ULLONG_MAX michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #define MP_NEG 1 michael@0: #define MP_ZPOS 0 michael@0: michael@0: #define MP_OKAY 0 /* no error, all is well */ michael@0: #define MP_YES 0 /* yes (boolean result) */ michael@0: #define MP_NO -1 /* no (boolean result) */ michael@0: #define MP_MEM -2 /* out of memory */ michael@0: #define MP_RANGE -3 /* argument out of range */ michael@0: #define MP_BADARG -4 /* invalid parameter */ michael@0: #define MP_UNDEF -5 /* answer is undefined */ michael@0: #define MP_LAST_CODE MP_UNDEF michael@0: michael@0: typedef unsigned int mp_sign; michael@0: typedef unsigned int mp_size; michael@0: typedef int mp_err; michael@0: michael@0: #define MP_32BIT_MAX 4294967295U michael@0: michael@0: #if !defined(ULONG_MAX) michael@0: #error "ULONG_MAX not defined" michael@0: #elif !defined(UINT_MAX) michael@0: #error "UINT_MAX not defined" michael@0: #elif !defined(USHRT_MAX) michael@0: #error "USHRT_MAX not defined" michael@0: #endif michael@0: michael@0: #if defined(ULLONG_MAX) /* C99, Solaris */ michael@0: #define MP_ULONG_LONG_MAX ULLONG_MAX michael@0: /* MP_ULONG_LONG_MAX was defined to be ULLONG_MAX */ michael@0: #elif defined(ULONG_LONG_MAX) /* HPUX */ michael@0: #define MP_ULONG_LONG_MAX ULONG_LONG_MAX michael@0: #elif defined(ULONGLONG_MAX) /* IRIX, AIX */ michael@0: #define MP_ULONG_LONG_MAX ULONGLONG_MAX michael@0: #endif michael@0: michael@0: /* We only use unsigned long for mp_digit iff long is more than 32 bits. */ michael@0: #if !defined(MP_USE_UINT_DIGIT) && ULONG_MAX > MP_32BIT_MAX michael@0: typedef unsigned long mp_digit; michael@0: #define MP_DIGIT_MAX ULONG_MAX michael@0: #define MP_DIGIT_FMT "%016lX" /* printf() format for 1 digit */ michael@0: #define MP_HALF_DIGIT_MAX UINT_MAX michael@0: #undef MP_NO_MP_WORD michael@0: #define MP_NO_MP_WORD 1 michael@0: #undef MP_USE_LONG_DIGIT michael@0: #define MP_USE_LONG_DIGIT 1 michael@0: #undef MP_USE_LONG_LONG_DIGIT michael@0: michael@0: #elif !defined(MP_USE_UINT_DIGIT) && defined(MP_ULONG_LONG_MAX) michael@0: typedef unsigned long long mp_digit; michael@0: #define MP_DIGIT_MAX MP_ULONG_LONG_MAX michael@0: #define MP_DIGIT_FMT "%016llX" /* printf() format for 1 digit */ michael@0: #define MP_HALF_DIGIT_MAX UINT_MAX michael@0: #undef MP_NO_MP_WORD michael@0: #define MP_NO_MP_WORD 1 michael@0: #undef MP_USE_LONG_LONG_DIGIT michael@0: #define MP_USE_LONG_LONG_DIGIT 1 michael@0: #undef MP_USE_LONG_DIGIT michael@0: michael@0: #else michael@0: typedef unsigned int mp_digit; michael@0: #define MP_DIGIT_MAX UINT_MAX michael@0: #define MP_DIGIT_FMT "%08X" /* printf() format for 1 digit */ michael@0: #define MP_HALF_DIGIT_MAX USHRT_MAX michael@0: #undef MP_USE_UINT_DIGIT michael@0: #define MP_USE_UINT_DIGIT 1 michael@0: #undef MP_USE_LONG_LONG_DIGIT michael@0: #undef MP_USE_LONG_DIGIT michael@0: #endif michael@0: michael@0: #if !defined(MP_NO_MP_WORD) michael@0: #if defined(MP_USE_UINT_DIGIT) && \ michael@0: (defined(MP_ULONG_LONG_MAX) || (ULONG_MAX > UINT_MAX)) michael@0: michael@0: #if (ULONG_MAX > UINT_MAX) michael@0: typedef unsigned long mp_word; michael@0: typedef long mp_sword; michael@0: #define MP_WORD_MAX ULONG_MAX michael@0: michael@0: #else michael@0: typedef unsigned long long mp_word; michael@0: typedef long long mp_sword; michael@0: #define MP_WORD_MAX MP_ULONG_LONG_MAX michael@0: #endif michael@0: michael@0: #else michael@0: #define MP_NO_MP_WORD 1 michael@0: #endif michael@0: #endif /* !defined(MP_NO_MP_WORD) */ michael@0: michael@0: #if !defined(MP_WORD_MAX) && defined(MP_DEFINE_SMALL_WORD) michael@0: typedef unsigned int mp_word; michael@0: typedef int mp_sword; michael@0: #define MP_WORD_MAX UINT_MAX michael@0: #endif michael@0: michael@0: #define MP_DIGIT_BIT (CHAR_BIT*sizeof(mp_digit)) michael@0: #define MP_WORD_BIT (CHAR_BIT*sizeof(mp_word)) michael@0: #define MP_RADIX (1+(mp_word)MP_DIGIT_MAX) michael@0: michael@0: #define MP_HALF_DIGIT_BIT (MP_DIGIT_BIT/2) michael@0: #define MP_HALF_RADIX (1+(mp_digit)MP_HALF_DIGIT_MAX) michael@0: /* MP_HALF_RADIX really ought to be called MP_SQRT_RADIX, but it's named michael@0: ** MP_HALF_RADIX because it's the radix for MP_HALF_DIGITs, and it's michael@0: ** consistent with the other _HALF_ names. michael@0: */ michael@0: michael@0: michael@0: /* Macros for accessing the mp_int internals */ michael@0: #define MP_SIGN(MP) ((MP)->sign) michael@0: #define MP_USED(MP) ((MP)->used) michael@0: #define MP_ALLOC(MP) ((MP)->alloc) michael@0: #define MP_DIGITS(MP) ((MP)->dp) michael@0: #define MP_DIGIT(MP,N) (MP)->dp[(N)] michael@0: michael@0: /* This defines the maximum I/O base (minimum is 2) */ michael@0: #define MP_MAX_RADIX 64 michael@0: michael@0: typedef struct { michael@0: mp_sign sign; /* sign of this quantity */ michael@0: mp_size alloc; /* how many digits allocated */ michael@0: mp_size used; /* how many digits used */ michael@0: mp_digit *dp; /* the digits themselves */ michael@0: } mp_int; michael@0: michael@0: /* Default precision */ michael@0: mp_size mp_get_prec(void); michael@0: void mp_set_prec(mp_size prec); michael@0: michael@0: /* Memory management */ michael@0: mp_err mp_init(mp_int *mp); michael@0: mp_err mp_init_size(mp_int *mp, mp_size prec); michael@0: mp_err mp_init_copy(mp_int *mp, const mp_int *from); michael@0: mp_err mp_copy(const mp_int *from, mp_int *to); michael@0: void mp_exch(mp_int *mp1, mp_int *mp2); michael@0: void mp_clear(mp_int *mp); michael@0: void mp_zero(mp_int *mp); michael@0: void mp_set(mp_int *mp, mp_digit d); michael@0: mp_err mp_set_int(mp_int *mp, long z); michael@0: #define mp_set_long(mp,z) mp_set_int(mp,z) michael@0: mp_err mp_set_ulong(mp_int *mp, unsigned long z); michael@0: michael@0: /* Single digit arithmetic */ michael@0: mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b); michael@0: mp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b); michael@0: mp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b); michael@0: mp_err mp_mul_2(const mp_int *a, mp_int *c); michael@0: mp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r); michael@0: mp_err mp_div_2(const mp_int *a, mp_int *c); michael@0: mp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c); michael@0: michael@0: /* Sign manipulations */ michael@0: mp_err mp_abs(const mp_int *a, mp_int *b); michael@0: mp_err mp_neg(const mp_int *a, mp_int *b); michael@0: michael@0: /* Full arithmetic */ michael@0: mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c); michael@0: mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c); michael@0: mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c); michael@0: #if MP_SQUARE michael@0: mp_err mp_sqr(const mp_int *a, mp_int *b); michael@0: #else michael@0: #define mp_sqr(a, b) mp_mul(a, a, b) michael@0: #endif michael@0: mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r); michael@0: mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r); michael@0: mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c); michael@0: mp_err mp_2expt(mp_int *a, mp_digit k); michael@0: mp_err mp_sqrt(const mp_int *a, mp_int *b); michael@0: michael@0: /* Modular arithmetic */ michael@0: #if MP_MODARITH michael@0: mp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c); michael@0: mp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c); michael@0: mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c); michael@0: mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c); michael@0: mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c); michael@0: #if MP_SQUARE michael@0: mp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c); michael@0: #else michael@0: #define mp_sqrmod(a, m, c) mp_mulmod(a, a, m, c) michael@0: #endif michael@0: mp_err mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c); michael@0: mp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c); michael@0: #endif /* MP_MODARITH */ michael@0: michael@0: /* Comparisons */ michael@0: int mp_cmp_z(const mp_int *a); michael@0: int mp_cmp_d(const mp_int *a, mp_digit d); michael@0: int mp_cmp(const mp_int *a, const mp_int *b); michael@0: int mp_cmp_mag(mp_int *a, mp_int *b); michael@0: int mp_cmp_int(const mp_int *a, long z); michael@0: int mp_isodd(const mp_int *a); michael@0: int mp_iseven(const mp_int *a); michael@0: michael@0: /* Number theoretic */ michael@0: #if MP_NUMTH michael@0: mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c); michael@0: mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c); michael@0: mp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y); michael@0: mp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c); michael@0: mp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c); michael@0: #endif /* end MP_NUMTH */ michael@0: michael@0: /* Input and output */ michael@0: #if MP_IOFUNC michael@0: void mp_print(mp_int *mp, FILE *ofp); michael@0: #endif /* end MP_IOFUNC */ michael@0: michael@0: /* Base conversion */ michael@0: mp_err mp_read_raw(mp_int *mp, char *str, int len); michael@0: int mp_raw_size(mp_int *mp); michael@0: mp_err mp_toraw(mp_int *mp, char *str); michael@0: mp_err mp_read_radix(mp_int *mp, const char *str, int radix); michael@0: mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix); michael@0: int mp_radix_size(mp_int *mp, int radix); michael@0: mp_err mp_toradix(mp_int *mp, char *str, int radix); michael@0: int mp_tovalue(char ch, int r); michael@0: michael@0: #define mp_tobinary(M, S) mp_toradix((M), (S), 2) michael@0: #define mp_tooctal(M, S) mp_toradix((M), (S), 8) michael@0: #define mp_todecimal(M, S) mp_toradix((M), (S), 10) michael@0: #define mp_tohex(M, S) mp_toradix((M), (S), 16) michael@0: michael@0: /* Error strings */ michael@0: const char *mp_strerror(mp_err ec); michael@0: michael@0: /* Octet string conversion functions */ michael@0: mp_err mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len); michael@0: int mp_unsigned_octet_size(const mp_int *mp); michael@0: mp_err mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen); michael@0: mp_err mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen); michael@0: mp_err mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size len); michael@0: michael@0: /* Miscellaneous */ michael@0: mp_size mp_trailing_zeros(const mp_int *mp); michael@0: void freebl_cpuid(unsigned long op, unsigned long *eax, michael@0: unsigned long *ebx, unsigned long *ecx, michael@0: unsigned long *edx); michael@0: michael@0: michael@0: #define MP_CHECKOK(x) if (MP_OKAY > (res = (x))) goto CLEANUP michael@0: #define MP_CHECKERR(x) if (MP_OKAY > (res = (x))) goto CLEANUP michael@0: michael@0: #if defined(MP_API_COMPATIBLE) michael@0: #define NEG MP_NEG michael@0: #define ZPOS MP_ZPOS michael@0: #define DIGIT_MAX MP_DIGIT_MAX michael@0: #define DIGIT_BIT MP_DIGIT_BIT michael@0: #define DIGIT_FMT MP_DIGIT_FMT michael@0: #define RADIX MP_RADIX michael@0: #define MAX_RADIX MP_MAX_RADIX michael@0: #define SIGN(MP) MP_SIGN(MP) michael@0: #define USED(MP) MP_USED(MP) michael@0: #define ALLOC(MP) MP_ALLOC(MP) michael@0: #define DIGITS(MP) MP_DIGITS(MP) michael@0: #define DIGIT(MP,N) MP_DIGIT(MP,N) michael@0: michael@0: #if MP_ARGCHK == 1 michael@0: #define ARGCHK(X,Y) {if(!(X)){return (Y);}} michael@0: #elif MP_ARGCHK == 2 michael@0: #include michael@0: #define ARGCHK(X,Y) assert(X) michael@0: #else michael@0: #define ARGCHK(X,Y) /* */ michael@0: #endif michael@0: #endif /* defined MP_API_COMPATIBLE */ michael@0: michael@0: #endif /* end _H_MPI_ */