bn_mp_kronecker.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_KRONECKER_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /*
  6. Kronecker symbol (a|p)
  7. Straightforward implementation of algorithm 1.4.10 in
  8. Henri Cohen: "A Course in Computational Algebraic Number Theory"
  9. @book{cohen2013course,
  10. title={A course in computational algebraic number theory},
  11. author={Cohen, Henri},
  12. volume={138},
  13. year={2013},
  14. publisher={Springer Science \& Business Media}
  15. }
  16. */
  17. mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c)
  18. {
  19. mp_int a1, p1, r;
  20. mp_err err;
  21. int v, k;
  22. static const int table[8] = {0, 1, 0, -1, 0, -1, 0, 1};
  23. if (MP_IS_ZERO(p)) {
  24. if ((a->used == 1) && (a->dp[0] == 1u)) {
  25. *c = 1;
  26. } else {
  27. *c = 0;
  28. }
  29. return MP_OKAY;
  30. }
  31. if (MP_IS_EVEN(a) && MP_IS_EVEN(p)) {
  32. *c = 0;
  33. return MP_OKAY;
  34. }
  35. if ((err = mp_init_copy(&a1, a)) != MP_OKAY) {
  36. return err;
  37. }
  38. if ((err = mp_init_copy(&p1, p)) != MP_OKAY) {
  39. goto LBL_KRON_0;
  40. }
  41. v = mp_cnt_lsb(&p1);
  42. if ((err = mp_div_2d(&p1, v, &p1, NULL)) != MP_OKAY) {
  43. goto LBL_KRON_1;
  44. }
  45. if ((v & 1) == 0) {
  46. k = 1;
  47. } else {
  48. k = table[a->dp[0] & 7u];
  49. }
  50. if (p1.sign == MP_NEG) {
  51. p1.sign = MP_ZPOS;
  52. if (a1.sign == MP_NEG) {
  53. k = -k;
  54. }
  55. }
  56. if ((err = mp_init(&r)) != MP_OKAY) {
  57. goto LBL_KRON_1;
  58. }
  59. for (;;) {
  60. if (MP_IS_ZERO(&a1)) {
  61. if (mp_cmp_d(&p1, 1uL) == MP_EQ) {
  62. *c = k;
  63. goto LBL_KRON;
  64. } else {
  65. *c = 0;
  66. goto LBL_KRON;
  67. }
  68. }
  69. v = mp_cnt_lsb(&a1);
  70. if ((err = mp_div_2d(&a1, v, &a1, NULL)) != MP_OKAY) {
  71. goto LBL_KRON;
  72. }
  73. if ((v & 1) == 1) {
  74. k = k * table[p1.dp[0] & 7u];
  75. }
  76. if (a1.sign == MP_NEG) {
  77. /*
  78. * Compute k = (-1)^((a1)*(p1-1)/4) * k
  79. * a1.dp[0] + 1 cannot overflow because the MSB
  80. * of the type mp_digit is not set by definition
  81. */
  82. if (((a1.dp[0] + 1u) & p1.dp[0] & 2u) != 0u) {
  83. k = -k;
  84. }
  85. } else {
  86. /* compute k = (-1)^((a1-1)*(p1-1)/4) * k */
  87. if ((a1.dp[0] & p1.dp[0] & 2u) != 0u) {
  88. k = -k;
  89. }
  90. }
  91. if ((err = mp_copy(&a1, &r)) != MP_OKAY) {
  92. goto LBL_KRON;
  93. }
  94. r.sign = MP_ZPOS;
  95. if ((err = mp_mod(&p1, &r, &a1)) != MP_OKAY) {
  96. goto LBL_KRON;
  97. }
  98. if ((err = mp_copy(&r, &p1)) != MP_OKAY) {
  99. goto LBL_KRON;
  100. }
  101. }
  102. LBL_KRON:
  103. mp_clear(&r);
  104. LBL_KRON_1:
  105. mp_clear(&p1);
  106. LBL_KRON_0:
  107. mp_clear(&a1);
  108. return err;
  109. }
  110. #endif