libtommath/mp_kronecker.c

130 lines
2.7 KiB
C
Raw Normal View History

2018-05-03 22:01:45 +00:00
#include "tommath_private.h"
#ifdef MP_KRONECKER_C
2018-05-03 22:01:45 +00:00
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2018-05-03 22:01:45 +00:00
/*
Kronecker symbol (a|p)
Straightforward implementation of algorithm 1.4.10 in
Henri Cohen: "A Course in Computational Algebraic Number Theory"
@book{cohen2013course,
title={A course in computational algebraic number theory},
author={Cohen, Henri},
volume={138},
year={2013},
publisher={Springer Science \& Business Media}
}
*/
mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c)
2018-05-03 22:01:45 +00:00
{
mp_int a1, p1, r;
2019-05-19 13:32:46 +00:00
mp_err err;
2018-05-03 22:01:45 +00:00
int v, k;
2018-12-26 07:08:00 +00:00
static const int table[8] = {0, 1, 0, -1, 0, -1, 0, 1};
2018-05-03 22:01:45 +00:00
if (MP_IS_ZERO(p)) {
2018-12-26 07:33:43 +00:00
if ((a->used == 1) && (a->dp[0] == 1u)) {
2018-05-03 22:01:45 +00:00
*c = 1;
} else {
*c = 0;
}
2019-05-19 13:32:46 +00:00
return MP_OKAY;
2018-05-03 22:01:45 +00:00
}
if (MP_IS_EVEN(a) && MP_IS_EVEN(p)) {
2018-05-03 22:01:45 +00:00
*c = 0;
2019-05-19 13:32:46 +00:00
return MP_OKAY;
2018-05-03 22:01:45 +00:00
}
2019-05-19 15:16:13 +00:00
if ((err = mp_init_copy(&a1, a)) != MP_OKAY) {
return err;
2018-05-03 22:01:45 +00:00
}
2019-05-19 15:16:13 +00:00
if ((err = mp_init_copy(&p1, p)) != MP_OKAY) {
2018-05-03 22:01:45 +00:00
goto LBL_KRON_0;
}
v = mp_cnt_lsb(&p1);
2019-05-19 15:16:13 +00:00
if ((err = mp_div_2d(&p1, v, &p1, NULL)) != MP_OKAY) {
2018-05-03 22:01:45 +00:00
goto LBL_KRON_1;
}
2019-05-19 14:40:38 +00:00
if ((v & 1) == 0) {
2018-05-03 22:01:45 +00:00
k = 1;
} else {
2018-12-26 07:21:51 +00:00
k = table[a->dp[0] & 7u];
2018-05-03 22:01:45 +00:00
}
if (p1.sign == MP_NEG) {
p1.sign = MP_ZPOS;
if (a1.sign == MP_NEG) {
k = -k;
}
}
2019-05-19 15:16:13 +00:00
if ((err = mp_init(&r)) != MP_OKAY) {
2018-05-03 22:01:45 +00:00
goto LBL_KRON_1;
}
for (;;) {
if (MP_IS_ZERO(&a1)) {
2018-12-26 07:21:51 +00:00
if (mp_cmp_d(&p1, 1uL) == MP_EQ) {
2018-05-03 22:01:45 +00:00
*c = k;
goto LBL_KRON;
} else {
*c = 0;
goto LBL_KRON;
}
}
v = mp_cnt_lsb(&a1);
2019-05-19 15:16:13 +00:00
if ((err = mp_div_2d(&a1, v, &a1, NULL)) != MP_OKAY) {
2018-05-03 22:01:45 +00:00
goto LBL_KRON;
}
2019-05-19 14:40:38 +00:00
if ((v & 1) == 1) {
2018-12-26 07:21:51 +00:00
k = k * table[p1.dp[0] & 7u];
2018-05-03 22:01:45 +00:00
}
if (a1.sign == MP_NEG) {
/*
* Compute k = (-1)^((a1)*(p1-1)/4) * k
* a1.dp[0] + 1 cannot overflow because the MSB
* of the type mp_digit is not set by definition
*/
2018-12-26 07:24:49 +00:00
if (((a1.dp[0] + 1u) & p1.dp[0] & 2u) != 0u) {
2018-05-03 22:01:45 +00:00
k = -k;
}
} else {
/* compute k = (-1)^((a1-1)*(p1-1)/4) * k */
2018-12-26 07:24:49 +00:00
if ((a1.dp[0] & p1.dp[0] & 2u) != 0u) {
2018-05-03 22:01:45 +00:00
k = -k;
}
}
2019-05-19 15:16:13 +00:00
if ((err = mp_copy(&a1, &r)) != MP_OKAY) {
2018-05-03 22:01:45 +00:00
goto LBL_KRON;
}
r.sign = MP_ZPOS;
2019-05-19 15:16:13 +00:00
if ((err = mp_mod(&p1, &r, &a1)) != MP_OKAY) {
2018-05-03 22:01:45 +00:00
goto LBL_KRON;
}
2019-05-19 15:16:13 +00:00
if ((err = mp_copy(&r, &p1)) != MP_OKAY) {
2018-05-03 22:01:45 +00:00
goto LBL_KRON;
}
}
LBL_KRON:
mp_clear(&r);
LBL_KRON_1:
mp_clear(&p1);
LBL_KRON_0:
mp_clear(&a1);
2019-05-19 15:16:13 +00:00
return err;
2018-05-03 22:01:45 +00:00
}
#endif