2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
|
|
|
|
2015-11-12 00:49:07 +00:00
|
|
|
#ifndef TOMMATH_PRIV_H_
|
|
|
|
#define TOMMATH_PRIV_H_
|
|
|
|
|
2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath.h"
|
2015-11-12 00:49:07 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
2015-11-19 20:53:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* define heap macros */
|
2019-04-09 09:08:26 +00:00
|
|
|
#ifndef MP_MALLOC
|
2017-08-30 17:15:27 +00:00
|
|
|
/* default to libc stuff */
|
2019-04-09 09:08:26 +00:00
|
|
|
# define MP_MALLOC(size) malloc(size)
|
|
|
|
# define MP_REALLOC(mem, oldsize, newsize) realloc(mem, newsize)
|
|
|
|
# define MP_CALLOC(nmemb, size) calloc(nmemb, size)
|
|
|
|
# define MP_FREE(mem, size) free(mem)
|
2015-11-19 20:53:32 +00:00
|
|
|
#else
|
2017-08-30 17:15:27 +00:00
|
|
|
/* prototypes for our heap functions */
|
2019-04-09 09:08:26 +00:00
|
|
|
extern void *MP_MALLOC(size_t size);
|
|
|
|
extern void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize);
|
|
|
|
extern void *MP_CALLOC(size_t nmemb, size_t size);
|
|
|
|
extern void MP_FREE(void *mem, size_t size);
|
2015-11-12 00:49:07 +00:00
|
|
|
#endif
|
|
|
|
|
2019-04-09 09:08:26 +00:00
|
|
|
#define MP_MIN(x, y) (((x) < (y)) ? (x) : (y))
|
|
|
|
#define MP_MAX(x, y) (((x) > (y)) ? (x) : (y))
|
|
|
|
|
2018-12-28 08:01:01 +00:00
|
|
|
/* ---> Basic Manipulations <--- */
|
2019-04-09 09:08:26 +00:00
|
|
|
#define MP_IS_ZERO(a) ((a)->used == 0)
|
|
|
|
#define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
|
|
|
|
#define MP_IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
|
2018-12-28 08:01:01 +00:00
|
|
|
|
2015-11-12 00:49:07 +00:00
|
|
|
/* lowlevel functions, do not call! */
|
2017-09-20 14:59:43 +00:00
|
|
|
int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
|
2019-04-12 12:56:29 +00:00
|
|
|
int s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs);
|
2017-09-20 14:59:43 +00:00
|
|
|
int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
|
2019-04-12 12:56:29 +00:00
|
|
|
int s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs);
|
2017-09-20 14:59:43 +00:00
|
|
|
int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
|
2019-04-12 12:56:29 +00:00
|
|
|
int s_mp_sqr_fast(const mp_int *a, mp_int *b);
|
2017-09-20 14:59:43 +00:00
|
|
|
int s_mp_sqr(const mp_int *a, mp_int *b);
|
2019-04-12 12:56:29 +00:00
|
|
|
int s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
int s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
int s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
int s_mp_karatsuba_sqr(const mp_int *a, mp_int *b);
|
|
|
|
int s_mp_toom_sqr(const mp_int *a, mp_int *b);
|
|
|
|
int s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
int s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
int s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho);
|
|
|
|
int s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
|
2017-09-20 14:59:43 +00:00
|
|
|
int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
|
2019-04-12 12:56:29 +00:00
|
|
|
void s_mp_reverse(unsigned char *s, int len);
|
2015-11-12 00:49:07 +00:00
|
|
|
|
2018-05-03 01:44:47 +00:00
|
|
|
extern const char *const mp_s_rmap;
|
2017-10-09 12:34:20 +00:00
|
|
|
extern const uint8_t mp_s_rmap_reverse[];
|
|
|
|
extern const size_t mp_s_rmap_reverse_sz;
|
2015-11-12 00:49:07 +00:00
|
|
|
|
|
|
|
/* Fancy macro to set an MPI from another type.
|
|
|
|
* There are several things assumed:
|
2019-03-26 10:30:21 +00:00
|
|
|
* x is the counter
|
2015-11-12 00:49:07 +00:00
|
|
|
* a is the pointer to the MPI
|
|
|
|
* b is the original value that should be set in the MPI.
|
|
|
|
*/
|
|
|
|
#define MP_SET_XLONG(func_name, type) \
|
|
|
|
int func_name (mp_int * a, type b) \
|
|
|
|
{ \
|
2019-03-26 10:30:21 +00:00
|
|
|
int x = 0; \
|
2019-03-26 17:51:35 +00:00
|
|
|
int new_size = (((CHAR_BIT * sizeof(type)) + DIGIT_BIT) - 1) / DIGIT_BIT; \
|
|
|
|
int res = mp_grow(a, new_size); \
|
2019-03-26 10:30:21 +00:00
|
|
|
if (res == MP_OKAY) { \
|
|
|
|
mp_zero(a); \
|
2019-03-26 17:42:18 +00:00
|
|
|
while (b != 0u) { \
|
2019-03-26 10:30:21 +00:00
|
|
|
a->dp[x++] = ((mp_digit)b & MP_MASK); \
|
2019-04-04 10:57:22 +00:00
|
|
|
if ((CHAR_BIT * sizeof (b)) <= DIGIT_BIT) { break; } \
|
2019-04-09 18:26:08 +00:00
|
|
|
b >>= (((CHAR_BIT * sizeof (b)) <= DIGIT_BIT) ? 0 : DIGIT_BIT); \
|
2019-03-26 10:30:21 +00:00
|
|
|
} \
|
|
|
|
a->used = x; \
|
|
|
|
} \
|
|
|
|
return res; \
|
2015-11-12 00:49:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 03:02:32 +00:00
|
|
|
/* deprecated functions */
|
2019-04-12 12:56:29 +00:00
|
|
|
MP_DEPRECATED(s_mp_invmod_fast) int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
MP_DEPRECATED(s_mp_montgomery_reduce_fast) int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);
|
|
|
|
MP_DEPRECATED(s_mp_mul_digs_fast) int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
|
|
|
|
MP_DEPRECATED(s_mp_mul_high_digs_fast) int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c,
|
|
|
|
int digs);
|
|
|
|
MP_DEPRECATED(s_mp_sqr_fast) int fast_s_mp_sqr(const mp_int *a, mp_int *b);
|
|
|
|
MP_DEPRECATED(s_mp_balance_mul) int mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
MP_DEPRECATED(s_mp_exptmod_fast) int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y,
|
|
|
|
int redmode);
|
|
|
|
MP_DEPRECATED(s_mp_invmod_slow) int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
MP_DEPRECATED(s_mp_karatsuba_mul) int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
MP_DEPRECATED(s_mp_karatsuba_sqr) int mp_karatsuba_sqr(const mp_int *a, mp_int *b);
|
|
|
|
MP_DEPRECATED(s_mp_toom_mul) int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
MP_DEPRECATED(s_mp_toom_sqr) int mp_toom_sqr(const mp_int *a, mp_int *b);
|
|
|
|
MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
|
2019-03-07 03:02:32 +00:00
|
|
|
|
2015-11-12 00:49:07 +00:00
|
|
|
#ifdef __cplusplus
|
2017-08-30 17:15:27 +00:00
|
|
|
}
|
2015-11-12 00:49:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|