1 #include "tommath_private.h"
4 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
5 /* SPDX-License-Identifier: Unlicense */
9 Straightforward implementation of algorithm 1.4.10 in
10 Henri Cohen: "A Course in Computational Algebraic Number Theory"
12 @book{cohen2013course,
13 title={A course in computational algebraic number theory},
14 author={Cohen, Henri},
17 publisher={Springer Science \& Business Media}
20 mp_err
mp_kronecker(const mp_int
*a
, const mp_int
*p
, int *c
)
26 static const char table
[] = {0, 1, 0, -1, 0, -1, 0, 1};
29 if ((a
->used
== 1) && (a
->dp
[0] == 1u)) {
37 if (mp_iseven(a
) && mp_iseven(p
)) {
42 if ((err
= mp_init_copy(&a1
, a
)) != MP_OKAY
) {
45 if ((err
= mp_init_copy(&p1
, p
)) != MP_OKAY
) {
50 if ((err
= mp_div_2d(&p1
, v
, &p1
, NULL
)) != MP_OKAY
) {
57 k
= table
[a
->dp
[0] & 7u];
67 if ((err
= mp_init(&r
)) != MP_OKAY
) {
73 if (mp_cmp_d(&p1
, 1uL) == MP_EQ
) {
83 if ((err
= mp_div_2d(&a1
, v
, &a1
, NULL
)) != MP_OKAY
) {
88 k
= k
* table
[p1
.dp
[0] & 7u];
93 * Compute k = (-1)^((a1)*(p1-1)/4) * k
94 * a1.dp[0] + 1 cannot overflow because the MSB
95 * of the type mp_digit is not set by definition
97 if (((a1
.dp
[0] + 1u) & p1
.dp
[0] & 2u) != 0u) {
101 /* compute k = (-1)^((a1-1)*(p1-1)/4) * k */
102 if ((a1
.dp
[0] & p1
.dp
[0] & 2u) != 0u) {
107 if ((err
= mp_copy(&a1
, &r
)) != MP_OKAY
) {
111 if ((err
= mp_mod(&p1
, &r
, &a1
)) != MP_OKAY
) {
114 if ((err
= mp_copy(&r
, &p1
)) != MP_OKAY
) {