summaryrefslogtreecommitdiff
path: root/tests/mpz/t-invert.c
blob: b9822c07897cf46160afe53e6082b751b8949575 (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
/* Test mpz_invert.

Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005,
2008, 2009, 2012 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 3 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.  If not, see http://www.gnu.org/licenses/.  */

#include <stdio.h>
#include <stdlib.h>

#include "gmp.h"
#include "gmp-impl.h"
#include "tests.h"

int
main (int argc, char **argv)
{
  mpz_t a, m, ainv, t;
  int test, r;
  gmp_randstate_ptr rands;
  mpz_t bs;
  unsigned long bsi, size_range;
  int reps = 1000;

  tests_start ();
  TESTS_REPS (reps, argv, argc);

  rands = RANDS;

  mpz_init (bs);
  mpz_init (a);
  mpz_init (m);
  mpz_init (ainv);
  mpz_init (t);

  for (test = 0; test < reps; test++)
    {
      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 16 + 2;

      mpz_urandomb (bs, rands, size_range);
      mpz_rrandomb (a, rands, mpz_get_ui (bs));
      mpz_urandomb (bs, rands, size_range);
      mpz_rrandomb (m, rands, mpz_get_ui (bs));

      mpz_urandomb (bs, rands, 8);
      bsi = mpz_get_ui (bs);

      if ((bsi & 1) != 0)
	mpz_neg (a, a);
      if ((bsi & 2) != 0)
	mpz_neg (m, m);

      r = mpz_invert (ainv, a, m);
      if (r != 0)
	{
	  MPZ_CHECK_FORMAT (ainv);

	  if (mpz_cmp_ui (ainv, 0) <= 0 || mpz_cmpabs (ainv, m) > 0)
	    {
	      fprintf (stderr, "ERROR in test %d\n", test);
	      gmp_fprintf (stderr, "Inverse out of range.\n");
	      gmp_fprintf (stderr, "a = %Zx\n", a);
	      gmp_fprintf (stderr, "m = %Zx\n", m);
	      abort ();
	    }

	  mpz_mul (t, ainv, a);
	  mpz_mod (t, t, m);

	  if (mpz_cmp_ui (t, 1) != 0)
	    {
	      fprintf (stderr, "ERROR in test %d\n", test);
	      gmp_fprintf (stderr, "a^(-1)*a != 1 (mod m)\n");
	      gmp_fprintf (stderr, "a = %Zx\n", a);
	      gmp_fprintf (stderr, "m = %Zx\n", m);
	      abort ();
	    }
	}
      else /* Inverse deos not exist */
	{
	  if (mpz_cmpabs_ui (m, 1) <= 0)
	    continue; /* OK */

	  mpz_gcd (t, a, m);
	  if (mpz_cmp_ui (t, 1) == 0)
	    {
	      fprintf (stderr, "ERROR in test %d\n", test);
	      gmp_fprintf (stderr, "Inverse exists, but was not found.\n");
	      gmp_fprintf (stderr, "a = %Zx\n", a);
	      gmp_fprintf (stderr, "m = %Zx\n", m);
	      abort ();
	    }
	}
    }

  mpz_clear (bs);
  mpz_clear (a);
  mpz_clear (m);
  mpz_clear (ainv);
  mpz_clear (t);

  tests_end ();
  exit (0);
}