1 /* Exercise mpz_*_kronecker_*() and mpz_jacobi() functions.
3 Copyright 1999-2004, 2013 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library test suite.
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
21 /* With no arguments the various Kronecker/Jacobi symbol routines are
22 checked against some test data and a lot of derived data.
24 To check the test data against PARI-GP, run
28 It takes a while because the output from "t-jac -p" is big.
33 More big test cases than those given by check_squares_zi would be good. */
44 #ifdef _LONG_LONG_LIMB
55 mpz_mod4 (mpz_srcptr z)
61 mpz_fdiv_r_2exp (m, z, 2);
68 mpz_fits_ulimb_p (mpz_srcptr z)
70 return (SIZ(z) == 1 || SIZ(z) == 0);
74 mpz_get_ulimb (mpz_srcptr z)
84 try_base (mp_limb_t a, mp_limb_t b, int answer)
88 if ((b & 1) == 0 || b == 1 || a > b)
91 got = mpn_jacobi_base (a, b, 0);
94 printf (LL("mpn_jacobi_base (%lu, %lu) is %d should be %d\n",
95 "mpn_jacobi_base (%llu, %llu) is %d should be %d\n"),
103 try_zi_ui (mpz_srcptr a, unsigned long b, int answer)
107 got = mpz_kronecker_ui (a, b);
110 printf ("mpz_kronecker_ui (");
111 mpz_out_str (stdout, 10, a);
112 printf (", %lu) is %d should be %d\n", b, got, answer);
119 try_zi_si (mpz_srcptr a, long b, int answer)
123 got = mpz_kronecker_si (a, b);
126 printf ("mpz_kronecker_si (");
127 mpz_out_str (stdout, 10, a);
128 printf (", %ld) is %d should be %d\n", b, got, answer);
135 try_ui_zi (unsigned long a, mpz_srcptr b, int answer)
139 got = mpz_ui_kronecker (a, b);
142 printf ("mpz_ui_kronecker (%lu, ", a);
143 mpz_out_str (stdout, 10, b);
144 printf (") is %d should be %d\n", got, answer);
151 try_si_zi (long a, mpz_srcptr b, int answer)
155 got = mpz_si_kronecker (a, b);
158 printf ("mpz_si_kronecker (%ld, ", a);
159 mpz_out_str (stdout, 10, b);
160 printf (") is %d should be %d\n", got, answer);
166 /* Don't bother checking mpz_jacobi, since it only differs for b even, and
167 we don't have an actual expected answer for it. tests/devel/try.c does
168 some checks though. */
170 try_zi_zi (mpz_srcptr a, mpz_srcptr b, int answer)
174 got = mpz_kronecker (a, b);
177 printf ("mpz_kronecker (");
178 mpz_out_str (stdout, 10, a);
180 mpz_out_str (stdout, 10, b);
181 printf (") is %d should be %d\n", got, answer);
188 try_pari (mpz_srcptr a, mpz_srcptr b, int answer)
191 mpz_out_str (stdout, 10, a);
193 mpz_out_str (stdout, 10, b);
194 printf (",%d)\n", answer);
199 try_each (mpz_srcptr a, mpz_srcptr b, int answer)
202 fprintf(stderr, "asize = %d, bsize = %d\n",
203 mpz_sizeinbase (a, 2), mpz_sizeinbase (b, 2));
207 try_pari (a, b, answer);
211 if (mpz_fits_ulimb_p (a) && mpz_fits_ulimb_p (b))
212 try_base (mpz_get_ulimb (a), mpz_get_ulimb (b), answer);
214 if (mpz_fits_ulong_p (b))
215 try_zi_ui (a, mpz_get_ui (b), answer);
217 if (mpz_fits_slong_p (b))
218 try_zi_si (a, mpz_get_si (b), answer);
220 if (mpz_fits_ulong_p (a))
221 try_ui_zi (mpz_get_ui (a), b, answer);
223 if (mpz_fits_sint_p (a))
224 try_si_zi (mpz_get_si (a), b, answer);
226 try_zi_zi (a, b, answer);
230 /* Try (a/b) and (a/-b). */
232 try_pn (mpz_srcptr a, mpz_srcptr b_orig, int answer)
236 mpz_init_set (b, b_orig);
237 try_each (a, b, answer);
243 try_each (a, b, answer);
249 /* Try (a+k*p/b) for various k, using the fact (a/b) is periodic in a with
250 period p. For b>0, p=b if b!=2mod4 or p=4*b if b==2mod4. */
253 try_periodic_num (mpz_srcptr a_orig, mpz_srcptr b, int answer)
258 if (mpz_sgn (b) <= 0)
261 mpz_init_set (a, a_orig);
262 mpz_init_set (a_period, b);
263 if (mpz_mod4 (b) == 2)
264 mpz_mul_ui (a_period, a_period, 4);
266 /* don't bother with these tests if they're only going to produce
268 if (mpz_even_p (a) && mpz_even_p (b) && mpz_even_p (a_period))
271 for (i = 0; i < 6; i++)
273 mpz_add (a, a, a_period);
274 try_pn (a, b, answer);
278 for (i = 0; i < 6; i++)
280 mpz_sub (a, a, a_period);
281 try_pn (a, b, answer);
286 mpz_clear (a_period);
290 /* Try (a/b+k*p) for various k, using the fact (a/b) is periodic in b of
296 a==3mod4 and b odd 4*a
297 a==3mod4 and b even 8*a
299 In Henri Cohen's book the period is given as 4*a for all a==2,3mod4, but
300 a counterexample would seem to be (3/2)=-1 which with (3/14)=+1 doesn't
301 have period 4*a (but rather 8*a with (3/26)=-1). Maybe the plain 4*a is
302 to be read as applying to a plain Jacobi symbol with b odd, rather than
303 the Kronecker extension to b even. */
306 try_periodic_den (mpz_srcptr a, mpz_srcptr b_orig, int answer)
311 if (mpz_sgn (a) == 0 || mpz_sgn (b_orig) == 0)
314 mpz_init_set (b, b_orig);
316 mpz_init_set (b_period, a);
317 if (mpz_mod4 (a) == 3 && mpz_even_p (b))
318 mpz_mul_ui (b_period, b_period, 8L);
319 else if (mpz_mod4 (a) >= 2)
320 mpz_mul_ui (b_period, b_period, 4L);
322 /* don't bother with these tests if they're only going to produce
324 if (mpz_even_p (a) && mpz_even_p (b) && mpz_even_p (b_period))
327 for (i = 0; i < 6; i++)
329 mpz_add (b, b, b_period);
330 try_pn (a, b, answer);
334 for (i = 0; i < 6; i++)
336 mpz_sub (b, b, b_period);
337 try_pn (a, b, answer);
342 mpz_clear (b_period);
346 static const unsigned long ktable[] = {
347 0, 1, 2, 3, 4, 5, 6, 7,
348 GMP_NUMB_BITS-1, GMP_NUMB_BITS, GMP_NUMB_BITS+1,
349 2*GMP_NUMB_BITS-1, 2*GMP_NUMB_BITS, 2*GMP_NUMB_BITS+1,
350 3*GMP_NUMB_BITS-1, 3*GMP_NUMB_BITS, 3*GMP_NUMB_BITS+1
354 /* Try (a/b*2^k) for various k. */
356 try_2den (mpz_srcptr a, mpz_srcptr b_orig, int answer)
360 int answer_a2, answer_k;
363 /* don't bother when b==0 */
364 if (mpz_sgn (b_orig) == 0)
367 mpz_init_set (b, b_orig);
369 /* (a/2) is 0 if a even, 1 if a==1 or 7 mod 8, -1 if a==3 or 5 mod 8 */
370 answer_a2 = (mpz_even_p (a) ? 0
371 : (((SIZ(a) >= 0 ? PTR(a)[0] : -PTR(a)[0]) + 2) & 7) < 4 ? 1
374 for (kindex = 0; kindex < numberof (ktable); kindex++)
378 /* answer_k = answer*(answer_a2^k) */
379 answer_k = (answer_a2 == 0 && k != 0 ? 0
380 : (k & 1) == 1 && answer_a2 == -1 ? -answer
383 mpz_mul_2exp (b, b_orig, k);
384 try_pn (a, b, answer_k);
391 /* Try (a*2^k/b) for various k. If it happens mpz_ui_kronecker() gets (2/b)
392 wrong it will show up as wrong answers demanded. */
394 try_2num (mpz_srcptr a_orig, mpz_srcptr b, int answer)
398 int answer_2b, answer_k;
401 /* don't bother when a==0 */
402 if (mpz_sgn (a_orig) == 0)
407 /* (2/b) is 0 if b even, 1 if b==1 or 7 mod 8, -1 if b==3 or 5 mod 8 */
408 answer_2b = (mpz_even_p (b) ? 0
409 : (((SIZ(b) >= 0 ? PTR(b)[0] : -PTR(b)[0]) + 2) & 7) < 4 ? 1
412 for (kindex = 0; kindex < numberof (ktable); kindex++)
416 /* answer_k = answer*(answer_2b^k) */
417 answer_k = (answer_2b == 0 && k != 0 ? 0
418 : (k & 1) == 1 && answer_2b == -1 ? -answer
421 mpz_mul_2exp (a, a_orig, k);
422 try_pn (a, b, answer_k);
429 /* The try_2num() and try_2den() routines don't in turn call
430 try_periodic_num() and try_periodic_den() because it hugely increases the
431 number of tests performed, without obviously increasing coverage.
433 Useful extra derived cases can be added here. */
436 try_all (mpz_t a, mpz_t b, int answer)
438 try_pn (a, b, answer);
439 try_periodic_num (a, b, answer);
440 try_periodic_den (a, b, answer);
441 try_2num (a, b, answer);
442 try_2den (a, b, answer);
449 static const struct {
456 /* Note that the various derived checks in try_all() reduce the cases
457 that need to be given here. */
467 In particular note (0/1)=1 so that (a/b)=(a mod b/b). */
475 /* (0/b) = 0, b != 1 */
492 /* (-1/b) = (-1)^((b-1)/2) which is -1 for b==3 mod 4 */
504 /* (2/b) = (-1)^((b^2-1)/8) which is -1 for b==3,5 mod 8.
505 try_2num() will exercise multiple powers of 2 in the numerator. */
516 /* (-2/b) = (-1)^((b^2-1)/8)*(-1)^((b-1)/2) which is -1 for b==5,7mod8.
517 try_2num() will exercise multiple powers of 2 in the numerator, which
518 will test that the shift in mpz_si_kronecker() uses unsigned not
531 try_2den() will exercise multiple powers of 2 in the denominator. */
538 /* Harriet Griffin, "Elementary Theory of Numbers", page 155, various
559 /* Griffin page 147 */
566 /* Griffin page 148 */
575 /* H. Davenport, "The Higher Arithmetic", page 65, the quadratic
576 residues and non-residues mod 19. */
596 /* Residues and non-residues mod 13 */
616 /* special values inducing a==b==1 at the end of jac_or_kron() */
617 { "0x10000000000000000000000000000000000000000000000001",
618 "0x10000000000000000000000000000000000000000000000003", 1 },
620 /* Test for previous bugs in jacobi_2. */
621 { "0x43900000000", "0x42400000439", -1 }, /* 32-bit limbs */
622 { "0x4390000000000000000", "0x4240000000000000439", -1 }, /* 64-bit limbs */
624 { "198158408161039063", "198158360916398807", -1 },
626 /* Some tests involving large quotients in the continued fraction
628 { "37200210845139167613356125645445281805",
629 "451716845976689892447895811408978421929", -1 },
630 { "67674091930576781943923596701346271058970643542491743605048620644676477275152701774960868941561652032482173612421015",
631 "4902678867794567120224500687210807069172039735", 0 },
632 { "2666617146103764067061017961903284334497474492754652499788571378062969111250584288683585223600172138551198546085281683283672592", "2666617146103764067061017961903284334497474492754652499788571378062969111250584288683585223600172138551198546085281683290481773", 1 },
634 /* Exercises the case asize == 1, btwos > 0 in mpz_jacobi. */
635 { "804609", "421248363205206617296534688032638102314410556521742428832362659824", 1 } ,
636 { "4190209", "2239744742177804210557442048984321017460028974602978995388383905961079286530650825925074203175536427000", 1 },
638 /* Exercises the case asize == 1, btwos = 63 in mpz_jacobi
639 (relevant when GMP_LIMB_BITS == 64). */
640 { "17311973299000934401", "1675975991242824637446753124775689449936871337036614677577044717424700351103148799107651171694863695242089956242888229458836426332300124417011114380886016", 1 },
641 { "3220569220116583677", "41859917623035396746", -1 },
643 /* Other test cases that triggered bugs during development. */
644 { "37200210845139167613356125645445281805", "340116213441272389607827434472642576514", -1 },
645 { "74400421690278335226712251290890563610", "451716845976689892447895811408978421929", -1 },
654 for (i = 0; i < numberof (data); i++)
656 mpz_set_str_or_abort (a, data[i].a, 0);
657 mpz_set_str_or_abort (b, data[i].b, 0);
658 try_all (a, b, data[i].answer);
666 /* (a^2/b)=1 if gcd(a,b)=1, or (a^2/b)=0 if gcd(a,b)!=1.
667 This includes when a=0 or b=0. */
669 check_squares_zi (void)
671 gmp_randstate_ptr rands = RANDS;
674 mp_size_t size_range, an, bn;
682 for (i = 0; i < 50; i++)
684 mpz_urandomb (bs, rands, 32);
685 size_range = mpz_get_ui (bs) % 10 + i/8 + 2;
687 mpz_urandomb (bs, rands, size_range);
688 an = mpz_get_ui (bs);
689 mpz_rrandomb (a, rands, an);
691 mpz_urandomb (bs, rands, size_range);
692 bn = mpz_get_ui (bs);
693 mpz_rrandomb (b, rands, bn);
696 if (mpz_cmp_ui (g, 1L) == 0)
703 try_all (a, b, answer);
713 /* Check the handling of asize==0, make sure it isn't affected by the low
720 mpz_init_set_ui (a, 0);
725 try_all (a, b, 1); /* (0/1)=1 */
727 try_all (a, b, 1); /* (0/1)=1 */
731 try_all (a, b, 1); /* (0/-1)=1 */
733 try_all (a, b, 1); /* (0/-1)=1 */
737 try_all (a, b, 0); /* (0/0)=0 */
739 try_all (a, b, 0); /* (0/0)=0 */
743 try_all (a, b, 0); /* (0/2)=0 */
745 try_all (a, b, 0); /* (0/2)=0 */
752 /* Assumes that b = prod p_k^e_k */
754 ref_jacobi (mpz_srcptr a, mpz_srcptr b, unsigned nprime,
755 mpz_t prime[], unsigned *exp)
760 for (i = 0, res = 1; i < nprime; i++)
763 int legendre = refmpz_legendre (a, prime[i]);
773 check_jacobi_factored (void)
776 #define PRIME_MAX_SIZE 50
777 #define PRIME_MAX_EXP 4
778 #define PRIME_A_COUNT 10
779 #define PRIME_B_COUNT 5
780 #define PRIME_MAX_B_SIZE 2000
782 gmp_randstate_ptr rands = RANDS;
783 mpz_t prime[PRIME_N];
784 unsigned exp[PRIME_N];
793 /* Generate primes */
794 for (i = 0; i < PRIME_N; i++)
798 mpz_urandomb (bs, rands, 32);
799 size = mpz_get_ui (bs) % PRIME_MAX_SIZE + 2;
800 mpz_rrandomb (prime[i], rands, size);
801 if (mpz_cmp_ui (prime[i], 3) <= 0)
802 mpz_set_ui (prime[i], 3);
804 mpz_nextprime (prime[i], prime[i]);
807 for (i = 0; i < PRIME_B_COUNT; i++)
815 for (j = 0; j < PRIME_N && bsize < PRIME_MAX_B_SIZE; j++)
817 mpz_urandomb (bs, rands, 32);
818 exp[j] = mpz_get_ui (bs) % PRIME_MAX_EXP;
819 mpz_pow_ui (t, prime[j], exp[j]);
821 bsize = mpz_sizeinbase (b, 2);
823 for (k = 0; k < PRIME_A_COUNT; k++)
826 mpz_rrandomb (a, rands, bsize + 2);
827 answer = ref_jacobi (a, b, j, prime, exp);
828 try_all (a, b, answer);
831 for (i = 0; i < PRIME_N; i++)
832 mpz_clear (prime[i]);
840 #undef PRIME_MAX_SIZE
844 #undef PRIME_MAX_B_SIZE
847 /* These tests compute (a|n), where the quotient sequence includes
848 large quotients, and n has a known factorization. Such inputs are
849 generated as follows. First, construct a large n, as a power of a
850 prime p of moderate size.
852 Next, compute a matrix from factors (q,1;1,0), with q chosen with
853 uniformly distributed size. We must stop with matrix elements of
854 roughly half the size of n. Denote elements of M as M = (m00, m01;
857 We now look for solutions to
862 with x,y > 0. Since n >= m00 * m01, there exists a positive
863 solution to the first equation. Find those x, y, and substitute in
864 the second equation to get a. Then the quotient sequence for (a|n)
865 is precisely the quotients used when constructing M, followed by
866 the quotient sequence for (x|y).
868 Numbers should also be large enough that we exercise hgcd_jacobi,
869 which means that they should be larger than
871 max (GCD_DC_THRESHOLD, 3 * HGCD_THRESHOLD)
873 With an n of roughly 40000 bits, this should hold on most machines.
877 check_large_quotients (void)
882 #define MAX_QBITS 500
884 gmp_randstate_ptr rands = RANDS;
886 mpz_t p, n, q, g, s, t, x, y, bs;
905 /* First generate a number with known factorization, as a random
906 smallish prime raised to an odd power. Then (a|n) = (a|p). */
907 mpz_rrandomb (p, rands, PBITS);
908 mpz_nextprime (p, p);
909 mpz_pow_ui (n, p, PPOWER);
911 nsize = mpz_sizeinbase (n, 2);
913 for (i = 0; i < COUNT; i++)
918 mpz_set_ui (M[0][0], 1);
919 mpz_set_ui (M[0][1], 0);
920 mpz_set_ui (M[1][0], 0);
921 mpz_set_ui (M[1][1], 1);
923 for (msize = 1; 2*(msize + MAX_QBITS) + 1 < nsize ;)
926 mpz_rrandomb (bs, rands, 32);
927 mpz_rrandomb (q, rands, 1 + mpz_get_ui (bs) % MAX_QBITS);
929 /* Multiply by (q, 1; 1,0) from the right */
930 for (i = 0; i < 2; i++)
933 mpz_swap (M[i][0], M[i][1]);
934 mpz_addmul (M[i][0], M[i][1], q);
935 size = mpz_sizeinbase (M[i][0], 2);
940 mpz_gcdext (g, s, t, M[0][0], M[0][1]);
941 ASSERT_ALWAYS (mpz_cmp_ui (g, 1) == 0);
943 /* Solve n = M[0][0] * x + M[0][1] * y */
947 mpz_fdiv_qr (q, x, x, M[0][1]);
948 mpz_mul (y, q, M[0][0]);
949 mpz_addmul (y, t, n);
950 ASSERT_ALWAYS (mpz_sgn (y) > 0);
955 mpz_fdiv_qr (q, y, y, M[0][0]);
956 mpz_mul (x, q, M[0][1]);
957 mpz_addmul (x, s, n);
958 ASSERT_ALWAYS (mpz_sgn (x) > 0);
960 mpz_mul (x, x, M[1][0]);
961 mpz_addmul (x, y, M[1][1]);
963 /* Now (x|n) has the selected large quotients */
964 answer = refmpz_legendre (x, p);
965 try_zi_zi (x, n, answer);
987 main (int argc, char *argv[])
991 if (argc >= 2 && strcmp (argv[1], "-p") == 0)
998 if (kronecker(a,b) != answer,\n\
999 print(\"wrong at \", a, \",\", b,\n\
1000 \" expected \", answer,\n\
1001 \" pari says \", kronecker(a,b)))\n\
1006 check_squares_zi ();
1008 check_jacobi_factored ();
1009 check_large_quotients ();