Commit 7ecd1ad4f6c2624e8a02f3490b7fb6e606db0391

czurnieden 2019-03-23T18:50:14

bugfix in final fips loop II

diff --git a/bn_mp_prime_is_prime.c b/bn_mp_prime_is_prime.c
index c49e3e3..5efab41 100644
--- a/bn_mp_prime_is_prime.c
+++ b/bn_mp_prime_is_prime.c
@@ -335,22 +335,22 @@ int mp_prime_is_prime(const mp_int *a, int t, int *result)
           * smaller than or equal to "a"
           */
          len = mp_count_bits(&b);
-         if (len >= size_a) {
-            /* Witness and test subject must not be equal */
-            if( (len == size_a) && (mp_cmp(a, &b) == MP_EQ) ) {
-               len++;
-            }
+         if (len > size_a) {
             len = len - size_a;
             if ((err = mp_div_2d(&b, len, &b, NULL)) != MP_OKAY) {
                goto LBL_B;
             }
          }
-
          /* Although the chance for b <= 3 is miniscule, try again. */
          if (mp_cmp_d(&b, 3uL) != MP_GT) {
             ix--;
             continue;
          }
+         /* Witness and test subject must not be equal */
+         if ( (size_a ==  mp_count_bits(&b)) && (mp_cmp(a, &b) == MP_EQ) ) {
+            ix--;
+            continue;
+         }
          if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
             goto LBL_B;
          }