Path: blob/master/libs/tomcrypt/src/headers/tommath_private.h
5971 views
/* LibTomMath, multiple-precision integer library -- Tom St Denis1*2* LibTomMath is a library that provides multiple-precision3* integer arithmetic as well as number theoretic functionality.4*5* The library was designed directly after the MPI library by6* Michael Fromberger but has been written from scratch with7* additional optimizations in place.8*9* SPDX-License-Identifier: Unlicense10*/11#ifndef TOMMATH_PRIV_H_12#define TOMMATH_PRIV_H_1314#include "tommath.h"15#include <ctype.h>1617#ifndef MIN18#define MIN(x, y) (((x) < (y)) ? (x) : (y))19#endif2021#ifndef MAX22#define MAX(x, y) (((x) > (y)) ? (x) : (y))23#endif2425#ifdef __cplusplus26extern "C" {2728/* C++ compilers don't like assigning void * to mp_digit * */29#define OPT_CAST(x) (x *)3031#else3233/* C on the other hand doesn't care */34#define OPT_CAST(x)3536#endif3738/* define heap macros */39#ifndef XMALLOC40/* default to libc stuff */41# define XMALLOC malloc42# define XFREE free43# define XREALLOC realloc44# define XCALLOC calloc45#else46/* prototypes for our heap functions */47extern void *XMALLOC(size_t n);48extern void *XREALLOC(void *p, size_t n);49extern void *XCALLOC(size_t n, size_t s);50extern void XFREE(void *p);51#endif5253/* lowlevel functions, do not call! */54int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);55int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c);56#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)57int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);58int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);59int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);60int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);61int fast_s_mp_sqr(const mp_int *a, mp_int *b);62int s_mp_sqr(const mp_int *a, mp_int *b);63int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);64int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);65int mp_karatsuba_sqr(const mp_int *a, mp_int *b);66int mp_toom_sqr(const mp_int *a, mp_int *b);67int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);68int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);69int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);70int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);71int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);72void bn_reverse(unsigned char *s, int len);7374extern const char *const mp_s_rmap;75extern const uint8_t mp_s_rmap_reverse[];76extern const size_t mp_s_rmap_reverse_sz;7778/* Fancy macro to set an MPI from another type.79* There are several things assumed:80* x is the counter and unsigned81* a is the pointer to the MPI82* b is the original value that should be set in the MPI.83*/84#define MP_SET_XLONG(func_name, type) \85int func_name (mp_int * a, type b) \86{ \87unsigned int x; \88int res; \89\90mp_zero (a); \91\92/* set four bits at a time */ \93for (x = 0; x < (sizeof(type) * 2u); x++) { \94/* shift the number up four bits */ \95if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \96return res; \97} \98\99/* OR in the top four bits of the source */ \100a->dp[0] |= (mp_digit)(b >> ((sizeof(type) * 8u) - 4u)) & 15uL;\101\102/* shift the source up to the next four bits */ \103b <<= 4; \104\105/* ensure that digits are not clamped off */ \106a->used += 1; \107} \108mp_clamp (a); \109return MP_OKAY; \110}111112#ifdef __cplusplus113}114#endif115116#endif117118119