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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
index 03ef6fa..7df66f9 100644
--- a/bn_mp_grow.c
+++ b/bn_mp_grow.c
@@ -11,9 +11,6 @@ int mp_grow(mp_int *a, int size)
/* if the alloc size is smaller alloc more ram */
if (a->alloc < size) {
- /* ensure there are always at least MP_PREC digits extra on top */
- size += (MP_PREC * 2) - (size % MP_PREC);
-
/* reallocate the array a->dp
*
* We store the return in a temporary variable
diff --git a/bn_mp_ilogb.c b/bn_mp_ilogb.c
index 9c32c5b..d1ff4e9 100644
--- a/bn_mp_ilogb.c
+++ b/bn_mp_ilogb.c
@@ -90,9 +90,7 @@ int mp_ilogb(mp_int *a, mp_digit base, mp_int *c)
}
if (base == 2u) {
cmp = mp_count_bits(a) - 1;
- if ((err = mp_set_int(c, (unsigned long)cmp)) != MP_OKAY) {
- goto LBL_ERR;
- }
+ mp_set_int(c, (unsigned long)cmp);
return err;
}
if (a->used == 1) {
@@ -165,21 +163,15 @@ int mp_ilogb(mp_int *a, mp_digit base, mp_int *c)
mp_exch(&bracket_mid, &bracket_low);
}
if (cmp == MP_EQ) {
- if ((err = mp_set_int(c, (unsigned long)mid)) != MP_OKAY) {
- goto LBL_ERR;
- }
+ mp_set_int(c, (unsigned long)mid);
goto LBL_END;
}
}
if (mp_cmp(&bracket_high, a) == MP_EQ) {
- if ((err = mp_set_int(c, (unsigned long)high)) != MP_OKAY) {
- goto LBL_ERR;
- }
+ mp_set_int(c, (unsigned long)high);
} else {
- if ((err = mp_set_int(c, (unsigned long)low)) != MP_OKAY) {
- goto LBL_ERR;
- }
+ mp_set_int(c, (unsigned long)low);
}
LBL_END:
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
index 35136f5..d97f8be 100644
--- a/bn_mp_init_size.c
+++ b/bn_mp_init_size.c
@@ -6,8 +6,7 @@
/* init an mp_init for a given size */
int mp_init_size(mp_int *a, int size)
{
- /* pad size so there are always extra digits */
- size += (MP_PREC * 2) - (size % MP_PREC);
+ size = MP_MAX(MP_MIN_PREC, size);
/* alloc mem */
a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit));
diff --git a/bn_mp_prime_frobenius_underwood.c b/bn_mp_prime_frobenius_underwood.c
index 71de5e9..8855cfb 100644
--- a/bn_mp_prime_frobenius_underwood.c
+++ b/bn_mp_prime_frobenius_underwood.c
@@ -43,9 +43,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
continue;
}
/* (32764^2 - 4) < 2^31, no bigint for >MP_8BIT needed) */
- if ((e = mp_set_long(&T1z, (unsigned long)a)) != MP_OKAY) {
- goto LBL_FU_ERR;
- }
+ mp_set_long(&T1z, (unsigned long)a);
if ((e = mp_sqr(&T1z, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
@@ -74,9 +72,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
goto LBL_FU_ERR;
}
/* Composite if N and (a+4)*(2*a+5) are not coprime */
- if ((e = mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)))) != MP_OKAY) {
- goto LBL_FU_ERR;
- }
+ mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)));
if ((e = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
@@ -165,9 +161,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
}
}
- if ((e = mp_set_long(&T1z, (unsigned long)((2 * a) + 5))) != MP_OKAY) {
- goto LBL_FU_ERR;
- }
+ mp_set_long(&T1z, (unsigned long)((2 * a) + 5));
if ((e = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
}
diff --git a/bn_mp_prime_strong_lucas_selfridge.c b/bn_mp_prime_strong_lucas_selfridge.c
index 74d8a5b..44143b0 100644
--- a/bn_mp_prime_strong_lucas_selfridge.c
+++ b/bn_mp_prime_strong_lucas_selfridge.c
@@ -36,9 +36,7 @@ static int s_mp_mul_si(const mp_int *a, long d, mp_int *c)
* mp_digit might be smaller than a long, which excludes
* the use of mp_mul_d() here.
*/
- if ((err = mp_set_long(&t, (unsigned long) d)) != MP_OKAY) {
- goto LBL_MPMULSI_ERR;
- }
+ mp_set_long(&t, (unsigned long) d);
if ((err = mp_mul(a, &t, c)) != MP_OKAY) {
goto LBL_MPMULSI_ERR;
}
@@ -95,9 +93,7 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
for (;;) {
Ds = sign * D;
sign = -sign;
- if ((e = mp_set_long(&Dz, (unsigned long)D)) != MP_OKAY) {
- goto LBL_LS_ERR;
- }
+ mp_set_long(&Dz, (unsigned long)D);
if ((e = mp_gcd(a, &Dz, &gcd)) != MP_OKAY) {
goto LBL_LS_ERR;
}
@@ -193,31 +189,23 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
if (Q < 0) {
Q = -Q;
- if ((e = mp_set_long(&Qmz, (unsigned long)Q)) != MP_OKAY) {
- goto LBL_LS_ERR;
- }
+ mp_set_long(&Qmz, (unsigned long)Q);
if ((e = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
goto LBL_LS_ERR;
}
/* Initializes calculation of Q^d */
- if ((e = mp_set_long(&Qkdz, (unsigned long)Q)) != MP_OKAY) {
- goto LBL_LS_ERR;
- }
+ mp_set_long(&Qkdz, (unsigned long)Q);
Qmz.sign = MP_NEG;
Q2mz.sign = MP_NEG;
Qkdz.sign = MP_NEG;
Q = -Q;
} else {
- if ((e = mp_set_long(&Qmz, (unsigned long)Q)) != MP_OKAY) {
- goto LBL_LS_ERR;
- }
+ mp_set_long(&Qmz, (unsigned long)Q);
if ((e = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
goto LBL_LS_ERR;
}
/* Initializes calculation of Q^d */
- if ((e = mp_set_long(&Qkdz, (unsigned long)Q)) != MP_OKAY) {
- goto LBL_LS_ERR;
- }
+ mp_set_long(&Qkdz, (unsigned long)Q);
}
Nbits = mp_count_bits(&Dz);
diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c
index 0ccd340..bd2ea37 100644
--- a/bn_mp_set_double.c
+++ b/bn_mp_set_double.c
@@ -22,10 +22,7 @@ int mp_set_double(mp_int *a, double b)
}
exp -= 1023 + 52;
- res = mp_set_long_long(a, frac);
- if (res != MP_OKAY) {
- return res;
- }
+ mp_set_long_long(a, frac);
res = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a);
if (res != MP_OKAY) {
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index e7a204b..fec5841 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -7,20 +7,15 @@
int mp_shrink(mp_int *a)
{
mp_digit *tmp;
- int used = 1;
-
- if (a->used > 0) {
- used = a->used;
- }
-
- if (a->alloc != used) {
+ int alloc = MP_MAX(MP_MIN_PREC, a->used);
+ if (a->alloc != alloc) {
if ((tmp = (mp_digit *) MP_REALLOC(a->dp,
(size_t)a->alloc * sizeof(mp_digit),
- (size_t)used * sizeof(mp_digit))) == NULL) {
+ (size_t)alloc * sizeof(mp_digit))) == NULL) {
return MP_MEM;
}
a->dp = tmp;
- a->alloc = used;
+ a->alloc = alloc;
}
return MP_OKAY;
}
diff --git a/bn_mp_sqrtmod_prime.c b/bn_mp_sqrtmod_prime.c
index a04e585..354e9f4 100644
--- a/bn_mp_sqrtmod_prime.c
+++ b/bn_mp_sqrtmod_prime.c
@@ -58,7 +58,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
}
/* find a Z such that the Legendre symbol (Z|prime) == -1 */
- if ((res = mp_set_int(&Z, 2uL)) != MP_OKAY) goto cleanup;
+ mp_set_int(&Z, 2uL);
/* Z = 2 */
while (1) {
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)
/* T = n ^ Q mod prime */
if ((res = mp_copy(&S, &M)) != MP_OKAY) goto cleanup;
/* M = S */
- if ((res = mp_set_int(&two, 2uL)) != MP_OKAY) goto cleanup;
+ mp_set_int(&two, 2uL);
res = MP_VAL;
while (1) {
diff --git a/demo/test.c b/demo/test.c
index 476aa9c..2d0348f 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -599,10 +599,7 @@ static int test_mp_get_long(void)
t = ~0UL;
printf(" t = 0x%lx i = %d\r", t, i);
do {
- if (mp_set_long(&a, t) != MP_OKAY) {
- printf("\nmp_set_long() error!");
- goto LBL_ERR;
- }
+ mp_set_long(&a, t);
s = mp_get_long(&a);
if (s != t) {
printf("\nmp_get_long() bad result! 0x%lx != 0x%lx", s, t);
@@ -635,10 +632,7 @@ static int test_mp_get_long_long(void)
r = ~0ULL;
printf(" r = 0x%llx i = %d\r", r, i);
do {
- if (mp_set_long_long(&a, r) != MP_OKAY) {
- printf("\nmp_set_long_long() error!");
- goto LBL_ERR;
- }
+ mp_set_long_long(&a, r);
q = mp_get_long_long(&a);
if (q != r) {
printf("\nmp_get_long_long() bad result! 0x%llx != 0x%llx", q, r);
diff --git a/doc/bn.tex b/doc/bn.tex
index a848e75..f4bfda5 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -856,11 +856,7 @@ int main(void)
\}
/* set the number to 654321 (note this is bigger than 127) */
- if ((result = mp_set_int(&number, 654321)) != MP_OKAY) \{
- printf("Error setting the value of the number. \%s",
- mp_error_to_string(result));
- return EXIT_FAILURE;
- \}
+ mp_set_int(&number, 654321);
printf("number == \%lu", mp_get_int(&number));
@@ -1396,17 +1392,8 @@ int main(void)
\}
/* set the terms */
- if ((result = mp_set_int(&number, 257)) != MP_OKAY) \{
- printf("Error setting number1. \%s",
- mp_error_to_string(result));
- return EXIT_FAILURE;
- \}
-
- if ((result = mp_set_int(&number2, 1023)) != MP_OKAY) \{
- printf("Error setting number2. \%s",
- mp_error_to_string(result));
- return EXIT_FAILURE;
- \}
+ mp_set_int(&number, 257);
+ mp_set_int(&number2, 1023);
/* multiply them */
if ((result = mp_mul(&number1, &number2,
diff --git a/doc/tommath.src b/doc/tommath.src
index 4c15e7e..b3f7909 100644
--- a/doc/tommath.src
+++ b/doc/tommath.src
@@ -947,9 +947,7 @@ correct no further memory re-allocations are required to work with the mp\_int.
EXAM,bn_mp_init_size.c
-The number of digits $b$ requested is padded (line @22,MP_PREC@) by first augmenting it to the next multiple of
-\textbf{MP\_PREC} and then adding \textbf{MP\_PREC} to the result. If the memory can be successfully allocated the
-mp\_int is placed in a default state representing the integer zero. Otherwise, the error code \textbf{MP\_MEM} will be
+If the memory can be successfully allocated the mp\_int is placed in a default state representing the integer zero. Otherwise, the error code \textbf{MP\_MEM} will be
returned (line @27,return@).
The digits are allocated with the malloc() function (line @27,XMALLOC@) and set to zero afterwards (line @38,for@). The
diff --git a/tommath.h b/tommath.h
index 8bff871..5dc2b3a 100644
--- a/tommath.h
+++ b/tommath.h
@@ -143,6 +143,8 @@ TOOM_SQR_CUTOFF;
#ifndef MP_PREC
# ifndef MP_LOW_MEM
# define MP_PREC 32 /* default digits of precision */
+# elif defined(MP_8BIT)
+# define MP_PREC 16 /* default digits of precision */
# else
# define MP_PREC 8 /* default digits of precision */
# endif
@@ -252,13 +254,13 @@ void mp_set(mp_int *a, mp_digit b);
MP_WUR int mp_set_double(mp_int *a, double b);
/* set a 32-bit const */
-MP_WUR int mp_set_int(mp_int *a, unsigned long b);
+/* TODO void - never fails */ int mp_set_int(mp_int *a, unsigned long b);
/* set a platform dependent unsigned long value */
-MP_WUR int mp_set_long(mp_int *a, unsigned long b);
+/* TODO void - never fails */ int mp_set_long(mp_int *a, unsigned long b);
/* set a platform dependent unsigned long long value */
-MP_WUR int mp_set_long_long(mp_int *a, unsigned long long b);
+/* TODO void - never fails */ int mp_set_long_long(mp_int *a, unsigned long long b);
/* get a double */
MP_WUR double mp_get_double(const mp_int *a);
diff --git a/tommath_private.h b/tommath_private.h
index 466e727..7ef3582 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -108,6 +108,9 @@ extern void MP_FREE(void *mem, size_t size);
/* random number source */
extern int (*s_rand_source)(void *out, size_t size);
+/* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
+#define MP_MIN_PREC ((CHAR_BIT * (int)sizeof(long long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)
+
/* lowlevel functions, do not call! */
MP_WUR int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);
MP_WUR int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
@@ -143,18 +146,14 @@ extern const size_t mp_s_rmap_reverse_sz;
int func_name (mp_int * a, type b) \
{ \
int x = 0; \
- int new_size = ((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT; \
- int res = mp_grow(a, new_size); \
- if (res == MP_OKAY) { \
- mp_zero(a); \
- while (b != 0u) { \
- a->dp[x++] = ((mp_digit)b & MP_MASK); \
- if (MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) { break; } \
- b >>= ((MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
- } \
- a->used = x; \
+ mp_zero(a); \
+ while (b != 0u) { \
+ a->dp[x++] = ((mp_digit)b & MP_MASK); \
+ if (MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) { break; } \
+ b >>= ((MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
} \
- return res; \
+ a->used = x; \
+ return MP_OKAY; \
}
/* deprecated functions */