Commit 7365442ace6292043f774ca647252b8c7be5ec20

Daniel Mendler 2019-05-10T23:59:46

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

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 */