Hash :
3289c959
Author :
Date :
2019-05-20T10:41:06
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#include "tommath_private.h"
#ifdef BN_CONVERSION_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#define MP_SET_UNSIGNED(name, w) \
void name(mp_int * a, uint##w##_t b) \
{ \
int i = 0; \
while (b != 0u) { \
a->dp[i++] = ((mp_digit)b & MP_MASK); \
if (w <= MP_DIGIT_BIT) { break; } \
b >>= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
} \
a->used = i; \
a->sign = MP_ZPOS; \
MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); \
}
#define MP_SET_SIGNED(name, uname, w) \
void name(mp_int * a, int##w##_t b) \
{ \
uname(a, b < 0 ? -(uint##w##_t)b : (uint##w##_t)b); \
if (b < 0) { a->sign = MP_NEG; } \
}
#define MP_INIT_INT(name , set, type) \
mp_err name(mp_int * a, type b) \
{ \
mp_err err; \
if ((err = mp_init(a)) != MP_OKAY) { \
return err; \
} \
set(a, b); \
return MP_OKAY; \
}
#define MP_GET_MAG(name, w) \
uint##w##_t name(const mp_int* a) \
{ \
unsigned i = MP_MIN((unsigned)a->used, (unsigned)((w + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
uint##w##_t res = 0; \
while (i --> 0) { \
res <<= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
res |= (uint##w##_t)a->dp[i]; \
if (w <= MP_DIGIT_BIT) { break; } \
} \
return res; \
}
#define MP_GET_SIGNED(name, mag, w) \
int##w##_t name(const mp_int* a) \
{ \
uint64_t res = mag(a); \
return a->sign == MP_NEG ? (int##w##_t)-res : (int##w##_t)res; \
}
#ifdef BN_MP_SET_UINT_C
MP_SET_UNSIGNED(mp_set_uint, 32)
#endif
#ifdef BN_MP_SET_UINT64_C
MP_SET_UNSIGNED(mp_set_uint64, 64)
#endif
#ifdef BN_MP_SET_SINT_C
MP_SET_SIGNED(mp_set_sint, mp_set_uint, 32)
#endif
#ifdef BN_MP_SET_SINT64_C
MP_SET_SIGNED(mp_set_sint64, mp_set_uint64, 64)
#endif
#if defined(BN_MP_GET_SINT_C) || defined(BN_MP_GET_UINT_C)
MP_GET_SIGNED(mp_get_sint, mp_get_mag, 32)
#endif
#if defined(BN_MP_GET_SINT64_C) || defined(BN_MP_GET_UINT64_C)
MP_GET_SIGNED(mp_get_sint64, mp_get_mag64, 64)
#endif
#ifdef BN_MP_GET_MAG_C
MP_GET_MAG(mp_get_mag, 32)
#endif
#ifdef BN_MP_GET_MAG64_C
MP_GET_MAG(mp_get_mag64, 64)
#endif
#ifdef BN_MP_INIT_UINT_C
MP_INIT_INT(mp_init_uint, mp_set_uint, uint32_t)
#endif
#ifdef BN_MP_INIT_SINT_C
MP_INIT_INT(mp_init_sint, mp_set_sint, int32_t)
#endif
#endif