Skip to content

Commit 3289c95

Browse files
committed
introduce various mp_set_sint/mp_set_uint functions with precise types
1 parent 51512f9 commit 3289c95

28 files changed

+583
-462
lines changed

bn_conversion.c

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

bn_deprecated.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,49 @@ mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
146146
return mp_signed_rsh(a, b, c);
147147
}
148148
#endif
149+
#ifdef BN_MP_INIT_SET_INT_C
150+
mp_err mp_init_set_int(mp_int *a, unsigned long b)
151+
{
152+
return mp_init_uint(a, (uint32_t)b);
153+
}
154+
#endif
155+
#ifdef BN_MP_SET_INT_C
156+
mp_err mp_set_int(mp_int *a, unsigned long b)
157+
{
158+
mp_set_uint(a, (uint32_t)b);
159+
return MP_OKAY;
160+
}
161+
#endif
162+
#ifdef BN_MP_SET_LONG_C
163+
mp_err mp_set_long(mp_int *a, unsigned long b)
164+
{
165+
mp_set_uint64(a, b);
166+
return MP_OKAY;
167+
}
168+
#endif
169+
#ifdef BN_MP_SET_LONG_LONG_C
170+
mp_err mp_set_long_long(mp_int *a, unsigned long long b)
171+
{
172+
mp_set_uint64(a, b);
173+
return MP_OKAY;
174+
}
175+
#endif
176+
#ifdef BN_MP_GET_INT_C
177+
unsigned long mp_get_int(const mp_int *a)
178+
{
179+
return mp_get_mag(a);
180+
}
181+
#endif
182+
#ifdef BN_MP_GET_LONG_C
183+
unsigned long mp_get_long(const mp_int *a)
184+
{
185+
return sizeof(long) > sizeof(int32_t) ? (unsigned long)mp_get_mag64(a) : (unsigned long)mp_get_mag(a);
186+
}
187+
#endif
188+
#ifdef BN_MP_GET_LONG_LONG_C
189+
unsigned long long mp_get_long_long(const mp_int *a)
190+
{
191+
return (unsigned long long)mp_get_mag64(a);
192+
}
193+
#endif
149194
#endif

bn_mp_get_int.c

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

bn_mp_get_long.c

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

bn_mp_get_long_long.c

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

bn_mp_ilogb.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
7676
mp_ord cmp;
7777
unsigned int high, low, mid;
7878
mp_int bracket_low, bracket_high, bracket_mid, t, bi_base;
79-
mp_digit tmp;
8079

8180
err = MP_OKAY;
8281
if (a->sign == MP_NEG) {
@@ -90,12 +89,11 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
9089
return MP_VAL;
9190
}
9291
if (base == 2u) {
93-
mp_set_int(c, (unsigned long)(mp_count_bits(a) - 1));
92+
mp_set_uint(c, (uint32_t)(mp_count_bits(a) - 1));
9493
return err;
9594
}
9695
if (a->used == 1) {
97-
tmp = s_digit_ilogb(base, a->dp[0]);
98-
mp_set(c, tmp);
96+
mp_set(c, s_digit_ilogb(base, a->dp[0]));
9997
return err;
10098
}
10199

@@ -106,7 +104,7 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
106104
return err;
107105
}
108106
if (cmp == MP_EQ) {
109-
mp_set(c, (mp_digit)1uL);
107+
mp_set(c, 1u);
110108
return err;
111109
}
112110

@@ -163,15 +161,15 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
163161
mp_exch(&bracket_mid, &bracket_low);
164162
}
165163
if (cmp == MP_EQ) {
166-
mp_set_int(c, (unsigned long)mid);
164+
mp_set_uint(c, mid);
167165
goto LBL_END;
168166
}
169167
}
170168

171169
if (mp_cmp(&bracket_high, a) == MP_EQ) {
172-
mp_set_int(c, (unsigned long)high);
170+
mp_set_uint(c, high);
173171
} else {
174-
mp_set_int(c, (unsigned long)low);
172+
mp_set_uint(c, low);
175173
}
176174

177175
LBL_END:

bn_mp_init_set_int.c

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

bn_mp_is_square.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
5858
}
5959

6060

61-
if ((err = mp_init_set_int(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
61+
if ((err = mp_init_uint(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
6262
return err;
6363
}
6464
if ((err = mp_mod(arg, &t, &t)) != MP_OKAY) {
6565
goto LBL_ERR;
6666
}
67-
r = mp_get_int(&t);
67+
r = mp_get_uint(&t);
6868
/* Check for other prime modules, note it's not an ERROR but we must
6969
* free "t" so the easiest way is to goto LBL_ERR. We know that err
7070
* is already equal to MP_OKAY from the mp_mod call

bn_mp_prime_frobenius_underwood.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
4343
continue;
4444
}
4545
/* (32764^2 - 4) < 2^31, no bigint for >MP_8BIT needed) */
46-
mp_set_long(&T1z, (unsigned long)a);
46+
mp_set_uint(&T1z, (uint32_t)a);
4747

4848
if ((err = mp_sqr(&T1z, &T1z)) != MP_OKAY) {
4949
goto LBL_FU_ERR;
@@ -72,7 +72,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
7272
goto LBL_FU_ERR;
7373
}
7474
/* Composite if N and (a+4)*(2*a+5) are not coprime */
75-
mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)));
75+
mp_set_uint(&T1z, (uint32_t)((a+4)*((2*a)+5)));
7676

7777
if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) {
7878
goto LBL_FU_ERR;
@@ -157,7 +157,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
157157
}
158158
}
159159

160-
mp_set_long(&T1z, (unsigned long)((2 * a) + 5));
160+
mp_set_uint(&T1z, (uint32_t)((2 * a) + 5));
161161
if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
162162
goto LBL_FU_ERR;
163163
}

0 commit comments

Comments
 (0)