diff --git a/bn_mp_grow.c b/bn_mp_grow.c index 801192344..8d88cfa3c 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 9c32c5b5c..d1ff4e9ba 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 35136f5d0..d97f8becf 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 71de5e909..8855cfb01 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 74d8a5b0f..44143b0c3 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 0ccd340f9..bd2ea3752 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 e7a204b89..fec584143 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 a04e58599..354e9f464 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 332348dfc..f55eade58 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 ed4c2f2fa..d19a02107 100644 --- a/doc/bn.tex +++ b/doc/bn.tex @@ -173,7 +173,7 @@ \subsubsection{OpenBSD} cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo... cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo... cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo... -libtool --mode=link --tag=CC cc bn_error.lo bn_s_mp_invmod_fast.lo bn_fast_mp_mo +libtool --mode=link --tag=CC cc bn_error.lo bn_s_mp_invmod_fast.lo bn_fast_mp_mo libtool: link: cc bn_error.lo bn_s_mp_invmod_fast.lo bn_s_mp_montgomery_reduce_fast0 bn_error.lo: file not recognized: File format not recognized cc: error: linker command failed with exit code 1 (use -v to see invocation) @@ -187,7 +187,7 @@ \subsubsection{OpenBSD} \end{alltt} At this time two versions of \texttt{libtool} are installed and both are named \texttt{libtool}, unfortunately but GNU \texttt{libtool} has been placed in \texttt{/usr/local/bin/} and the native version in \texttt{/usr/bin/}. The path might be different in other versions of OpenBSD but both programms differ in the output of \texttt{libtool --version} \begin{alltt} -$ /usr/local/bin/libtool --version +$ /usr/local/bin/libtool --version libtool (GNU libtool) 2.4.2 Written by Gordon Matzigkeit , 1996 @@ -856,11 +856,7 @@ \subsection{Long Constants} \} /* 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 @@ \section{Multiplication} \} /* 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 4c15e7ee2..b3f790978 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 5d8961b4c..d669434c4 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 diff --git a/tommath_private.h b/tommath_private.h index c4004543c..3b683d451 100644 --- a/tommath_private.h +++ b/tommath_private.h @@ -66,6 +66,9 @@ extern void MP_FREE(void *mem, size_t size); #define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u)) #define MP_IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u)) +/* 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! */ int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c); int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c); @@ -101,18 +104,14 @@ extern const size_t mp_s_rmap_reverse_sz; int func_name (mp_int * a, type b) \ { \ int x = 0; \ - int new_size = (((CHAR_BIT * sizeof(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 ((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) { break; } \ - b >>= (((CHAR_BIT * sizeof (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 ((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) { break; } \ + b >>= (((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \ } \ - return res; \ + a->used = x; \ + return MP_OKAY; \ } /* deprecated functions */