Home | History | Annotate | Line # | Download | only in mpz
t-invert.c revision 1.1.1.2
      1 /* Test mpz_invert.
      2 
      3 Copyright 1991, 1993, 1994, 1996, 1997, 2000-2005, 2008, 2009, 2012, 2014 Free
      4 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 https://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, "1/a = %Zx\n", ainv);
     80 	      gmp_fprintf (stderr, "m = %Zx\n", m);
     81 	      abort ();
     82 	    }
     83 
     84 	  mpz_mul (t, ainv, a);
     85 	  mpz_mod (t, t, m);
     86 
     87 	  if (mpz_cmp_ui (t, mpz_cmpabs_ui (m, 1) != 0) != 0)
     88 	    {
     89 	      fprintf (stderr, "ERROR in test %d\n", test);
     90 	      gmp_fprintf (stderr, "a^(-1)*a != 1 (mod m)\n");
     91 	      gmp_fprintf (stderr, "a = %Zx\n", a);
     92 	      gmp_fprintf (stderr, "m = %Zx\n", m);
     93 	      abort ();
     94 	    }
     95 	}
     96       else /* Inverse deos not exist */
     97 	{
     98 	  if (mpz_cmpabs_ui (m, 1) <= 0)
     99 	    continue; /* OK */
    100 
    101 	  mpz_gcd (t, a, m);
    102 	  if (mpz_cmp_ui (t, 1) == 0)
    103 	    {
    104 	      fprintf (stderr, "ERROR in test %d\n", test);
    105 	      gmp_fprintf (stderr, "Inverse exists, but was not found.\n");
    106 	      gmp_fprintf (stderr, "a = %Zx\n", a);
    107 	      gmp_fprintf (stderr, "m = %Zx\n", m);
    108 	      abort ();
    109 	    }
    110 	}
    111     }
    112 
    113   mpz_clear (bs);
    114   mpz_clear (a);
    115   mpz_clear (m);
    116   mpz_clear (ainv);
    117   mpz_clear (t);
    118 
    119   tests_end ();
    120   exit (0);
    121 }
    122