2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef MP_PRIME_MILLER_RABIN_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
|
|
|
|
2017-08-30 03:51:11 +00:00
|
|
|
/* Miller-Rabin test of "a" to the base of "b" as described in
|
2003-03-22 15:10:20 +00:00
|
|
|
* HAC pp. 139 Algorithm 4.24
|
|
|
|
*
|
|
|
|
* Sets result to 0 if definitely composite or 1 if probably prime.
|
2017-08-30 03:51:11 +00:00
|
|
|
* Randomly the chance of error is no more than 1/4 and often
|
2003-03-22 15:10:20 +00:00
|
|
|
* very much lower.
|
|
|
|
*/
|
2019-10-24 20:02:29 +00:00
|
|
|
mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result)
|
2003-03-22 15:10:20 +00:00
|
|
|
{
|
2017-08-30 18:23:46 +00:00
|
|
|
mp_int n1, y, r;
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err err;
|
|
|
|
int s, j;
|
2003-03-22 15:10:20 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* default */
|
2019-10-24 20:02:29 +00:00
|
|
|
*result = false;
|
2003-03-22 15:10:20 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* ensure b > 1 */
|
2017-10-15 14:11:09 +00:00
|
|
|
if (mp_cmp_d(b, 1uL) != MP_GT) {
|
2017-08-30 18:23:46 +00:00
|
|
|
return MP_VAL;
|
|
|
|
}
|
2003-07-12 14:31:43 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* get n1 = a - 1 */
|
|
|
|
if ((err = mp_init_copy(&n1, a)) != MP_OKAY) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-10-15 14:11:09 +00:00
|
|
|
if ((err = mp_sub_d(&n1, 1uL, &n1)) != MP_OKAY) {
|
2017-08-30 18:23:46 +00:00
|
|
|
goto LBL_N1;
|
|
|
|
}
|
2003-03-22 15:10:20 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* set 2**s * r = n1 */
|
|
|
|
if ((err = mp_init_copy(&r, &n1)) != MP_OKAY) {
|
|
|
|
goto LBL_N1;
|
|
|
|
}
|
2003-07-12 14:31:43 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* count the number of least significant bits
|
|
|
|
* which are zero
|
|
|
|
*/
|
|
|
|
s = mp_cnt_lsb(&r);
|
2003-07-12 14:31:43 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* now divide n - 1 by 2**s */
|
|
|
|
if ((err = mp_div_2d(&r, s, &r, NULL)) != MP_OKAY) {
|
|
|
|
goto LBL_R;
|
|
|
|
}
|
2003-03-22 15:10:20 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* compute y = b**r mod a */
|
|
|
|
if ((err = mp_init(&y)) != MP_OKAY) {
|
|
|
|
goto LBL_R;
|
|
|
|
}
|
|
|
|
if ((err = mp_exptmod(b, &r, a, &y)) != MP_OKAY) {
|
|
|
|
goto LBL_Y;
|
|
|
|
}
|
2003-03-22 15:10:20 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* if y != 1 and y != n1 do */
|
2017-10-15 14:11:09 +00:00
|
|
|
if ((mp_cmp_d(&y, 1uL) != MP_EQ) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
2017-08-30 18:23:46 +00:00
|
|
|
j = 1;
|
|
|
|
/* while j <= s-1 and y != n1 */
|
|
|
|
while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
|
|
|
if ((err = mp_sqrmod(&y, a, &y)) != MP_OKAY) {
|
|
|
|
goto LBL_Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if y == 1 then composite */
|
2017-10-15 14:11:09 +00:00
|
|
|
if (mp_cmp_d(&y, 1uL) == MP_EQ) {
|
2017-08-30 18:23:46 +00:00
|
|
|
goto LBL_Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
++j;
|
2003-03-22 15:10:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* if y != n1 then composite */
|
|
|
|
if (mp_cmp(&y, &n1) != MP_EQ) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_Y;
|
2003-03-22 15:10:20 +00:00
|
|
|
}
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
2003-03-22 15:10:20 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* probably prime now */
|
2019-10-24 20:02:29 +00:00
|
|
|
*result = true;
|
2017-08-28 20:34:46 +00:00
|
|
|
LBL_Y:
|
2017-08-30 18:23:46 +00:00
|
|
|
mp_clear(&y);
|
2017-08-28 20:34:46 +00:00
|
|
|
LBL_R:
|
2017-08-30 18:23:46 +00:00
|
|
|
mp_clear(&r);
|
2017-08-28 20:34:46 +00:00
|
|
|
LBL_N1:
|
2017-08-30 18:23:46 +00:00
|
|
|
mp_clear(&n1);
|
|
|
|
return err;
|
2003-03-22 15:10:20 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|