libtommath/bn_mp_is_square.c

94 lines
2.8 KiB
C
Raw Normal View History

#include "tommath_private.h"
2004-10-29 22:07:18 +00:00
#ifdef BN_MP_IS_SQUARE_C
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2004-04-11 20:46:22 +00:00
/* Check if remainders are possible squares - fast exclude non-squares */
static const char rem_128[128] = {
2017-08-30 17:11:35 +00:00
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1
2004-04-11 20:46:22 +00:00
};
static const char rem_105[105] = {
2017-08-30 17:11:35 +00:00
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1
2004-04-11 20:46:22 +00:00
};
/* Store non-zero to ret if arg is square, and zero if not */
mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
2004-04-11 20:46:22 +00:00
{
2019-05-19 15:16:13 +00:00
mp_err err;
2017-08-30 17:11:35 +00:00
mp_digit c;
mp_int t;
unsigned long r;
2004-04-11 20:46:22 +00:00
2017-08-30 17:11:35 +00:00
/* Default to Non-square :) */
*ret = MP_NO;
2004-04-11 20:46:22 +00:00
2017-08-30 17:11:35 +00:00
if (arg->sign == MP_NEG) {
return MP_VAL;
}
2004-04-11 20:46:22 +00:00
if (MP_IS_ZERO(arg)) {
2017-08-30 17:11:35 +00:00
return MP_OKAY;
}
2004-04-11 20:46:22 +00:00
2019-04-13 06:46:57 +00:00
/* First check mod 128 (suppose that MP_DIGIT_BIT is at least 7) */
2019-03-26 10:34:06 +00:00
if (rem_128[127u & arg->dp[0]] == (char)1) {
2017-08-30 17:11:35 +00:00
return MP_OKAY;
}
2004-04-11 20:46:22 +00:00
2017-08-30 17:11:35 +00:00
/* Next check mod 105 (3*5*7) */
2019-05-19 15:16:13 +00:00
if ((err = mp_mod_d(arg, 105uL, &c)) != MP_OKAY) {
return err;
2017-08-30 17:11:35 +00:00
}
2017-10-15 17:58:35 +00:00
if (rem_105[c] == (char)1) {
2017-08-30 17:11:35 +00:00
return MP_OKAY;
}
2004-04-11 20:46:22 +00:00
2004-10-29 22:07:18 +00:00
2019-05-26 06:33:49 +00:00
if ((err = mp_init_u32(&t, 11u*13u*17u*19u*23u*29u*31u)) != MP_OKAY) {
2019-05-19 15:16:13 +00:00
return err;
2017-08-30 17:11:35 +00:00
}
2019-05-19 15:16:13 +00:00
if ((err = mp_mod(arg, &t, &t)) != MP_OKAY) {
goto LBL_ERR;
2017-08-30 17:11:35 +00:00
}
2019-05-24 08:21:54 +00:00
r = mp_get_u32(&t);
2017-08-30 17:11:35 +00:00
/* Check for other prime modules, note it's not an ERROR but we must
2019-05-19 15:16:13 +00:00
* free "t" so the easiest way is to goto LBL_ERR. We know that err
2017-08-30 17:11:35 +00:00
* is already equal to MP_OKAY from the mp_mod call
*/
if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL) goto LBL_ERR;
2004-04-11 20:46:22 +00:00
2017-08-30 17:11:35 +00:00
/* Final check - is sqr(sqrt(arg)) == arg ? */
2019-05-19 15:16:13 +00:00
if ((err = mp_sqrt(arg, &t)) != MP_OKAY) {
goto LBL_ERR;
2017-08-30 17:11:35 +00:00
}
2019-05-19 15:16:13 +00:00
if ((err = mp_sqr(&t, &t)) != MP_OKAY) {
goto LBL_ERR;
2017-08-30 17:11:35 +00:00
}
2004-04-11 20:46:22 +00:00
2017-08-30 17:11:35 +00:00
*ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
LBL_ERR:
2017-08-30 17:11:35 +00:00
mp_clear(&t);
2019-05-19 15:16:13 +00:00
return err;
2004-04-11 20:46:22 +00:00
}
2004-10-29 22:07:18 +00:00
#endif