1 /* Test mpz_invert. 2 3 Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 4 2008, 2009, 2012 Free Software Foundation, Inc. 5 6 This file is part of the GNU MP Library test suite. 7 8 The GNU MP Library test suite is free software; you can redistribute it 9 and/or modify it under the terms of the GNU General Public License as 10 published by the Free Software Foundation; either version 3 of the License, 11 or (at your option) any later version. 12 13 The GNU MP Library test suite is distributed in the hope that it will be 14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 16 Public License for more details. 17 18 You should have received a copy of the GNU General Public License along with 19 the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */ 20 21 #include <stdio.h> 22 #include <stdlib.h> 23 24 #include "gmp.h" 25 #include "gmp-impl.h" 26 #include "tests.h" 27 28 int 29 main (int argc, char **argv) 30 { 31 mpz_t a, m, ainv, t; 32 int test, r; 33 gmp_randstate_ptr rands; 34 mpz_t bs; 35 unsigned long bsi, size_range; 36 int reps = 1000; 37 38 tests_start (); 39 TESTS_REPS (reps, argv, argc); 40 41 rands = RANDS; 42 43 mpz_init (bs); 44 mpz_init (a); 45 mpz_init (m); 46 mpz_init (ainv); 47 mpz_init (t); 48 49 for (test = 0; test < reps; test++) 50 { 51 mpz_urandomb (bs, rands, 32); 52 size_range = mpz_get_ui (bs) % 16 + 2; 53 54 mpz_urandomb (bs, rands, size_range); 55 mpz_rrandomb (a, rands, mpz_get_ui (bs)); 56 do { 57 mpz_urandomb (bs, rands, size_range); 58 mpz_rrandomb (m, rands, mpz_get_ui (bs)); 59 } while (mpz_sgn (m) == 0); 60 61 mpz_urandomb (bs, rands, 8); 62 bsi = mpz_get_ui (bs); 63 64 if ((bsi & 1) != 0) 65 mpz_neg (a, a); 66 if ((bsi & 2) != 0) 67 mpz_neg (m, m); 68 69 r = mpz_invert (ainv, a, m); 70 if (r != 0) 71 { 72 MPZ_CHECK_FORMAT (ainv); 73 74 if (mpz_cmp_ui (ainv, 0) <= 0 || mpz_cmpabs (ainv, m) >= 0) 75 { 76 fprintf (stderr, "ERROR in test %d\n", test); 77 gmp_fprintf (stderr, "Inverse out of range.\n"); 78 gmp_fprintf (stderr, "a = %Zx\n", a); 79 gmp_fprintf (stderr, "m = %Zx\n", m); 80 abort (); 81 } 82 83 mpz_mul (t, ainv, a); 84 mpz_mod (t, t, m); 85 86 if (mpz_cmp_ui (t, 1) != 0) 87 { 88 fprintf (stderr, "ERROR in test %d\n", test); 89 gmp_fprintf (stderr, "a^(-1)*a != 1 (mod m)\n"); 90 gmp_fprintf (stderr, "a = %Zx\n", a); 91 gmp_fprintf (stderr, "m = %Zx\n", m); 92 abort (); 93 } 94 } 95 else /* Inverse deos not exist */ 96 { 97 if (mpz_cmpabs_ui (m, 1) <= 0) 98 continue; /* OK */ 99 100 mpz_gcd (t, a, m); 101 if (mpz_cmp_ui (t, 1) == 0) 102 { 103 fprintf (stderr, "ERROR in test %d\n", test); 104 gmp_fprintf (stderr, "Inverse exists, but was not found.\n"); 105 gmp_fprintf (stderr, "a = %Zx\n", a); 106 gmp_fprintf (stderr, "m = %Zx\n", m); 107 abort (); 108 } 109 } 110 } 111 112 mpz_clear (bs); 113 mpz_clear (a); 114 mpz_clear (m); 115 mpz_clear (ainv); 116 mpz_clear (t); 117 118 tests_end (); 119 exit (0); 120 } 121