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