jpayne@69: /* LibTomMath, multiple-precision integer library -- Tom St Denis */ jpayne@69: /* SPDX-License-Identifier: Unlicense */ jpayne@69: jpayne@69: #ifndef BN_H_ jpayne@69: #define BN_H_ jpayne@69: jpayne@69: #ifndef MODULE_SCOPE jpayne@69: #define MODULE_SCOPE extern jpayne@69: #endif jpayne@69: jpayne@69: jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: extern "C" { jpayne@69: #endif jpayne@69: jpayne@69: /* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */ jpayne@69: #if (defined(_WIN32) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)) && !defined(MP_64BIT) jpayne@69: # define MP_32BIT jpayne@69: #endif jpayne@69: jpayne@69: /* detect 64-bit mode if possible */ jpayne@69: #if defined(NEVER) jpayne@69: # if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT)) jpayne@69: # if defined(__GNUC__) jpayne@69: /* we support 128bit integers only via: __attribute__((mode(TI))) */ jpayne@69: # define MP_64BIT jpayne@69: # else jpayne@69: /* otherwise we fall back to MP_32BIT even on 64bit platforms */ jpayne@69: # define MP_32BIT jpayne@69: # endif jpayne@69: # endif jpayne@69: #endif jpayne@69: jpayne@69: #ifdef MP_DIGIT_BIT jpayne@69: # error Defining MP_DIGIT_BIT is disallowed, use MP_8/16/31/32/64BIT jpayne@69: #endif jpayne@69: jpayne@69: /* some default configurations. jpayne@69: * jpayne@69: * A "mp_digit" must be able to hold MP_DIGIT_BIT + 1 bits jpayne@69: * A "mp_word" must be able to hold 2*MP_DIGIT_BIT + 1 bits jpayne@69: * jpayne@69: * At the very least a mp_digit must be able to hold 7 bits jpayne@69: * [any size beyond that is ok provided it doesn't overflow the data type] jpayne@69: */ jpayne@69: jpayne@69: #ifdef MP_8BIT jpayne@69: #ifndef MP_DIGIT_DECLARED jpayne@69: typedef unsigned char mp_digit; jpayne@69: #define MP_DIGIT_DECLARED jpayne@69: #endif jpayne@69: #ifndef MP_WORD_DECLARED jpayne@69: typedef unsigned short private_mp_word; jpayne@69: #define MP_WORD_DECLARED jpayne@69: #endif jpayne@69: # define MP_SIZEOF_MP_DIGIT 1 jpayne@69: # ifdef MP_DIGIT_BIT jpayne@69: # error You must not define MP_DIGIT_BIT when using MP_8BIT jpayne@69: # endif jpayne@69: #elif defined(MP_16BIT) jpayne@69: #ifndef MP_DIGIT_DECLARED jpayne@69: typedef unsigned short mp_digit; jpayne@69: #define MP_DIGIT_DECLARED jpayne@69: #endif jpayne@69: #ifndef MP_WORD_DECLARED jpayne@69: typedef unsigned int private_mp_word; jpayne@69: #define MP_WORD_DECLARED jpayne@69: #endif jpayne@69: # define MP_SIZEOF_MP_DIGIT 2 jpayne@69: # ifdef MP_DIGIT_BIT jpayne@69: # error You must not define MP_DIGIT_BIT when using MP_16BIT jpayne@69: # endif jpayne@69: #elif defined(MP_64BIT) jpayne@69: /* for GCC only on supported platforms */ jpayne@69: #ifndef MP_DIGIT_DECLARED jpayne@69: typedef unsigned long long mp_digit; jpayne@69: #define MP_DIGIT_DECLARED jpayne@69: #endif jpayne@69: typedef unsigned long private_mp_word __attribute__((mode(TI))); jpayne@69: # define MP_DIGIT_BIT 60 jpayne@69: #else jpayne@69: /* this is the default case, 28-bit digits */ jpayne@69: jpayne@69: /* this is to make porting into LibTomCrypt easier :-) */ jpayne@69: #ifndef MP_DIGIT_DECLARED jpayne@69: typedef unsigned int mp_digit; jpayne@69: #define MP_DIGIT_DECLARED jpayne@69: #endif jpayne@69: #ifndef MP_WORD_DECLARED jpayne@69: #ifdef _WIN32 jpayne@69: typedef unsigned __int64 private_mp_word; jpayne@69: #else jpayne@69: typedef unsigned long long private_mp_word; jpayne@69: #endif jpayne@69: #define MP_WORD_DECLARED jpayne@69: #endif jpayne@69: jpayne@69: # ifdef MP_31BIT jpayne@69: /* jpayne@69: * This is an extension that uses 31-bit digits. jpayne@69: * Please be aware that not all functions support this size, especially s_mp_mul_digs_fast jpayne@69: * will be reduced to work on small numbers only: jpayne@69: * Up to 8 limbs, 248 bits instead of up to 512 limbs, 15872 bits with MP_28BIT. jpayne@69: */ jpayne@69: # define MP_DIGIT_BIT 31 jpayne@69: # else jpayne@69: /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */ jpayne@69: # define MP_DIGIT_BIT 28 jpayne@69: # define MP_28BIT jpayne@69: # endif jpayne@69: #endif jpayne@69: jpayne@69: /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */ jpayne@69: #ifndef MP_DIGIT_BIT jpayne@69: # define MP_DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */ jpayne@69: #endif jpayne@69: jpayne@69: #define MP_MASK ((((mp_digit)1)<<((mp_digit)MP_DIGIT_BIT))-((mp_digit)1)) jpayne@69: #define MP_DIGIT_MAX MP_MASK jpayne@69: jpayne@69: /* Primality generation flags */ jpayne@69: #define MP_PRIME_BBS 0x0001 /* BBS style prime */ jpayne@69: #define MP_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */ jpayne@69: #define MP_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */ jpayne@69: jpayne@69: #define LTM_PRIME_BBS (MP_DEPRECATED_PRAGMA("LTM_PRIME_BBS has been deprecated, use MP_PRIME_BBS") MP_PRIME_BBS) jpayne@69: #define LTM_PRIME_SAFE (MP_DEPRECATED_PRAGMA("LTM_PRIME_SAFE has been deprecated, use MP_PRIME_SAFE") MP_PRIME_SAFE) jpayne@69: #define LTM_PRIME_2MSB_ON (MP_DEPRECATED_PRAGMA("LTM_PRIME_2MSB_ON has been deprecated, use MP_PRIME_2MSB_ON") MP_PRIME_2MSB_ON) jpayne@69: jpayne@69: #ifdef MP_USE_ENUMS jpayne@69: typedef enum { jpayne@69: MP_ZPOS = 0, /* positive */ jpayne@69: MP_NEG = 1 /* negative */ jpayne@69: } mp_sign; jpayne@69: typedef enum { jpayne@69: MP_LT = -1, /* less than */ jpayne@69: MP_EQ = 0, /* equal */ jpayne@69: MP_GT = 1 /* greater than */ jpayne@69: } mp_ord; jpayne@69: typedef enum { jpayne@69: MP_NO = 0, jpayne@69: MP_YES = 1 jpayne@69: } mp_bool; jpayne@69: typedef enum { jpayne@69: MP_OKAY = 0, /* no error */ jpayne@69: MP_ERR = -1, /* unknown error */ jpayne@69: MP_MEM = -2, /* out of mem */ jpayne@69: MP_VAL = -3, /* invalid input */ jpayne@69: MP_ITER = -4, /* maximum iterations reached */ jpayne@69: MP_BUF = -5 /* buffer overflow, supplied buffer too small */ jpayne@69: } mp_err; jpayne@69: typedef enum { jpayne@69: MP_LSB_FIRST = -1, jpayne@69: MP_MSB_FIRST = 1 jpayne@69: } mp_order; jpayne@69: typedef enum { jpayne@69: MP_LITTLE_ENDIAN = -1, jpayne@69: MP_NATIVE_ENDIAN = 0, jpayne@69: MP_BIG_ENDIAN = 1 jpayne@69: } mp_endian; jpayne@69: #else jpayne@69: typedef int mp_sign; jpayne@69: #define MP_ZPOS 0 /* positive integer */ jpayne@69: #define MP_NEG 1 /* negative */ jpayne@69: typedef int mp_ord; jpayne@69: #define MP_LT -1 /* less than */ jpayne@69: #define MP_EQ 0 /* equal to */ jpayne@69: #define MP_GT 1 /* greater than */ jpayne@69: typedef int mp_bool; jpayne@69: #define MP_YES 1 jpayne@69: #define MP_NO 0 jpayne@69: typedef int mp_err; jpayne@69: #define MP_OKAY 0 /* no error */ jpayne@69: #define MP_ERR -1 /* unknown error */ jpayne@69: #define MP_MEM -2 /* out of mem */ jpayne@69: #define MP_VAL -3 /* invalid input */ jpayne@69: #define MP_RANGE (MP_DEPRECATED_PRAGMA("MP_RANGE has been deprecated in favor of MP_VAL") MP_VAL) jpayne@69: #define MP_ITER -4 /* maximum iterations reached */ jpayne@69: #define MP_BUF -5 /* buffer overflow, supplied buffer too small */ jpayne@69: typedef int mp_order; jpayne@69: #define MP_LSB_FIRST -1 jpayne@69: #define MP_MSB_FIRST 1 jpayne@69: typedef int mp_endian; jpayne@69: #define MP_LITTLE_ENDIAN -1 jpayne@69: #define MP_NATIVE_ENDIAN 0 jpayne@69: #define MP_BIG_ENDIAN 1 jpayne@69: #endif jpayne@69: jpayne@69: /* tunable cutoffs */ jpayne@69: jpayne@69: #ifndef MP_FIXED_CUTOFFS jpayne@69: extern int jpayne@69: KARATSUBA_MUL_CUTOFF, jpayne@69: KARATSUBA_SQR_CUTOFF, jpayne@69: TOOM_MUL_CUTOFF, jpayne@69: TOOM_SQR_CUTOFF; jpayne@69: #endif jpayne@69: jpayne@69: /* define this to use lower memory usage routines (exptmods mostly) */ jpayne@69: /* #define MP_LOW_MEM */ jpayne@69: jpayne@69: /* default precision */ jpayne@69: #ifndef MP_PREC jpayne@69: # ifndef MP_LOW_MEM jpayne@69: # define MP_PREC 32 /* default digits of precision */ jpayne@69: # elif defined(MP_8BIT) jpayne@69: # define MP_PREC 16 /* default digits of precision */ jpayne@69: # else jpayne@69: # define MP_PREC 8 /* default digits of precision */ jpayne@69: # endif jpayne@69: #endif jpayne@69: jpayne@69: /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ jpayne@69: #define PRIVATE_MP_WARRAY (int)(1 << (((CHAR_BIT * sizeof(private_mp_word)) - (2 * MP_DIGIT_BIT)) + 1)) jpayne@69: jpayne@69: #if defined(__GNUC__) && __GNUC__ >= 4 jpayne@69: # define MP_NULL_TERMINATED __attribute__((sentinel)) jpayne@69: #else jpayne@69: # define MP_NULL_TERMINATED jpayne@69: #endif jpayne@69: jpayne@69: /* jpayne@69: * MP_WUR - warn unused result jpayne@69: * --------------------------- jpayne@69: * jpayne@69: * The result of functions annotated with MP_WUR must be jpayne@69: * checked and cannot be ignored. jpayne@69: * jpayne@69: * Most functions in libtommath return an error code. jpayne@69: * This error code must be checked in order to prevent crashes or invalid jpayne@69: * results. jpayne@69: * jpayne@69: * If you still want to avoid the error checks for quick and dirty programs jpayne@69: * without robustness guarantees, you can `#define MP_WUR` before including jpayne@69: * tommath.h, disabling the warnings. jpayne@69: */ jpayne@69: #ifndef MP_WUR jpayne@69: # if defined(__GNUC__) && __GNUC__ >= 4 jpayne@69: # define MP_WUR __attribute__((warn_unused_result)) jpayne@69: # else jpayne@69: # define MP_WUR jpayne@69: # endif jpayne@69: #endif jpayne@69: jpayne@69: #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 405) jpayne@69: # define MP_DEPRECATED(x) __attribute__((deprecated("replaced by " #x))) jpayne@69: #elif defined(_MSC_VER) && _MSC_VER >= 1500 jpayne@69: # define MP_DEPRECATED(x) __declspec(deprecated("replaced by " #x)) jpayne@69: #else jpayne@69: # define MP_DEPRECATED(x) jpayne@69: #endif jpayne@69: jpayne@69: #ifndef MP_NO_DEPRECATED_PRAGMA jpayne@69: #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301) jpayne@69: # define PRIVATE_MP_DEPRECATED_PRAGMA(s) _Pragma(#s) jpayne@69: # define MP_DEPRECATED_PRAGMA(s) PRIVATE_MP_DEPRECATED_PRAGMA(GCC warning s) jpayne@69: #elif defined(_MSC_VER) && _MSC_VER >= 1500 jpayne@69: # define MP_DEPRECATED_PRAGMA(s) __pragma(message(s)) jpayne@69: #endif jpayne@69: #endif jpayne@69: jpayne@69: #ifndef MP_DEPRECATED_PRAGMA jpayne@69: # define MP_DEPRECATED_PRAGMA(s) jpayne@69: #endif jpayne@69: jpayne@69: #define DIGIT_BIT MP_DIGIT_BIT jpayne@69: #define USED(m) ((m)->used) jpayne@69: #define DIGIT(m,k) ((m)->dp[(k)]) jpayne@69: #define SIGN(m) ((m)->sign) jpayne@69: jpayne@69: /* the infamous mp_int structure */ jpayne@69: #ifndef MP_INT_DECLARED jpayne@69: #define MP_INT_DECLARED jpayne@69: typedef struct mp_int mp_int; jpayne@69: #endif jpayne@69: struct mp_int { jpayne@69: int used, alloc; jpayne@69: mp_sign sign; jpayne@69: mp_digit *dp; jpayne@69: }; jpayne@69: jpayne@69: /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */ jpayne@69: typedef int private_mp_prime_callback(unsigned char *dst, int len, void *dat); jpayne@69: typedef private_mp_prime_callback MP_DEPRECATED(mp_rand_source) ltm_prime_callback; jpayne@69: jpayne@69: /* error code to char* string */ jpayne@69: /* jpayne@69: const char *mp_error_to_string(mp_err code) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* ---> init and deinit bignum functions <--- */ jpayne@69: /* init a bignum */ jpayne@69: /* jpayne@69: mp_err mp_init(mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* free a bignum */ jpayne@69: /* jpayne@69: void mp_clear(mp_int *a); jpayne@69: */ jpayne@69: jpayne@69: /* init a null terminated series of arguments */ jpayne@69: /* jpayne@69: mp_err mp_init_multi(mp_int *mp, ...) MP_NULL_TERMINATED MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* clear a null terminated series of arguments */ jpayne@69: /* jpayne@69: void mp_clear_multi(mp_int *mp, ...) MP_NULL_TERMINATED; jpayne@69: */ jpayne@69: jpayne@69: /* exchange two ints */ jpayne@69: /* jpayne@69: void mp_exch(mp_int *a, mp_int *b); jpayne@69: */ jpayne@69: jpayne@69: /* shrink ram required for a bignum */ jpayne@69: /* jpayne@69: mp_err mp_shrink(mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* grow an int to a given size */ jpayne@69: /* jpayne@69: mp_err mp_grow(mp_int *a, int size) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* init to a given number of digits */ jpayne@69: /* jpayne@69: mp_err mp_init_size(mp_int *a, int size) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* ---> Basic Manipulations <--- */ jpayne@69: #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) jpayne@69: #define mp_isodd(a) (((a)->used != 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) jpayne@69: #define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) jpayne@69: #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO) jpayne@69: jpayne@69: /* set to zero */ jpayne@69: /* jpayne@69: void mp_zero(mp_int *a); jpayne@69: */ jpayne@69: jpayne@69: /* get and set doubles */ jpayne@69: /* jpayne@69: double mp_get_double(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_set_double(mp_int *a, double b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer and init with integer (int32_t) */ jpayne@69: #ifndef MP_NO_STDINT jpayne@69: /* jpayne@69: int32_t mp_get_i32(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: void mp_set_i32(mp_int *a, int32_t b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_i32(mp_int *a, int32_t b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint32_t) */ jpayne@69: #define mp_get_u32(a) ((uint32_t)mp_get_i32(a)) jpayne@69: /* jpayne@69: void mp_set_u32(mp_int *a, uint32_t b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_u32(mp_int *a, uint32_t b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer and init with integer (int64_t) */ jpayne@69: /* jpayne@69: int64_t mp_get_i64(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: void mp_set_i64(mp_int *a, int64_t b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_i64(mp_int *a, int64_t b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint64_t) */ jpayne@69: #define mp_get_u64(a) ((uint64_t)mp_get_i64(a)) jpayne@69: /* jpayne@69: void mp_set_u64(mp_int *a, uint64_t b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_u64(mp_int *a, uint64_t b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get magnitude */ jpayne@69: /* jpayne@69: uint32_t mp_get_mag_u32(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: uint64_t mp_get_mag_u64(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: #endif jpayne@69: /* jpayne@69: unsigned long mp_get_mag_ul(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: Tcl_WideUInt mp_get_mag_ull(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer (long) */ jpayne@69: /* jpayne@69: long mp_get_l(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: void mp_set_l(mp_int *a, long b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_l(mp_int *a, long b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer (unsigned long) */ jpayne@69: #define mp_get_ul(a) ((unsigned long)mp_get_l(a)) jpayne@69: /* jpayne@69: void mp_set_ul(mp_int *a, unsigned long b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_ul(mp_int *a, unsigned long b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer (Tcl_WideInt) */ jpayne@69: /* jpayne@69: Tcl_WideInt mp_get_ll(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: void mp_set_ll(mp_int *a, Tcl_WideInt b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_ll(mp_int *a, Tcl_WideInt b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer (Tcl_WideUInt) */ jpayne@69: #define mp_get_ull(a) ((Tcl_WideUInt)mp_get_ll(a)) jpayne@69: /* jpayne@69: void mp_set_ull(mp_int *a, Tcl_WideUInt b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_ull(mp_int *a, Tcl_WideUInt b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* set to single unsigned digit, up to MP_DIGIT_MAX */ jpayne@69: /* jpayne@69: void mp_set(mp_int *a, mp_digit b); jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_init_set(mp_int *a, mp_digit b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* get integer, set integer and init with integer (deprecated) */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_get_mag_u32/mp_get_u32) unsigned long mp_get_int(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_get_mag_ul/mp_get_ul) unsigned long mp_get_long(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_get_mag_ull/mp_get_ull) Tcl_WideUInt mp_get_long_long(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_set_ul) mp_err mp_set_int(mp_int *a, unsigned long b); jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_set_ul) mp_err mp_set_long(mp_int *a, unsigned long b); jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_set_ull) mp_err mp_set_long_long(mp_int *a, Tcl_WideUInt b); jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_init_ul) mp_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* copy, b = a */ jpayne@69: /* jpayne@69: mp_err mp_copy(const mp_int *a, mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* inits and copies, a = b */ jpayne@69: /* jpayne@69: mp_err mp_init_copy(mp_int *a, const mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* trim unused digits */ jpayne@69: /* jpayne@69: void mp_clamp(mp_int *a); jpayne@69: */ jpayne@69: jpayne@69: /* export binary data */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_pack) mp_err mp_export(void *rop, size_t *countp, int order, size_t size, jpayne@69: int endian, size_t nails, const mp_int *op) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* import binary data */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_unpack) mp_err mp_import(mp_int *rop, size_t count, int order, jpayne@69: size_t size, int endian, size_t nails, jpayne@69: const void *op) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* unpack binary data */ jpayne@69: /* jpayne@69: mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, mp_endian endian, jpayne@69: size_t nails, const void *op) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* pack binary data */ jpayne@69: /* jpayne@69: size_t mp_pack_count(const mp_int *a, size_t nails, size_t size) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size, jpayne@69: mp_endian endian, size_t nails, const mp_int *op) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* ---> digit manipulation <--- */ jpayne@69: jpayne@69: /* right shift by "b" digits */ jpayne@69: /* jpayne@69: void mp_rshd(mp_int *a, int b); jpayne@69: */ jpayne@69: jpayne@69: /* left shift by "b" digits */ jpayne@69: /* jpayne@69: mp_err mp_lshd(mp_int *a, int b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a / 2**b, implemented as c = a >> b */ jpayne@69: /* jpayne@69: mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* b = a/2 */ jpayne@69: /* jpayne@69: mp_err mp_div_2(const mp_int *a, mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* a/3 => 3c + d == a */ jpayne@69: /* jpayne@69: mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a * 2**b, implemented as c = a << b */ jpayne@69: /* jpayne@69: mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* b = a*2 */ jpayne@69: /* jpayne@69: mp_err mp_mul_2(const mp_int *a, mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a mod 2**b */ jpayne@69: /* jpayne@69: mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* computes a = 2**b */ jpayne@69: /* jpayne@69: mp_err mp_2expt(mp_int *a, int b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* Counts the number of lsbs which are zero before the first zero bit */ jpayne@69: /* jpayne@69: int mp_cnt_lsb(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* I Love Earth! */ jpayne@69: jpayne@69: /* makes a pseudo-random mp_int of a given size */ jpayne@69: /* jpayne@69: mp_err mp_rand(mp_int *a, int digits) MP_WUR; jpayne@69: */ jpayne@69: /* makes a pseudo-random small int of a given size */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_rand) mp_err mp_rand_digit(mp_digit *r) MP_WUR; jpayne@69: */ jpayne@69: /* use custom random data source instead of source provided the platform */ jpayne@69: /* jpayne@69: void mp_rand_source(mp_err(*source)(void *out, size_t size)); jpayne@69: */ jpayne@69: jpayne@69: #ifdef MP_PRNG_ENABLE_LTM_RNG jpayne@69: /* A last resort to provide random data on systems without any of the other jpayne@69: * implemented ways to gather entropy. jpayne@69: * It is compatible with `rng_get_bytes()` from libtomcrypt so you could jpayne@69: * provide that one and then set `ltm_rng = rng_get_bytes;` */ jpayne@69: extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); jpayne@69: extern void (*ltm_rng_callback)(void); jpayne@69: #endif jpayne@69: jpayne@69: /* ---> binary operations <--- */ jpayne@69: jpayne@69: /* Checks the bit at position b and returns MP_YES jpayne@69: * if the bit is 1, MP_NO if it is 0 and MP_VAL jpayne@69: * in case of error jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(s_mp_get_bit) int mp_get_bit(const mp_int *a, int b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a XOR b (two complement) */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_xor) mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a OR b (two complement) */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_or) mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a AND b (two complement) */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_and) mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* b = ~a (bitwise not, two complement) */ jpayne@69: /* jpayne@69: mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* right shift with sign extension */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_signed_rsh) mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* ---> Basic arithmetic <--- */ jpayne@69: jpayne@69: /* b = -a */ jpayne@69: /* jpayne@69: mp_err mp_neg(const mp_int *a, mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* b = |a| */ jpayne@69: /* jpayne@69: mp_err mp_abs(const mp_int *a, mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* compare a to b */ jpayne@69: /* jpayne@69: mp_ord mp_cmp(const mp_int *a, const mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* compare |a| to |b| */ jpayne@69: /* jpayne@69: mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a + b */ jpayne@69: /* jpayne@69: mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a - b */ jpayne@69: /* jpayne@69: mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a * b */ jpayne@69: /* jpayne@69: mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* b = a*a */ jpayne@69: /* jpayne@69: mp_err mp_sqr(const mp_int *a, mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* a/b => cb + d == a */ jpayne@69: /* jpayne@69: mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a mod b, 0 <= c < b */ jpayne@69: /* jpayne@69: mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* Increment "a" by one like "a++". Changes input! */ jpayne@69: /* jpayne@69: mp_err mp_incr(mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* Decrement "a" by one like "a--". Changes input! */ jpayne@69: /* jpayne@69: mp_err mp_decr(mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* ---> single digit functions <--- */ jpayne@69: jpayne@69: /* compare against a single digit */ jpayne@69: /* jpayne@69: mp_ord mp_cmp_d(const mp_int *a, mp_digit b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a + b */ jpayne@69: /* jpayne@69: mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a - b */ jpayne@69: /* jpayne@69: mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a * b */ jpayne@69: /* jpayne@69: mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* a/b => cb + d == a */ jpayne@69: /* jpayne@69: mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a mod b, 0 <= c < b */ jpayne@69: /* jpayne@69: mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* ---> number theory <--- */ jpayne@69: jpayne@69: /* d = a + b (mod c) */ jpayne@69: /* jpayne@69: mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* d = a - b (mod c) */ jpayne@69: /* jpayne@69: mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* d = a * b (mod c) */ jpayne@69: /* jpayne@69: mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a * a (mod b) */ jpayne@69: /* jpayne@69: mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = 1/a (mod b) */ jpayne@69: /* jpayne@69: mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = (a, b) */ jpayne@69: /* jpayne@69: mp_err mp_gcd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* produces value such that U1*a + U2*b = U3 */ jpayne@69: /* jpayne@69: mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = [a, b] or (a*b)/(a, b) */ jpayne@69: /* jpayne@69: mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* finds one of the b'th root of a, such that |c|**b <= |a| jpayne@69: * jpayne@69: * returns error if a < 0 and b is even jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_root_u32(const mp_int *a, unsigned int b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_root_u32) mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_root_u32) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* special sqrt algo */ jpayne@69: /* jpayne@69: mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* special sqrt (mod prime) */ jpayne@69: /* jpayne@69: mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* is number a square? */ jpayne@69: /* jpayne@69: mp_err mp_is_square(const mp_int *arg, mp_bool *ret) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* computes the jacobi c = (a | n) (or Legendre if b is prime) */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_kronecker) mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */ jpayne@69: /* jpayne@69: mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* used to setup the Barrett reduction for a given modulus b */ jpayne@69: /* jpayne@69: mp_err mp_reduce_setup(mp_int *a, const mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* Barrett Reduction, computes a (mod b) with a precomputed value c jpayne@69: * jpayne@69: * Assumes that 0 < x <= m*m, note if 0 > x > -(m*m) then you can merely jpayne@69: * compute the reduction as -1 * mp_reduce(mp_abs(x)) [pseudo code]. jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* setups the montgomery reduction */ jpayne@69: /* jpayne@69: mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* computes a = B**n mod b without division or multiplication useful for jpayne@69: * normalizing numbers in a Montgomery system. jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* computes x/R == x (mod N) via Montgomery Reduction */ jpayne@69: /* jpayne@69: mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* returns 1 if a is a valid DR modulus */ jpayne@69: /* jpayne@69: mp_bool mp_dr_is_modulus(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* sets the value of "d" required for mp_dr_reduce */ jpayne@69: /* jpayne@69: void mp_dr_setup(const mp_int *a, mp_digit *d); jpayne@69: */ jpayne@69: jpayne@69: /* reduces a modulo n using the Diminished Radix method */ jpayne@69: /* jpayne@69: mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* returns true if a can be reduced with mp_reduce_2k */ jpayne@69: /* jpayne@69: mp_bool mp_reduce_is_2k(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* determines k value for 2k reduction */ jpayne@69: /* jpayne@69: mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ jpayne@69: /* jpayne@69: mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* returns true if a can be reduced with mp_reduce_2k_l */ jpayne@69: /* jpayne@69: mp_bool mp_reduce_is_2k_l(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* determines k value for 2k reduction */ jpayne@69: /* jpayne@69: mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ jpayne@69: /* jpayne@69: mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* Y = G**X (mod P) */ jpayne@69: /* jpayne@69: mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* ---> Primes <--- */ jpayne@69: jpayne@69: /* number of primes */ jpayne@69: #ifdef MP_8BIT jpayne@69: # define PRIVATE_MP_PRIME_TAB_SIZE 31 jpayne@69: #else jpayne@69: # define PRIVATE_MP_PRIME_TAB_SIZE 256 jpayne@69: #endif jpayne@69: #define PRIME_SIZE (MP_DEPRECATED_PRAGMA("PRIME_SIZE has been made internal") PRIVATE_MP_PRIME_TAB_SIZE) jpayne@69: jpayne@69: /* table of first PRIME_SIZE primes */ jpayne@69: #if defined(BUILD_tcl) || !defined(_WIN32) jpayne@69: MODULE_SCOPE const mp_digit ltm_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE]; jpayne@69: #endif jpayne@69: jpayne@69: /* result=1 if a is divisible by one of the first PRIME_SIZE primes */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_prime_is_prime) mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* performs one Fermat test of "a" using base "b". jpayne@69: * Sets result to 0 if composite or 1 if probable prime jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* performs one Miller-Rabin test of "a" using base "b". jpayne@69: * Sets result to 0 if composite or 1 if probable prime jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* This gives [for a given bit size] the number of trials required jpayne@69: * such that Miller-Rabin gives a prob of failure lower than 2^-96 jpayne@69: */ jpayne@69: /* jpayne@69: int mp_prime_rabin_miller_trials(int size) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* performs one strong Lucas-Selfridge test of "a". jpayne@69: * Sets result to 0 if composite or 1 if probable prime jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* performs one Frobenius test of "a" as described by Paul Underwood. jpayne@69: * Sets result to 0 if composite or 1 if probable prime jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* performs t random rounds of Miller-Rabin on "a" additional to jpayne@69: * bases 2 and 3. Also performs an initial sieve of trial jpayne@69: * division. Determines if "a" is prime with probability jpayne@69: * of error no more than (1/4)**t. jpayne@69: * Both a strong Lucas-Selfridge to complete the BPSW test jpayne@69: * and a separate Frobenius test are available at compile time. jpayne@69: * With t<0 a deterministic test is run for primes up to jpayne@69: * 318665857834031151167461. With t<13 (abs(t)-13) additional jpayne@69: * tests with sequential small primes are run starting at 43. jpayne@69: * Is Fips 186.4 compliant if called with t as computed by jpayne@69: * mp_prime_rabin_miller_trials(); jpayne@69: * jpayne@69: * Sets result to 1 if probably prime, 0 otherwise jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* finds the next prime after the number "a" using "t" trials jpayne@69: * of Miller-Rabin. jpayne@69: * jpayne@69: * bbs_style = 1 means the prime must be congruent to 3 mod 4 jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* makes a truly random prime of a given size (bytes), jpayne@69: * call with bbs = 1 if you want it to be congruent to 3 mod 4 jpayne@69: * jpayne@69: * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can jpayne@69: * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself jpayne@69: * so it can be NULL jpayne@69: * jpayne@69: * The prime generated will be larger than 2^(8*size). jpayne@69: */ jpayne@69: #define mp_prime_random(a, t, size, bbs, cb, dat) (MP_DEPRECATED_PRAGMA("mp_prime_random has been deprecated, use mp_prime_rand instead") mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?MP_PRIME_BBS:0, cb, dat)) jpayne@69: jpayne@69: /* makes a truly random prime of a given size (bits), jpayne@69: * jpayne@69: * Flags are as follows: jpayne@69: * jpayne@69: * MP_PRIME_BBS - make prime congruent to 3 mod 4 jpayne@69: * MP_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies MP_PRIME_BBS) jpayne@69: * MP_PRIME_2MSB_ON - make the 2nd highest bit one jpayne@69: * jpayne@69: * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can jpayne@69: * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself jpayne@69: * so it can be NULL jpayne@69: * jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_prime_rand) mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags, jpayne@69: private_mp_prime_callback cb, void *dat) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* Integer logarithm to integer base */ jpayne@69: /* jpayne@69: mp_err mp_log_u32(const mp_int *a, unsigned int base, unsigned int *c) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* c = a**b */ jpayne@69: /* jpayne@69: mp_err mp_expt_u32(const mp_int *a, unsigned int b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* ---> radix conversion <--- */ jpayne@69: /* jpayne@69: int mp_count_bits(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_ubin_size) int mp_unsigned_bin_size(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_from_ubin) mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_sbin_size) int mp_signed_bin_size(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_from_sbin) mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* jpayne@69: size_t mp_ubin_size(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* jpayne@69: size_t mp_sbin_size(const mp_int *a) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: /* jpayne@69: mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR; jpayne@69: */ jpayne@69: jpayne@69: #ifndef MP_NO_FILE jpayne@69: /* jpayne@69: mp_err mp_fread(mp_int *a, int radix, FILE *stream) MP_WUR; jpayne@69: */ jpayne@69: /* jpayne@69: mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR; jpayne@69: */ jpayne@69: #endif jpayne@69: jpayne@69: #define mp_read_raw(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_signed_bin") mp_read_signed_bin((mp), (str), (len))) jpayne@69: #define mp_raw_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_signed_bin_size") mp_signed_bin_size(mp)) jpayne@69: #define mp_toraw(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_signed_bin") mp_to_signed_bin((mp), (str))) jpayne@69: #define mp_read_mag(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_unsigned_bin") mp_read_unsigned_bin((mp), (str), (len)) jpayne@69: #define mp_mag_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_unsigned_bin_size") mp_unsigned_bin_size(mp)) jpayne@69: #define mp_tomag(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_unsigned_bin") mp_to_unsigned_bin((mp), (str))) jpayne@69: jpayne@69: #define mp_tobinary(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_binary") mp_toradix((M), (S), 2)) jpayne@69: #define mp_tooctal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_octal") mp_toradix((M), (S), 8)) jpayne@69: #define mp_todecimal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_decimal") mp_toradix((M), (S), 10)) jpayne@69: #define mp_tohex(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_hex") mp_toradix((M), (S), 16)) jpayne@69: jpayne@69: #define mp_to_binary(M, S, N) mp_to_radix((M), (S), (N), NULL, 2) jpayne@69: #define mp_to_octal(M, S, N) mp_to_radix((M), (S), (N), NULL, 8) jpayne@69: #define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), NULL, 10) jpayne@69: #define mp_to_hex(M, S, N) mp_to_radix((M), (S), (N), NULL, 16) jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: } jpayne@69: #endif jpayne@69: jpayne@69: #include "tclTomMathDecls.h" jpayne@69: jpayne@69: #endif