2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2004-10-29 22:07:18 +00:00
|
|
|
#ifdef BN_MP_PRIME_NEXT_PRIME_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2003-03-22 15:10:20 +00:00
|
|
|
|
|
|
|
/* finds the next prime after the number "a" using "t" trials
|
|
|
|
* of Miller-Rabin.
|
2003-07-12 14:31:43 +00:00
|
|
|
*
|
|
|
|
* bbs_style = 1 means the prime must be congruent to 3 mod 4
|
2003-03-22 15:10:20 +00:00
|
|
|
*/
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
2003-03-22 15:10:20 +00:00
|
|
|
{
|
2019-09-02 23:19:48 +00:00
|
|
|
int x, y, cmp;
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err err;
|
|
|
|
mp_bool res = MP_NO;
|
2019-05-22 13:34:49 +00:00
|
|
|
mp_digit res_tab[PRIVATE_MP_PRIME_TAB_SIZE], step, kstep;
|
2003-07-12 14:31:43 +00:00
|
|
|
mp_int b;
|
2003-05-17 12:33:54 +00:00
|
|
|
|
2003-07-12 14:31:43 +00:00
|
|
|
/* force positive */
|
2003-08-05 01:24:44 +00:00
|
|
|
a->sign = MP_ZPOS;
|
2003-07-12 14:31:43 +00:00
|
|
|
|
|
|
|
/* simple algo if a is less than the largest prime in the table */
|
2019-05-22 13:34:49 +00:00
|
|
|
if (mp_cmp_d(a, s_mp_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE-1]) == MP_LT) {
|
2019-09-02 23:19:48 +00:00
|
|
|
/* 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;
|
2017-08-30 17:13:53 +00:00
|
|
|
} else {
|
2019-09-02 23:19:48 +00:00
|
|
|
mp_set(a, s_mp_prime_tab[x]);
|
2017-08-30 17:13:53 +00:00
|
|
|
return MP_OKAY;
|
|
|
|
}
|
|
|
|
}
|
2003-07-12 14:31:43 +00:00
|
|
|
}
|
|
|
|
/* fall through to the sieve */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate a prime congruent to 3 mod 4 or 1/3 mod 4? */
|
|
|
|
if (bbs_style == 1) {
|
|
|
|
kstep = 4;
|
|
|
|
} else {
|
|
|
|
kstep = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* at this point we will use a combination of a sieve and Miller-Rabin */
|
|
|
|
|
|
|
|
if (bbs_style == 1) {
|
|
|
|
/* if a mod 4 != 3 subtract the correct value to make it so */
|
2017-10-15 17:57:12 +00:00
|
|
|
if ((a->dp[0] & 3u) != 3u) {
|
|
|
|
if ((err = mp_sub_d(a, (a->dp[0] & 3u) + 1u, a)) != MP_OKAY) {
|
2017-08-30 17:19:29 +00:00
|
|
|
return err;
|
2019-05-19 09:16:54 +00:00
|
|
|
}
|
2003-03-22 15:10:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-09 09:08:26 +00:00
|
|
|
if (MP_IS_EVEN(a)) {
|
2003-07-12 14:31:43 +00:00
|
|
|
/* force odd */
|
2017-10-15 14:11:09 +00:00
|
|
|
if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
|
2003-07-12 14:31:43 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate the restable */
|
2019-05-22 13:34:49 +00:00
|
|
|
for (x = 1; x < PRIVATE_MP_PRIME_TAB_SIZE; x++) {
|
|
|
|
if ((err = mp_mod_d(a, s_mp_prime_tab[x], res_tab + x)) != MP_OKAY) {
|
2003-03-22 15:10:20 +00:00
|
|
|
return err;
|
|
|
|
}
|
2003-05-17 12:33:54 +00:00
|
|
|
}
|
|
|
|
|
2003-07-12 14:31:43 +00:00
|
|
|
/* init temp used for Miller-Rabin Testing */
|
|
|
|
if ((err = mp_init(&b)) != MP_OKAY) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2003-03-22 15:10:20 +00:00
|
|
|
for (;;) {
|
2003-07-12 14:31:43 +00:00
|
|
|
/* skip to the next non-trivially divisible candidate */
|
|
|
|
step = 0;
|
|
|
|
do {
|
|
|
|
/* y == 1 if any residue was zero [e.g. cannot be prime] */
|
|
|
|
y = 0;
|
|
|
|
|
2003-07-16 00:26:58 +00:00
|
|
|
/* increase step to next candidate */
|
2003-07-12 14:31:43 +00:00
|
|
|
step += kstep;
|
|
|
|
|
|
|
|
/* compute the new residue without using division */
|
2019-05-22 13:34:49 +00:00
|
|
|
for (x = 1; x < PRIVATE_MP_PRIME_TAB_SIZE; x++) {
|
2017-08-30 17:13:53 +00:00
|
|
|
/* add the step to each residue */
|
|
|
|
res_tab[x] += kstep;
|
|
|
|
|
|
|
|
/* subtract the modulus [instead of using division] */
|
2019-05-22 13:34:49 +00:00
|
|
|
if (res_tab[x] >= s_mp_prime_tab[x]) {
|
|
|
|
res_tab[x] -= s_mp_prime_tab[x];
|
2017-08-30 17:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set flag if zero */
|
2017-10-15 17:57:12 +00:00
|
|
|
if (res_tab[x] == 0u) {
|
2017-08-30 17:13:53 +00:00
|
|
|
y = 1;
|
|
|
|
}
|
2003-07-12 14:31:43 +00:00
|
|
|
}
|
2019-04-13 06:46:57 +00:00
|
|
|
} while ((y == 1) && (step < (((mp_digit)1 << MP_DIGIT_BIT) - kstep)));
|
2003-07-12 14:31:43 +00:00
|
|
|
|
|
|
|
/* add the step */
|
|
|
|
if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_ERR;
|
2003-07-12 14:31:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 09:08:26 +00:00
|
|
|
/* if didn't pass sieve and step == MP_MAX then skip test */
|
2019-04-13 06:46:57 +00:00
|
|
|
if ((y == 1) && (step >= (((mp_digit)1 << MP_DIGIT_BIT) - kstep))) {
|
2003-07-12 14:31:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:11:04 +00:00
|
|
|
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
|
|
|
|
goto LBL_ERR;
|
2003-03-22 15:10:20 +00:00
|
|
|
}
|
2003-12-24 18:59:22 +00:00
|
|
|
if (res == MP_YES) {
|
2003-03-22 15:10:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-05-17 12:33:54 +00:00
|
|
|
|
2003-07-12 14:31:43 +00:00
|
|
|
err = MP_OKAY;
|
2004-12-23 02:40:37 +00:00
|
|
|
LBL_ERR:
|
2003-07-12 14:31:43 +00:00
|
|
|
mp_clear(&b);
|
|
|
|
return err;
|
2003-03-22 15:10:20 +00:00
|
|
|
}
|
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|