Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ UpgradeLog*.htm
*.vspx
*.sap

# Linux perf profiler
perf.data
perf.data.old

# ignore mpi.c generated by make
mpi.c

Expand Down
103 changes: 103 additions & 0 deletions bn_conversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#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_U32_C
MP_SET_UNSIGNED(mp_set_u32, 32)
#endif

#ifdef BN_MP_SET_U64_C
MP_SET_UNSIGNED(mp_set_u64, 64)
#endif

#ifdef BN_MP_SET_I32_C
MP_SET_SIGNED(mp_set_i32, mp_set_u32, 32)
#endif

#ifdef BN_MP_SET_I64_C
MP_SET_SIGNED(mp_set_i64, mp_set_u64, 64)
#endif

#if defined(BN_MP_GET_I32_C) || defined(BN_MP_GET_U32_C)
MP_GET_SIGNED(mp_get_i32, mp_get_mag32, 32)
#endif

#if defined(BN_MP_GET_I64_C) || defined(BN_MP_GET_U64_C)
MP_GET_SIGNED(mp_get_i64, mp_get_mag64, 64)
#endif

#ifdef BN_MP_GET_MAG32_C
MP_GET_MAG(mp_get_mag32, 32)
#endif

#ifdef BN_MP_GET_MAG64_C
MP_GET_MAG(mp_get_mag64, 64)
#endif

#ifdef BN_MP_INIT_U32_C
MP_INIT_INT(mp_init_u32, mp_set_u32, uint32_t)
#endif

#ifdef BN_MP_INIT_I32_C
MP_INIT_INT(mp_init_i32, mp_set_i32, int32_t)
#endif

#ifdef BN_MP_INIT_U64_C
MP_INIT_INT(mp_init_u64, mp_set_u64, uint64_t)
#endif

#ifdef BN_MP_INIT_I64_C
MP_INIT_INT(mp_init_i64, mp_set_i64, int64_t)
#endif

#endif
45 changes: 45 additions & 0 deletions bn_deprecated.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,49 @@ mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
return mp_signed_rsh(a, b, c);
}
#endif
#ifdef BN_MP_INIT_SET_INT_C
mp_err mp_init_set_int(mp_int *a, unsigned long b)
{
return mp_init_u32(a, (uint32_t)b);
}
#endif
#ifdef BN_MP_SET_INT_C
mp_err mp_set_int(mp_int *a, unsigned long b)
{
mp_set_u32(a, (uint32_t)b);
return MP_OKAY;
}
#endif
#ifdef BN_MP_SET_LONG_C
mp_err mp_set_long(mp_int *a, unsigned long b)
{
mp_set_u64(a, b);
return MP_OKAY;
}
#endif
#ifdef BN_MP_SET_LONG_LONG_C
mp_err mp_set_long_long(mp_int *a, unsigned long long b)
{
mp_set_u64(a, b);
return MP_OKAY;
}
#endif
#ifdef BN_MP_GET_INT_C
unsigned long mp_get_int(const mp_int *a)
{
return mp_get_mag32(a);
}
#endif
#ifdef BN_MP_GET_LONG_C
unsigned long mp_get_long(const mp_int *a)
{
return sizeof(long) > sizeof(int32_t) ? (unsigned long)mp_get_mag64(a) : (unsigned long)mp_get_mag32(a);
}
#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
#endif
12 changes: 0 additions & 12 deletions bn_mp_get_int.c

This file was deleted.

29 changes: 0 additions & 29 deletions bn_mp_get_long.c

This file was deleted.

29 changes: 0 additions & 29 deletions bn_mp_get_long_long.c

This file was deleted.

14 changes: 6 additions & 8 deletions bn_mp_ilogb.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
mp_ord cmp;
unsigned int high, low, mid;
mp_int bracket_low, bracket_high, bracket_mid, t, bi_base;
mp_digit tmp;

err = MP_OKAY;
if (a->sign == MP_NEG) {
Expand All @@ -90,12 +89,11 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
return MP_VAL;
}
if (base == 2u) {
mp_set_int(c, (unsigned long)(mp_count_bits(a) - 1));
mp_set_u32(c, (uint32_t)(mp_count_bits(a) - 1));
return err;
}
if (a->used == 1) {
tmp = s_digit_ilogb(base, a->dp[0]);
mp_set(c, tmp);
mp_set(c, s_digit_ilogb(base, a->dp[0]));
return err;
}

Expand All @@ -106,7 +104,7 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
return err;
}
if (cmp == MP_EQ) {
mp_set(c, (mp_digit)1uL);
mp_set(c, 1u);
return err;
}

Expand Down Expand Up @@ -163,15 +161,15 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
mp_exch(&bracket_mid, &bracket_low);
}
if (cmp == MP_EQ) {
mp_set_int(c, (unsigned long)mid);
mp_set_u32(c, mid);
goto LBL_END;
}
}

if (mp_cmp(&bracket_high, a) == MP_EQ) {
mp_set_int(c, (unsigned long)high);
mp_set_u32(c, high);
} else {
mp_set_int(c, (unsigned long)low);
mp_set_u32(c, low);
}

LBL_END:
Expand Down
15 changes: 0 additions & 15 deletions bn_mp_init_set_int.c

This file was deleted.

4 changes: 2 additions & 2 deletions bn_mp_is_square.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
}


if ((err = mp_init_set_int(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
if ((err = mp_init_u32(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
return err;
}
if ((err = mp_mod(arg, &t, &t)) != MP_OKAY) {
goto LBL_ERR;
}
r = mp_get_int(&t);
r = mp_get_u32(&t);
/* Check for other prime modules, note it's not an ERROR but we must
* free "t" so the easiest way is to goto LBL_ERR. We know that err
* is already equal to MP_OKAY from the mp_mod call
Expand Down
6 changes: 3 additions & 3 deletions bn_mp_prime_frobenius_underwood.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
continue;
}
/* (32764^2 - 4) < 2^31, no bigint for >MP_8BIT needed) */
mp_set_long(&T1z, (unsigned long)a);
mp_set_u32(&T1z, (uint32_t)a);

if ((err = mp_sqr(&T1z, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
Expand Down Expand Up @@ -72,7 +72,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
goto LBL_FU_ERR;
}
/* Composite if N and (a+4)*(2*a+5) are not coprime */
mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)));
mp_set_u32(&T1z, (uint32_t)((a+4)*((2*a)+5)));

if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
Expand Down Expand Up @@ -157,7 +157,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
}
}

mp_set_long(&T1z, (unsigned long)((2 * a) + 5));
mp_set_u32(&T1z, (uint32_t)((2 * a) + 5));
if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
}
Expand Down
Loading