Skip to content

Commit 32c9405

Browse files
committed
Removed bn_conversion.c in favor of individual files
1 parent 380d03b commit 32c9405

21 files changed

+423
-206
lines changed

bn_conversion.c

Lines changed: 0 additions & 103 deletions
This file was deleted.

bn_mp_get_i32.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_GET_I32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* Return the least significant 32 bits */
8+
int32_t mp_get_i32(const mp_int *a)
9+
{
10+
uint64_t res = mp_get_mag32(a);
11+
return (a->sign == MP_NEG) ? (int32_t)-res : (int32_t)res;
12+
}
13+
14+
15+
#endif

bn_mp_get_i64.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_GET_I64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* Return the least significant 64 bits */
8+
int64_t mp_get_i64(const mp_int *a)
9+
{
10+
uint64_t res = mp_get_mag64(a);
11+
return (a->sign == MP_NEG) ? (int64_t)-res : (int64_t)res;
12+
}
13+
14+
15+
#endif

bn_mp_get_mag32.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_GET_MAG32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* Return the absolute value of the least significant 32 bits */
8+
uint32_t mp_get_mag32(const mp_int *a)
9+
{
10+
unsigned i = MP_MIN((unsigned)a->used, (unsigned)((32 + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT));
11+
uint32_t res = 0uL;
12+
while (i-- > 0) {
13+
res <<= ((32 <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);
14+
res |= (uint32_t)a->dp[i];
15+
if (32 <= MP_DIGIT_BIT) {
16+
break;
17+
}
18+
}
19+
return res;
20+
}
21+
#endif

bn_mp_get_mag64.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_GET_MAG64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* Return the absolute value of the least significant 64 bits */
8+
uint64_t mp_get_mag64(const mp_int *a)
9+
{
10+
unsigned i = MP_MIN((unsigned)a->used, (unsigned)((64 + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT));
11+
uint64_t res = 0uL;
12+
while (i-- > 0) {
13+
res <<= ((64 <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);
14+
res |= (uint64_t)a->dp[i];
15+
if (64 <= MP_DIGIT_BIT) {
16+
break;
17+
}
18+
}
19+
return res;
20+
}
21+
22+
23+
#endif

bn_mp_init_i32.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_INIT_I32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* initialize "a" with a signed 32-bit integer */
8+
mp_err mp_init_i32(mp_int *a, int32_t b)
9+
{
10+
mp_err err;
11+
if ((err = mp_init(a)) != 0) {
12+
return err;
13+
}
14+
mp_set_i32(a, b);
15+
return 0;
16+
}
17+
18+
19+
#endif

bn_mp_init_i64.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_INIT_I64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* initialize "a" with a signed 64-bit integer */
8+
mp_err mp_init_i64(mp_int *a, int64_t b)
9+
{
10+
mp_err err;
11+
if ((err = mp_init(a)) != 0) {
12+
return err;
13+
}
14+
mp_set_i64(a, b);
15+
return 0;
16+
}
17+
18+
19+
#endif

bn_mp_init_u32.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_INIT_U32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* initialize "a" with an unsigned 32-bit integer */
8+
mp_err mp_init_u32(mp_int *a, uint32_t b)
9+
{
10+
mp_err err;
11+
if ((err = mp_init(a)) != 0) {
12+
return err;
13+
}
14+
mp_set_u32(a, b);
15+
return 0;
16+
}
17+
18+
19+
20+
#endif

bn_mp_init_u64.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_INIT_U64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* initialize "a" with an unsigned 64-bit integer */
8+
mp_err mp_init_u64(mp_int *a, uint64_t b)
9+
{
10+
mp_err err;
11+
if ((err = mp_init(a)) != 0) {
12+
return err;
13+
}
14+
mp_set_u64(a, b);
15+
return 0;
16+
}
17+
18+
19+
20+
#endif

bn_mp_set_i32.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_SET_I32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* Set the least signficant 32 bit of "a" to b, signed */
8+
void mp_set_i32(mp_int *a, int32_t b)
9+
{
10+
mp_set_u32(a, (b < 0) ? -(uint32_t)b : (uint32_t)b);
11+
if (b < 0) {
12+
a->sign = MP_NEG;
13+
}
14+
}
15+
#endif

bn_mp_set_i64.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_SET_I64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* Set the least signficant 64 bit of "a" to b, signed */
8+
void mp_set_i64(mp_int *a, int64_t b)
9+
{
10+
mp_set_u64(a, (b < 0) ? -(uint64_t)b : (uint64_t)b);
11+
if (b < 0) {
12+
a->sign = MP_NEG;
13+
}
14+
}
15+
16+
17+
#endif

bn_mp_set_u32.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_SET_U32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* Set the least signficant 32 bit of "a" to b, unsigned */
8+
void mp_set_u32(mp_int *a, uint32_t b)
9+
{
10+
int i = 0;
11+
while (b != 0u) {
12+
a->dp[i++] = ((mp_digit)b & MP_MASK);
13+
if (32 <= MP_DIGIT_BIT) {
14+
break;
15+
}
16+
b >>= ((32 <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);
17+
}
18+
a->used = i;
19+
a->sign = MP_ZPOS;
20+
MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used);
21+
}
22+
#endif

bn_mp_set_u64.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_SET_U64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
7+
/* Set the least signficant 64 bit of "a" to b, unsigned */
8+
void mp_set_u64(mp_int *a, uint64_t b)
9+
{
10+
int i = 0;
11+
while (b != 0u) {
12+
a->dp[i++] = ((mp_digit)b & MP_MASK);
13+
if (64 <= MP_DIGIT_BIT) {
14+
break;
15+
}
16+
b >>= ((64 <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);
17+
}
18+
a->used = i;
19+
a->sign = MP_ZPOS;
20+
MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used);
21+
}
22+
23+
24+
#endif

0 commit comments

Comments
 (0)