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 TOMMATH_PRIV_H_ 13 #define TOMMATH_PRIV_H_ 14 15 #include "tommath.h" 16 #include <ctype.h> 17 18 #ifndef MIN 19 #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 20 #endif 21 22 #ifndef MAX 23 #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 24 #endif 25 26 #ifdef __cplusplus 27 extern "C" { 28 29 /* C++ compilers don't like assigning void * to mp_digit * */ 30 #define OPT_CAST(x) (x *) 31 32 #else 33 34 /* C on the other hand doesn't care */ 35 #define OPT_CAST(x) 36 37 #endif 38 39 /* define heap macros */ 40 #ifndef XMALLOC 41 /* default to libc stuff */ 42 # define XMALLOC malloc 43 # define XFREE free 44 # define XREALLOC realloc 45 # define XCALLOC calloc 46 #else 47 /* prototypes for our heap functions */ 48 extern void *XMALLOC(size_t n); 49 extern void *XREALLOC(void *p, size_t n); 50 extern void *XCALLOC(size_t n, size_t s); 51 extern void XFREE(void *p); 52 #endif 53 54 /* lowlevel functions, do not call! */ 55 int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c); 56 int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c); 57 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1) 58 int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs); 59 int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs); 60 int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs); 61 int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs); 62 int fast_s_mp_sqr(const mp_int *a, mp_int *b); 63 int s_mp_sqr(const mp_int *a, mp_int *b); 64 int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c); 65 int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c); 66 int mp_karatsuba_sqr(const mp_int *a, mp_int *b); 67 int mp_toom_sqr(const mp_int *a, mp_int *b); 68 int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c); 69 int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c); 70 int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho); 71 int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode); 72 int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode); 73 void bn_reverse(unsigned char *s, int len); 74 75 extern const char *const mp_s_rmap; 76 extern const unsigned char mp_s_rmap_reverse[]; 77 extern const size_t mp_s_rmap_reverse_sz; 78 79 /* Fancy macro to set an MPI from another type. 80 * There are several things assumed: 81 * x is the counter and unsigned 82 * a is the pointer to the MPI 83 * b is the original value that should be set in the MPI. 84 */ 85 #define MP_SET_XLONG(func_name, type) \ 86 int func_name (mp_int * a, type b) \ 87 { \ 88 unsigned int x; \ 89 int res; \ 90 \ 91 mp_zero (a); \ 92 \ 93 /* set four bits at a time */ \ 94 for (x = 0; x < (sizeof(type) * 2u); x++) { \ 95 /* shift the number up four bits */ \ 96 if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \ 97 return res; \ 98 } \ 99 \ 100 /* OR in the top four bits of the source */ \ 101 a->dp[0] |= (mp_digit)(b >> ((sizeof(type) * 8u) - 4u)) & 15uL;\ 102 \ 103 /* shift the source up to the next four bits */ \ 104 b <<= 4; \ 105 \ 106 /* ensure that digits are not clamped off */ \ 107 a->used += 1; \ 108 } \ 109 mp_clamp (a); \ 110 return MP_OKAY; \ 111 } 112 113 #ifdef __cplusplus 114 } 115 #endif 116 117 #endif 118 119 120 /* ref: $Format:%D$ */ 121 /* git commit: $Format:%H$ */ 122 /* commit time: $Format:%ai$ */ 123