simplified code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c
index 17abbf8..1e971fa 100644
--- a/bn_mp_prime_next_prime.c
+++ b/bn_mp_prime_next_prime.c
@@ -10,7 +10,7 @@
*/
mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style)
{
- int x, y;
+ int x, y, cmp;
mp_err err;
mp_bool res = MP_NO;
mp_digit res_tab[PRIVATE_MP_PRIME_TAB_SIZE], step, kstep;
@@ -21,37 +21,22 @@ mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style)
/* simple algo if a is less than the largest prime in the table */
if (mp_cmp_d(a, s_mp_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE-1]) == MP_LT) {
- /* find which prime it is bigger than */
- for (x = PRIVATE_MP_PRIME_TAB_SIZE - 2; x >= 0; x--) {
- if (mp_cmp_d(a, s_mp_prime_tab[x]) != MP_LT) {
- /* ok we found a prime smaller or
- * equal [so the next is larger]
- */
- if (bbs_style == 1) {
- /* ... however, the prime must be
- * congruent to 3 mod 4
- * so do a scan upwards for such a prime */
- for (y = x + 1; y < PRIVATE_MP_PRIME_TAB_SIZE; y++) {
- if ((s_mp_prime_tab[y] & 3u) == 3u) {
- mp_set(a, s_mp_prime_tab[y]);
- return MP_OKAY;
- }
- }
+ /* find which prime it is bigger than "a" */
+ for (x = 0; x < PRIVATE_MP_PRIME_TAB_SIZE; x++) {
+ cmp = mp_cmp_d(a, s_mp_prime_tab[x]);
+ if (cmp == MP_EQ) {
+ continue;
+ }
+ if (cmp != MP_GT) {
+ if ((bbs_style == 1) && ((s_mp_prime_tab[x] & 3u) != 3u)) {
+ /* try again until we get a prime congruent to 3 mod 4 */
+ continue;
} else {
- mp_set(a, s_mp_prime_tab[x + 1]);
+ mp_set(a, s_mp_prime_tab[x]);
return MP_OKAY;
}
}
}
- /* at this point a maybe smaller than the smallest prime in the table */
- if (mp_cmp_d(a, 2uL) != MP_GT) {
- if (bbs_style == 1) {
- mp_set(a, 3uL);
- } else {
- mp_set(a, 2uL);
- }
- return MP_OKAY;
- }
/* fall through to the sieve */
}