Merge pull request #397 from libtom/bbs-style mp_prime_next_prime: use mp_bool for bbs_style
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
diff --git a/demo/test.c b/demo/test.c
index bfe934b..0f0152a 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -1061,7 +1061,7 @@ static int test_mp_prime_next_prime(void)
/* edge cases */
mp_set(&a, 0u);
- if ((err = mp_prime_next_prime(&a, 5, 0)) != MP_OKAY) {
+ if ((err = mp_prime_next_prime(&a, 5, MP_NO)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp_d(&a, 2u) != MP_EQ) {
@@ -1072,7 +1072,7 @@ static int test_mp_prime_next_prime(void)
}
mp_set(&a, 0u);
- if ((err = mp_prime_next_prime(&a, 5, 1)) != MP_OKAY) {
+ if ((err = mp_prime_next_prime(&a, 5, MP_YES)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp_d(&a, 3u) != MP_EQ) {
@@ -1083,7 +1083,7 @@ static int test_mp_prime_next_prime(void)
}
mp_set(&a, 2u);
- if ((err = mp_prime_next_prime(&a, 5, 0)) != MP_OKAY) {
+ if ((err = mp_prime_next_prime(&a, 5, MP_NO)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp_d(&a, 3u) != MP_EQ) {
@@ -1094,7 +1094,7 @@ static int test_mp_prime_next_prime(void)
}
mp_set(&a, 2u);
- if ((err = mp_prime_next_prime(&a, 5, 1)) != MP_OKAY) {
+ if ((err = mp_prime_next_prime(&a, 5, MP_YES)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp_d(&a, 3u) != MP_EQ) {
@@ -1104,7 +1104,7 @@ static int test_mp_prime_next_prime(void)
goto LBL_ERR;
}
mp_set(&a, 8);
- if ((err = mp_prime_next_prime(&a, 5, 1)) != MP_OKAY) {
+ if ((err = mp_prime_next_prime(&a, 5, MP_YES)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp_d(&a, 11u) != MP_EQ) {
@@ -1130,7 +1130,7 @@ static int test_mp_prime_next_prime(void)
if ((err = mp_add(&b, &c, &b)) != MP_OKAY) {
goto LBL_ERR;
}
- if ((err = mp_prime_next_prime(&a, 5, 0)) != MP_OKAY) {
+ if ((err = mp_prime_next_prime(&a, 5, MP_NO)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp(&a, &b) != MP_EQ) {
@@ -1160,7 +1160,7 @@ static int test_mp_prime_next_prime(void)
if ((err = mp_add(&b, &c, &b)) != MP_OKAY) {
goto LBL_ERR;
}
- if ((err = mp_prime_next_prime(&a, 5, 1)) != MP_OKAY) {
+ if ((err = mp_prime_next_prime(&a, 5, MP_YES)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_cmp(&a, &b) != MP_EQ) {
@@ -1284,7 +1284,7 @@ static int test_mp_read_radix(void)
char *s = fgets(buf, sizeof(buf), stdin);
if (s != buf) break;
mp_read_radix(&a, buf, 10);
- mp_prime_next_prime(&a, 5, 1);
+ mp_prime_next_prime(&a, 5, MP_YES);
mp_to_radix(&a, buf, sizeof(buf), NULL, 10);
printf("%s, %lu\n", buf, (unsigned long)a.dp[0] & 3uL);
}
diff --git a/mp_prime_next_prime.c b/mp_prime_next_prime.c
index f8b2212..3256e37 100644
--- a/mp_prime_next_prime.c
+++ b/mp_prime_next_prime.c
@@ -6,9 +6,9 @@
/* finds the next prime after the number "a" using "t" trials
* of Miller-Rabin.
*
- * bbs_style = 1 means the prime must be congruent to 3 mod 4
+ * bbs_style = MP_YES means the prime must be congruent to 3 mod 4
*/
-mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style)
+mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style)
{
int x, y;
mp_ord cmp;
@@ -29,7 +29,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style)
continue;
}
if (cmp != MP_GT) {
- if ((bbs_style == 1) && ((s_mp_prime_tab[x] & 3u) != 3u)) {
+ if ((bbs_style == MP_YES) && ((s_mp_prime_tab[x] & 3u) != 3u)) {
/* try again until we get a prime congruent to 3 mod 4 */
continue;
} else {
@@ -42,7 +42,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style)
}
/* generate a prime congruent to 3 mod 4 or 1/3 mod 4? */
- if (bbs_style == 1) {
+ if (bbs_style == MP_YES) {
kstep = 4;
} else {
kstep = 2;
@@ -50,7 +50,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style)
/* at this point we will use a combination of a sieve and Miller-Rabin */
- if (bbs_style == 1) {
+ if (bbs_style == MP_YES) {
/* if a mod 4 != 3 subtract the correct value to make it so */
if ((a->dp[0] & 3u) != 3u) {
if ((err = mp_sub_d(a, (a->dp[0] & 3u) + 1u, a)) != MP_OKAY) {
diff --git a/tommath.h b/tommath.h
index a401be4..9421c6d 100644
--- a/tommath.h
+++ b/tommath.h
@@ -550,9 +550,9 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) MP_WUR;
/* finds the next prime after the number "a" using "t" trials
* of Miller-Rabin.
*
- * bbs_style = 1 means the prime must be congruent to 3 mod 4
+ * bbs_style = MP_YES means the prime must be congruent to 3 mod 4
*/
-mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) MP_WUR;
+mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style) MP_WUR;
/* makes a truly random prime of a given size (bits),
*