summaryrefslogtreecommitdiff
path: root/mpz/legendre.c
blob: ab665f70d0eaf6ce02e9014f2bd8642abc22162e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* mpz_legendre (op1, op2).
   Contributed by Bennet Yee (bsy) at Carnegie-Mellon University

Copyright (C) 1992, 1996 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include "gmp.h"

#if defined (DEBUG)
#include <stdio.h>
#endif

/* Precondition:  both p and q are positive */

int
#if __STDC__
mpz_legendre (mpz_srcptr pi, mpz_srcptr qi)
#else
mpz_legendre (pi, qi)
mpz_srcptr pi, qi;
#endif
{
  mpz_t p, q, qdiv2;
#ifdef Q_MINUS_1
  mpz_t q_minus_1;
#endif
  mpz_ptr mtmp;
  register mpz_ptr pptr, qptr;
  register int retval = 1;
  register unsigned long int s;

  pptr = p;
  mpz_init_set (pptr, pi);
  qptr = q;
  mpz_init_set (qptr, qi);

#ifdef Q_MINUS_1
  mpz_init (q_minus_1);
#endif
  mpz_init (qdiv2);

tail_recurse2:
#ifdef DEBUG
  printf ("tail_recurse2: p=");
  mpz_out_str (stdout, 10, pptr);
  printf ("\nq=");
  mpz_out_str (stdout, 10, qptr);
  putchar ('\n');
#endif
  s = mpz_scan1 (qptr, 0);
  if (s) mpz_tdiv_q_2exp (qptr, qptr, s); /* J(a,2) = 1 */
#ifdef DEBUG
  printf ("2 factor decomposition: p=");
  mpz_out_str (stdout, 10, pptr);
  printf ("\nq=");
  mpz_out_str (stdout, 10, qptr);
  putchar ('\n');
#endif
  /* postcondition q odd */
  if (!mpz_cmp_ui (qptr, 1L))  /* J(a,1) = 1 */
    goto done;
  mpz_mod (pptr, pptr, qptr); /* J(a,q) = J(b,q) when a == b mod q */
#ifdef DEBUG
  printf ("mod out by q: p=");
  mpz_out_str (stdout, 10, pptr);
  printf ("\nq=");
  mpz_out_str (stdout, 10, qptr);
  putchar ('\n');
#endif
  /* quick calculation to get approximate size first */
  /* precondition: p < q */
  if ((mpz_sizeinbase (pptr, 2) + 1 >= mpz_sizeinbase (qptr,2))
      && (mpz_tdiv_q_2exp (qdiv2, qptr, 1L), mpz_cmp (pptr, qdiv2) > 0))
    {
      /* p > q/2 */
      mpz_sub (pptr, qptr, pptr);
      /* J(-1,q) = (-1)^((q-1)/2), q odd */
      if (mpz_get_ui (qptr) & 2)
	retval = -retval;
    }
  /* p < q/2 */
#ifdef Q_MINUS_1
  mpz_sub_ui (q_minus_q, qptr, 1L);
#endif
tail_recurse: /* we use tail_recurse only if q has not changed */
#ifdef DEBUG
  printf ("tail_recurse1: p=");
  mpz_out_str (stdout, 10, pptr);
  printf ("\nq=");
  mpz_out_str (stdout, 10, qptr);
  putchar ('\n');
#endif
  /*
   * J(0,q) = 0
   * this occurs only if gcd(p,q) != 1 which is never true for
   * Legendre function.
   */
  if (!mpz_cmp_ui (pptr, 0L))
    {
      retval = 0;
      goto done;
    }

  if (!mpz_cmp_ui (pptr, 1L))
    {
      /* J(1,q) = 1 */
      /* retval *= 1; */
      goto done;
    }
#ifdef Q_MINUS_1
  if (!mpz_cmp (pptr, q_minus_1))
    {
      /* J(-1,q) = (-1)^((q-1)/2) */
      if (mpz_get_ui (qptr) & 2)
	retval = -retval;
      /* else    retval *= 1; */
      goto done;
    }
#endif
  /*
   * we do not handle J(xy,q) except for x==2
   * since we do not want to factor
   */
  if ((s = mpz_scan1 (pptr, 0)) != 0)
    {
      /*
       * J(2,q) = (-1)^((q^2-1)/8)
       *
       * Note that q odd guarantees that q^2-1 is divisible by 8:
       * Let a: q=2a+1.  q^2 = 4a^2+4a+1, (q^2-1)/8 = a(a+1)/2, qed
       *
       * Now, note that this means that the low two bits of _a_
       * (or the low bits of q shifted over by 1 determines
       * the factor).
       */
      mpz_tdiv_q_2exp (pptr, pptr, s);

      /* even powers of 2 gives J(2,q)^{2n} = 1 */
      if (s & 1)
	{
	  s = mpz_get_ui (qptr) >> 1;
	  s = s * (s + 1);
	  if (s & 2)
	    retval = -retval;
	}
      goto tail_recurse;
    }
  /*
   * we know p is odd since we have cast out 2s
   * precondition that q is odd guarantees both odd.
   *
   * quadratic reciprocity
   * J(p,q) = (-1)^((p-1)(q-1)/4) * J(q,p)
   */
  if ((s = mpz_scan1 (pptr, 1)) <= 2 && (s + mpz_scan1 (qptr, 1)) <= 2)
    retval = -retval;

  mtmp = pptr; pptr = qptr; qptr = mtmp;
  goto tail_recurse2;
done:
  mpz_clear (p);
  mpz_clear (q);
  mpz_clear (qdiv2);
#ifdef Q_MINUS_1
  mpz_clear (q_minus_1);
#endif
  return retval;
}