tommath/bn_mp_is_square.c

111 lines
3.3 KiB
C
Raw Normal View History

#include <tommath_private.h>
2004-10-29 18:07:18 -04:00
#ifdef BN_MP_IS_SQUARE_C
2004-04-11 16:46:22 -04:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
2004-04-11 16:46:22 -04:00
*/
/* Check if remainders are possible squares - fast exclude non-squares */
static const char rem_128[128] = {
2017-08-30 13:11:35 -04: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 16:46:22 -04:00
};
static const char rem_105[105] = {
2017-08-30 13:11:35 -04: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 16:46:22 -04:00
};
/* Store non-zero to ret if arg is square, and zero if not */
2017-09-20 10:59:43 -04:00
int mp_is_square(const mp_int *arg, int *ret)
2004-04-11 16:46:22 -04:00
{
2017-08-30 13:11:35 -04:00
int res;
mp_digit c;
mp_int t;
unsigned long r;
2004-04-11 16:46:22 -04:00
2017-08-30 13:11:35 -04:00
/* Default to Non-square :) */
*ret = MP_NO;
2004-04-11 16:46:22 -04:00
2017-08-30 13:11:35 -04:00
if (arg->sign == MP_NEG) {
return MP_VAL;
}
2004-04-11 16:46:22 -04:00
2017-08-30 13:11:35 -04:00
/* digits used? (TSD) */
if (arg->used == 0) {
return MP_OKAY;
}
2004-04-11 16:46:22 -04:00
2017-08-30 13:11:35 -04:00
/* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
2017-10-15 13:58:35 -04:00
if (rem_128[127u & DIGIT(arg, 0)] == (char)1) {
2017-08-30 13:11:35 -04:00
return MP_OKAY;
}
2004-04-11 16:46:22 -04:00
2017-08-30 13:11:35 -04:00
/* Next check mod 105 (3*5*7) */
2017-10-15 10:11:09 -04:00
if ((res = mp_mod_d(arg, 105uL, &c)) != MP_OKAY) {
2017-08-30 13:11:35 -04:00
return res;
}
2017-10-15 13:58:35 -04:00
if (rem_105[c] == (char)1) {
2017-08-30 13:11:35 -04:00
return MP_OKAY;
}
2004-04-11 16:46:22 -04:00
2004-10-29 18:07:18 -04:00
2017-08-30 13:11:35 -04:00
if ((res = mp_init_set_int(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
return res;
}
if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
goto ERR;
}
r = mp_get_int(&t);
/* Check for other prime modules, note it's not an ERROR but we must
* free "t" so the easiest way is to goto ERR. We know that res
* is already equal to MP_OKAY from the mp_mod call
*/
2017-10-15 13:57:12 -04:00
if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL) goto ERR;
if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL) goto ERR;
if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL) goto ERR;
if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL) goto ERR;
if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL) goto ERR;
if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL) goto ERR;
if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL) goto ERR;
2004-04-11 16:46:22 -04:00
2017-08-30 13:11:35 -04:00
/* Final check - is sqr(sqrt(arg)) == arg ? */
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
goto ERR;
}
if ((res = mp_sqr(&t, &t)) != MP_OKAY) {
goto ERR;
}
2004-04-11 16:46:22 -04:00
2017-08-30 13:11:35 -04:00
*ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
2017-08-28 16:34:46 -04:00
ERR:
2017-08-30 13:11:35 -04:00
mp_clear(&t);
return res;
2004-04-11 16:46:22 -04:00
}
2004-10-29 18:07:18 -04:00
#endif
2005-08-01 12:37:28 -04:00
2017-08-28 10:27:26 -04:00
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */