bn_mp_is_square.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_IS_SQUARE_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* Check if remainders are possible squares - fast exclude non-squares */
  6. static const char rem_128[128] = {
  7. 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  8. 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  9. 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  10. 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  11. 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  12. 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  13. 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  14. 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1
  15. };
  16. static const char rem_105[105] = {
  17. 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
  18. 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
  19. 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
  20. 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  21. 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
  22. 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1,
  23. 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1
  24. };
  25. /* Store non-zero to ret if arg is square, and zero if not */
  26. mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
  27. {
  28. mp_err err;
  29. mp_digit c;
  30. mp_int t;
  31. unsigned long r;
  32. /* Default to Non-square :) */
  33. *ret = MP_NO;
  34. if (arg->sign == MP_NEG) {
  35. return MP_VAL;
  36. }
  37. if (MP_IS_ZERO(arg)) {
  38. return MP_OKAY;
  39. }
  40. /* First check mod 128 (suppose that MP_DIGIT_BIT is at least 7) */
  41. if (rem_128[127u & arg->dp[0]] == (char)1) {
  42. return MP_OKAY;
  43. }
  44. /* Next check mod 105 (3*5*7) */
  45. if ((err = mp_mod_d(arg, 105uL, &c)) != MP_OKAY) {
  46. return err;
  47. }
  48. if (rem_105[c] == (char)1) {
  49. return MP_OKAY;
  50. }
  51. if ((err = mp_init_u32(&t, 11u*13u*17u*19u*23u*29u*31u)) != MP_OKAY) {
  52. return err;
  53. }
  54. if ((err = mp_mod(arg, &t, &t)) != MP_OKAY) {
  55. goto LBL_ERR;
  56. }
  57. r = mp_get_u32(&t);
  58. /* Check for other prime modules, note it's not an ERROR but we must
  59. * free "t" so the easiest way is to goto LBL_ERR. We know that err
  60. * is already equal to MP_OKAY from the mp_mod call
  61. */
  62. if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL) goto LBL_ERR;
  63. if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL) goto LBL_ERR;
  64. if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL) goto LBL_ERR;
  65. if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL) goto LBL_ERR;
  66. if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL) goto LBL_ERR;
  67. if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL) goto LBL_ERR;
  68. if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL) goto LBL_ERR;
  69. /* Final check - is sqr(sqrt(arg)) == arg ? */
  70. if ((err = mp_sqrt(arg, &t)) != MP_OKAY) {
  71. goto LBL_ERR;
  72. }
  73. if ((err = mp_sqr(&t, &t)) != MP_OKAY) {
  74. goto LBL_ERR;
  75. }
  76. *ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
  77. LBL_ERR:
  78. mp_clear(&t);
  79. return err;
  80. }
  81. #endif