Skip to content

Commit 7c2740b

Browse files
committed
No grow necessary in mp_set_int* functions
* mp_set_int* always return MP_OKAY * remove return checks for mp_set_int* * introduce MP_MIN_PREC
1 parent 235e831 commit 7c2740b

13 files changed

+42
-100
lines changed

bn_mp_grow.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ int mp_grow(mp_int *a, int size)
1111

1212
/* if the alloc size is smaller alloc more ram */
1313
if (a->alloc < size) {
14-
/* ensure there are always at least MP_PREC digits extra on top */
15-
size += (MP_PREC * 2) - (size % MP_PREC);
16-
1714
/* reallocate the array a->dp
1815
*
1916
* We store the return in a temporary variable

bn_mp_ilogb.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ int mp_ilogb(mp_int *a, mp_digit base, mp_int *c)
9090
}
9191
if (base == 2u) {
9292
cmp = mp_count_bits(a) - 1;
93-
if ((err = mp_set_int(c, (unsigned long)cmp)) != MP_OKAY) {
94-
goto LBL_ERR;
95-
}
93+
mp_set_int(c, (unsigned long)cmp);
9694
return err;
9795
}
9896
if (a->used == 1) {
@@ -165,21 +163,15 @@ int mp_ilogb(mp_int *a, mp_digit base, mp_int *c)
165163
mp_exch(&bracket_mid, &bracket_low);
166164
}
167165
if (cmp == MP_EQ) {
168-
if ((err = mp_set_int(c, (unsigned long)mid)) != MP_OKAY) {
169-
goto LBL_ERR;
170-
}
166+
mp_set_int(c, (unsigned long)mid);
171167
goto LBL_END;
172168
}
173169
}
174170

175171
if (mp_cmp(&bracket_high, a) == MP_EQ) {
176-
if ((err = mp_set_int(c, (unsigned long)high)) != MP_OKAY) {
177-
goto LBL_ERR;
178-
}
172+
mp_set_int(c, (unsigned long)high);
179173
} else {
180-
if ((err = mp_set_int(c, (unsigned long)low)) != MP_OKAY) {
181-
goto LBL_ERR;
182-
}
174+
mp_set_int(c, (unsigned long)low);
183175
}
184176

185177
LBL_END:

bn_mp_init_size.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
/* init an mp_init for a given size */
77
int mp_init_size(mp_int *a, int size)
88
{
9-
/* pad size so there are always extra digits */
10-
size += (MP_PREC * 2) - (size % MP_PREC);
9+
size = MP_MAX(MP_MIN_PREC, size);
1110

1211
/* alloc mem */
1312
a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit));

bn_mp_prime_frobenius_underwood.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
4343
continue;
4444
}
4545
/* (32764^2 - 4) < 2^31, no bigint for >MP_8BIT needed) */
46-
if ((e = mp_set_long(&T1z, (unsigned long)a)) != MP_OKAY) {
47-
goto LBL_FU_ERR;
48-
}
46+
mp_set_long(&T1z, (unsigned long)a);
4947

5048
if ((e = mp_sqr(&T1z, &T1z)) != MP_OKAY) {
5149
goto LBL_FU_ERR;
@@ -74,9 +72,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
7472
goto LBL_FU_ERR;
7573
}
7674
/* Composite if N and (a+4)*(2*a+5) are not coprime */
77-
if ((e = mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)))) != MP_OKAY) {
78-
goto LBL_FU_ERR;
79-
}
75+
mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)));
8076

8177
if ((e = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) {
8278
goto LBL_FU_ERR;
@@ -165,9 +161,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
165161
}
166162
}
167163

168-
if ((e = mp_set_long(&T1z, (unsigned long)((2 * a) + 5))) != MP_OKAY) {
169-
goto LBL_FU_ERR;
170-
}
164+
mp_set_long(&T1z, (unsigned long)((2 * a) + 5));
171165
if ((e = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
172166
goto LBL_FU_ERR;
173167
}

bn_mp_prime_strong_lucas_selfridge.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ static int s_mp_mul_si(const mp_int *a, long d, mp_int *c)
3636
* mp_digit might be smaller than a long, which excludes
3737
* the use of mp_mul_d() here.
3838
*/
39-
if ((err = mp_set_long(&t, (unsigned long) d)) != MP_OKAY) {
40-
goto LBL_MPMULSI_ERR;
41-
}
39+
mp_set_long(&t, (unsigned long) d);
4240
if ((err = mp_mul(a, &t, c)) != MP_OKAY) {
4341
goto LBL_MPMULSI_ERR;
4442
}
@@ -95,9 +93,7 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
9593
for (;;) {
9694
Ds = sign * D;
9795
sign = -sign;
98-
if ((e = mp_set_long(&Dz, (unsigned long)D)) != MP_OKAY) {
99-
goto LBL_LS_ERR;
100-
}
96+
mp_set_long(&Dz, (unsigned long)D);
10197
if ((e = mp_gcd(a, &Dz, &gcd)) != MP_OKAY) {
10298
goto LBL_LS_ERR;
10399
}
@@ -193,31 +189,23 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
193189

194190
if (Q < 0) {
195191
Q = -Q;
196-
if ((e = mp_set_long(&Qmz, (unsigned long)Q)) != MP_OKAY) {
197-
goto LBL_LS_ERR;
198-
}
192+
mp_set_long(&Qmz, (unsigned long)Q);
199193
if ((e = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
200194
goto LBL_LS_ERR;
201195
}
202196
/* Initializes calculation of Q^d */
203-
if ((e = mp_set_long(&Qkdz, (unsigned long)Q)) != MP_OKAY) {
204-
goto LBL_LS_ERR;
205-
}
197+
mp_set_long(&Qkdz, (unsigned long)Q);
206198
Qmz.sign = MP_NEG;
207199
Q2mz.sign = MP_NEG;
208200
Qkdz.sign = MP_NEG;
209201
Q = -Q;
210202
} else {
211-
if ((e = mp_set_long(&Qmz, (unsigned long)Q)) != MP_OKAY) {
212-
goto LBL_LS_ERR;
213-
}
203+
mp_set_long(&Qmz, (unsigned long)Q);
214204
if ((e = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
215205
goto LBL_LS_ERR;
216206
}
217207
/* Initializes calculation of Q^d */
218-
if ((e = mp_set_long(&Qkdz, (unsigned long)Q)) != MP_OKAY) {
219-
goto LBL_LS_ERR;
220-
}
208+
mp_set_long(&Qkdz, (unsigned long)Q);
221209
}
222210

223211
Nbits = mp_count_bits(&Dz);

bn_mp_set_double.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ int mp_set_double(mp_int *a, double b)
2222
}
2323
exp -= 1023 + 52;
2424

25-
res = mp_set_long_long(a, frac);
26-
if (res != MP_OKAY) {
27-
return res;
28-
}
25+
mp_set_long_long(a, frac);
2926

3027
res = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a);
3128
if (res != MP_OKAY) {

bn_mp_shrink.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@
77
int mp_shrink(mp_int *a)
88
{
99
mp_digit *tmp;
10-
int used = 1;
11-
12-
if (a->used > 0) {
13-
used = a->used;
14-
}
15-
16-
if (a->alloc != used) {
10+
int alloc = MP_MAX(MP_MIN_PREC, a->used);
11+
if (a->alloc != alloc) {
1712
if ((tmp = (mp_digit *) MP_REALLOC(a->dp,
1813
(size_t)a->alloc * sizeof(mp_digit),
19-
(size_t)used * sizeof(mp_digit))) == NULL) {
14+
(size_t)alloc * sizeof(mp_digit))) == NULL) {
2015
return MP_MEM;
2116
}
2217
a->dp = tmp;
23-
a->alloc = used;
18+
a->alloc = alloc;
2419
}
2520
return MP_OKAY;
2621
}

bn_mp_sqrtmod_prime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
5858
}
5959

6060
/* find a Z such that the Legendre symbol (Z|prime) == -1 */
61-
if ((res = mp_set_int(&Z, 2uL)) != MP_OKAY) goto cleanup;
61+
mp_set_int(&Z, 2uL);
6262
/* Z = 2 */
6363
while (1) {
6464
if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY) goto cleanup;
@@ -78,7 +78,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
7878
/* T = n ^ Q mod prime */
7979
if ((res = mp_copy(&S, &M)) != MP_OKAY) goto cleanup;
8080
/* M = S */
81-
if ((res = mp_set_int(&two, 2uL)) != MP_OKAY) goto cleanup;
81+
mp_set_int(&two, 2uL);
8282

8383
res = MP_VAL;
8484
while (1) {

demo/test.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,7 @@ static int test_mp_get_long(void)
599599
t = ~0UL;
600600
printf(" t = 0x%lx i = %d\r", t, i);
601601
do {
602-
if (mp_set_long(&a, t) != MP_OKAY) {
603-
printf("\nmp_set_long() error!");
604-
goto LBL_ERR;
605-
}
602+
mp_set_long(&a, t);
606603
s = mp_get_long(&a);
607604
if (s != t) {
608605
printf("\nmp_get_long() bad result! 0x%lx != 0x%lx", s, t);
@@ -635,10 +632,7 @@ static int test_mp_get_long_long(void)
635632
r = ~0ULL;
636633
printf(" r = 0x%llx i = %d\r", r, i);
637634
do {
638-
if (mp_set_long_long(&a, r) != MP_OKAY) {
639-
printf("\nmp_set_long_long() error!");
640-
goto LBL_ERR;
641-
}
635+
mp_set_long_long(&a, r);
642636
q = mp_get_long_long(&a);
643637
if (q != r) {
644638
printf("\nmp_get_long_long() bad result! 0x%llx != 0x%llx", q, r);

doc/bn.tex

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -856,11 +856,7 @@ \subsection{Long Constants}
856856
\}
857857
858858
/* set the number to 654321 (note this is bigger than 127) */
859-
if ((result = mp_set_int(&number, 654321)) != MP_OKAY) \{
860-
printf("Error setting the value of the number. \%s",
861-
mp_error_to_string(result));
862-
return EXIT_FAILURE;
863-
\}
859+
mp_set_int(&number, 654321);
864860
865861
printf("number == \%lu", mp_get_int(&number));
866862
@@ -1396,17 +1392,8 @@ \section{Multiplication}
13961392
\}
13971393
13981394
/* set the terms */
1399-
if ((result = mp_set_int(&number, 257)) != MP_OKAY) \{
1400-
printf("Error setting number1. \%s",
1401-
mp_error_to_string(result));
1402-
return EXIT_FAILURE;
1403-
\}
1404-
1405-
if ((result = mp_set_int(&number2, 1023)) != MP_OKAY) \{
1406-
printf("Error setting number2. \%s",
1407-
mp_error_to_string(result));
1408-
return EXIT_FAILURE;
1409-
\}
1395+
mp_set_int(&number, 257);
1396+
mp_set_int(&number2, 1023);
14101397
14111398
/* multiply them */
14121399
if ((result = mp_mul(&number1, &number2,

0 commit comments

Comments
 (0)