2019-04-12 12:56:29 +00:00
|
|
|
#include "tommath_private.h"
|
|
|
|
#ifdef BN_DEPRECATED_C
|
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
|
|
|
|
2019-05-13 09:32:42 +00:00
|
|
|
#ifdef BN_MP_GET_BIT_C
|
|
|
|
int mp_get_bit(const mp_int *a, int b)
|
|
|
|
{
|
|
|
|
if (b < 0) {
|
|
|
|
return MP_VAL;
|
|
|
|
}
|
2019-05-18 09:12:07 +00:00
|
|
|
return (s_mp_get_bit(a, (unsigned int)b) == MP_YES) ? MP_YES : MP_NO;
|
2019-05-13 09:32:42 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-05-22 13:34:49 +00:00
|
|
|
#ifdef BN_MP_JACOBI_C
|
|
|
|
mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c)
|
2019-05-13 00:46:11 +00:00
|
|
|
{
|
|
|
|
if (a->sign == MP_NEG) {
|
|
|
|
return MP_VAL;
|
|
|
|
}
|
|
|
|
if (mp_cmp_d(n, 0uL) != MP_GT) {
|
|
|
|
return MP_VAL;
|
|
|
|
}
|
|
|
|
return mp_kronecker(a, n, c);
|
|
|
|
}
|
2019-05-19 08:55:27 +00:00
|
|
|
#endif
|
2019-05-13 09:18:34 +00:00
|
|
|
#ifdef BN_MP_PRIME_RANDOM_EX_C
|
|
|
|
mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
|
|
|
|
{
|
|
|
|
return s_mp_prime_random_ex(a, t, size, flags, cb, dat);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-13 09:14:57 +00:00
|
|
|
#ifdef BN_MP_RAND_DIGIT_C
|
|
|
|
mp_err mp_rand_digit(mp_digit *r)
|
|
|
|
{
|
2019-05-19 15:16:13 +00:00
|
|
|
mp_err err = s_mp_rand_source(r, sizeof(mp_digit));
|
2019-05-13 09:14:57 +00:00
|
|
|
*r &= MP_MASK;
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2019-05-13 09:14:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-04-12 12:56:29 +00:00
|
|
|
#ifdef BN_FAST_MP_INVMOD_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_invmod_fast(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_montgomery_reduce_fast(x, n, rho);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err fast_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
|
|
|
{
|
|
|
|
return s_mp_mul_digs_fast(a, b, c, digs);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err fast_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
|
|
|
{
|
|
|
|
return s_mp_mul_high_digs_fast(a, b, c, digs);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_FAST_S_MP_SQR_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_sqr_fast(a, b);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_BALANCE_MUL_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_balance_mul(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_EXPTMOD_FAST_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_exptmod_fast(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
|
|
|
{
|
|
|
|
return s_mp_exptmod_fast(G, X, P, Y, redmode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_INVMOD_SLOW_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_invmod_slow(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_KARATSUBA_MUL_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_karatsuba_mul(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_KARATSUBA_SQR_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_karatsuba_sqr(a, b);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_TOOM_MUL_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_toom_mul(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_TOOM_SQR_C
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_toom_sqr(const mp_int *a, mp_int *b)
|
2019-04-12 12:56:29 +00:00
|
|
|
{
|
|
|
|
return s_mp_toom_sqr(a, b);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-19 18:43:42 +00:00
|
|
|
#ifdef S_MP_REVERSE_C
|
2019-04-12 12:56:29 +00:00
|
|
|
void bn_reverse(unsigned char *s, int len)
|
|
|
|
{
|
|
|
|
s_mp_reverse(s, len);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-20 17:11:25 +00:00
|
|
|
#ifdef BN_MP_TC_AND_C
|
|
|
|
mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
|
|
|
|
{
|
|
|
|
return mp_and(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_TC_OR_C
|
|
|
|
mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
|
|
|
|
{
|
|
|
|
return mp_or(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_TC_XOR_C
|
|
|
|
mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
|
|
|
|
{
|
|
|
|
return mp_xor(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_TC_DIV_2D_C
|
|
|
|
mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
|
|
|
|
{
|
|
|
|
return mp_signed_rsh(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-20 08:41:06 +00:00
|
|
|
#ifdef BN_MP_INIT_SET_INT_C
|
|
|
|
mp_err mp_init_set_int(mp_int *a, unsigned long b)
|
|
|
|
{
|
2019-05-24 08:21:54 +00:00
|
|
|
return mp_init_u32(a, (uint32_t)b);
|
2019-05-20 08:41:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_SET_INT_C
|
|
|
|
mp_err mp_set_int(mp_int *a, unsigned long b)
|
|
|
|
{
|
2019-05-24 08:21:54 +00:00
|
|
|
mp_set_u32(a, (uint32_t)b);
|
2019-05-20 08:41:06 +00:00
|
|
|
return MP_OKAY;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_SET_LONG_C
|
|
|
|
mp_err mp_set_long(mp_int *a, unsigned long b)
|
|
|
|
{
|
2019-05-24 08:21:54 +00:00
|
|
|
mp_set_u64(a, b);
|
2019-05-20 08:41:06 +00:00
|
|
|
return MP_OKAY;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_SET_LONG_LONG_C
|
|
|
|
mp_err mp_set_long_long(mp_int *a, unsigned long long b)
|
|
|
|
{
|
2019-05-24 08:21:54 +00:00
|
|
|
mp_set_u64(a, b);
|
2019-05-20 08:41:06 +00:00
|
|
|
return MP_OKAY;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_GET_INT_C
|
|
|
|
unsigned long mp_get_int(const mp_int *a)
|
|
|
|
{
|
2019-05-23 21:39:47 +00:00
|
|
|
return mp_get_mag32(a);
|
2019-05-20 08:41:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_GET_LONG_C
|
|
|
|
unsigned long mp_get_long(const mp_int *a)
|
|
|
|
{
|
2019-05-31 08:53:18 +00:00
|
|
|
return (sizeof(long) > sizeof(int32_t)) ? (unsigned long)mp_get_mag64(a) : (unsigned long)mp_get_mag32(a);
|
2019-05-20 08:41:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_GET_LONG_LONG_C
|
|
|
|
unsigned long long mp_get_long_long(const mp_int *a)
|
|
|
|
{
|
|
|
|
return (unsigned long long)mp_get_mag64(a);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-22 13:34:49 +00:00
|
|
|
#ifdef BN_MP_PRIME_IS_DIVISIBLE_C
|
|
|
|
mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result)
|
|
|
|
{
|
|
|
|
return s_mp_prime_is_divisible(a, result);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-25 04:42:01 +00:00
|
|
|
#ifdef BN_MP_EXPT_D_EX_C
|
|
|
|
mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|
|
|
{
|
|
|
|
(void)fast;
|
|
|
|
return mp_expt_d(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BN_MP_N_ROOT_EX_C
|
|
|
|
mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
|
|
|
{
|
|
|
|
(void)fast;
|
|
|
|
return mp_n_root(a, b, c);
|
|
|
|
}
|
|
|
|
#endif
|
2019-04-12 12:56:29 +00:00
|
|
|
#endif
|