186d7f5d3SJohn Marino /* mpz_ui_kronecker -- ulong+mpz Kronecker/Jacobi symbol.
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
486d7f5d3SJohn Marino
586d7f5d3SJohn Marino This file is part of the GNU MP Library.
686d7f5d3SJohn Marino
786d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
886d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
986d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1086d7f5d3SJohn Marino option) any later version.
1186d7f5d3SJohn Marino
1286d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1386d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1486d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1586d7f5d3SJohn Marino License for more details.
1686d7f5d3SJohn Marino
1786d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
1886d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
1986d7f5d3SJohn Marino
2086d7f5d3SJohn Marino #include "gmp.h"
2186d7f5d3SJohn Marino #include "gmp-impl.h"
2286d7f5d3SJohn Marino #include "longlong.h"
2386d7f5d3SJohn Marino
2486d7f5d3SJohn Marino
2586d7f5d3SJohn Marino int
mpz_ui_kronecker(unsigned long a,mpz_srcptr b)2686d7f5d3SJohn Marino mpz_ui_kronecker (unsigned long a, mpz_srcptr b)
2786d7f5d3SJohn Marino {
2886d7f5d3SJohn Marino mp_srcptr b_ptr;
2986d7f5d3SJohn Marino mp_limb_t b_low;
3086d7f5d3SJohn Marino int b_abs_size;
3186d7f5d3SJohn Marino mp_limb_t b_rem;
3286d7f5d3SJohn Marino int twos;
3386d7f5d3SJohn Marino int result_bit1;
3486d7f5d3SJohn Marino
3586d7f5d3SJohn Marino /* (a/-1)=1 when a>=0, so the sign of b is ignored */
3686d7f5d3SJohn Marino b_abs_size = ABSIZ (b);
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino if (b_abs_size == 0)
3986d7f5d3SJohn Marino return JACOBI_U0 (a); /* (a/0) */
4086d7f5d3SJohn Marino
4186d7f5d3SJohn Marino if (a > GMP_NUMB_MAX)
4286d7f5d3SJohn Marino {
4386d7f5d3SJohn Marino mp_limb_t alimbs[2];
4486d7f5d3SJohn Marino mpz_t az;
4586d7f5d3SJohn Marino ALLOC(az) = numberof (alimbs);
4686d7f5d3SJohn Marino PTR(az) = alimbs;
4786d7f5d3SJohn Marino mpz_set_ui (az, a);
4886d7f5d3SJohn Marino return mpz_kronecker (az, b);
4986d7f5d3SJohn Marino }
5086d7f5d3SJohn Marino
5186d7f5d3SJohn Marino b_ptr = PTR(b);
5286d7f5d3SJohn Marino b_low = b_ptr[0];
5386d7f5d3SJohn Marino result_bit1 = 0;
5486d7f5d3SJohn Marino
5586d7f5d3SJohn Marino if (! (b_low & 1))
5686d7f5d3SJohn Marino {
5786d7f5d3SJohn Marino /* (0/b)=0 for b!=+/-1; and (even/even)=0 */
5886d7f5d3SJohn Marino if (! (a & 1))
5986d7f5d3SJohn Marino return 0;
6086d7f5d3SJohn Marino
6186d7f5d3SJohn Marino /* a odd, b even
6286d7f5d3SJohn Marino
6386d7f5d3SJohn Marino Establish shifted b_low with valid bit1 for the RECIP below. Zero
6486d7f5d3SJohn Marino limbs stripped are accounted for, but zero bits on b_low are not
6586d7f5d3SJohn Marino because they remain in {b_ptr,b_abs_size} for
6686d7f5d3SJohn Marino JACOBI_MOD_OR_MODEXACT_1_ODD. */
6786d7f5d3SJohn Marino
6886d7f5d3SJohn Marino JACOBI_STRIP_LOW_ZEROS (result_bit1, a, b_ptr, b_abs_size, b_low);
6986d7f5d3SJohn Marino if (! (b_low & 1))
7086d7f5d3SJohn Marino {
7186d7f5d3SJohn Marino if (UNLIKELY (b_low == GMP_NUMB_HIGHBIT))
7286d7f5d3SJohn Marino {
7386d7f5d3SJohn Marino /* need b_ptr[1] to get bit1 in b_low */
7486d7f5d3SJohn Marino if (b_abs_size == 1)
7586d7f5d3SJohn Marino {
7686d7f5d3SJohn Marino /* (a/0x80...00) == (a/2)^(NUMB-1) */
7786d7f5d3SJohn Marino if ((GMP_NUMB_BITS % 2) == 0)
7886d7f5d3SJohn Marino {
7986d7f5d3SJohn Marino /* JACOBI_STRIP_LOW_ZEROS does nothing to result_bit1
8086d7f5d3SJohn Marino when GMP_NUMB_BITS is even, so it's still 0. */
8186d7f5d3SJohn Marino ASSERT (result_bit1 == 0);
8286d7f5d3SJohn Marino result_bit1 = JACOBI_TWO_U_BIT1 (a);
8386d7f5d3SJohn Marino }
8486d7f5d3SJohn Marino return JACOBI_BIT1_TO_PN (result_bit1);
8586d7f5d3SJohn Marino }
8686d7f5d3SJohn Marino
8786d7f5d3SJohn Marino /* b_abs_size > 1 */
8886d7f5d3SJohn Marino b_low = b_ptr[1] << 1;
8986d7f5d3SJohn Marino }
9086d7f5d3SJohn Marino else
9186d7f5d3SJohn Marino {
9286d7f5d3SJohn Marino count_trailing_zeros (twos, b_low);
9386d7f5d3SJohn Marino b_low >>= twos;
9486d7f5d3SJohn Marino }
9586d7f5d3SJohn Marino }
9686d7f5d3SJohn Marino }
9786d7f5d3SJohn Marino else
9886d7f5d3SJohn Marino {
9986d7f5d3SJohn Marino if (a == 0) /* (0/b)=1 for b=+/-1, 0 otherwise */
10086d7f5d3SJohn Marino return (b_abs_size == 1 && b_low == 1);
10186d7f5d3SJohn Marino
10286d7f5d3SJohn Marino if (! (a & 1))
10386d7f5d3SJohn Marino {
10486d7f5d3SJohn Marino /* a even, b odd */
10586d7f5d3SJohn Marino count_trailing_zeros (twos, a);
10686d7f5d3SJohn Marino a >>= twos;
10786d7f5d3SJohn Marino /* (a*2^n/b) = (a/b) * (2/a)^n */
10886d7f5d3SJohn Marino result_bit1 = JACOBI_TWOS_U_BIT1 (twos, b_low);
10986d7f5d3SJohn Marino }
11086d7f5d3SJohn Marino }
11186d7f5d3SJohn Marino
11286d7f5d3SJohn Marino if (a == 1)
11386d7f5d3SJohn Marino return JACOBI_BIT1_TO_PN (result_bit1); /* (1/b)=1 */
11486d7f5d3SJohn Marino
11586d7f5d3SJohn Marino /* (a/b*2^n) = (b*2^n mod a / a) * RECIP(a,b) */
11686d7f5d3SJohn Marino JACOBI_MOD_OR_MODEXACT_1_ODD (result_bit1, b_rem, b_ptr, b_abs_size, a);
11786d7f5d3SJohn Marino result_bit1 ^= JACOBI_RECIP_UU_BIT1 (a, b_low);
11886d7f5d3SJohn Marino return mpn_jacobi_base (b_rem, (mp_limb_t) a, result_bit1);
11986d7f5d3SJohn Marino }
120