diff libtommath/bn_mp_prime_frobenius_underwood.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents f52919ffd3b1
children
line wrap: on
line diff
--- a/libtommath/bn_mp_prime_frobenius_underwood.c	Tue May 26 23:27:26 2020 +0800
+++ b/libtommath/bn_mp_prime_frobenius_underwood.c	Tue May 26 17:36:47 2020 +0200
@@ -1,22 +1,13 @@
 #include "tommath_private.h"
 #ifdef BN_MP_PRIME_FROBENIUS_UNDERWOOD_C
 
-/* LibTomMath, multiple-precision integer library -- Tom St Denis
- *
- * LibTomMath is a library that provides multiple-precision
- * integer arithmetic as well as number theoretic functionality.
- *
- * The library was designed directly after the MPI library by
- * Michael Fromberger but has been written from scratch with
- * additional optimizations in place.
- *
- * SPDX-License-Identifier: Unlicense
- */
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
 
 /*
  *  See file bn_mp_prime_is_prime.c or the documentation in doc/bn.tex for the details
  */
-#ifndef LTM_USE_FIPS_ONLY
+#ifndef LTM_USE_ONLY_MR
 
 #ifdef MP_8BIT
 /*
@@ -32,17 +23,17 @@
 #else
 #define LTM_FROBENIUS_UNDERWOOD_A 32764
 #endif
-int mp_prime_frobenius_underwood(const mp_int *N, int *result)
+mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
 {
    mp_int T1z, T2z, Np1z, sz, tz;
 
-   int a, ap2, length, i, j, isset;
-   int e;
+   int a, ap2, length, i, j;
+   mp_err err;
 
    *result = MP_NO;
 
-   if ((e = mp_init_multi(&T1z, &T2z, &Np1z, &sz, &tz, NULL)) != MP_OKAY) {
-      return e;
+   if ((err = mp_init_multi(&T1z, &T2z, &Np1z, &sz, &tz, NULL)) != MP_OKAY) {
+      return err;
    }
 
    for (a = 0; a < LTM_FROBENIUS_UNDERWOOD_A; a++) {
@@ -52,21 +43,13 @@
          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_u32(&T1z, (uint32_t)a);
 
-      if ((e = mp_sqr(&T1z, &T1z)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
+      if ((err = mp_sqr(&T1z, &T1z)) != MP_OKAY)                  goto LBL_FU_ERR;
 
-      if ((e = mp_sub_d(&T1z, 4uL, &T1z)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
+      if ((err = mp_sub_d(&T1z, 4uL, &T1z)) != MP_OKAY)           goto LBL_FU_ERR;
 
-      if ((e = mp_kronecker(&T1z, N, &j)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
+      if ((err = mp_kronecker(&T1z, N, &j)) != MP_OKAY)           goto LBL_FU_ERR;
 
       if (j == -1) {
          break;
@@ -79,26 +62,18 @@
    }
    /* Tell it a composite and set return value accordingly */
    if (a >= LTM_FROBENIUS_UNDERWOOD_A) {
-      e = MP_ITER;
+      err = MP_ITER;
       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_u32(&T1z, (uint32_t)((a+4)*((2*a)+5)));
 
-   if ((e = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) {
-      goto LBL_FU_ERR;
-   }
+   if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY)                  goto LBL_FU_ERR;
 
-   if (!((T1z.used == 1) && (T1z.dp[0] == 1u))) {
-      goto LBL_FU_ERR;
-   }
+   if (!((T1z.used == 1) && (T1z.dp[0] == 1u)))                   goto LBL_FU_ERR;
 
    ap2 = a + 2;
-   if ((e = mp_add_d(N, 1uL, &Np1z)) != MP_OKAY) {
-      goto LBL_FU_ERR;
-   }
+   if ((err = mp_add_d(N, 1uL, &Np1z)) != MP_OKAY)                goto LBL_FU_ERR;
 
    mp_set(&sz, 1uL);
    mp_set(&tz, 2uL);
@@ -110,89 +85,48 @@
        * tz   = ((tz-sz)*(tz+sz))%N;
        * sz   = temp;
        */
-      if ((e = mp_mul_2(&tz, &T2z)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
+      if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY)                 goto LBL_FU_ERR;
 
       /* a = 0 at about 50% of the cases (non-square and odd input) */
       if (a != 0) {
-         if ((e = mp_mul_d(&sz, (mp_digit)a, &T1z)) != MP_OKAY) {
-            goto LBL_FU_ERR;
-         }
-         if ((e = mp_add(&T1z, &T2z, &T2z)) != MP_OKAY) {
-            goto LBL_FU_ERR;
-         }
+         if ((err = mp_mul_d(&sz, (mp_digit)a, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
+         if ((err = mp_add(&T1z, &T2z, &T2z)) != MP_OKAY)         goto LBL_FU_ERR;
       }
 
-      if ((e = mp_mul(&T2z, &sz, &T1z)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
-      if ((e = mp_sub(&tz, &sz, &T2z)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
-      if ((e = mp_add(&sz, &tz, &sz)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
-      if ((e = mp_mul(&sz, &T2z, &tz)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
-      if ((e = mp_mod(&tz, N, &tz)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
-      if ((e = mp_mod(&T1z, N, &sz)) != MP_OKAY) {
-         goto LBL_FU_ERR;
-      }
-      if ((isset = mp_get_bit(&Np1z, i)) == MP_VAL) {
-         e = isset;
-         goto LBL_FU_ERR;
-      }
-      if (isset == MP_YES) {
+      if ((err = mp_mul(&T2z, &sz, &T1z)) != MP_OKAY)             goto LBL_FU_ERR;
+      if ((err = mp_sub(&tz, &sz, &T2z)) != MP_OKAY)              goto LBL_FU_ERR;
+      if ((err = mp_add(&sz, &tz, &sz)) != MP_OKAY)               goto LBL_FU_ERR;
+      if ((err = mp_mul(&sz, &T2z, &tz)) != MP_OKAY)              goto LBL_FU_ERR;
+      if ((err = mp_mod(&tz, N, &tz)) != MP_OKAY)                 goto LBL_FU_ERR;
+      if ((err = mp_mod(&T1z, N, &sz)) != MP_OKAY)                goto LBL_FU_ERR;
+      if (s_mp_get_bit(&Np1z, (unsigned int)i) == MP_YES) {
          /*
           *  temp = (a+2) * sz + tz
           *  tz   = 2 * tz - sz
           *  sz   = temp
           */
          if (a == 0) {
-            if ((e = mp_mul_2(&sz, &T1z)) != MP_OKAY) {
-               goto LBL_FU_ERR;
-            }
+            if ((err = mp_mul_2(&sz, &T1z)) != MP_OKAY)           goto LBL_FU_ERR;
          } else {
-            if ((e = mp_mul_d(&sz, (mp_digit)ap2, &T1z)) != MP_OKAY) {
-               goto LBL_FU_ERR;
-            }
+            if ((err = mp_mul_d(&sz, (mp_digit)ap2, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
          }
-         if ((e = mp_add(&T1z, &tz, &T1z)) != MP_OKAY) {
-            goto LBL_FU_ERR;
-         }
-         if ((e = mp_mul_2(&tz, &T2z)) != MP_OKAY) {
-            goto LBL_FU_ERR;
-         }
-         if ((e = mp_sub(&T2z, &sz, &tz)) != MP_OKAY) {
-            goto LBL_FU_ERR;
-         }
+         if ((err = mp_add(&T1z, &tz, &T1z)) != MP_OKAY)          goto LBL_FU_ERR;
+         if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY)              goto LBL_FU_ERR;
+         if ((err = mp_sub(&T2z, &sz, &tz)) != MP_OKAY)           goto LBL_FU_ERR;
          mp_exch(&sz, &T1z);
       }
    }
 
-   if ((e = mp_set_long(&T1z, (unsigned long)((2 * a) + 5))) != MP_OKAY) {
-      goto LBL_FU_ERR;
-   }
-   if ((e = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
-      goto LBL_FU_ERR;
-   }
-   if ((mp_iszero(&sz) != MP_NO) && (mp_cmp(&tz, &T1z) == MP_EQ)) {
+   mp_set_u32(&T1z, (uint32_t)((2 * a) + 5));
+   if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY)                  goto LBL_FU_ERR;
+   if (MP_IS_ZERO(&sz) && (mp_cmp(&tz, &T1z) == MP_EQ)) {
       *result = MP_YES;
-      goto LBL_FU_ERR;
    }
 
 LBL_FU_ERR:
    mp_clear_multi(&tz, &sz, &Np1z, &T2z, &T1z, NULL);
-   return e;
+   return err;
 }
 
 #endif
 #endif
-
-/* ref:         HEAD -> master, tag: v1.1.0 */
-/* git commit:  08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
-/* commit time: 2019-01-28 20:32:32 +0100 */