Commit ba786e6b3936e484d47a99691c730681a514cc06

czurnieden 2018-05-11T00:50:52

Call mp_kronecker from mp_jacobi to save some bytes while keeping the API

diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c
index fe37f22..65fb2d2 100644
--- a/bn_mp_jacobi.c
+++ b/bn_mp_jacobi.c
@@ -14,16 +14,10 @@
  */
 
 /* computes the jacobi c = (a | n) (or Legendre if n is prime)
- * HAC pp. 73 Algorithm 2.149
- * HAC is wrong here, as the special case of (0 | 1) is not
- * handled correctly.
+ * Kept for legacy reasons, please use mp_kronecker() instead
  */
 int mp_jacobi(const mp_int *a, const mp_int *n, int *c)
 {
-   mp_int  a1, p1;
-   int     k, s, r, res;
-   mp_digit residue;
-
    /* if a < 0 return MP_VAL */
    if (mp_isneg(a) == MP_YES) {
       return MP_VAL;
@@ -34,81 +28,7 @@ int mp_jacobi(const mp_int *a, const mp_int *n, int *c)
       return MP_VAL;
    }
 
-   /* step 1. handle case of a == 0 */
-   if (mp_iszero(a) == MP_YES) {
-      /* special case of a == 0 and n == 1 */
-      if (mp_cmp_d(n, 1uL) == MP_EQ) {
-         *c = 1;
-      } else {
-         *c = 0;
-      }
-      return MP_OKAY;
-   }
-
-   /* step 2.  if a == 1, return 1 */
-   if (mp_cmp_d(a, 1uL) == MP_EQ) {
-      *c = 1;
-      return MP_OKAY;
-   }
-
-   /* default */
-   s = 0;
-
-   /* step 3.  write a = a1 * 2**k  */
-   if ((res = mp_init_copy(&a1, a)) != MP_OKAY) {
-      return res;
-   }
-
-   if ((res = mp_init(&p1)) != MP_OKAY) {
-      goto LBL_A1;
-   }
-
-   /* divide out larger power of two */
-   k = mp_cnt_lsb(&a1);
-   if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) {
-      goto LBL_P1;
-   }
-
-   /* step 4.  if e is even set s=1 */
-   if (((unsigned)k & 1u) == 0u) {
-      s = 1;
-   } else {
-      /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
-      residue = n->dp[0] & 7u;
-
-      if ((residue == 1u) || (residue == 7u)) {
-         s = 1;
-      } else if ((residue == 3u) || (residue == 5u)) {
-         s = -1;
-      }
-   }
-
-   /* step 5.  if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
-   if (((n->dp[0] & 3u) == 3u) && ((a1.dp[0] & 3u) == 3u)) {
-      s = -s;
-   }
-
-   /* if a1 == 1 we're done */
-   if (mp_cmp_d(&a1, 1uL) == MP_EQ) {
-      *c = s;
-   } else {
-      /* n1 = n mod a1 */
-      if ((res = mp_mod(n, &a1, &p1)) != MP_OKAY) {
-         goto LBL_P1;
-      }
-      if ((res = mp_jacobi(&p1, &a1, &r)) != MP_OKAY) {
-         goto LBL_P1;
-      }
-      *c = s * r;
-   }
-
-   /* done */
-   res = MP_OKAY;
-LBL_P1:
-   mp_clear(&p1);
-LBL_A1:
-   mp_clear(&a1);
-   return res;
+   return mp_kronecker(a,n,c);
 }
 #endif