3 * Compute Jacobi symbol
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 /*----- Main code ---------------------------------------------------------*/
34 /* --- @mp_jacobi@ --- *
36 * Arguments: @mp *a@ = an integer
37 * @mp *n@ = another integer
39 * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
41 * Use: Computes the Kronecker symbol %$\jacobi{a}{n}$%. If @n@ is
42 * prime, this is the Legendre symbol and is equal to 1 if and
43 * only if @a@ is a quadratic residue mod @n@. The result is
44 * zero if and only if @a@ and @n@ have a common factor greater
47 * If @n@ is composite, then this computes the Kronecker symbol
49 * %$\jacobi{a}{n}=\jacobi{a}{u}\prod_i\jacobi{a}{p_i}^{e_i}$%
51 * where %$n = u p_0^{e_0} \ldots p_{n-1}^{e_{n-1}}$% is the
52 * prime factorization of %$n$%. The missing bits are:
54 * * %$\jacobi{a}{1} = 1$%;
55 * * %$\jacobi{a}{-1} = 1$% if @a@ is negative, or 1 if
57 * * %$\jacobi{a}{0} = 0$%;
58 * * %$\jacobi{a}{2}$ is 0 if @a@ is even, 1 if @a@ is
59 * congruent to 1 or 7 (mod 8), or %$-1$% otherwise.
61 * If %$n$% is positive and odd, then this is the Jacobi
62 * symbol. (The Kronecker symbol is a consistant domain
63 * extension; the Jacobi symbol was implemented first, and the
67 int mp_jacobi(mp *a, mp *n)
72 /* --- Handle zero specially --- *
74 * I can't find any specific statement for what to do when %$n = 0$%; PARI
75 * opts to set %$\jacobi{\pm1}{0} = \pm 1$% and %$\jacobi{a}{0} = 0$% for
80 if (MP_EQ(a, MP_ONE)) return (+1);
81 else if (MP_EQ(a, MP_MONE)) return (-1);
85 /* --- Deal with powers of two --- *
87 * This implicitly takes a copy of %$n$%. Copy %$a$% at the same time to
88 * make cleanup easier.
92 n = mp_odd(MP_NEW, n, &p2);
97 } else if ((p2 & 1) && ((a->v[0] & 7) == 3 || (a->v[0] & 7) == 5))
101 /* --- Deal with negative %$n$% --- */
109 /* --- Check for unit %$n$% --- */
111 if (MP_EQ(n, MP_ONE))
114 /* --- Reduce %$a$% modulo %$n$% --- */
116 if (MP_NEGP(a) || MP_CMP(a, >=, n))
119 /* --- Main recursive mess, flattened out into something nice --- */
125 /* --- Some simple special cases --- */
133 /* --- Main case with powers of two --- */
135 a = mp_odd(a, a, &e);
137 if ((e & 1) && (nn == 3 || nn == 5))
139 if (MP_LEN(a) == 1 && a->v[0] == 1)
141 if ((nn & 3) == 3 && (a->v[0] & 3) == 3)
144 /* --- Reduce and swap --- */
147 { mp *t = n; n = a; a = t; }
150 /* --- Wrap everything up --- */
158 /*----- Test rig ----------------------------------------------------------*/
162 #include <mLib/testrig.h>
164 static int verify(dstr *v)
166 mp *a = *(mp **)v[0].buf;
167 mp *n = *(mp **)v[1].buf;
168 int s = *(int *)v[2].buf;
169 int j = mp_jacobi(a, n);
173 fputs("\n*** fail", stderr);
174 fputs("a = ", stderr); mp_writefile(a, stderr, 10); fputc('\n', stderr);
175 fputs("n = ", stderr); mp_writefile(n, stderr, 10); fputc('\n', stderr);
176 fprintf(stderr, "s = %i\n", s);
177 fprintf(stderr, "j = %i\n", j);
183 assert(mparena_count(MPARENA_GLOBAL) == 0);
187 static test_chunk tests[] = {
188 { "jacobi", verify, { &type_mp, &type_mp, &type_int, 0 } },
192 int main(int argc, char *argv[])
195 test_run(argc, argv, tests, SRCDIR "/t/mp");
201 /*----- That's all, folks -------------------------------------------------*/